Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +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 |
-
# Load
|
7 |
-
|
8 |
-
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
with gr.Row():
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
)
|
46 |
-
|
47 |
-
with gr.Row():
|
48 |
-
output_hindi = gr.Textbox(label="📝 Hindi Transliteration", interactive=False)
|
49 |
-
output_emotion = gr.JSON(label="🎭 Detected Emotions")
|
50 |
-
|
51 |
-
run_button = gr.Button("🚀 Run Analysis", variant="primary")
|
52 |
-
run_button.click(process_text, inputs=input_text, outputs=[output_hindi, output_emotion])
|
53 |
|
54 |
-
|
55 |
-
demo.launch()
|
56 |
|
|
|
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", # Hindi emotion detection 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 |
+
normalized = normalize_hindi(hindi_text)
|
37 |
+
|
38 |
+
# Step 3: Predict Emotion
|
39 |
+
results = emotion_model(normalized)
|
40 |
+
|
41 |
+
# Prepare output
|
42 |
+
return {
|
43 |
+
"Input (Hinglish)": hinglish_text,
|
44 |
+
"Transliterated (Raw Hindi)": hindi_text,
|
45 |
+
"Normalized (Clean Hindi)": normalized,
|
46 |
+
"Predicted Emotions": results
|
47 |
+
}
|
48 |
+
|
49 |
+
# 5. Gradio UI
|
50 |
+
with gr.Blocks() as demo:
|
51 |
+
gr.Markdown("## Hinglish → Hindi Transliteration + Emotion Detection")
|
52 |
with gr.Row():
|
53 |
+
hinglish_in = gr.Textbox(label="Enter Hinglish Text", placeholder="e.g. Aam pataa nahi dil kuchh udas udas sa hai bhaaiyon")
|
54 |
+
output = gr.JSON(label="Results")
|
55 |
+
submit_btn = gr.Button("Analyze")
|
56 |
+
submit_btn.click(analyze_emotion, inputs=hinglish_in, outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
if __name__ == "__main__":
|
59 |
+
demo.launch()
|
60 |
|