Spaces:
Sleeping

SoniaD commited on
Commit
f1d8459
·
verified ·
1 Parent(s): 9a596f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -45
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
- def analyze_reviews(file):
20
- df = pd.read_excel(file)
 
21
  if 'text' not in df.columns:
22
- return "❌ Error: Your file must contain a column named 'text'."
 
 
 
23
 
24
- # Create a column contains cleaned text
25
- df["text_clean"] = df["text"].apply(clean_text)
26
-
27
- results = df["text_clean"].apply(lambda x: classifier(x)[0])
28
-
29
- # Extract label (Complaint or Not Complaint) and score (confidence)
30
- df["label"] = results.apply(lambda r: r["label"])
31
- df["score"] = results.apply(lambda r: round(r["score"], 2))
32
-
33
- # Filter only the complaints and sort them by confidence score (descending)
34
- complaints = df[df["label"] == "Complaint"].sort_values(by="score", ascending=False)
35
-
36
- # If no complaints are found, return a positive result
37
- if complaints.empty:
38
- return "✅ No complaints found in the uploaded file!"
39
-
40
- # Merge all complaint texts into one long string, and split into words
41
- all_complaints = " ".join(complaints["text_clean"]).split()
42
-
43
- # Use the summarization model to extract core problem themes
44
- summary = problem_extractor(all_complaints, max_length=60, min_length=20, do_sample=False)[0]["summary_text"]
45
-
46
- # Return the summary text + complaint samples (text only, score hidden)
47
- return (
48
- f"🧾 Total Reviews: {len(df)}\n"
49
- f"❗ Complaints Detected: {len(complaints)}\n\n"
50
- f"📌 Key Customer Issue Summary (from complaints):\n{summary}\n\n",
51
- complaints[["text"]] # Show only the complaint text without score
52
- )
53
-
54
- demo = gr.Interface(
55
- fn=analyze_reviews,
56
- inputs=gr.File(label="Upload Excel File (.xlsx)"),
57
- outputs=[
58
- gr.Textbox(label="Summary"),
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
+ )