Spaces:
Running
Running
Update app.py
Browse filessentiment for different languages
app.py
CHANGED
@@ -1,32 +1,59 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
3 |
|
4 |
-
#
|
5 |
-
MODEL = "
|
6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
7 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
8 |
sentiment_model = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def analyze_sentiment(text):
|
12 |
result = sentiment_model(text)[0]
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
-
# Example texts
|
16 |
examples = [
|
17 |
-
["I absolutely love this new phone, the camera is stunning!"],
|
18 |
-
["
|
19 |
-
["
|
20 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Gradio UI
|
23 |
demo = gr.Interface(
|
24 |
fn=analyze_sentiment,
|
25 |
-
inputs=gr.Textbox(lines=3, placeholder="Type a sentence here..."),
|
26 |
-
outputs=
|
|
|
|
|
|
|
|
|
27 |
examples=examples,
|
28 |
-
title="Sentiment Analyzer",
|
29 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
30 |
)
|
31 |
|
32 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
3 |
|
4 |
+
# Multilingual model (10+ languages)
|
5 |
+
MODEL = "nlptown/bert-base-multilingual-uncased-sentiment"
|
6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
7 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
8 |
sentiment_model = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
9 |
|
10 |
+
# Map stars (1–5) to emotion labels with emojis
|
11 |
+
STAR_EMOJIS = {
|
12 |
+
1: "😡 Very Negative",
|
13 |
+
2: "☹️ Negative",
|
14 |
+
3: "😐 Neutral",
|
15 |
+
4: "🙂 Positive",
|
16 |
+
5: "🤩 Very Positive"
|
17 |
+
}
|
18 |
+
|
19 |
def analyze_sentiment(text):
|
20 |
result = sentiment_model(text)[0]
|
21 |
+
stars = int(result["label"][0]) # "1 star" → 1
|
22 |
+
sentiment = STAR_EMOJIS.get(stars, result["label"])
|
23 |
+
confidence = f"{result['score']:.2f}"
|
24 |
+
return [[sentiment, confidence]]
|
25 |
|
26 |
+
# Example texts in different languages
|
27 |
examples = [
|
28 |
+
["I absolutely love this new phone, the camera is stunning!"], # English
|
29 |
+
["Je déteste quand cette application plante sans cesse."], # French
|
30 |
+
["Das Essen in diesem Restaurant war fantastisch!"], # German
|
31 |
+
["Este producto es muy malo y no funciona."], # Spanish
|
32 |
+
["Questo film è stato noioso e troppo lungo."], # Italian
|
33 |
+
["Eu gostei muito do serviço, foi excelente!"], # Portuguese
|
34 |
+
["Эта книга ужасна, я еле её дочитал."], # Russian
|
35 |
+
["هذا الهاتف رائع للغاية، أنا سعيد جدًا به."], # Arabic
|
36 |
+
["この映画は本当に面白かった!"], # Japanese
|
37 |
+
["De app werkt prima, maar kan beter."], # Dutch
|
38 |
+
]
|
39 |
|
40 |
# Gradio UI
|
41 |
demo = gr.Interface(
|
42 |
fn=analyze_sentiment,
|
43 |
+
inputs=gr.Textbox(lines=3, placeholder="Type a sentence here in one of 10 languages..."),
|
44 |
+
outputs=gr.Dataframe(
|
45 |
+
headers=["Emotion (1–5 Stars)", "Confidence"],
|
46 |
+
row_count=1,
|
47 |
+
col_count=(2, "fixed"),
|
48 |
+
),
|
49 |
examples=examples,
|
50 |
+
title="🌍 Multilingual Emotion & Sentiment Analyzer",
|
51 |
+
description=(
|
52 |
+
"Supports 10+ languages (English, French, German, Spanish, Italian, Dutch, "
|
53 |
+
"Portuguese, Russian, Arabic, Japanese). Detects fine-grained emotions "
|
54 |
+
"with 5 levels:\n\n"
|
55 |
+
"😡 Very Negative | ☹️ Negative | 😐 Neutral | 🙂 Positive | 🤩 Very Positive"
|
56 |
+
),
|
57 |
)
|
58 |
|
59 |
if __name__ == "__main__":
|