Spaces:
Running
Running
File size: 2,003 Bytes
f3cfe1c 848cc33 6ab1c15 848cc33 5b74429 6ab1c15 5b74429 6ab1c15 848cc33 6ab1c15 848cc33 6ab1c15 848cc33 6ab1c15 848cc33 6ab1c15 848cc33 9124263 6ab1c15 5b74429 6ab1c15 848cc33 6ab1c15 848cc33 6ab1c15 f3cfe1c 6ab1c15 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import gradio as gr
from transformers import pipeline
from langdetect import detect
import matplotlib.pyplot as plt
# Load multilingual sentiment model
sentiment_model = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
# Emotion model (English-based)
emotion_model = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
def analyze_text(text):
try:
# Detect language
lang = detect(text)
except:
lang = "unknown"
# Sentiment analysis
sentiment_result = sentiment_model(text)[0]
sentiment_label = sentiment_result['label']
confidence = round(sentiment_result['score'], 4)
# Emotion analysis
emotions = emotion_model(text)[0]
emotion_scores = {e["label"]: round(e["score"], 4) for e in emotions}
# Plot emotion chart
plt.figure(figsize=(6, 3))
plt.bar(emotion_scores.keys(), emotion_scores.values(), color="skyblue")
plt.title("Emotion Confidence Levels")
plt.ylabel("Score")
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig("emotion_chart.png")
plt.close()
return {
"Detected Language": lang,
"Sentiment": sentiment_label,
"Confidence": confidence,
"Emotions": emotion_scores
}, "emotion_chart.png"
with gr.Blocks(title="K1ng Analyzer AI") as demo:
gr.Image("K1nganalyzer_logo.png", elem_id="logo", show_label=False, height=120)
gr.Markdown("### π§ Welcome to **K1ng Analyzer AI** β Smart Multilingual Emotion & Sentiment Analyzer π")
# (Your interface components go here)
# Gradio interface
demo = gr.Interface(
fn=analyze_text,
inputs=gr.Textbox(label="Enter text to analyze"),
outputs=[
gr.JSON(label="Analysis Result"),
gr.Image(label="Emotion Confidence Chart")
],
title="K1ng Analyzer V3 ππ§ ",
description="Multilingual Sentiment + Emotion Analyzer with Visualization"
)
demo.launch()
|