Spaces:
Sleeping
Sleeping
File size: 2,822 Bytes
479c058 4e7df43 2ad6233 4e7df43 479c058 4e7df43 2562763 4e7df43 2562763 4e7df43 479c058 4e7df43 479c058 4e7df43 2562763 2ad6233 4e7df43 2ad6233 4e7df43 2ad6233 4e7df43 2ad6233 4e7df43 2ad6233 4e7df43 2ad6233 4e7df43 2ad6233 4e7df43 2ad6233 4e7df43 0feed92 |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import gradio as gr
from transformers import pipeline
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import json
import os
# Load sentiment analysis model
sentiment_pipeline = pipeline("sentiment-analysis")
# Store last input/output using localStorage
STATE_FILE = "state.json"
def save_state(text, sentiment, confidence):
with open(STATE_FILE, "w") as f:
json.dump({"text": text, "sentiment": sentiment, "confidence": confidence}, f)
def load_state():
if os.path.exists(STATE_FILE):
with open(STATE_FILE, "r") as f:
return json.load(f)
return {"text": "", "sentiment": "", "confidence": ""}
# Function to analyze sentiment in real-time
def analyze_sentiment(text):
if not text.strip():
return "Enter text to analyze.", "", None, ""
result = sentiment_pipeline(text)[0]
sentiment, confidence = result['label'], result['score']
sentiment_map = {
"POSITIVE": ("π’ Positive π", "green"),
"NEGATIVE": ("π΄ Negative π ", "red"),
"NEUTRAL": ("π‘ Neutral π", "orange")
}
sentiment_label, color = sentiment_map.get(sentiment.upper(), ("βͺ Unknown β", "gray"))
# Generate Word Cloud
wordcloud = WordCloud(width=400, height=200, background_color="white").generate(text)
fig, ax = plt.subplots()
ax.imshow(wordcloud, interpolation='bilinear')
ax.axis("off")
save_state(text, sentiment_label, confidence)
return sentiment_label, f"Confidence: {confidence:.2%}", fig, text
# UI Layout
with gr.Blocks(theme=gr.themes.Soft()) as iface:
gr.Markdown("# π AI Sentiment Analyzer")
gr.Markdown("Analyze sentiment in real-time with enhanced visualization.")
with gr.Row():
dark_mode = gr.Checkbox(label="π Dark Mode")
with gr.Row():
text_input = gr.Textbox(lines=3, placeholder="Type your text here...", label="Your Input", live=True)
analyze_button = gr.Button("Analyze Sentiment β¨")
reset_button = gr.Button("π Reset")
with gr.Row():
sentiment_output = gr.Label(label="Sentiment Result")
confidence_output = gr.Label(label="Confidence Score")
wordcloud_output = gr.Plot(label="Word Cloud Visualization")
# Load previous session state
state = load_state()
text_input.value, sentiment_output.value, confidence_output.value = state['text'], state['sentiment'], state['confidence']
analyze_button.click(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
reset_button.click(lambda: ("", "", None, ""), inputs=[], outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
# Launch the app
if __name__ == "__main__":
iface.launch()
|