Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,51 +15,55 @@ problem_extractor = pipeline("summarization", model="sshleifer/distilbart-cnn-12
|
|
| 15 |
def clean_text(text):
|
| 16 |
return re.sub(r'\s+', ' ', str(text)).strip()
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Check if the required column 'text' exists
|
| 19 |
-
|
| 20 |
-
df = pd.read_excel(
|
|
|
|
| 21 |
if 'text' not in df.columns:
|
| 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 |
-
gr.Dataframe(label="Complaint Samples", wrap=True)
|
| 60 |
-
],
|
| 61 |
-
title="Customer Feedback Analyzer for Uber",
|
| 62 |
-
description="Upload a .xlsx file with a 'text' column. This app will identify complaints and summarize customer issues."
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
demo.launch()
|
|
|
|
| 15 |
def clean_text(text):
|
| 16 |
return re.sub(r'\s+', ' ', str(text)).strip()
|
| 17 |
|
| 18 |
+
# Page
|
| 19 |
+
st.set_page_config(page_title="Uber Complaint Detector", layout="wide")
|
| 20 |
+
st.title("Uber Complaint Detector")
|
| 21 |
+
st.write("Upload an Excel file (.xlsx) with a **`text`** column. This app will identify complaints and summarize key issues.")
|
| 22 |
+
|
| 23 |
+
uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx"])
|
| 24 |
+
|
| 25 |
# Check if the required column 'text' exists
|
| 26 |
+
if uploaded_file:
|
| 27 |
+
df = pd.read_excel(uploaded_file)
|
| 28 |
+
|
| 29 |
if 'text' not in df.columns:
|
| 30 |
+
st.error("❌ Error: Your file must contain a column named 'text'.")
|
| 31 |
+
else:
|
| 32 |
+
# Create a column contains cleaned text
|
| 33 |
+
df["text_clean"] = df["text"].apply(clean_text)
|
| 34 |
|
| 35 |
+
results = df["text_clean"].apply(lambda x: classifier(x)[0])
|
| 36 |
+
|
| 37 |
+
# Extract label (Complaint or Not Complaint) and score (confidence)
|
| 38 |
+
df["label"] = results.apply(lambda r: r["label"])
|
| 39 |
+
df["score"] = results.apply(lambda r: round(r["score"], 2))
|
| 40 |
+
|
| 41 |
+
# Filter only the complaints and sort them by confidence score (descending)
|
| 42 |
+
complaints = df[df["label"] == "Complaint"].sort_values(by="score", ascending=False)
|
| 43 |
+
|
| 44 |
+
# If no complaints are found, return a positive result
|
| 45 |
+
if complaints.empty:
|
| 46 |
+
st.success("✅ No complaints found in the uploaded file!")
|
| 47 |
+
else:
|
| 48 |
+
# Merge all complaint texts into one long string, and split into words
|
| 49 |
+
all_complaints = " ".join(complaints["text_clean"]).tolist())
|
| 50 |
+
|
| 51 |
+
# Use the summarization model to extract core problem themes
|
| 52 |
+
summary = problem_extractor(all_complaints, max_length=60, min_length=20, do_sample=False)[0]["summary_text"]
|
| 53 |
+
|
| 54 |
+
# Return the summary text + complaint samples (text only, score hidden)
|
| 55 |
+
st.subheader("🧾 Complaint Summary")
|
| 56 |
+
st.markdown(f"🧾 Total Reviews: {len(df)}\n")
|
| 57 |
+
st.markdown(f"❗ Complaints Detected: {len(complaints)}\n\n")
|
| 58 |
+
st.markdown(f"📌 Key Customer Issue Summary (from complaints):{summary})
|
| 59 |
+
st.subheader("📋 Complaint Samples")
|
| 60 |
+
st.dataframe(complaints[["text"]].reset_index(drop=True))
|
| 61 |
+
|
| 62 |
+
# Add download button to export complaints as CSV
|
| 63 |
+
csv_data = complaints[["text"]].to_csv(index=False).encode("utf-8")
|
| 64 |
+
st.download_button(
|
| 65 |
+
label="Download Complaints as CSV",
|
| 66 |
+
data=csv_data,
|
| 67 |
+
file_name="uber_complaints.csv",
|
| 68 |
+
mime="text/csv"
|
| 69 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|