anabury commited on
Commit
14d7e08
·
verified ·
1 Parent(s): 21cc4b5

Update app.py

Browse files

sentiment for different languages

Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -1,32 +1,59 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
3
 
4
- # Load model
5
- MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
6
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
7
  model = AutoModelForSequenceClassification.from_pretrained(MODEL)
8
  sentiment_model = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
9
 
10
- # Function for Gradio
 
 
 
 
 
 
 
 
11
  def analyze_sentiment(text):
12
  result = sentiment_model(text)[0]
13
- return f"Sentiment: {result['label']} | Confidence: {result['score']:.2f}"
 
 
 
14
 
15
- # Example texts
16
  examples = [
17
- ["I absolutely love this new phone, the camera is stunning!"],
18
- ["I hate the way this app keeps crashing."],
19
- ["It’s fine, nothing special but not terrible either."],
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="text",
 
 
 
 
27
  examples=examples,
28
- title="Sentiment Analyzer",
29
- description="A simple Hugging Face Space to analyze sentiment in tweets using CardiffNLP's RoBERTa model."
 
 
 
 
 
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__":