Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,16 +3,17 @@ from indic_transliteration import sanscript
|
|
3 |
from indic_transliteration.sanscript import transliterate
|
4 |
from transformers import pipeline
|
5 |
|
6 |
-
# 1. Load
|
|
|
7 |
emotion_model = pipeline(
|
8 |
"text-classification",
|
9 |
-
model="
|
10 |
return_all_scores=True
|
11 |
)
|
12 |
|
13 |
sentiment_model = pipeline(
|
14 |
"text-classification",
|
15 |
-
model="
|
16 |
return_all_scores=True
|
17 |
)
|
18 |
|
@@ -33,55 +34,48 @@ def normalize_hindi(text: str) -> str:
|
|
33 |
text = text.replace(wrong, right)
|
34 |
return text
|
35 |
|
36 |
-
# 4. Emoji
|
37 |
EMOJI_MAP = {
|
38 |
-
"anger":
|
39 |
-
"disgust": "๐คข",
|
40 |
-
"fear": "๐ฑ",
|
41 |
-
"joy": "๐",
|
42 |
-
"neutral": "๐",
|
43 |
-
"sadness": "๐ข",
|
44 |
-
"surprise": "๐ฒ"
|
45 |
}
|
46 |
|
47 |
SENTIMENT_MAP = {
|
48 |
-
"
|
49 |
-
"
|
50 |
-
"
|
|
|
|
|
51 |
}
|
52 |
|
53 |
# 5. Full pipeline
|
54 |
def analyze_text_visual(hinglish_text: str):
|
55 |
-
# Hinglish โ Hindi โ Normalize
|
56 |
hindi_text = normalize_hindi(hinglish_to_hindi(hinglish_text))
|
57 |
|
58 |
-
# Emotion
|
59 |
emotion_scores = emotion_model(hindi_text)
|
60 |
top_emotion = max(emotion_scores[0], key=lambda x: x['score'])
|
61 |
-
emotion_result = f"{EMOJI_MAP.get(top_emotion['label'], '')} {top_emotion['label']} ({top_emotion['score']:.2f})"
|
62 |
|
63 |
-
# Sentiment
|
64 |
sentiment_scores = sentiment_model(hindi_text)
|
65 |
top_sentiment = max(sentiment_scores[0], key=lambda x: x['score'])
|
66 |
-
sentiment_result = f"{SENTIMENT_MAP.get(top_sentiment['label'], '')} ({top_sentiment['score']:.2f})"
|
67 |
|
68 |
-
|
|
|
|
|
69 |
"Hindi Text": hindi_text,
|
70 |
-
"Top Emotion":
|
71 |
-
"Top Sentiment":
|
72 |
}
|
|
|
73 |
|
74 |
-
# 6. Gradio
|
75 |
iface = gr.Interface(
|
76 |
fn=analyze_text_visual,
|
77 |
-
inputs=
|
78 |
-
outputs=
|
79 |
-
|
80 |
-
|
81 |
-
gr.Textbox(label="Top Sentiment")
|
82 |
-
],
|
83 |
-
title="Hindi Emotion & Sentiment Analyzer",
|
84 |
-
description="Hinglish input เคธเฅ Hindi เคฎเฅเค convert เคเคฐเคเฅ Emotion เคเคฐ Sentiment เคฆเคฟเคเคพเคเคเฅค"
|
85 |
)
|
86 |
|
87 |
iface.launch()
|
|
|
|
3 |
from indic_transliteration.sanscript import transliterate
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
# 1. Load public models
|
7 |
+
# Public Hindi sentiment & emotion models (authentication-free)
|
8 |
emotion_model = pipeline(
|
9 |
"text-classification",
|
10 |
+
model="l3cube-pune/hindi-emotion", # Public Hindi emotion model
|
11 |
return_all_scores=True
|
12 |
)
|
13 |
|
14 |
sentiment_model = pipeline(
|
15 |
"text-classification",
|
16 |
+
model="nlptown/bert-base-multilingual-uncased-sentiment", # Multilingual sentiment model
|
17 |
return_all_scores=True
|
18 |
)
|
19 |
|
|
|
34 |
text = text.replace(wrong, right)
|
35 |
return text
|
36 |
|
37 |
+
# 4. Emoji mapping
|
38 |
EMOJI_MAP = {
|
39 |
+
"anger":"๐ก","disgust":"๐คข","fear":"๐ฑ","joy":"๐","neutral":"๐","sadness":"๐ข","surprise":"๐ฒ"
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
}
|
41 |
|
42 |
SENTIMENT_MAP = {
|
43 |
+
"1":"๐ Negative",
|
44 |
+
"2":"๐ Slightly Negative",
|
45 |
+
"3":"๐ Neutral",
|
46 |
+
"4":"๐ Positive",
|
47 |
+
"5":"๐ Very Positive"
|
48 |
}
|
49 |
|
50 |
# 5. Full pipeline
|
51 |
def analyze_text_visual(hinglish_text: str):
|
|
|
52 |
hindi_text = normalize_hindi(hinglish_to_hindi(hinglish_text))
|
53 |
|
54 |
+
# Emotion analysis
|
55 |
emotion_scores = emotion_model(hindi_text)
|
56 |
top_emotion = max(emotion_scores[0], key=lambda x: x['score'])
|
|
|
57 |
|
58 |
+
# Sentiment analysis
|
59 |
sentiment_scores = sentiment_model(hindi_text)
|
60 |
top_sentiment = max(sentiment_scores[0], key=lambda x: x['score'])
|
|
|
61 |
|
62 |
+
# Prepare user-friendly output
|
63 |
+
output = {
|
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=analyze_text_visual,
|
74 |
+
inputs="text",
|
75 |
+
outputs="json",
|
76 |
+
title="Hinglish โ Hindi Emotion & Sentiment Analysis",
|
77 |
+
description="Type Hinglish text, see Hindi transliteration, top emotion and sentiment with emojis."
|
|
|
|
|
|
|
|
|
78 |
)
|
79 |
|
80 |
iface.launch()
|
81 |
+
|