import streamlit as st import pandas as pd from transformers import pipeline, AutoTokenizer from collections import Counter import re # Use Finetuned model as classifier of the text model_id = "SoniaD/CustomModel_Uber" classifier = pipeline("text-classification", model=model_id, tokenizer=model_id) # Load a pre-trained summarization model for extracting key problems from complaints summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") zero_classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli") # Load tokenizer separately for token count check tokenizer = AutoTokenizer.from_pretrained("SoniaD/CustomModel_Uber") # Clean the text uploaded def clean_text(text): return re.sub(r'\s+', ' ', str(text)).strip() # Check if the required column 'text' exists def analyze_reviews(uploaded_file): df = pd.read_excel(uploaded_file) # Error files if 'text' not in df.columns: st.error("โŒ Error: Your file must contain a column named 'text'.") return # Create a column contains cleaned text df["text_clean"] = df["text"].apply(clean_text) results = df["text_clean"].apply(lambda x: classifier(x)[0]) # Extract label (Complaint or Not Complaint) and score (confidence) df["label"] = results.apply(lambda r: r["label"]) df["score"] = results.apply(lambda r: round(r["score"], 2)) # Filter only the complaints and sort them by confidence score (descending) complaints = df[df["label"] == "Complaint"].sort_values(by="score", ascending=False) # If no complaints are found, return a positive result if complaints.empty: st.success("โœ… No complaints found in the uploaded file!") return # Use the summarization model to extract core problem themes topic_summary_pairs = [] complaint_texts = complaints["text_clean"].tolist() candidate_labels = ["Driver Issue", "App Issue", "Payment Issue", "Customer Support", "Wait Time"] for complaint in complaint_texts: try: token_len = len(tokenizer(complaint)["input_ids"]) if token_len > 40: summary = summarizer(complaint, max_length=50, min_length=15, do_sample=False)[0]["summary_text"] else: summary = complaint topic_result = zero_classifier(summary, candidate_labels) top_topic = topic_result["labels"][0] topic_summary_pairs.append((top_topic, summary.strip())) except Exception as e: st.warning(f"โ— Error processing complaint: {e}") continue # Count how many complaints fall under each topic topic_counts = Counter([pair[0] for pair in topic_summary_pairs]) # Return the summary text + complaint samples (text only, score hidden) st.subheader("๐Ÿงพ Complaint Summary") st.markdown(f"๐Ÿงพ Total Reviews: {len(df)}\n") st.markdown(f"โ— Complaints Detected: {len(complaints)}\n\n") st.markdown(f"๐Ÿ“Œ Key Customer Issue Summary (Categorized)") # Display categorized summaries sorted by frequency for topic, count in topic_counts.most_common(): st.markdown(f"**๐Ÿ“‚ {topic}** ({count} complaints)") for t, s in topic_summary_pairs: if t == topic: st.markdown(f"- {s}") st.subheader("๐Ÿ“‹ Complaint Samples") st.dataframe(complaints[["text"]].reset_index(drop=True)) # Add download button to export complaints as CSV csv_data = complaints[["text"]].to_csv(index=False).encode("utf-8") st.download_button( label="Download Complaints as CSV", data=csv_data, file_name="uber_complaints.csv", mime="text/csv" ) # Page Layout st.set_page_config(page_title="Uber Complaint Detector", layout="wide") st.title("Uber Complaint Detector") st.write("Upload an Excel file (.xlsx) with a **`text`** column. This app will identify complaints and summarize key issues.") uploaded_file = st.file_uploader("Upload Excel File", type=["xlsx"]) if uploaded_file: analyze_reviews(uploaded_file)