Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 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 |
-
|
| 12 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
plt.figure(figsize=(6, 3))
|
| 28 |
-
plt.bar(
|
| 29 |
-
plt.title(
|
| 30 |
-
plt.ylabel("
|
| 31 |
-
plt.xticks(rotation=
|
| 32 |
plt.tight_layout()
|
| 33 |
plt.savefig("emotion_chart.png")
|
| 34 |
plt.close()
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"
|
| 39 |
-
"Sentiment": label,
|
| 40 |
"Confidence": confidence,
|
| 41 |
-
"Emotions":
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
return json.dumps(result, indent=2), "emotion_chart.png"
|
| 45 |
|
| 46 |
-
# Gradio
|
| 47 |
-
|
| 48 |
fn=analyze_text,
|
| 49 |
-
inputs=gr.Textbox(
|
| 50 |
outputs=[
|
| 51 |
-
gr.JSON(label="Analysis
|
| 52 |
-
gr.Image(label="Emotion
|
| 53 |
],
|
| 54 |
-
title="K1ng Analyzer V3
|
| 55 |
-
description="Multilingual Sentiment + Emotion
|
| 56 |
-
theme="gradio/soft"
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
| 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()
|
|
|