Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import json
|
4 |
+
import joblib
|
5 |
+
import gdown
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Function to download and load model from Google Drive
|
9 |
+
def load_model_from_drive(file_url, model_name):
|
10 |
+
"""Downloads the model from Google Drive and loads it."""
|
11 |
+
# Specify where to save the model
|
12 |
+
model_folder = 'models'
|
13 |
+
if not os.path.exists(model_folder):
|
14 |
+
os.makedirs(model_folder)
|
15 |
+
|
16 |
+
# Download the model using gdown
|
17 |
+
output_path = os.path.join(model_folder, model_name)
|
18 |
+
gdown.download(file_url, output_path, quiet=False)
|
19 |
+
|
20 |
+
# Load and return the model using joblib
|
21 |
+
model = joblib.load(output_path)
|
22 |
+
return model
|
23 |
+
|
24 |
+
# URLs for the models on Google Drive (using file IDs)
|
25 |
+
distilbert_model_url = 'https://drive.google.com/uc?export=download&id=1WfjeGSQ7j4id1VSeGU8s2VzMBzNtImFT'
|
26 |
+
bert_topic_model_url = 'https://drive.google.com/uc?export=download&id=164n8QfrQF4RB2LlQzGe1BbaFmugbzBGR'
|
27 |
+
recommendation_model_url = 'https://drive.google.com/uc?export=download&id=17wFjVd9zTfHG33Eg7378Z6a1reohIkfE'
|
28 |
+
|
29 |
+
# Model file names
|
30 |
+
distilbert_model_name = 'distilbert_model.joblib'
|
31 |
+
bert_topic_model_name = 'bertopic_model.joblib'
|
32 |
+
recommendation_model_name = 'recommendation_model.joblib'
|
33 |
+
|
34 |
+
# Load all three models
|
35 |
+
distilbert_model = load_model_from_drive(distilbert_model_url, distilbert_model_name)
|
36 |
+
bert_topic_model = load_model_from_drive(bert_topic_model_url, bert_topic_model_name)
|
37 |
+
recommendation_model = load_model_from_drive(recommendation_model_url, recommendation_model_name)
|
38 |
+
|
39 |
+
# Streamlit app layout
|
40 |
+
st.title("Intelligent Customer Feedback Analyzer")
|
41 |
+
st.write("Analyze customer feedback for sentiment, topics, and get personalized recommendations.")
|
42 |
+
|
43 |
+
# User input for customer feedback file
|
44 |
+
uploaded_file = st.file_uploader("Upload a Feedback File (CSV, JSON, TXT)", type=["csv", "json", "txt"])
|
45 |
+
|
46 |
+
# Function to extract feedback text from different file formats
|
47 |
+
def extract_feedback(file):
|
48 |
+
if file.type == "text/csv":
|
49 |
+
# If the file is CSV, read it and extract all text content (even if unlabelled)
|
50 |
+
df = pd.read_csv(file)
|
51 |
+
feedback_text = []
|
52 |
+
for column in df.columns:
|
53 |
+
feedback_text.extend(df[column].dropna().astype(str).tolist()) # Include all text in the CSV
|
54 |
+
return feedback_text
|
55 |
+
elif file.type == "application/json":
|
56 |
+
# If the file is JSON, try to parse and extract the feedback text
|
57 |
+
json_data = json.load(file)
|
58 |
+
feedback_text = []
|
59 |
+
# Adjust this depending on how the JSON is structured (e.g., each feedback is a list of feedback entries)
|
60 |
+
if isinstance(json_data, list):
|
61 |
+
feedback_text = [item.get('feedback', '') for item in json_data if 'feedback' in item]
|
62 |
+
elif isinstance(json_data, dict):
|
63 |
+
feedback_text = list(json_data.values()) # Include all values if feedback key doesn't exist
|
64 |
+
return feedback_text
|
65 |
+
elif file.type == "text/plain":
|
66 |
+
# If the file is plain text, read it directly
|
67 |
+
return [file.getvalue().decode("utf-8")]
|
68 |
+
else:
|
69 |
+
return ["Unsupported file type"]
|
70 |
+
|
71 |
+
# Display error or feedback extraction status
|
72 |
+
if uploaded_file:
|
73 |
+
feedback_text_list = extract_feedback(uploaded_file)
|
74 |
+
|
75 |
+
# If feedback is extracted, analyze it
|
76 |
+
if feedback_text_list:
|
77 |
+
for feedback_text in feedback_text_list:
|
78 |
+
if st.button(f'Analyze Feedback: "{feedback_text[:30]}..."'):
|
79 |
+
# Sentiment Analysis
|
80 |
+
sentiment = distilbert_model.predict([feedback_text])
|
81 |
+
sentiment_result = 'Positive' if sentiment == 1 else 'Negative'
|
82 |
+
st.write(f"Sentiment: {sentiment_result}")
|
83 |
+
|
84 |
+
# Topic Modeling
|
85 |
+
topics = bert_topic_model.predict([feedback_text])
|
86 |
+
st.write(f"Predicted Topic(s): {topics}")
|
87 |
+
|
88 |
+
# Recommendation System
|
89 |
+
recommendations = recommendation_model.predict([feedback_text])
|
90 |
+
st.write(f"Recommended Actions: {recommendations}")
|
91 |
+
else:
|
92 |
+
st.error("Unable to extract feedback from the file.")
|
93 |
+
else:
|
94 |
+
st.info("Please upload a feedback file to analyze.")
|