Spaces:
Running
Running
| 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() | |