Upload 2 files
Browse files- app.py +38 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
MODEL_NAME = "nlptown/bert-base-multilingual-uncased-sentiment"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
8 |
+
|
9 |
+
LABELS = {
|
10 |
+
0: "Very Negative",
|
11 |
+
1: "Negative",
|
12 |
+
2: "Neutral",
|
13 |
+
3: "Positive",
|
14 |
+
4: "Very Positive"
|
15 |
+
}
|
16 |
+
|
17 |
+
def predict_sentiment(text):
|
18 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
19 |
+
outputs = model(**inputs)
|
20 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
21 |
+
confidence, prediction = torch.max(probs, dim=1)
|
22 |
+
sentiment = LABELS[prediction.item()]
|
23 |
+
return {
|
24 |
+
"Sentiment feeling": sentiment,
|
25 |
+
"Confidence score": f"{confidence.item():.3f} ({'Highly Certain' if confidence.item() > 0.8 else 'Somewhat Certain' if confidence.item() > 0.6 else 'Uncertain'})"
|
26 |
+
}
|
27 |
+
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=predict_sentiment,
|
30 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text (any language)..."),
|
31 |
+
outputs="json",
|
32 |
+
title="🌍 Multilingual Sentiment Analysis",
|
33 |
+
description="Check your text's sentiment instantly using a multilingual BERT model trained on reviews. Supports languages like English, Spanish, French, German, etc.",
|
34 |
+
theme="soft",
|
35 |
+
allow_flagging="never"
|
36 |
+
)
|
37 |
+
|
38 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|