skmdud commited on
Commit
edfe17e
·
verified ·
1 Parent(s): ebabd9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -36
app.py CHANGED
@@ -1,54 +1,60 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  from indic_transliteration import sanscript
4
  from indic_transliteration.sanscript import transliterate
 
5
 
6
- # Emotion Model
7
  emotion_model = pipeline(
8
  "text-classification",
9
- model="vashuag/HindiEmotion",
10
- return_all_scores=True
11
- )
12
-
13
- # Sentiment Model
14
- sentiment_model = pipeline(
15
- "text-classification",
16
- model="LondonStory/txlm-roberta-hindi-sentiment",
17
  return_all_scores=True
18
  )
19
 
20
- # Transliterate Hinglish Hindi
21
- def hinglish_to_hindi(text):
22
  return transliterate(text, sanscript.ITRANS, sanscript.DEVANAGARI)
23
 
24
- # Full Pipeline
25
- def analyze_text(hinglish_text):
26
- raw_hindi = hinglish_to_hindi(hinglish_text)
27
- clean_hindi = raw_hindi # Can insert normalization if needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- emotions = emotion_model(clean_hindi)[0]
30
- sentiments = sentiment_model(clean_hindi)[0]
31
 
 
 
 
 
32
  return {
33
  "Hinglish Input": hinglish_text,
34
- "Raw Hindi": raw_hindi,
35
- "Emotion Scores": {e["label"]: round(e["score"], 3) for e in emotions},
36
- "Sentiment Scores": {s["label"]: round(s["score"], 3) for s in sentiments}
37
  }
38
 
39
- iface = gr.Interface(
40
- fn=analyze_text,
41
- inputs=gr.Textbox(label="Enter Hinglish Text"),
42
- outputs="json",
43
- title="Hinglish → Hindi Emotion & Sentiment Analyzer",
44
- description=(
45
- "This app uses **documented and public models**:\n"
46
- "- **vashuag/HindiEmotion** for emotion classification\n"
47
- "- **LondonStory/txlm-roberta-hindi-sentiment** for sentiment (3-class)\n"
48
- "Both are transparent, reliable, and well-evaluated on Hindi data."
49
- )
50
- )
51
-
52
- if __name__ == "__main__":
53
- iface.launch()
54
 
 
 
1
  import gradio as gr
 
2
  from indic_transliteration import sanscript
3
  from indic_transliteration.sanscript import transliterate
4
+ from transformers import pipeline
5
 
6
+ # 1. Load Hindi Emotion Detection Model
7
  emotion_model = pipeline(
8
  "text-classification",
9
+ model="s-nlp/HiEmotions", # Replace with a public, reliable Hindi emotion model
 
 
 
 
 
 
 
10
  return_all_scores=True
11
  )
12
 
13
+ # 2. Basic Hinglish -> Hindi transliteration
14
+ def hinglish_to_hindi(text: str) -> str:
15
  return transliterate(text, sanscript.ITRANS, sanscript.DEVANAGARI)
16
 
17
+ # 3. Normalization layer (common spelling fixes)
18
+ def normalize_hindi(text: str) -> str:
19
+ corrections = {
20
+ "उदस्": "उदास",
21
+ "प्यर्": "प्यार",
22
+ "भाइयोन्": "भाइयों",
23
+ "कित्न": "कितना",
24
+ "टुझे": "तुझे"
25
+ }
26
+ for wrong, right in corrections.items():
27
+ text = text.replace(wrong, right)
28
+ return text
29
+
30
+ # 4. Full pipeline
31
+ def analyze_emotion(hinglish_text: str):
32
+ # Step 1: Transliterate Hinglish → Hindi
33
+ hindi_text = hinglish_to_hindi(hinglish_text)
34
+
35
+ # Step 2: Normalize Hindi text
36
+ clean_text = normalize_hindi(hindi_text)
37
 
38
+ # Step 3: Predict emotion
39
+ emotion_results = emotion_model(clean_text)
40
 
41
+ # Step 4: Find highest emotion
42
+ top_emotion = max(emotion_results[0], key=lambda x: x['score'])
43
+
44
+ # Step 5: Summarize output for user
45
  return {
46
  "Hinglish Input": hinglish_text,
47
+ "Clean Hindi": clean_text,
48
+ "Predicted Emotion": top_emotion['label'],
49
+ "Confidence": round(top_emotion['score'], 3)
50
  }
51
 
52
+ # 5. Gradio UI
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown("## Hybrid Emotion Detection App (Hinglish → Hindi)")
55
+ inp = gr.Textbox(label="Enter Hinglish Text")
56
+ out = gr.JSON(label="Emotion Analysis Result")
57
+ btn = gr.Button("Analyze Emotion")
58
+ btn.click(fn=analyze_emotion, inputs=inp, outputs=out)
 
 
 
 
 
 
 
 
59
 
60
+ demo.launch()