Spaces:
Sleeping
Sleeping
adding app.py file
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import csv
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load model and tokenizer
|
| 9 |
+
model_path = "model"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 12 |
+
|
| 13 |
+
label_map = {0: "Negative", 1: "Neutral", 2: "Positive"}
|
| 14 |
+
colors = {"Negative": "red", "Neutral": "gray", "Positive": "green"}
|
| 15 |
+
FEEDBACK_FILE = "user_feedback.csv"
|
| 16 |
+
|
| 17 |
+
def predict_sentiment(text):
|
| 18 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=256)
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model(**inputs)
|
| 21 |
+
probs = torch.softmax(outputs.logits, dim=1).squeeze()
|
| 22 |
+
predicted_class = torch.argmax(probs).item()
|
| 23 |
+
|
| 24 |
+
label = label_map[predicted_class]
|
| 25 |
+
confidence = probs[predicted_class].item()
|
| 26 |
+
warning = "<br><span style='color:orange'>β οΈ Low confidence. Try rephrasing the review.</span>" if confidence < 0.5 else ""
|
| 27 |
+
|
| 28 |
+
result_html = f"""
|
| 29 |
+
<div style="border: 2px solid {colors[label]}; padding: 10px; border-radius: 10px;">
|
| 30 |
+
<h3 style='margin-bottom: 5px;'>Prediction: <span style='color:{colors[label]}'>{label}</span></h3>
|
| 31 |
+
<p>Confidence: {confidence:.2%}</p>
|
| 32 |
+
{warning}
|
| 33 |
+
</div>
|
| 34 |
+
"""
|
| 35 |
+
return result_html, label, confidence
|
| 36 |
+
|
| 37 |
+
def save_feedback(label, confidence, correct):
|
| 38 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 39 |
+
file_exists = os.path.isfile(FEEDBACK_FILE)
|
| 40 |
+
with open(FEEDBACK_FILE, mode="a", newline="", encoding="utf-8") as file:
|
| 41 |
+
writer = csv.writer(file)
|
| 42 |
+
if not file_exists:
|
| 43 |
+
writer.writerow(["timestamp", "predicted_label", "confidence", "correct_prediction"])
|
| 44 |
+
writer.writerow([timestamp, label, f"{confidence:.2%}", correct])
|
| 45 |
+
return "β
Thanks for your feedback!"
|
| 46 |
+
|
| 47 |
+
with gr.Blocks(title="Amazon Review Sentiment App") as demo:
|
| 48 |
+
gr.Markdown(
|
| 49 |
+
"<div style='text-align: center; font-size: 24px;'>π¦ <b>Amazon Review Sentiment Classifier</b></div>"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
gr.Markdown("Enter a review below to check if it's **Positive**, **Neutral**, or **Negative**.")
|
| 53 |
+
|
| 54 |
+
with gr.Row():
|
| 55 |
+
review_input = gr.Textbox(lines=10, placeholder="Type or paste a review here...", label="Your Review")
|
| 56 |
+
output_box = gr.HTML(label="Prediction Result")
|
| 57 |
+
|
| 58 |
+
predict_btn = gr.Button("π Predict Sentiment")
|
| 59 |
+
hidden_label = gr.Textbox(visible=False)
|
| 60 |
+
hidden_conf = gr.Number(visible=False)
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
yes_btn = gr.Button("π Yes")
|
| 64 |
+
no_btn = gr.Button("π No")
|
| 65 |
+
|
| 66 |
+
feedback_output = gr.Textbox(label="Feedback Status", interactive=False)
|
| 67 |
+
|
| 68 |
+
predict_btn.click(fn=predict_sentiment, inputs=[review_input], outputs=[output_box, hidden_label, hidden_conf])
|
| 69 |
+
yes_btn.click(fn=save_feedback, inputs=[hidden_label, hidden_conf, gr.Textbox(value="yes", visible=False)], outputs=feedback_output)
|
| 70 |
+
no_btn.click(fn=save_feedback, inputs=[hidden_label, hidden_conf, gr.Textbox(value="no", visible=False)], outputs=feedback_output)
|
| 71 |
+
|
| 72 |
+
gr.Examples(
|
| 73 |
+
examples=[
|
| 74 |
+
"This phone exceeded all my expectations.",
|
| 75 |
+
"Battery life is just okay, not great.",
|
| 76 |
+
"Worst product I've ever purchased.",
|
| 77 |
+
"Highly recommended!",
|
| 78 |
+
"Meh. It's just fine, nothing special."
|
| 79 |
+
],
|
| 80 |
+
inputs=review_input
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
demo.launch(debug=True)
|