Spaces:
Sleeping
Sleeping
import gradio as gr | |
from indic_transliteration import sanscript | |
from indic_transliteration.sanscript import transliterate | |
from transformers import pipeline | |
# 1. Emotion और Sentiment models (public, authentication-free) | |
emotion_model = pipeline( | |
"text-classification", | |
model="j-hartmann/emotion-english-distilroberta-base", | |
return_all_scores=True | |
) | |
sentiment_model = pipeline( | |
"text-classification", | |
model="nlptown/bert-base-multilingual-uncased-sentiment", | |
return_all_scores=True | |
) | |
# 2. Hinglish -> Hindi transliteration | |
def hinglish_to_hindi(text: str) -> str: | |
return transliterate(text, sanscript.ITRANS, sanscript.DEVANAGARI) | |
# 3. Basic Hindi normalization | |
def normalize_hindi(text: str) -> str: | |
corrections = { | |
"उदस्": "उदास", | |
"प्यर्": "प्यार", | |
"भाइयोन्": "भाइयों", | |
"कित्न": "कितना", | |
"टुझे": "तुझे" | |
} | |
for wrong, right in corrections.items(): | |
text = text.replace(wrong, right) | |
return text | |
# 4. Emoji mapping for quick visual | |
EMOJI_MAP = { | |
"anger":"😡","disgust":"🤢","fear":"😱","joy":"😄", | |
"neutral":"😐","sadness":"😢","surprise":"😲" | |
} | |
SENTIMENT_MAP = { | |
"1 star":"😞 Negative", | |
"2 stars":"😟 Negative", | |
"3 stars":"😐 Neutral", | |
"4 stars":"🙂 Positive", | |
"5 stars":"😃 Positive" | |
} | |
# 5. Complete pipeline | |
def analyze_text(hinglish_text: str): | |
# Transliterate + normalize | |
hindi_text = normalize_hindi(hinglish_to_hindi(hinglish_text)) | |
# Emotion prediction | |
emotions = emotion_model(hindi_text)[0] | |
top_emotion = max(emotions, key=lambda x: x['score']) | |
emotion_label = top_emotion['label'] | |
emotion_score = top_emotion['score'] | |
emoji = EMOJI_MAP.get(emotion_label.lower(), "❓") | |
# Sentiment prediction | |
sentiments = sentiment_model(hindi_text)[0] | |
top_sentiment = max(sentiments, key=lambda x: x['score']) | |
sentiment_label = top_sentiment['label'] | |
sentiment_score = top_sentiment['score'] | |
sentiment_display = SENTIMENT_MAP.get(sentiment_label, sentiment_label) | |
# Return readable summary | |
return f"हिसाब से भावनाएँ: {emotion_label} {emoji} ({emotion_score:.2f})\nसेंटिमेंट: {sentiment_display} ({sentiment_score:.2f})" | |
# 6. Gradio interface | |
iface = gr.Interface( | |
fn=analyze_text, | |
inputs="text", | |
outputs="text", | |
title="Hinglish → Hindi Emotion & Sentiment Detector", | |
description="Hinglish या Hindi text डालें, परिणाम emoji और readable format में मिलेगा।" | |
) | |
iface.launch() | |