Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,88 @@
|
|
| 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
|
| 9 |
-
# Baselines
|
| 10 |
-
with open('
|
| 11 |
-
nb_model = pickle.load(f)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
text =
|
| 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 |
demo.launch()
|
|
|
|
| 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 assets from the ROOT directory
|
| 9 |
+
# Loading Baselines
|
| 10 |
+
with open('nb_model.pkl', 'rb') as f:
|
| 11 |
+
nb_model = pickle.load(f)
|
| 12 |
+
|
| 13 |
+
with open('lr_model.pkl', 'rb') as f:
|
| 14 |
+
lr_model = pickle.load(f)
|
| 15 |
+
|
| 16 |
+
# Loading Custom Deep Learning Model
|
| 17 |
+
nn_model = tf.keras.models.load_model('sentiment_model.h5')
|
| 18 |
+
|
| 19 |
+
# Loading Preprocessing Assets
|
| 20 |
+
with open('tfidf_vectorizer.pkl', 'rb') as f:
|
| 21 |
+
tfidf = pickle.load(f)
|
| 22 |
+
|
| 23 |
+
with open('label_encoder.pkl', 'rb') as f:
|
| 24 |
+
le = pickle.load(f)
|
| 25 |
+
|
| 26 |
+
# 2. Text Cleaning Function (must be identical to your training notebook)
|
| 27 |
+
def clean_tweet(text):
|
| 28 |
+
if not isinstance(text, str):
|
| 29 |
+
return ""
|
| 30 |
+
text = text.lower()
|
| 31 |
+
# Remove URLs, mentions, and punctuation
|
| 32 |
+
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
|
| 33 |
+
text = re.sub(r'@\w+', '', text)
|
| 34 |
+
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 35 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 36 |
+
return text
|
| 37 |
+
|
| 38 |
+
# 3. Prediction Logic
|
| 39 |
+
def predict_sentiment(tweet, model_choice):
|
| 40 |
+
cleaned_text = clean_tweet(tweet)
|
| 41 |
+
# Convert text to the same 5000-feature vector used in training
|
| 42 |
+
vectorized_text = tfidf.transform([cleaned_text]).toarray()
|
| 43 |
+
|
| 44 |
+
if model_choice == "Naive Bayes":
|
| 45 |
+
pred = nb_model.predict(vectorized_text)[0]
|
| 46 |
+
label = le.inverse_transform([pred])[0]
|
| 47 |
+
elif model_choice == "Logistic Regression":
|
| 48 |
+
pred = lr_model.predict(vectorized_text)[0]
|
| 49 |
+
label = le.inverse_transform([pred])[0]
|
| 50 |
+
else: # Custom Neural Network (MLP)
|
| 51 |
+
pred_probs = nn_model.predict(vectorized_text)
|
| 52 |
+
label_idx = np.argmax(pred_probs)
|
| 53 |
+
label = le.inverse_transform([label_idx])[0]
|
| 54 |
+
|
| 55 |
+
return f"Result: {label.upper()}"
|
| 56 |
+
|
| 57 |
+
# 4. Gradio UI Layout
|
| 58 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 59 |
+
gr.Markdown("# 🐦 Twitter Entity Sentiment Intelligence")
|
| 60 |
+
gr.Markdown("Compare the performance of three different algorithms on real-world noisy data.")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
input_text = gr.Textbox(
|
| 65 |
+
label="Enter Tweet Content",
|
| 66 |
+
placeholder="e.g., I just bought the new @BrandX phone and I love it!",
|
| 67 |
+
lines=3
|
| 68 |
+
)
|
| 69 |
+
model_dropdown = gr.Dropdown(
|
| 70 |
+
choices=["Naive Bayes", "Logistic Regression", "Custom Neural Network (MLP)"],
|
| 71 |
+
label="Choose Algorithm",
|
| 72 |
+
value="Custom Neural Network (MLP)"
|
| 73 |
+
)
|
| 74 |
+
submit_btn = gr.Button("Analyze Sentiment", variant="primary")
|
| 75 |
+
|
| 76 |
+
with gr.Column():
|
| 77 |
+
output_text = gr.Label(label="Model Prediction")
|
| 78 |
+
|
| 79 |
+
# Interactive components
|
| 80 |
+
submit_btn.click(
|
| 81 |
+
fn=predict_sentiment,
|
| 82 |
+
inputs=[input_text, model_dropdown],
|
| 83 |
+
outputs=output_text
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Launch the app
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
demo.launch()
|