selva1909 commited on
Commit
02c39b2
Β·
verified Β·
1 Parent(s): 97f1d09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -56
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
- return {
40
- "Original Text": user_input,
41
- "Translated Text": translated_text,
42
- "Detected Language": detected_language,
43
- "Predicted Sentiment": emotion
44
- }
45
-
46
- # Build Gradio UI
47
- iface = gr.Interface(
48
- fn=analyze_sentiment,
49
- inputs=gr.Textbox(lines=3, placeholder="Enter text in any language..."),
50
- outputs="json",
51
- title="🌍 Multilingual Sentiment Analysis",
52
- description="Enter text in any language. The system will auto-detect, translate to English, and predict sentiment (Positive/Neutral/Negative)."
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()