MitchelleK1ng commited on
Commit
6ab1c15
Β·
verified Β·
1 Parent(s): 701e572

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -42
app.py CHANGED
@@ -1,60 +1,57 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import json
4
  import matplotlib.pyplot as plt
5
 
6
- # Load models
7
- sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
8
- emotion_analyzer = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
9
- language_detector = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
10
 
11
- def analyze_text(text):
12
- # Detect language
13
- lang_result = language_detector(text)[0]
14
- detected_lang = lang_result['label']
15
-
16
- # Sentiment
17
- sentiment = sentiment_analyzer(text)[0]
18
- label = "Positive 😊" if sentiment["label"] == "POSITIVE" else "Negative 😞"
19
- confidence = round(sentiment["score"], 4)
20
-
21
- # Emotions
22
- emotion_scores = emotion_analyzer(text)[0]
23
- emotions = {e['label']: round(e['score'], 4) for e in emotion_scores}
24
- dominant = max(emotions, key=emotions.get)
25
 
26
- # Plot emotions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  plt.figure(figsize=(6, 3))
28
- plt.bar(emotions.keys(), emotions.values(), color='skyblue')
29
- plt.title(f"Emotion Breakdown (Dominant: {dominant})")
30
- plt.ylabel("Confidence")
31
- plt.xticks(rotation=30)
32
  plt.tight_layout()
33
  plt.savefig("emotion_chart.png")
34
  plt.close()
35
 
36
- # JSON output
37
- result = {
38
- "Detected Language": detected_lang,
39
- "Sentiment": label,
40
  "Confidence": confidence,
41
- "Emotions": emotions
42
- }
43
-
44
- return json.dumps(result, indent=2), "emotion_chart.png"
45
 
46
- # Gradio UI
47
- iface = gr.Interface(
48
  fn=analyze_text,
49
- inputs=gr.Textbox(lines=3, placeholder="Type your text here..."),
50
  outputs=[
51
- gr.JSON(label="Analysis Results"),
52
- gr.Image(label="Emotion Visualization")
53
  ],
54
- title="K1ng Analyzer V3 πŸ‘‘",
55
- description="Multilingual Sentiment + Emotion + Language Analyzer with Visualization",
56
- theme="gradio/soft"
57
  )
58
 
59
- iface.launch()
60
-
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from langdetect import detect
4
  import matplotlib.pyplot as plt
5
 
6
+ # Load multilingual sentiment model
7
+ sentiment_model = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
 
 
8
 
9
+ # Emotion model (English-based)
10
+ emotion_model = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ def analyze_text(text):
13
+ try:
14
+ # Detect language
15
+ lang = detect(text)
16
+ except:
17
+ lang = "unknown"
18
+
19
+ # Sentiment analysis
20
+ sentiment_result = sentiment_model(text)[0]
21
+ sentiment_label = sentiment_result['label']
22
+ confidence = round(sentiment_result['score'], 4)
23
+
24
+ # Emotion analysis
25
+ emotions = emotion_model(text)[0]
26
+ emotion_scores = {e["label"]: round(e["score"], 4) for e in emotions}
27
+
28
+ # Plot emotion chart
29
  plt.figure(figsize=(6, 3))
30
+ plt.bar(emotion_scores.keys(), emotion_scores.values(), color="skyblue")
31
+ plt.title("Emotion Confidence Levels")
32
+ plt.ylabel("Score")
33
+ plt.xticks(rotation=45)
34
  plt.tight_layout()
35
  plt.savefig("emotion_chart.png")
36
  plt.close()
37
 
38
+ return {
39
+ "Detected Language": lang,
40
+ "Sentiment": sentiment_label,
 
41
  "Confidence": confidence,
42
+ "Emotions": emotion_scores
43
+ }, "emotion_chart.png"
 
 
44
 
45
+ # Gradio interface
46
+ demo = gr.Interface(
47
  fn=analyze_text,
48
+ inputs=gr.Textbox(label="Enter text to analyze"),
49
  outputs=[
50
+ gr.JSON(label="Analysis Result"),
51
+ gr.Image(label="Emotion Confidence Chart")
52
  ],
53
+ title="K1ng Analyzer V3 🌍🧠",
54
+ description="Multilingual Sentiment + Emotion Analyzer with Visualization"
 
55
  )
56
 
57
+ demo.launch()