Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import tensorflow as tf | |
| from tensorflow.keras.preprocessing.sequence import pad_sequences | |
| from sklearn.preprocessing import LabelEncoder | |
| import gradio as gr | |
| import pickle | |
| import matplotlib.pyplot as plt | |
| from matplotlib import cm | |
| import pandas as pd | |
| import time | |
| import json | |
| # Load model and tokenizer | |
| model = tf.keras.models.load_model('sentiment_rnn.h5') | |
| # Load tokenizer | |
| with open('tokenizer.pkl', 'rb') as f: | |
| tokenizer = pickle.load(f) | |
| # Initialize label encoder | |
| label_encoder = LabelEncoder() | |
| label_encoder.fit(["Happy", "Sad", "Neutral"]) | |
| # Load sample data for examples | |
| sample_data = pd.read_csv("sentiment_dataset_1000.csv") | |
| def predict_sentiment(text, show_details=False): | |
| """ | |
| Predict sentiment with detailed analysis | |
| """ | |
| start_time = time.time() | |
| # Preprocess the text | |
| sequence = tokenizer.texts_to_sequences([text]) | |
| padded = pad_sequences(sequence, maxlen=50) | |
| # Make prediction | |
| prediction = model.predict(padded, verbose=0)[0] | |
| processing_time = time.time() - start_time | |
| predicted_class = np.argmax(prediction) | |
| sentiment = label_encoder.inverse_transform([predicted_class])[0] | |
| confidence = float(prediction[predicted_class]) | |
| # Create confidence dictionary | |
| confidences = { | |
| "Happy": float(prediction[0]), | |
| "Sad": float(prediction[1]), | |
| "Neutral": float(prediction[2]) | |
| } | |
| # Create visualization | |
| fig = create_confidence_plot(confidences) | |
| # Additional analysis | |
| word_count = len(text.split()) | |
| char_count = len(text) | |
| result = { | |
| "sentiment": sentiment, | |
| "confidence": round(confidence * 100, 2), | |
| "confidences": confidences, | |
| "processing_time": round(processing_time * 1000, 2), | |
| "word_count": word_count, | |
| "char_count": char_count, | |
| "plot": fig | |
| } | |
| return result | |
| def create_confidence_plot(confidences): | |
| """Create a beautiful confidence plot""" | |
| labels = list(confidences.keys()) | |
| values = list(confidences.values()) | |
| colors = cm.get_cmap('RdYlGn')(np.linspace(0.2, 0.8, len(labels))) | |
| fig, ax = plt.subplots(figsize=(8, 4)) | |
| bars = ax.barh(labels, values, color=colors) | |
| # Add value labels | |
| for bar in bars: | |
| width = bar.get_width() | |
| ax.text(width + 0.02, bar.get_y() + bar.get_height()/2, | |
| f'{width:.2%}', | |
| ha='left', va='center', fontsize=10) | |
| ax.set_xlim(0, 1) | |
| ax.set_title('Sentiment Confidence Scores', pad=20) | |
| ax.spines['top'].set_visible(False) | |
| ax.spines['right'].set_visible(False) | |
| ax.spines['left'].set_visible(False) | |
| ax.spines['bottom'].set_visible(False) | |
| ax.grid(axis='x', linestyle='--', alpha=0.7) | |
| ax.set_facecolor('#f8f9fa') | |
| fig.patch.set_facecolor('#f8f9fa') | |
| return fig | |
| def get_sentiment_emoji(sentiment): | |
| """Get emoji for sentiment""" | |
| emojis = { | |
| "Happy": "π", | |
| "Sad": "π’", | |
| "Neutral": "π" | |
| } | |
| return emojis.get(sentiment, "") | |
| def analyze_text(text): | |
| """Main analysis function""" | |
| result = predict_sentiment(text) | |
| emoji = get_sentiment_emoji(result["sentiment"]) | |
| # Create HTML output | |
| html_output = f""" | |
| <div style="background-color:#f8f9fa; padding:20px; border-radius:10px; margin-bottom:20px;"> | |
| <h2 style="color:#2c3e50; margin-top:0;">Analysis Result {emoji}</h2> | |
| <p><strong>Text:</strong> {text[:200]}{'...' if len(text) > 200 else ''}</p> | |
| <p><strong>Sentiment:</strong> <span style="font-weight:bold; color:{'#27ae60' if result['sentiment'] == 'Happy' else '#e74c3c' if result['sentiment'] == 'Sad' else '#3498db'}">{result['sentiment']}</span></p> | |
| <p><strong>Confidence:</strong> {result['confidence']}%</p> | |
| <p><strong>Processing Time:</strong> {result['processing_time']} ms</p> | |
| <p><strong>Word Count:</strong> {result['word_count']}</p> | |
| <p><strong>Character Count:</strong> {result['char_count']}</p> | |
| </div> | |
| """ | |
| return html_output, result['plot'], json.dumps(result['confidences'], indent=2) | |
| # Create Gradio interface | |
| with gr.Blocks(theme=gr.themes.Soft(), title="Sentiment Analysis Dashboard") as demo: | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown(""" | |
| # π Sentiment Analysis Dashboard | |
| **Analyze text for emotional sentiment** using our advanced RNN model. | |
| """) | |
| with gr.Group(): | |
| text_input = gr.Textbox( | |
| label="Enter your text", | |
| placeholder="Type something to analyze its sentiment...", | |
| lines=4, | |
| max_lines=8 | |
| ) | |
| analyze_btn = gr.Button("Analyze", variant="primary") | |
| with gr.Accordion("Advanced Settings", open=False): | |
| show_details = gr.Checkbox( | |
| label="Show detailed analysis", | |
| value=True | |
| ) | |
| gr.Markdown("### Try these examples:") | |
| examples = gr.Examples( | |
| examples=[ | |
| ["I'm feeling great today!"], | |
| ["My dog passed away..."], | |
| ["The office is closed tomorrow."], | |
| ["This is the best day ever!"], | |
| ["I feel completely devastated."], | |
| ["The meeting is scheduled for 2 PM."] | |
| ], | |
| inputs=[text_input], | |
| label="Quick Examples" | |
| ) | |
| with gr.Column(scale=2): | |
| with gr.Tab("Results"): | |
| html_output = gr.HTML(label="Analysis Summary") | |
| plot_output = gr.Plot(label="Confidence Distribution") | |
| with gr.Tab("Raw Data"): | |
| json_output = gr.JSON(label="Confidence Scores") | |
| with gr.Tab("About"): | |
| gr.Markdown(""" | |
| ## About This Dashboard | |
| This sentiment analysis tool uses a **Recurrent Neural Network (RNN)** with **LSTM** layers to classify text into three sentiment categories: | |
| - π Happy (Positive) | |
| - π’ Sad (Negative) | |
| - π Neutral | |
| **Model Details:** | |
| - Trained on 1,000 labeled examples | |
| - 64-unit LSTM layer with regularization | |
| - 92% test accuracy | |
| **How to use:** | |
| 1. Type or paste text in the input box | |
| 2. Click "Analyze" or press Enter | |
| 3. View the sentiment analysis results | |
| **Try the examples above for quick testing!** | |
| """) | |
| # Event handlers | |
| analyze_btn.click( | |
| fn=analyze_text, | |
| inputs=[text_input], | |
| outputs=[html_output, plot_output, json_output] | |
| ) | |
| text_input.submit( | |
| fn=analyze_text, | |
| inputs=[text_input], | |
| outputs=[html_output, plot_output, json_output] | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| demo.launch() |