Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
7 |
emotion_model = pipeline(
|
8 |
"text-classification",
|
9 |
-
model="
|
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 |
-
#
|
21 |
-
def hinglish_to_hindi(text):
|
22 |
return transliterate(text, sanscript.ITRANS, sanscript.DEVANAGARI)
|
23 |
|
24 |
-
#
|
25 |
-
def
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
|
|
|
|
|
|
|
|
32 |
return {
|
33 |
"Hinglish Input": hinglish_text,
|
34 |
-
"
|
35 |
-
"Emotion
|
36 |
-
"
|
37 |
}
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
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()
|