skmdud commited on
Commit
7a2ac4c
ยท
verified ยท
1 Parent(s): f4e1fad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -31
app.py CHANGED
@@ -3,16 +3,17 @@ from indic_transliteration import sanscript
3
  from indic_transliteration.sanscript import transliterate
4
  from transformers import pipeline
5
 
6
- # 1. Load models (public models)
 
7
  emotion_model = pipeline(
8
  "text-classification",
9
- model="monsoon-nlp/hindi-emotion", # Public Hindi emotion model
10
  return_all_scores=True
11
  )
12
 
13
  sentiment_model = pipeline(
14
  "text-classification",
15
- model="madhav112/hindi-sentiment-analysis", # Public Hindi sentiment model
16
  return_all_scores=True
17
  )
18
 
@@ -33,55 +34,48 @@ def normalize_hindi(text: str) -> str:
33
  text = text.replace(wrong, right)
34
  return text
35
 
36
- # 4. Emoji & Label mapping
37
  EMOJI_MAP = {
38
- "anger": "๐Ÿ˜ก",
39
- "disgust": "๐Ÿคข",
40
- "fear": "๐Ÿ˜ฑ",
41
- "joy": "๐Ÿ˜„",
42
- "neutral": "๐Ÿ˜",
43
- "sadness": "๐Ÿ˜ข",
44
- "surprise": "๐Ÿ˜ฒ"
45
  }
46
 
47
  SENTIMENT_MAP = {
48
- "LABEL_0": "๐Ÿ˜ž Negative",
49
- "LABEL_1": "๐Ÿ˜ Neutral",
50
- "LABEL_2": "๐Ÿ˜ƒ Positive"
 
 
51
  }
52
 
53
  # 5. Full pipeline
54
  def analyze_text_visual(hinglish_text: str):
55
- # Hinglish โ†’ Hindi โ†’ Normalize
56
  hindi_text = normalize_hindi(hinglish_to_hindi(hinglish_text))
57
 
58
- # Emotion
59
  emotion_scores = emotion_model(hindi_text)
60
  top_emotion = max(emotion_scores[0], key=lambda x: x['score'])
61
- emotion_result = f"{EMOJI_MAP.get(top_emotion['label'], '')} {top_emotion['label']} ({top_emotion['score']:.2f})"
62
 
63
- # Sentiment
64
  sentiment_scores = sentiment_model(hindi_text)
65
  top_sentiment = max(sentiment_scores[0], key=lambda x: x['score'])
66
- sentiment_result = f"{SENTIMENT_MAP.get(top_sentiment['label'], '')} ({top_sentiment['score']:.2f})"
67
 
68
- return {
 
 
69
  "Hindi Text": hindi_text,
70
- "Top Emotion": emotion_result,
71
- "Top Sentiment": sentiment_result
72
  }
 
73
 
74
- # 6. Gradio Interface
75
  iface = gr.Interface(
76
  fn=analyze_text_visual,
77
- inputs=gr.Textbox(label="Hinglish Text"),
78
- outputs=[
79
- gr.Textbox(label="Hindi Text"),
80
- gr.Textbox(label="Top Emotion"),
81
- gr.Textbox(label="Top Sentiment")
82
- ],
83
- title="Hindi Emotion & Sentiment Analyzer",
84
- description="Hinglish input เคธเฅ‡ Hindi เคฎเฅ‡เค‚ convert เค•เคฐเค•เฅ‡ Emotion เค”เคฐ Sentiment เคฆเคฟเค–เคพเคเคเฅค"
85
  )
86
 
87
  iface.launch()
 
 
3
  from indic_transliteration.sanscript import transliterate
4
  from transformers import pipeline
5
 
6
+ # 1. Load public models
7
+ # Public Hindi sentiment & emotion models (authentication-free)
8
  emotion_model = pipeline(
9
  "text-classification",
10
+ model="l3cube-pune/hindi-emotion", # Public Hindi emotion model
11
  return_all_scores=True
12
  )
13
 
14
  sentiment_model = pipeline(
15
  "text-classification",
16
+ model="nlptown/bert-base-multilingual-uncased-sentiment", # Multilingual sentiment model
17
  return_all_scores=True
18
  )
19
 
 
34
  text = text.replace(wrong, right)
35
  return text
36
 
37
+ # 4. Emoji mapping
38
  EMOJI_MAP = {
39
+ "anger":"๐Ÿ˜ก","disgust":"๐Ÿคข","fear":"๐Ÿ˜ฑ","joy":"๐Ÿ˜„","neutral":"๐Ÿ˜","sadness":"๐Ÿ˜ข","surprise":"๐Ÿ˜ฒ"
 
 
 
 
 
 
40
  }
41
 
42
  SENTIMENT_MAP = {
43
+ "1":"๐Ÿ˜ž Negative",
44
+ "2":"๐Ÿ˜ Slightly Negative",
45
+ "3":"๐Ÿ˜ Neutral",
46
+ "4":"๐Ÿ˜ƒ Positive",
47
+ "5":"๐Ÿ˜ƒ Very Positive"
48
  }
49
 
50
  # 5. Full pipeline
51
  def analyze_text_visual(hinglish_text: str):
 
52
  hindi_text = normalize_hindi(hinglish_to_hindi(hinglish_text))
53
 
54
+ # Emotion analysis
55
  emotion_scores = emotion_model(hindi_text)
56
  top_emotion = max(emotion_scores[0], key=lambda x: x['score'])
 
57
 
58
+ # Sentiment analysis
59
  sentiment_scores = sentiment_model(hindi_text)
60
  top_sentiment = max(sentiment_scores[0], key=lambda x: x['score'])
 
61
 
62
+ # Prepare user-friendly output
63
+ output = {
64
+ "Hinglish Input": hinglish_text,
65
  "Hindi Text": hindi_text,
66
+ "Top Emotion": f"{EMOJI_MAP.get(top_emotion['label'], '')} {top_emotion['label']} ({top_emotion['score']:.2f})",
67
+ "Top Sentiment": f"{SENTIMENT_MAP.get(str(int(top_sentiment['label'][-1])), '')} ({top_sentiment['score']:.2f})"
68
  }
69
+ return output
70
 
71
+ # 6. Gradio interface
72
  iface = gr.Interface(
73
  fn=analyze_text_visual,
74
+ inputs="text",
75
+ outputs="json",
76
+ title="Hinglish โ†’ Hindi Emotion & Sentiment Analysis",
77
+ description="Type Hinglish text, see Hindi transliteration, top emotion and sentiment with emojis."
 
 
 
 
78
  )
79
 
80
  iface.launch()
81
+