Upload 5 files
Browse files- app.py +76 -0
- label_encoder.pkl +3 -0
- requirements.xtx +5 -0
- sentiment_model.h5 +3 -0
- tfidf_vectorizer.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import pickle
|
| 4 |
+
import re
|
| 5 |
+
import string
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# 1. Load the models and preprocessing assets
|
| 9 |
+
# Baselines
|
| 10 |
+
with open('models/nb_model.pkl', 'rb') as f:
|
| 11 |
+
nb_model = pickle.load(f)
|
| 12 |
+
with open('models/lr_model.pkl', 'rb') as f:
|
| 13 |
+
lr_model = pickle.load(f)
|
| 14 |
+
|
| 15 |
+
# Custom Deep Learning Model
|
| 16 |
+
nn_model = tf.keras.models.load_model('models/sentiment_model.h5')
|
| 17 |
+
|
| 18 |
+
# Preprocessing assets
|
| 19 |
+
with open('models/tfidf_vectorizer.pkl', 'rb') as f:
|
| 20 |
+
tfidf = pickle.load(f)
|
| 21 |
+
with open('models/label_encoder.pkl', 'rb') as f:
|
| 22 |
+
le = pickle.load(f)
|
| 23 |
+
|
| 24 |
+
# 2. Preprocessing function (must match your notebook)
|
| 25 |
+
def clean_tweet(text):
|
| 26 |
+
text = text.lower()
|
| 27 |
+
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
|
| 28 |
+
text = re.sub(r'@\w+', '', text)
|
| 29 |
+
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 30 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 31 |
+
return text
|
| 32 |
+
|
| 33 |
+
# 3. Unified prediction function
|
| 34 |
+
def predict_sentiment(tweet, model_choice):
|
| 35 |
+
cleaned_text = clean_tweet(tweet)
|
| 36 |
+
vectorized_text = tfidf.transform([cleaned_text]).toarray()
|
| 37 |
+
|
| 38 |
+
if model_choice == "Naive Bayes":
|
| 39 |
+
pred = nb_model.predict(vectorized_text)[0]
|
| 40 |
+
label = le.inverse_transform([pred])[0]
|
| 41 |
+
elif model_choice == "Logistic Regression":
|
| 42 |
+
pred = lr_model.predict(vectorized_text)[0]
|
| 43 |
+
label = le.inverse_transform([pred])[0]
|
| 44 |
+
else: # Custom MLP
|
| 45 |
+
pred_probs = nn_model.predict(vectorized_text)
|
| 46 |
+
label_idx = np.argmax(pred_probs)
|
| 47 |
+
label = le.inverse_transform([label_idx])[0]
|
| 48 |
+
|
| 49 |
+
return label
|
| 50 |
+
|
| 51 |
+
# 4. Gradio Interface Layout
|
| 52 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 53 |
+
gr.Markdown("# 🐦 Multi-Model Twitter Sentiment Analyzer")
|
| 54 |
+
gr.Markdown("Compare how different algorithms interpret the sentiment of a tweet.")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
input_text = gr.Textbox(label="Enter Tweet", placeholder="I love the new update!")
|
| 59 |
+
model_dropdown = gr.Dropdown(
|
| 60 |
+
choices=["Naive Bayes", "Logistic Regression", "Custom Neural Network (MLP)"],
|
| 61 |
+
label="Select Model",
|
| 62 |
+
value="Custom Neural Network (MLP)"
|
| 63 |
+
)
|
| 64 |
+
submit_btn = gr.Button("Analyze Sentiment")
|
| 65 |
+
|
| 66 |
+
with gr.Column():
|
| 67 |
+
output_text = gr.Label(label="Result")
|
| 68 |
+
|
| 69 |
+
submit_btn.click(
|
| 70 |
+
fn=predict_sentiment,
|
| 71 |
+
inputs=[input_text, model_dropdown],
|
| 72 |
+
outputs=output_text
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
demo.launch()
|
label_encoder.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8bae58051a52e2701279568a9a7b8d64fd7b94d9cab3581f810d096f239ce90b
|
| 3 |
+
size 288
|
requirements.xtx
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
scikit-learn
|
| 3 |
+
gradio
|
| 4 |
+
pandas
|
| 5 |
+
numpy
|
sentiment_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5459dbddd31f9c72548a0832c476c3fc08dc05ad89ec52884f6819149c1adb81
|
| 3 |
+
size 32783760
|
tfidf_vectorizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:82115f4953e158a490740cf5a675c365229955a867ee16c657d8eb21f81c2d27
|
| 3 |
+
size 181300
|