Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from scripts.load_models import distilbert_model, bert_topic_model, recommendation_model
|
| 4 |
+
import gdown
|
| 5 |
+
# Streamlit app layout
|
| 6 |
+
st.title("Intelligent Customer Feedback Analyzer")
|
| 7 |
+
st.write("Analyze customer feedback for sentiment, topics, and get personalized recommendations.")
|
| 8 |
+
|
| 9 |
+
# User input for customer feedback file
|
| 10 |
+
uploaded_file = st.file_uploader("Upload a Feedback File (CSV, JSON, TXT)", type=["csv", "json", "txt"])
|
| 11 |
+
|
| 12 |
+
# Function to extract feedback text from different file formats
|
| 13 |
+
def extract_feedback(file):
|
| 14 |
+
if file.type == "text/csv":
|
| 15 |
+
df = pd.read_csv(file)
|
| 16 |
+
feedback_text = []
|
| 17 |
+
for column in df.columns:
|
| 18 |
+
feedback_text.extend(df[column].dropna().astype(str).tolist()) # Include all text in the CSV
|
| 19 |
+
return feedback_text
|
| 20 |
+
elif file.type == "application/json":
|
| 21 |
+
json_data = json.load(file)
|
| 22 |
+
feedback_text = []
|
| 23 |
+
if isinstance(json_data, list):
|
| 24 |
+
feedback_text = [item.get('feedback', '') for item in json_data if 'feedback' in item]
|
| 25 |
+
elif isinstance(json_data, dict):
|
| 26 |
+
feedback_text = list(json_data.values()) # Include all values if feedback key doesn't exist
|
| 27 |
+
return feedback_text
|
| 28 |
+
elif file.type == "text/plain":
|
| 29 |
+
return [file.getvalue().decode("utf-8")]
|
| 30 |
+
else:
|
| 31 |
+
return ["Unsupported file type"]
|
| 32 |
+
|
| 33 |
+
# Display error or feedback extraction status
|
| 34 |
+
if uploaded_file:
|
| 35 |
+
feedback_text_list = extract_feedback(uploaded_file)
|
| 36 |
+
|
| 37 |
+
if feedback_text_list:
|
| 38 |
+
for feedback_text in feedback_text_list:
|
| 39 |
+
if st.button(f'Analyze Feedback: "{feedback_text[:30]}..."'):
|
| 40 |
+
# Sentiment Analysis
|
| 41 |
+
sentiment = distilbert_model.predict([feedback_text])
|
| 42 |
+
sentiment_result = 'Positive' if sentiment == 1 else 'Negative'
|
| 43 |
+
st.write(f"Sentiment: {sentiment_result}")
|
| 44 |
+
|
| 45 |
+
# Topic Modeling
|
| 46 |
+
topics = bert_topic_model.predict([feedback_text])
|
| 47 |
+
st.write(f"Predicted Topic(s): {topics}")
|
| 48 |
+
|
| 49 |
+
# Recommendation System
|
| 50 |
+
recommendations = recommendation_model.predict([feedback_text])
|
| 51 |
+
st.write(f"Recommended Actions: {recommendations}")
|
| 52 |
+
else:
|
| 53 |
+
st.error("Unable to extract feedback from the file.")
|
| 54 |
+
else:
|
| 55 |
+
st.info("Please upload a feedback file to analyze.")
|
| 56 |
+
|