Spaces:
Sleeping
Sleeping
File size: 2,717 Bytes
3b1930e 3d3dd7e edfe17e 5a57a8a 75e0995 f4e1fad 75e0995 f4e1fad 75e0995 f4e1fad 9bafa86 75e0995 edfe17e f22fd35 75e0995 edfe17e f4e1fad edfe17e 75e0995 a870cad 75e0995 a870cad f4e1fad 75e0995 f4e1fad 75e0995 a870cad 75e0995 a870cad 75e0995 a870cad 75e0995 a870cad 7a2ac4c a870cad 75e0995 7a2ac4c 75e0995 a870cad 7a2ac4c |
1 2 3 4 5 6 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
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()
|