MitchelleK1ng commited on
Commit
5b74429
Β·
verified Β·
1 Parent(s): df362b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -25
app.py CHANGED
@@ -1,31 +1,42 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
-
5
- # Load model and tokenizer
6
- model_name = "distilbert-base-uncased-finetuned-sst-2-english"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
-
10
- # Define prediction function
11
- def analyze_sentiment(text):
12
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
13
- with torch.no_grad():
14
- outputs = model(**inputs)
15
- probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
16
- label = torch.argmax(probs).item()
17
- confidence = torch.max(probs).item()
18
- sentiment = "Positive 😊" if label == 1 else "Negative 😞"
19
- return {"Sentiment": sentiment, "Confidence": round(confidence, 4)}
20
-
21
- # Build Gradio app
 
 
 
 
 
 
 
 
 
 
22
  demo = gr.Interface(
23
- fn=analyze_sentiment,
24
- inputs=gr.Textbox(lines=2, placeholder="Type your text here..."),
25
- outputs="json",
26
- title="Sentiment Analyzer",
27
- description="A fine-tuned DistilBERT model for sentiment classification."
28
  )
29
 
30
  if __name__ == "__main__":
31
  demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ import pandas as pd
4
+
5
+ # Load multilingual emotion model
6
+ emotion_model = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
7
+ sentiment_model = pipeline("sentiment-analysis", model="distilbert-base-multilingual-cased")
8
+
9
+ def analyze_text(text):
10
+ # Sentiment
11
+ sentiment = sentiment_model(text)[0]
12
+ sentiment_label = sentiment["label"]
13
+ sentiment_score = round(sentiment["score"], 4)
14
+
15
+ # Emotions
16
+ emotion_results = emotion_model(text)[0]
17
+ df = pd.DataFrame(emotion_results)
18
+ df["score"] = df["score"].round(4)
19
+
20
+ # Prepare response
21
+ emotions_dict = {row["label"]: row["score"] for _, row in df.iterrows()}
22
+
23
+ result = {
24
+ "Sentiment": f"{sentiment_label} 😊" if sentiment_label == "POSITIVE" else f"{sentiment_label} 😞",
25
+ "Confidence": sentiment_score,
26
+ "Emotions": emotions_dict
27
+ }
28
+
29
+ return result, gr.BarPlot(value=df, x="label", y="score", title="Emotion Confidence")
30
+
31
+ # Gradio Interface
32
  demo = gr.Interface(
33
+ fn=analyze_text,
34
+ inputs=gr.Textbox(label="Enter text in any language"),
35
+ outputs=[gr.JSON(label="Analysis Result"), gr.BarPlot(label="Emotion Breakdown")],
36
+ title="K1ng Analyzer V3 🌍",
37
+ description="Now detects sentiment + emotional tone (joy, anger, sadness, fear, surprise) across multiple languages."
38
  )
39
 
40
  if __name__ == "__main__":
41
  demo.launch()
42
+