Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
# Load
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
demo = gr.Interface(
|
| 23 |
-
fn=
|
| 24 |
-
inputs=gr.Textbox(
|
| 25 |
-
outputs="
|
| 26 |
-
title="
|
| 27 |
-
description="
|
| 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 |
+
|