Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,25 +3,24 @@ from indic_transliteration import sanscript
|
|
3 |
from indic_transliteration.sanscript import transliterate
|
4 |
from transformers import pipeline
|
5 |
|
6 |
-
# 1.
|
7 |
-
# Public Hindi sentiment & emotion models (authentication-free)
|
8 |
emotion_model = pipeline(
|
9 |
"text-classification",
|
10 |
-
model="
|
11 |
return_all_scores=True
|
12 |
)
|
13 |
|
14 |
sentiment_model = pipeline(
|
15 |
"text-classification",
|
16 |
-
model="nlptown/bert-base-multilingual-uncased-sentiment",
|
17 |
return_all_scores=True
|
18 |
)
|
19 |
|
20 |
-
# 2. Hinglish -> Hindi
|
21 |
def hinglish_to_hindi(text: str) -> str:
|
22 |
return transliterate(text, sanscript.ITRANS, sanscript.DEVANAGARI)
|
23 |
|
24 |
-
# 3.
|
25 |
def normalize_hindi(text: str) -> str:
|
26 |
corrections = {
|
27 |
"उदस्": "उदास",
|
@@ -34,47 +33,49 @@ def normalize_hindi(text: str) -> str:
|
|
34 |
text = text.replace(wrong, right)
|
35 |
return text
|
36 |
|
37 |
-
# 4. Emoji mapping
|
38 |
EMOJI_MAP = {
|
39 |
-
"anger":"😡","disgust":"🤢","fear":"😱","joy":"😄",
|
|
|
40 |
}
|
41 |
|
42 |
SENTIMENT_MAP = {
|
43 |
-
"1":"😞 Negative",
|
44 |
-
"2":"
|
45 |
-
"3":"😐 Neutral",
|
46 |
-
"4":"
|
47 |
-
"5":"😃
|
48 |
}
|
49 |
|
50 |
-
# 5.
|
51 |
-
def
|
|
|
52 |
hindi_text = normalize_hindi(hinglish_to_hindi(hinglish_text))
|
53 |
|
54 |
-
# Emotion
|
55 |
-
|
56 |
-
top_emotion = max(
|
|
|
|
|
|
|
57 |
|
58 |
-
# Sentiment
|
59 |
-
|
60 |
-
top_sentiment = max(
|
|
|
|
|
|
|
61 |
|
62 |
-
#
|
63 |
-
|
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=
|
74 |
inputs="text",
|
75 |
-
outputs="
|
76 |
-
title="Hinglish → Hindi Emotion & Sentiment
|
77 |
-
description="
|
78 |
)
|
79 |
|
80 |
iface.launch()
|
|
|
3 |
from indic_transliteration.sanscript import transliterate
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
# 1. Emotion और Sentiment models (public, authentication-free)
|
|
|
7 |
emotion_model = pipeline(
|
8 |
"text-classification",
|
9 |
+
model="j-hartmann/emotion-english-distilroberta-base",
|
10 |
return_all_scores=True
|
11 |
)
|
12 |
|
13 |
sentiment_model = pipeline(
|
14 |
"text-classification",
|
15 |
+
model="nlptown/bert-base-multilingual-uncased-sentiment",
|
16 |
return_all_scores=True
|
17 |
)
|
18 |
|
19 |
+
# 2. Hinglish -> Hindi transliteration
|
20 |
def hinglish_to_hindi(text: str) -> str:
|
21 |
return transliterate(text, sanscript.ITRANS, sanscript.DEVANAGARI)
|
22 |
|
23 |
+
# 3. Basic Hindi normalization
|
24 |
def normalize_hindi(text: str) -> str:
|
25 |
corrections = {
|
26 |
"उदस्": "उदास",
|
|
|
33 |
text = text.replace(wrong, right)
|
34 |
return text
|
35 |
|
36 |
+
# 4. Emoji mapping for quick visual
|
37 |
EMOJI_MAP = {
|
38 |
+
"anger":"😡","disgust":"🤢","fear":"😱","joy":"😄",
|
39 |
+
"neutral":"😐","sadness":"😢","surprise":"😲"
|
40 |
}
|
41 |
|
42 |
SENTIMENT_MAP = {
|
43 |
+
"1 star":"😞 Negative",
|
44 |
+
"2 stars":"😟 Negative",
|
45 |
+
"3 stars":"😐 Neutral",
|
46 |
+
"4 stars":"🙂 Positive",
|
47 |
+
"5 stars":"😃 Positive"
|
48 |
}
|
49 |
|
50 |
+
# 5. Complete pipeline
|
51 |
+
def analyze_text(hinglish_text: str):
|
52 |
+
# Transliterate + normalize
|
53 |
hindi_text = normalize_hindi(hinglish_to_hindi(hinglish_text))
|
54 |
|
55 |
+
# Emotion prediction
|
56 |
+
emotions = emotion_model(hindi_text)[0]
|
57 |
+
top_emotion = max(emotions, key=lambda x: x['score'])
|
58 |
+
emotion_label = top_emotion['label']
|
59 |
+
emotion_score = top_emotion['score']
|
60 |
+
emoji = EMOJI_MAP.get(emotion_label.lower(), "❓")
|
61 |
|
62 |
+
# Sentiment prediction
|
63 |
+
sentiments = sentiment_model(hindi_text)[0]
|
64 |
+
top_sentiment = max(sentiments, key=lambda x: x['score'])
|
65 |
+
sentiment_label = top_sentiment['label']
|
66 |
+
sentiment_score = top_sentiment['score']
|
67 |
+
sentiment_display = SENTIMENT_MAP.get(sentiment_label, sentiment_label)
|
68 |
|
69 |
+
# Return readable summary
|
70 |
+
return f"हिसाब से भावनाएँ: {emotion_label} {emoji} ({emotion_score:.2f})\nसेंटिमेंट: {sentiment_display} ({sentiment_score:.2f})"
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# 6. Gradio interface
|
73 |
iface = gr.Interface(
|
74 |
+
fn=analyze_text,
|
75 |
inputs="text",
|
76 |
+
outputs="text",
|
77 |
+
title="Hinglish → Hindi Emotion & Sentiment Detector",
|
78 |
+
description="Hinglish या Hindi text डालें, परिणाम emoji और readable format में मिलेगा।"
|
79 |
)
|
80 |
|
81 |
iface.launch()
|