File size: 13,618 Bytes
6791b49 0bbab7f 6791b49 0bbab7f fa243e5 0bbab7f fa243e5 0bbab7f 6791b49 fa243e5 0bbab7f fa243e5 6791b49 fa243e5 6791b49 0bbab7f fa243e5 0bbab7f 6791b49 0bbab7f 6791b49 0bbab7f fa243e5 6791b49 fa243e5 6791b49 fa243e5 6791b49 fa243e5 0bbab7f a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 a6a1010 6791b49 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
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())
|