Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# ---------------- LOAD MODEL ----------------
|
| 10 |
+
|
| 11 |
+
print("Loading sentiment model...")
|
| 12 |
+
|
| 13 |
+
# model_path = (
|
| 14 |
+
# "../Models/models--distilbert--distilbert-base-uncased-finetuned-sst-2-english"
|
| 15 |
+
# "/snapshots/714eb0fa89d2f80546fda750413ed43d93601a13"
|
| 16 |
+
# )
|
| 17 |
+
|
| 18 |
+
analyzer = pipeline(
|
| 19 |
+
"text-classification",
|
| 20 |
+
model= "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
|
| 21 |
+
device = -1 # CPU
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
print("Model loaded.")
|
| 25 |
+
|
| 26 |
+
# ---------------- SINGLE REVIEW ----------------
|
| 27 |
+
|
| 28 |
+
def sentiment_analyzer(review):
|
| 29 |
+
result = analyzer(review)[0]
|
| 30 |
+
return f"{result['label']} (Confidence: {round(result['score'], 4)})"
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# ---------------- EXCEL + VISUALIZATION ----------------
|
| 34 |
+
|
| 35 |
+
def analyze_excel(file):
|
| 36 |
+
df = pd.read_excel(file.name)
|
| 37 |
+
|
| 38 |
+
if "review" not in df.columns:
|
| 39 |
+
return None, None, None, None
|
| 40 |
+
|
| 41 |
+
sentiments = []
|
| 42 |
+
scores = []
|
| 43 |
+
|
| 44 |
+
for review in df["review"]:
|
| 45 |
+
result = analyzer(review)[0]
|
| 46 |
+
sentiments.append(result["label"])
|
| 47 |
+
scores.append(round(result["score"], 4))
|
| 48 |
+
|
| 49 |
+
df["sentiment"] = sentiments
|
| 50 |
+
df["confidence_score"] = scores
|
| 51 |
+
|
| 52 |
+
# ---- Sentiment counts ----
|
| 53 |
+
counts = df["sentiment"].value_counts()
|
| 54 |
+
|
| 55 |
+
# ---- Bar Chart ----
|
| 56 |
+
fig_bar = plt.figure()
|
| 57 |
+
counts.plot(
|
| 58 |
+
kind="bar",
|
| 59 |
+
color=["#22c55e", "#ef4444"],
|
| 60 |
+
xlabel="Sentiment",
|
| 61 |
+
ylabel="Number of Reviews",
|
| 62 |
+
title="Sentiment Distribution (Bar Chart)"
|
| 63 |
+
)
|
| 64 |
+
plt.xticks(rotation=0)
|
| 65 |
+
plt.tight_layout()
|
| 66 |
+
|
| 67 |
+
# ---- Pie Chart ----
|
| 68 |
+
fig_pie = plt.figure()
|
| 69 |
+
counts.plot(
|
| 70 |
+
kind="pie",
|
| 71 |
+
autopct="%1.1f%%",
|
| 72 |
+
startangle=90,
|
| 73 |
+
colors=["#22c55e", "#ef4444"],
|
| 74 |
+
ylabel="",
|
| 75 |
+
title="Sentiment Distribution (Pie Chart)"
|
| 76 |
+
)
|
| 77 |
+
plt.tight_layout()
|
| 78 |
+
|
| 79 |
+
# ---- Save Excel for download ----
|
| 80 |
+
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 81 |
+
df.to_excel(tmp_file.name, index=False)
|
| 82 |
+
|
| 83 |
+
return df, fig_bar, fig_pie, tmp_file.name
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# ---------------- GRADIO UI ----------------
|
| 87 |
+
|
| 88 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Gen AI Project 3: Sentiment Analyzer") as demo:
|
| 89 |
+
|
| 90 |
+
gr.Markdown("""
|
| 91 |
+
Gen AI Project 3: Sentiment Analyzer
|
| 92 |
+
Analyze **individual reviews** or **bulk Excel reviews** using a fine-tuned **DistilBERT** model.
|
| 93 |
+
Get **AI insights + visual analytics + downloadable results**.
|
| 94 |
+
""")
|
| 95 |
+
|
| 96 |
+
with gr.Tabs():
|
| 97 |
+
|
| 98 |
+
# -------- SINGLE REVIEW TAB --------
|
| 99 |
+
with gr.Tab("π Single Review"):
|
| 100 |
+
with gr.Row():
|
| 101 |
+
review_input = gr.Textbox(
|
| 102 |
+
label="Enter Review Text",
|
| 103 |
+
placeholder="e.g. I absolutely loved this product!",
|
| 104 |
+
lines=4
|
| 105 |
+
)
|
| 106 |
+
sentiment_output = gr.Textbox(label="Predicted Sentiment")
|
| 107 |
+
|
| 108 |
+
gr.Button("Analyze Sentiment π").click(
|
| 109 |
+
fn=sentiment_analyzer,
|
| 110 |
+
inputs=review_input,
|
| 111 |
+
outputs=sentiment_output
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# -------- EXCEL TAB --------
|
| 115 |
+
with gr.Tab("π Excel Upload & Analysis"):
|
| 116 |
+
file_input = gr.File(
|
| 117 |
+
label="Upload Excel File (.xlsx)",
|
| 118 |
+
file_types=[".xlsx"]
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
analyze_btn = gr.Button("Analyze Excel π")
|
| 122 |
+
|
| 123 |
+
df_output = gr.Dataframe(label="Sentiment Results Table")
|
| 124 |
+
bar_output = gr.Plot(label="Bar Chart")
|
| 125 |
+
pie_output = gr.Plot(label="Pie Chart")
|
| 126 |
+
download_output = gr.File(label="Download Analyzed Excel")
|
| 127 |
+
|
| 128 |
+
analyze_btn.click(
|
| 129 |
+
fn=analyze_excel,
|
| 130 |
+
inputs=file_input,
|
| 131 |
+
outputs=[df_output, bar_output, pie_output, download_output]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
gr.Markdown("""
|
| 135 |
+
---
|
| 136 |
+
β
**Tech Stack:** Python, Transformers, DistilBERT, Pandas, Matplotlib, Gradio
|
| 137 |
+
π **Use Case:** Product reviews, customer feedback, social media sentiment
|
| 138 |
+
""")
|
| 139 |
+
|
| 140 |
+
demo.launch()
|