Update app.py
Browse files
app.py
CHANGED
|
@@ -93,4 +93,254 @@ def create_dataset():
|
|
| 93 |
"Beautiful design and impressive performance",
|
| 94 |
"I enjoyed every moment of the event",
|
| 95 |
"The results are better than I expected",
|
| 96 |
-
"Excellent quality, friendly service, and fair price",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
"Beautiful design and impressive performance",
|
| 94 |
"I enjoyed every moment of the event",
|
| 95 |
"The results are better than I expected",
|
| 96 |
+
"Excellent quality, friendly service, and fair price",
|
| 97 |
+
"This book is inspiring and well written",
|
| 98 |
+
"The update made the software much better",
|
| 99 |
+
"I am satisfied with my purchase",
|
| 100 |
+
"A lovely place with a welcoming atmosphere",
|
| 101 |
+
"The course is helpful and easy to understand",
|
| 102 |
+
"The new feature saves time and works well",
|
| 103 |
+
"The staff were polite and made us feel welcome",
|
| 104 |
+
"The trip was comfortable and memorable",
|
| 105 |
+
"The headphones sound clear and feel comfortable",
|
| 106 |
+
"I would happily buy this again",
|
| 107 |
+
],
|
| 108 |
+
"negative": [
|
| 109 |
+
"I hate this product, it is terrible",
|
| 110 |
+
"The service was awful and the staff were rude",
|
| 111 |
+
"This was a disappointing experience",
|
| 112 |
+
"The movie was boring and poorly made",
|
| 113 |
+
"I am unhappy with the quality",
|
| 114 |
+
"The food tasted stale and unpleasant",
|
| 115 |
+
"Nothing worked and I cannot recommend it",
|
| 116 |
+
"The delivery was late and the package was damaged",
|
| 117 |
+
"The support team ignored my problem",
|
| 118 |
+
"I had a horrible day and feel upset",
|
| 119 |
+
"The app is confusing and keeps crashing",
|
| 120 |
+
"Poor design and disappointing performance",
|
| 121 |
+
"I regret attending this event",
|
| 122 |
+
"The results were much worse than expected",
|
| 123 |
+
"Bad quality, rude service, and high price",
|
| 124 |
+
"This book is dull and badly written",
|
| 125 |
+
"The update made the software worse",
|
| 126 |
+
"I am dissatisfied with my purchase",
|
| 127 |
+
"A noisy place with an unpleasant atmosphere",
|
| 128 |
+
"The course is confusing and not helpful",
|
| 129 |
+
"The new feature wastes time and fails often",
|
| 130 |
+
"The staff were impolite and unwelcoming",
|
| 131 |
+
"The trip was uncomfortable and stressful",
|
| 132 |
+
"The headphones sound poor and hurt my ears",
|
| 133 |
+
"I would never buy this again",
|
| 134 |
+
],
|
| 135 |
+
"neutral": [
|
| 136 |
+
"The product arrived in a standard box",
|
| 137 |
+
"The service started at nine in the morning",
|
| 138 |
+
"I watched the movie yesterday",
|
| 139 |
+
"The package contains three separate items",
|
| 140 |
+
"The food was served on a white plate",
|
| 141 |
+
"The app has five tabs on the main screen",
|
| 142 |
+
"The delivery took two days",
|
| 143 |
+
"The support team replied to my message",
|
| 144 |
+
"I attended the event on Friday",
|
| 145 |
+
"The book has twelve chapters",
|
| 146 |
+
"The software update was released today",
|
| 147 |
+
"I purchased the item last week",
|
| 148 |
+
"The restaurant is located near the station",
|
| 149 |
+
"The course includes six lessons",
|
| 150 |
+
"The feature is available in the settings menu",
|
| 151 |
+
"The staff asked me to wait outside",
|
| 152 |
+
"The trip lasted around four hours",
|
| 153 |
+
"The headphones come with a charging cable",
|
| 154 |
+
"The meeting is scheduled for tomorrow",
|
| 155 |
+
"The report contains several sections",
|
| 156 |
+
"The website has a search bar",
|
| 157 |
+
"The order includes a receipt",
|
| 158 |
+
"The room has two windows",
|
| 159 |
+
"The class begins at ten o'clock",
|
| 160 |
+
"The device is available in black and white",
|
| 161 |
+
],
|
| 162 |
+
}
|
| 163 |
+
return pd.DataFrame(
|
| 164 |
+
[
|
| 165 |
+
{"text": sentence, "sentiment": label}
|
| 166 |
+
for label, sentences in examples.items()
|
| 167 |
+
for sentence in sentences
|
| 168 |
+
]
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
DATA = create_dataset()
|
| 173 |
+
DATA["clean_text"] = DATA["text"].apply(preprocess_text)
|
| 174 |
+
|
| 175 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 176 |
+
DATA["clean_text"],
|
| 177 |
+
DATA["sentiment"],
|
| 178 |
+
test_size=0.25,
|
| 179 |
+
random_state=42,
|
| 180 |
+
stratify=DATA["sentiment"],
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
MODEL = Pipeline(
|
| 184 |
+
[
|
| 185 |
+
("tfidf", TfidfVectorizer(ngram_range=(1, 2), sublinear_tf=True)),
|
| 186 |
+
("classifier", LogisticRegression(max_iter=1000, class_weight="balanced")),
|
| 187 |
+
]
|
| 188 |
+
)
|
| 189 |
+
MODEL.fit(X_train, y_train)
|
| 190 |
+
|
| 191 |
+
TEST_PREDICTIONS = MODEL.predict(X_test)
|
| 192 |
+
LABELS = sorted(DATA["sentiment"].unique())
|
| 193 |
+
|
| 194 |
+
ACCURACY = accuracy_score(y_test, TEST_PREDICTIONS)
|
| 195 |
+
MACRO_F1 = f1_score(y_test, TEST_PREDICTIONS, average="macro", zero_division=0)
|
| 196 |
+
MACRO_PRECISION = precision_score(
|
| 197 |
+
y_test, TEST_PREDICTIONS, average="macro", zero_division=0
|
| 198 |
+
)
|
| 199 |
+
MACRO_RECALL = recall_score(
|
| 200 |
+
y_test, TEST_PREDICTIONS, average="macro", zero_division=0
|
| 201 |
+
)
|
| 202 |
+
REPORT_DF = pd.DataFrame(
|
| 203 |
+
classification_report(
|
| 204 |
+
y_test, TEST_PREDICTIONS, output_dict=True, zero_division=0
|
| 205 |
+
)
|
| 206 |
+
).T.round(3)
|
| 207 |
+
CM = confusion_matrix(y_test, TEST_PREDICTIONS, labels=LABELS)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def predict_sentiment(text):
|
| 211 |
+
if not text or not text.strip():
|
| 212 |
+
return "Please enter a sentence or review.", None, ""
|
| 213 |
+
|
| 214 |
+
cleaned = preprocess_text(text)
|
| 215 |
+
if not cleaned:
|
| 216 |
+
return (
|
| 217 |
+
"No useful words remained after preprocessing. Please try a longer sentence.",
|
| 218 |
+
None,
|
| 219 |
+
"",
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
prediction = MODEL.predict([cleaned])[0]
|
| 223 |
+
probabilities = MODEL.predict_proba([cleaned])[0]
|
| 224 |
+
probability_data = {
|
| 225 |
+
label.title(): float(probability)
|
| 226 |
+
for label, probability in zip(MODEL.classes_, probabilities)
|
| 227 |
+
}
|
| 228 |
+
icon = {"positive": "π", "negative": "π", "neutral": "π"}.get(
|
| 229 |
+
prediction, "π¬"
|
| 230 |
+
)
|
| 231 |
+
result = (
|
| 232 |
+
f"{icon} **Predicted sentiment: {prediction.title()}**\n\n"
|
| 233 |
+
f"Estimated model confidence: **{probability_data[prediction.title()]:.1%}**\n\n"
|
| 234 |
+
"_Confidence is the model's estimate, not a guarantee._"
|
| 235 |
+
)
|
| 236 |
+
return result, probability_data, cleaned
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def make_confusion_matrix():
|
| 240 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
| 241 |
+
image = ax.imshow(CM, interpolation="nearest", cmap="Blues")
|
| 242 |
+
fig.colorbar(image, ax=ax)
|
| 243 |
+
ax.set(
|
| 244 |
+
xticks=range(len(LABELS)),
|
| 245 |
+
yticks=range(len(LABELS)),
|
| 246 |
+
xticklabels=[label.title() for label in LABELS],
|
| 247 |
+
yticklabels=[label.title() for label in LABELS],
|
| 248 |
+
xlabel="Predicted label",
|
| 249 |
+
ylabel="True label",
|
| 250 |
+
title="Confusion Matrix",
|
| 251 |
+
)
|
| 252 |
+
plt.setp(ax.get_xticklabels(), rotation=20, ha="right", rotation_mode="anchor")
|
| 253 |
+
threshold = CM.max() / 2 if CM.size else 0
|
| 254 |
+
for row in range(CM.shape[0]):
|
| 255 |
+
for col in range(CM.shape[1]):
|
| 256 |
+
ax.text(
|
| 257 |
+
col,
|
| 258 |
+
row,
|
| 259 |
+
str(CM[row, col]),
|
| 260 |
+
ha="center",
|
| 261 |
+
va="center",
|
| 262 |
+
color="white" if CM[row, col] > threshold else "black",
|
| 263 |
+
)
|
| 264 |
+
fig.tight_layout()
|
| 265 |
+
return fig
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def get_dataset_csv():
|
| 269 |
+
return DATA[["text", "sentiment"]].to_csv(index=False).encode("utf-8")
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
with gr.Blocks(title="Multi-Class Sentiment Classifier", theme=gr.themes.Soft()) as demo:
|
| 273 |
+
gr.Markdown(
|
| 274 |
+
"""
|
| 275 |
+
# π¬ Intelligent Multi-Class Sentiment Classifier
|
| 276 |
+
|
| 277 |
+
Classify text as **positive**, **negative**, or **neutral** using
|
| 278 |
+
text preprocessing, TF-IDF features, and a Scikit-learn Logistic
|
| 279 |
+
Regression model.
|
| 280 |
+
"""
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
with gr.Tab("π Predict Sentiment"):
|
| 284 |
+
text_input = gr.Textbox(
|
| 285 |
+
label="Enter a sentence or review",
|
| 286 |
+
placeholder="Example: The product is excellent and easy to use!",
|
| 287 |
+
lines=4,
|
| 288 |
+
)
|
| 289 |
+
analyze_button = gr.Button("Analyze Sentiment", variant="primary")
|
| 290 |
+
prediction_output = gr.Markdown()
|
| 291 |
+
probability_output = gr.Label(label="Class probabilities", num_top_classes=3)
|
| 292 |
+
cleaned_output = gr.Textbox(label="Preprocessed text", interactive=False)
|
| 293 |
+
analyze_button.click(
|
| 294 |
+
fn=predict_sentiment,
|
| 295 |
+
inputs=text_input,
|
| 296 |
+
outputs=[prediction_output, probability_output, cleaned_output],
|
| 297 |
+
)
|
| 298 |
+
text_input.submit(
|
| 299 |
+
fn=predict_sentiment,
|
| 300 |
+
inputs=text_input,
|
| 301 |
+
outputs=[prediction_output, probability_output, cleaned_output],
|
| 302 |
+
)
|
| 303 |
+
gr.Examples(
|
| 304 |
+
examples=[
|
| 305 |
+
["I absolutely love this product, it works wonderfully"],
|
| 306 |
+
["The service was awful and the staff were rude"],
|
| 307 |
+
["The meeting is scheduled for tomorrow"],
|
| 308 |
+
],
|
| 309 |
+
inputs=text_input,
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
with gr.Tab("π Model Evaluation"):
|
| 313 |
+
gr.Markdown(
|
| 314 |
+
"### Performance on a held-out test set\n"
|
| 315 |
+
"These metrics are calculated from a stratified split of the built-in sample dataset."
|
| 316 |
+
)
|
| 317 |
+
with gr.Row():
|
| 318 |
+
gr.Number(value=round(ACCURACY, 3), label="Accuracy", interactive=False)
|
| 319 |
+
gr.Number(value=round(MACRO_F1, 3), label="Macro F1-score", interactive=False)
|
| 320 |
+
gr.Number(value=round(MACRO_PRECISION, 3), label="Macro precision", interactive=False)
|
| 321 |
+
gr.Number(value=round(MACRO_RECALL, 3), label="Macro recall", interactive=False)
|
| 322 |
+
gr.Markdown("### Classification report")
|
| 323 |
+
gr.Dataframe(value=REPORT_DF.reset_index(names=["class"]), interactive=False)
|
| 324 |
+
gr.Markdown("### Confusion matrix")
|
| 325 |
+
gr.Plot(value=make_confusion_matrix())
|
| 326 |
+
|
| 327 |
+
with gr.Tab("ποΈ Dataset"):
|
| 328 |
+
gr.Markdown(
|
| 329 |
+
f"The built-in sample dataset contains **{len(DATA)} text examples** "
|
| 330 |
+
"across three sentiment classes. It is intended for coursework demonstration."
|
| 331 |
+
)
|
| 332 |
+
gr.Dataframe(value=DATA[["text", "sentiment"]], interactive=False)
|
| 333 |
+
gr.File(
|
| 334 |
+
value=("sentiment_dataset.csv", get_dataset_csv()),
|
| 335 |
+
label="Sample dataset CSV",
|
| 336 |
+
interactive=False,
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
gr.Markdown(
|
| 340 |
+
"---\n"
|
| 341 |
+
"**Note:** This is an educational prototype trained on a small sample dataset. "
|
| 342 |
+
"Its predictions and scores should not be treated as production-grade results."
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
if __name__ == "__main__":
|
| 346 |
+
demo.launch()
|