-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (48 loc) · 2.12 KB
/
app.py
File metadata and controls
56 lines (48 loc) · 2.12 KB
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
import joblib
import streamlit as st
import pandas as pd
from pathlib import Path
st.set_page_config(page_title="CommentSense – Feedback Classifier", page_icon="💬", layout="centered")
st.title("💬 CommentSense – Customer Feedback Classifier")
st.caption("Multiclass: praise | complaint | toxic")
MODELS = Path(__file__).parent / "models"
VEC_PATH = MODELS / "vectorizer.pkl"
CLF_PATH = MODELS / "classifier.pkl"
@st.cache_resource(show_spinner=False)
def load_artifacts():
vec = joblib.load(VEC_PATH)
clf = joblib.load(CLF_PATH)
return vec, clf
vec, clf = load_artifacts()
labels = getattr(clf, "classes_", ["complaint","praise","toxic"])
tab1, tab2 = st.tabs(["🔤 Single Text", "📁 Batch (CSV)"])
with tab1:
txt = st.text_area("Enter a customer comment", height=140, placeholder="Type/paste feedback here...")
if st.button("Classify", type="primary"):
if not txt.strip():
st.warning("Please enter some text.")
else:
X = vec.transform([txt])
pred = clf.predict(X)[0]
st.success(f"Prediction: **{pred.upper()}**")
if hasattr(clf, "predict_proba"):
probs = clf.predict_proba(X)[0]
st.subheader("Class probabilities")
for c, p in sorted(zip(labels, probs), key=lambda x: -x[1]):
st.write(f"- **{c}**: {p:.2f}")
with tab2:
file = st.file_uploader("Upload CSV with a 'text' column", type=["csv"])
if file is not None:
df = pd.read_csv(file)
if "text" not in df.columns:
st.error("CSV must include a 'text' column.")
else:
X = vec.transform(df["text"].astype(str))
preds = clf.predict(X)
out = df.copy()
out["prediction"] = preds
st.dataframe(out.head(50))
st.download_button("Download predictions", data=out.to_csv(index=False).encode("utf-8"),
file_name="commentsense_predictions.csv", mime="text/csv")
st.divider()
st.caption("Model: TF‑IDF + Linear SVM (calibrated). Replace data in 'data/customer_feedback.csv' and run train.py to retrain.")