ilsa15's picture
Update app.py
6791b49 verified
Raw History Blame Contribute Delete
13.6 kB
import re
import string
from pathlib import Path
import gradio as gr
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score,
classification_report,
confusion_matrix,
f1_score,
precision_score,
recall_score,
)
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
# Download NLTK resources
for package in ("stopwords", "wordnet", "omw-1.4"):
try:
nltk.download(package, quiet=True)
except Exception:
pass
try:
STOP_WORDS = set(stopwords.words("english"))
except LookupError:
STOP_WORDS = {
"a", "an", "the", "and", "or", "but", "if", "of",
"at", "by", "for", "with", "to", "from", "in", "on",
"is", "am", "are", "was", "were", "be", "been",
"have", "has", "had", "do", "does", "did", "i",
"you", "he", "she", "it", "we", "they", "this",
"that", "these", "those", "my", "your", "our",
"their", "me", "him", "her", "as", "not", "very",
}
LEMMATIZER = WordNetLemmatizer()
def preprocess_text(text):
text = str(text).lower()
text = re.sub(r"https?://\S+|www\.\S+", " ", text)
text = re.sub(r"\S+@\S+", " ", text)
text = re.sub(r"\d+", " ", text)
text = text.translate(
str.maketrans("", "", string.punctuation)
)
words = re.findall(r"\b[a-z]+\b", text)
cleaned = []
for word in words:
if word in STOP_WORDS or len(word) <= 1:
continue
try:
word = LEMMATIZER.lemmatize(word)
except LookupError:
pass
cleaned.append(word)
return " ".join(cleaned)
# Sample dataset
def create_dataset():
examples = {
"positive": [
"I absolutely love this product, it works wonderfully",
"The service was excellent and the staff were helpful",
"This is a fantastic experience, I am very happy",
"The movie was brilliant and entertaining",
"I am pleased with the quality and value",
"The food tasted amazing and fresh",
"Everything worked perfectly and I recommend it",
"The delivery was quick and the package was great",
"The support team solved my issue quickly",
"I had a wonderful day and feel great",
"The app is easy to use and very useful",
"Beautiful design and impressive performance",
"I enjoyed every moment of the event",
"The results are better than I expected",
"Excellent quality, friendly service, and fair price",
"This book is inspiring and well written",
"The update made the software much better",
"I am satisfied with my purchase",
"A lovely place with a welcoming atmosphere",
"The course is helpful and easy to understand",
"The new feature saves time and works well",
"The staff were polite and made us feel welcome",
"The trip was comfortable and memorable",
"The headphones sound clear and feel comfortable",
"I would happily buy this again",
],
"negative": [
"I hate this product, it is terrible",
"The service was awful and the staff were rude",
"This was a disappointing experience",
"The movie was boring and poorly made",
"I am unhappy with the quality",
"The food tasted stale and unpleasant",
"Nothing worked and I cannot recommend it",
"The delivery was late and the package was damaged",
"The support team ignored my problem",
"I had a horrible day and feel upset",
"The app is confusing and keeps crashing",
"Poor design and disappointing performance",
"I regret attending this event",
"The results were much worse than expected",
"Bad quality, rude service, and high price",
"This book is dull and badly written",
"The update made the software worse",
"I am dissatisfied with my purchase",
"A noisy place with an unpleasant atmosphere",
"The course is confusing and not helpful",
"The new feature wastes time and fails often",
"The staff were impolite and unwelcoming",
"The trip was uncomfortable and stressful",
"The headphones sound poor and hurt my ears",
"I would never buy this again",
],
"neutral": [
"The product arrived in a standard box",
"The service started at nine in the morning",
"I watched the movie yesterday",
"The package contains three separate items",
"The food was served on a white plate",
"The app has five tabs on the main screen",
"The delivery took two days",
"The support team replied to my message",
"I attended the event on Friday",
"The book has twelve chapters",
"The software update was released today",
"I purchased the item last week",
"The restaurant is located near the station",
"The course includes six lessons",
"The feature is available in the settings menu",
"The staff asked me to wait outside",
"The trip lasted around four hours",
"The headphones come with a charging cable",
"The meeting is scheduled for tomorrow",
"The report contains several sections",
"The website has a search bar",
"The order includes a receipt",
"The room has two windows",
"The class begins at ten o'clock",
"The device is available in black and white",
],
}
rows = []
for label, sentences in examples.items():
for sentence in sentences:
rows.append({
"text": sentence,
"sentiment": label,
})
return pd.DataFrame(rows)
DATA = create_dataset()
DATA["clean_text"] = DATA["text"].apply(preprocess_text)
# Save the dataset as a real file for Gradio's file component
DATA_CSV_PATH = str(Path("sentiment_dataset.csv").resolve())
DATA[["text", "sentiment"]].to_csv(
DATA_CSV_PATH, index=False
)
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
DATA["clean_text"],
DATA["sentiment"],
test_size=0.25,
random_state=42,
stratify=DATA["sentiment"],
)
# Build and train the classifier
MODEL = Pipeline([
(
"tfidf",
TfidfVectorizer(
ngram_range=(1, 2),
sublinear_tf=True,
),
),
(
"classifier",
LogisticRegression(
max_iter=1000,
class_weight="balanced",
),
),
])
MODEL.fit(X_train, y_train)
# Evaluate the model
TEST_PREDICTIONS = MODEL.predict(X_test)
LABELS = sorted(DATA["sentiment"].unique())
ACCURACY = accuracy_score(y_test, TEST_PREDICTIONS)
MACRO_F1 = f1_score(
y_test, TEST_PREDICTIONS,
average="macro", zero_division=0
)
MACRO_PRECISION = precision_score(
y_test, TEST_PREDICTIONS,
average="macro", zero_division=0
)
MACRO_RECALL = recall_score(
y_test, TEST_PREDICTIONS,
average="macro", zero_division=0
)
REPORT = classification_report(
y_test,
TEST_PREDICTIONS,
output_dict=True,
zero_division=0,
)
REPORT_DF = pd.DataFrame(REPORT).T.round(3)
REPORT_DF.index.name = "Class"
REPORT_DF = REPORT_DF.reset_index()
CM = confusion_matrix(
y_test, TEST_PREDICTIONS, labels=LABELS
)
# Predict sentiment for user-entered text
def predict_sentiment(text):
if not text or not text.strip():
return (
"Please enter a sentence or review.",
{},
"",
)
cleaned = preprocess_text(text)
if not cleaned:
return (
"No useful words remained after preprocessing. "
"Please enter a longer sentence.",
{},
"",
)
prediction = MODEL.predict([cleaned])[0]
probabilities = MODEL.predict_proba([cleaned])[0]
probability_data = {
label.title(): float(probability)
for label, probability in zip(
MODEL.classes_, probabilities
)
}
icons = {
"positive": "😊",
"negative": "😞",
"neutral": "😐",
}
confidence = probability_data[prediction.title()]
result = (
f"### {icons.get(prediction, 'πŸ’¬')} "
f"Predicted sentiment: {prediction.title()}\n\n"
f"**Estimated confidence:** {confidence:.1%}\n\n"
"Confidence is the model's estimate, not a guarantee."
)
return result, probability_data, cleaned
# Confusion matrix visualization
def make_confusion_matrix():
fig, ax = plt.subplots(figsize=(6, 4))
ax.imshow(CM, interpolation="nearest", cmap="Blues")
ax.set_xticks(range(len(LABELS)))
ax.set_yticks(range(len(LABELS)))
ax.set_xticklabels(
[label.title() for label in LABELS]
)
ax.set_yticklabels(
[label.title() for label in LABELS]
)
ax.set_xlabel("Predicted label")
ax.set_ylabel("True label")
ax.set_title("Confusion Matrix")
threshold = CM.max() / 2
for row in range(CM.shape[0]):
for col in range(CM.shape[1]):
ax.text(
col,
row,
str(CM[row, col]),
ha="center",
va="center",
color=(
"white"
if CM[row, col] > threshold
else "black"
),
)
fig.tight_layout()
return fig
# Gradio interface
with gr.Blocks(
title="Intelligent Multi-Class Sentiment Classifier"
) as demo:
gr.Markdown(
"""
# πŸ’¬ Intelligent Multi-Class Sentiment Classifier
Classify text into **positive**, **negative**, or **neutral**
sentiment using NLP preprocessing, TF-IDF, and Logistic Regression.
"""
)
with gr.Tab("πŸ”Ž Predict Sentiment"):
text_input = gr.Textbox(
label="Enter a sentence or review",
placeholder=(
"Example: The product is excellent "
"and easy to use!"
),
lines=4,
)
analyze_button = gr.Button(
"Analyze Sentiment",
variant="primary",
)
prediction_output = gr.Markdown()
probability_output = gr.Label(
label="Class probabilities",
num_top_classes=3,
)
cleaned_output = gr.Textbox(
label="Preprocessed text",
interactive=False,
)
analyze_button.click(
fn=predict_sentiment,
inputs=text_input,
outputs=[
prediction_output,
probability_output,
cleaned_output,
],
)
text_input.submit(
fn=predict_sentiment,
inputs=text_input,
outputs=[
prediction_output,
probability_output,
cleaned_output,
],
)
gr.Examples(
examples=[
["I absolutely love this product"],
["The service was awful and disappointing"],
["The meeting is scheduled for tomorrow"],
],
inputs=text_input,
)
with gr.Tab("πŸ“Š Model Evaluation"):
gr.Markdown("### Held-out test set performance")
with gr.Row():
gr.Number(
value=round(ACCURACY, 3),
label="Accuracy",
interactive=False,
)
gr.Number(
value=round(MACRO_F1, 3),
label="Macro F1-score",
interactive=False,
)
gr.Number(
value=round(MACRO_PRECISION, 3),
label="Macro Precision",
interactive=False,
)
gr.Number(
value=round(MACRO_RECALL, 3),
label="Macro Recall",
interactive=False,
)
gr.Markdown("### Classification report")
gr.Dataframe(
value=REPORT_DF,
interactive=False,
)
gr.Markdown("### Confusion matrix")
gr.Plot(value=make_confusion_matrix())
with gr.Tab("πŸ—‚οΈ Dataset"):
gr.Markdown(
f"The built-in dataset contains **{len(DATA)} examples** "
"across three sentiment classes. It is intended for "
"educational demonstration."
)
gr.Dataframe(
value=DATA[["text", "sentiment"]],
interactive=False,
)
gr.File(
value=DATA_CSV_PATH,
label="Sample dataset CSV",
interactive=False,
)
gr.Markdown(
"""
---
**Note:** This educational prototype uses a small sample dataset.
Predictions and evaluation scores are not production-grade benchmarks.
"""
)
if __name__ == "__main__":
demo.launch(theme=gr.themes.Soft())