skmdud commited on
Commit
f22fd35
·
verified ·
1 Parent(s): d8e7da4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -49
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 Hugging Face emotion detection model
7
- emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/bert-base-go-emotion")
8
-
9
- # Function: Transliterate + Emotion Detection
10
- def process_text(input_text):
11
- # 1. Roman Hinglish → Devanagari Hindi
12
- try:
13
- hindi_text = transliterate(input_text, sanscript.ITRANS, sanscript.DEVANAGARI)
14
- except Exception as e:
15
- hindi_text = f"[⚠️ Transliteration Error: {str(e)}]"
16
-
17
- # 2. Emotion Detection
18
- try:
19
- emotions = emotion_pipeline(input_text) # original Hinglish gives better semantic results
20
- # Convert to clean dictionary (label → score)
21
- emotion_result = {emo['label']: round(emo['score'], 3) for emo in emotions}
22
- except Exception as e:
23
- emotion_result = {"Error": str(e)}
24
-
25
- return hindi_text, emotion_result
26
-
27
-
28
- # ================== Gradio UI ==================
29
- with gr.Blocks(css=".gradio-container {background-color: #f9fafb;}") as demo:
30
- gr.Markdown(
31
- """
32
- # 🔤 Hinglish → Hindi + 😃 Emotion Detection
33
- Enter your Hinglish text below and get:
34
- 1. **Hindi Transliteration (Devanagari)**
35
- 2. **Detected Emotions**
36
- ---
37
- """
38
- )
39
-
 
 
 
 
 
 
 
 
 
 
 
 
40
  with gr.Row():
41
- input_text = gr.Textbox(
42
- label="✍️ Enter Hinglish Text",
43
- placeholder="e.g. Tujhe kitna pyar hai mujhse",
44
- lines=2
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
- # Run app
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