Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
5 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
6 |
+
from sklearn.preprocessing import LabelEncoder
|
7 |
+
import gradio as gr
|
8 |
+
import pickle
|
9 |
+
import os
|
10 |
+
|
11 |
+
# Load model and tokenizer
|
12 |
+
model = tf.keras.models.load_model('sentiment_rnn.h5')
|
13 |
+
|
14 |
+
# Load tokenizer
|
15 |
+
with open('tokenizer.pkl', 'rb') as f:
|
16 |
+
tokenizer = pickle.load(f)
|
17 |
+
|
18 |
+
# Initialize label encoder
|
19 |
+
label_encoder = LabelEncoder()
|
20 |
+
label_encoder.fit(["Happy", "Sad", "Neutral"])
|
21 |
+
|
22 |
+
def predict_sentiment(text):
|
23 |
+
"""
|
24 |
+
Predict sentiment for a given text
|
25 |
+
"""
|
26 |
+
# Preprocess the text
|
27 |
+
sequence = tokenizer.texts_to_sequences([text])
|
28 |
+
padded = pad_sequences(sequence, maxlen=50)
|
29 |
+
|
30 |
+
# Make prediction
|
31 |
+
prediction = model.predict(padded, verbose=0)[0]
|
32 |
+
predicted_class = np.argmax(prediction)
|
33 |
+
sentiment = label_encoder.inverse_transform([predicted_class])[0]
|
34 |
+
confidence = float(prediction[predicted_class])
|
35 |
+
|
36 |
+
# Create confidence dictionary for all classes
|
37 |
+
confidences = {
|
38 |
+
"Happy": float(prediction[0]),
|
39 |
+
"Sad": float(prediction[1]),
|
40 |
+
"Neutral": float(prediction[2])
|
41 |
+
}
|
42 |
+
|
43 |
+
return sentiment, confidences
|
44 |
+
|
45 |
+
# Create Gradio interface
|
46 |
+
with gr.Blocks(title="Sentiment Analysis with RNN") as demo:
|
47 |
+
gr.Markdown("# Sentiment Analysis with RNN")
|
48 |
+
gr.Markdown("Enter text to analyze its sentiment (Happy, Sad, or Neutral)")
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
text_input = gr.Textbox(label="Input Text", placeholder="Type your text here...")
|
52 |
+
sentiment_output = gr.Label(label="Predicted Sentiment")
|
53 |
+
confidence_output = gr.Label(label="Confidence Scores")
|
54 |
+
|
55 |
+
submit_btn = gr.Button("Analyze Sentiment")
|
56 |
+
|
57 |
+
examples = gr.Examples(
|
58 |
+
examples=[
|
59 |
+
["I'm feeling great today!"],
|
60 |
+
["My dog passed away..."],
|
61 |
+
["The office is closed tomorrow."],
|
62 |
+
["This is the best day ever!"],
|
63 |
+
["I feel miserable."],
|
64 |
+
["There are 12 books on the shelf."]
|
65 |
+
],
|
66 |
+
inputs=text_input
|
67 |
+
)
|
68 |
+
|
69 |
+
def analyze_text(text):
|
70 |
+
sentiment, confidences = predict_sentiment(text)
|
71 |
+
return sentiment, confidences
|
72 |
+
|
73 |
+
submit_btn.click(
|
74 |
+
fn=analyze_text,
|
75 |
+
inputs=text_input,
|
76 |
+
outputs=[sentiment_output, confidence_output]
|
77 |
+
)
|
78 |
+
|
79 |
+
text_input.submit(
|
80 |
+
fn=analyze_text,
|
81 |
+
inputs=text_input,
|
82 |
+
outputs=[sentiment_output, confidence_output]
|
83 |
+
)
|
84 |
+
|
85 |
+
# Launch the app
|
86 |
+
if __name__ == "__main__":
|
87 |
+
demo.launch()
|