Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,52 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
-
from deep_translator import GoogleTranslator
|
| 4 |
-
from langdetect import detect
|
| 5 |
-
import torch
|
| 6 |
-
|
| 7 |
-
# Load model & tokenizer
|
| 8 |
-
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
-
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 11 |
-
|
| 12 |
-
# Sentiment labels
|
| 13 |
-
emotion_labels = {
|
| 14 |
-
0: "Negative π",
|
| 15 |
-
1: "Neutral π",
|
| 16 |
-
2: "Positive π"
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
# Translator
|
| 20 |
-
translator = GoogleTranslator(source="auto", target="en")
|
| 21 |
-
|
| 22 |
-
def analyze_sentiment(user_input):
|
| 23 |
-
# Detect language
|
| 24 |
-
detected_language = detect(user_input)
|
| 25 |
-
|
| 26 |
-
# Translate if not English
|
| 27 |
-
translated_text = translator.translate(user_input) if detected_language != "en" else user_input
|
| 28 |
-
|
| 29 |
-
# Tokenize
|
| 30 |
-
inputs = tokenizer(translated_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 31 |
-
|
| 32 |
-
with torch.no_grad():
|
| 33 |
-
outputs = model(**inputs)
|
| 34 |
-
|
| 35 |
-
logits = outputs.logits
|
| 36 |
-
predicted_class = torch.argmax(logits, dim=-1).item()
|
| 37 |
-
emotion = emotion_labels.get(predicted_class, "Unknown")
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
if __name__ == "__main__":
|
| 56 |
-
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
from deep_translator import GoogleTranslator
|
| 4 |
+
from langdetect import detect
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Load model & tokenizer
|
| 8 |
+
MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 11 |
+
|
| 12 |
+
# Sentiment labels
|
| 13 |
+
emotion_labels = {
|
| 14 |
+
0: "Negative π",
|
| 15 |
+
1: "Neutral π",
|
| 16 |
+
2: "Positive π"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Translator
|
| 20 |
+
translator = GoogleTranslator(source="auto", target="en")
|
| 21 |
+
|
| 22 |
+
def analyze_sentiment(user_input):
|
| 23 |
+
# Detect language
|
| 24 |
+
detected_language = detect(user_input)
|
| 25 |
+
|
| 26 |
+
# Translate if not English
|
| 27 |
+
translated_text = translator.translate(user_input) if detected_language != "en" else user_input
|
| 28 |
+
|
| 29 |
+
# Tokenize
|
| 30 |
+
inputs = tokenizer(translated_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 31 |
+
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model(**inputs)
|
| 34 |
+
|
| 35 |
+
logits = outputs.logits
|
| 36 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
| 37 |
+
emotion = emotion_labels.get(predicted_class, "Unknown")
|
| 38 |
+
|
| 39 |
+
# Return only predicted sentiment
|
| 40 |
+
return emotion
|
| 41 |
+
|
| 42 |
+
# Build Gradio UI
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=analyze_sentiment,
|
| 45 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter text in any language..."),
|
| 46 |
+
outputs=gr.Label(label="Predicted Sentiment"),
|
| 47 |
+
title="π Multilingual Sentiment Analysis",
|
| 48 |
+
description="Enter text in any language. The system will auto-detect, translate to English, and predict sentiment (Positive/Neutral/Negative)."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|