Update app.py
Browse files
app.py
CHANGED
|
@@ -4,23 +4,22 @@ import pickle
|
|
| 4 |
import re
|
| 5 |
import string
|
| 6 |
import numpy as np
|
| 7 |
-
from datetime import datetime
|
| 8 |
-
import plotly.graph_objects as go
|
| 9 |
|
| 10 |
-
# 1. LOAD ASSETS (
|
| 11 |
-
with open('nb_model.pkl', 'rb') as f:
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
nn_model = tf.keras.models.load_model('sentiment_model.h5')
|
| 14 |
-
with open('tfidf_vectorizer.pkl', 'rb') as f: tfidf = pickle.load(f)
|
| 15 |
-
with open('label_encoder.pkl', 'rb') as f: le = pickle.load(f)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
.
|
| 20 |
-
|
| 21 |
-
"""
|
| 22 |
|
| 23 |
-
#
|
| 24 |
def clean_tweet(text):
|
| 25 |
text = text.lower()
|
| 26 |
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
|
|
@@ -28,6 +27,7 @@ def clean_tweet(text):
|
|
| 28 |
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 29 |
return re.sub(r'\s+', ' ', text).strip()
|
| 30 |
|
|
|
|
| 31 |
def predict_sentiment(tweet, model_choice):
|
| 32 |
cleaned = clean_tweet(tweet)
|
| 33 |
vec = tfidf.transform([cleaned]).toarray()
|
|
@@ -35,53 +35,31 @@ def predict_sentiment(tweet, model_choice):
|
|
| 35 |
if model_choice == "Naive Bayes":
|
| 36 |
pred = nb_model.predict(vec)[0]
|
| 37 |
label = le.inverse_transform([pred])[0]
|
| 38 |
-
prob = 0.85
|
| 39 |
elif model_choice == "Logistic Regression":
|
| 40 |
pred = lr_model.predict(vec)[0]
|
| 41 |
label = le.inverse_transform([pred])[0]
|
| 42 |
-
prob = 0.88
|
| 43 |
else:
|
| 44 |
probs = nn_model.predict(vec)
|
| 45 |
label = le.inverse_transform([np.argmax(probs)])[0]
|
| 46 |
-
prob = float(np.max(probs))
|
| 47 |
-
|
| 48 |
-
fig = go.Figure(go.Indicator(
|
| 49 |
-
mode = "gauge+number", value = prob * 100,
|
| 50 |
-
gauge = {'axis': {'range': [0, 100]}, 'bar': {'color': "#6366f1"}},
|
| 51 |
-
title = {'text': f"Confidence ({label})"}
|
| 52 |
-
))
|
| 53 |
-
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)', font={'color': "white"}, height=250)
|
| 54 |
|
| 55 |
-
return label.upper()
|
| 56 |
-
|
| 57 |
-
# 4. DASHBOARD UI (Gradio 6.0 Compliant)
|
| 58 |
-
with gr.Blocks() as demo: # theme and css removed from here
|
| 59 |
-
with gr.Row():
|
| 60 |
-
with gr.Column(scale=5):
|
| 61 |
-
# HEADER - Removed 'scale' from gr.Markdown
|
| 62 |
-
with gr.Row():
|
| 63 |
-
gr.Textbox(show_label=False, placeholder="Search analytics...", scale=4)
|
| 64 |
-
gr.Markdown("<span class='status-active'>● Model: Active</span>")
|
| 65 |
-
|
| 66 |
-
# CORE SECTION: LIVE INFERENCE
|
| 67 |
-
with gr.Row():
|
| 68 |
-
with gr.Column(scale=2):
|
| 69 |
-
input_text = gr.Textbox(label="Tweet Input", lines=3)
|
| 70 |
-
model_select = gr.Dropdown(["Naive Bayes", "Logistic Regression", "Custom Neural Network (MLP)"],
|
| 71 |
-
label="Select Architecture", value="Custom Neural Network (MLP)")
|
| 72 |
-
btn = gr.Button("Run Inference", variant="primary")
|
| 73 |
-
with gr.Column(scale=1):
|
| 74 |
-
prob_meter = gr.Plot(label="Probability Meter")
|
| 75 |
-
res_label = gr.Label(label="Classification")
|
| 76 |
-
|
| 77 |
-
with gr.Row():
|
| 78 |
-
history = gr.Dataframe(headers=["Timestamp", "Entity", "Sentiment"], datatype=["str", "str", "str"])
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
# 5. LAUNCH
|
| 83 |
if __name__ == "__main__":
|
| 84 |
-
demo.launch(
|
| 85 |
-
theme=gr.themes.Base(),
|
| 86 |
-
css=custom_css
|
| 87 |
-
)
|
|
|
|
| 4 |
import re
|
| 5 |
import string
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# 1. LOAD ASSETS (Assumes files are in the root directory)
|
| 9 |
+
with open('nb_model.pkl', 'rb') as f:
|
| 10 |
+
nb_model = pickle.load(f)
|
| 11 |
+
with open('lr_model.pkl', 'rb') as f:
|
| 12 |
+
lr_model = pickle.load(f)
|
| 13 |
+
|
| 14 |
+
# Load Custom MLP
|
| 15 |
nn_model = tf.keras.models.load_model('sentiment_model.h5')
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
with open('tfidf_vectorizer.pkl', 'rb') as f:
|
| 18 |
+
tfidf = pickle.load(f)
|
| 19 |
+
with open('label_encoder.pkl', 'rb') as f:
|
| 20 |
+
le = pickle.load(f)
|
|
|
|
| 21 |
|
| 22 |
+
# 2. SIMPLE CLEANING FUNCTION
|
| 23 |
def clean_tweet(text):
|
| 24 |
text = text.lower()
|
| 25 |
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
|
|
|
|
| 27 |
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 28 |
return re.sub(r'\s+', ' ', text).strip()
|
| 29 |
|
| 30 |
+
# 3. PREDICTION LOGIC
|
| 31 |
def predict_sentiment(tweet, model_choice):
|
| 32 |
cleaned = clean_tweet(tweet)
|
| 33 |
vec = tfidf.transform([cleaned]).toarray()
|
|
|
|
| 35 |
if model_choice == "Naive Bayes":
|
| 36 |
pred = nb_model.predict(vec)[0]
|
| 37 |
label = le.inverse_transform([pred])[0]
|
|
|
|
| 38 |
elif model_choice == "Logistic Regression":
|
| 39 |
pred = lr_model.predict(vec)[0]
|
| 40 |
label = le.inverse_transform([pred])[0]
|
|
|
|
| 41 |
else:
|
| 42 |
probs = nn_model.predict(vec)
|
| 43 |
label = le.inverse_transform([np.argmax(probs)])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
return f"Sentiment: {label.upper()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# 4. BASIC INTERFACE
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
+
fn=predict_sentiment,
|
| 50 |
+
inputs=[
|
| 51 |
+
gr.Textbox(label="Enter Tweet Content", lines=3),
|
| 52 |
+
gr.Dropdown(
|
| 53 |
+
["Naive Bayes", "Logistic Regression", "Custom Neural Network (MLP)"],
|
| 54 |
+
label="Select Model",
|
| 55 |
+
value="Custom Neural Network (MLP)"
|
| 56 |
+
)
|
| 57 |
+
],
|
| 58 |
+
outputs=gr.Text(label="Analysis Result"),
|
| 59 |
+
title="Twitter Entity Sentiment Analyzer",
|
| 60 |
+
description="A simple tool to analyze brand sentiment using baseline and deep learning models."
|
| 61 |
+
)
|
| 62 |
|
| 63 |
+
# 5. LAUNCH (Gradio 6.0 compliant)
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|
|
|
|
|
|
|
|
|