Update app.py
Browse files
app.py
CHANGED
|
@@ -3,63 +3,89 @@ import pandas as pd
|
|
| 3 |
import json
|
| 4 |
import joblib
|
| 5 |
import torch
|
|
|
|
| 6 |
|
| 7 |
-
# Function to load
|
| 8 |
-
def load_model(model_path):
|
| 9 |
-
"""Loads
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Model file paths (ensure they
|
| 13 |
-
distilbert_model_path = "models/distilbert_model.
|
| 14 |
bert_topic_model_path = "models/bertopic_model.joblib"
|
| 15 |
recommendation_model_path = "models/recommendation_model.joblib"
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
bert_topic_model = load_model(bert_topic_model_path)
|
| 20 |
recommendation_model = load_model(recommendation_model_path)
|
| 21 |
|
| 22 |
# Streamlit app layout
|
| 23 |
-
st.title("Intelligent Customer Feedback Analyzer")
|
| 24 |
st.write("Analyze customer feedback for sentiment, topics, and get personalized recommendations.")
|
| 25 |
|
| 26 |
# User input for customer feedback file
|
| 27 |
-
uploaded_file = st.file_uploader("Upload a Feedback File (CSV, JSON, TXT)", type=["csv", "json", "txt"])
|
| 28 |
|
| 29 |
# Function to extract feedback text from different file formats
|
| 30 |
def extract_feedback(file):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
feedback_text.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
#
|
| 47 |
if uploaded_file:
|
| 48 |
feedback_text_list = extract_feedback(uploaded_file)
|
| 49 |
|
| 50 |
-
if feedback_text_list:
|
| 51 |
for feedback_text in feedback_text_list:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
else:
|
| 63 |
-
st.error("Unable to
|
| 64 |
else:
|
| 65 |
-
st.info("Please upload a feedback file to analyze.")
|
|
|
|
| 3 |
import json
|
| 4 |
import joblib
|
| 5 |
import torch
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Function to load models safely
|
| 9 |
+
def load_model(model_path, is_pytorch=False):
|
| 10 |
+
"""Loads a model based on the file type, ensuring safe execution."""
|
| 11 |
+
try:
|
| 12 |
+
if is_pytorch:
|
| 13 |
+
return torch.load(model_path, map_location=torch.device('cpu'), weights_only=False)
|
| 14 |
+
else:
|
| 15 |
+
return joblib.load(model_path)
|
| 16 |
+
except Exception as e:
|
| 17 |
+
st.error(f"Error loading model {model_path}: {e}")
|
| 18 |
+
return None
|
| 19 |
|
| 20 |
+
# Model file paths (ensure they exist)
|
| 21 |
+
distilbert_model_path = "models/distilbert_model.pt"
|
| 22 |
bert_topic_model_path = "models/bertopic_model.joblib"
|
| 23 |
recommendation_model_path = "models/recommendation_model.joblib"
|
| 24 |
|
| 25 |
+
# Check if models exist before loading
|
| 26 |
+
if not os.path.exists(distilbert_model_path):
|
| 27 |
+
st.error(f"Model not found: {distilbert_model_path}")
|
| 28 |
+
if not os.path.exists(bert_topic_model_path):
|
| 29 |
+
st.error(f"Model not found: {bert_topic_model_path}")
|
| 30 |
+
if not os.path.exists(recommendation_model_path):
|
| 31 |
+
st.error(f"Model not found: {recommendation_model_path}")
|
| 32 |
+
|
| 33 |
+
# Load models safely
|
| 34 |
+
distilbert_model = load_model(distilbert_model_path, is_pytorch=True)
|
| 35 |
bert_topic_model = load_model(bert_topic_model_path)
|
| 36 |
recommendation_model = load_model(recommendation_model_path)
|
| 37 |
|
| 38 |
# Streamlit app layout
|
| 39 |
+
st.title("π Intelligent Customer Feedback Analyzer")
|
| 40 |
st.write("Analyze customer feedback for sentiment, topics, and get personalized recommendations.")
|
| 41 |
|
| 42 |
# User input for customer feedback file
|
| 43 |
+
uploaded_file = st.file_uploader("π Upload a Feedback File (CSV, JSON, TXT)", type=["csv", "json", "txt"])
|
| 44 |
|
| 45 |
# Function to extract feedback text from different file formats
|
| 46 |
def extract_feedback(file):
|
| 47 |
+
"""Extracts text data from CSV, JSON, or TXT files."""
|
| 48 |
+
try:
|
| 49 |
+
if file.type == "text/csv":
|
| 50 |
+
df = pd.read_csv(file)
|
| 51 |
+
feedback_text = df.iloc[:, 0].dropna().astype(str).tolist()
|
| 52 |
+
return feedback_text
|
| 53 |
+
elif file.type == "application/json":
|
| 54 |
+
json_data = json.load(file)
|
| 55 |
+
feedback_text = [item.get('feedback', '') for item in json_data if isinstance(item, dict)]
|
| 56 |
+
return feedback_text
|
| 57 |
+
elif file.type == "text/plain":
|
| 58 |
+
return file.getvalue().decode("utf-8").split("\n")
|
| 59 |
+
else:
|
| 60 |
+
return ["Unsupported file type"]
|
| 61 |
+
except Exception as e:
|
| 62 |
+
st.error(f"Error processing file: {e}")
|
| 63 |
+
return []
|
| 64 |
|
| 65 |
+
# Process uploaded file
|
| 66 |
if uploaded_file:
|
| 67 |
feedback_text_list = extract_feedback(uploaded_file)
|
| 68 |
|
| 69 |
+
if feedback_text_list and distilbert_model and bert_topic_model and recommendation_model:
|
| 70 |
for feedback_text in feedback_text_list:
|
| 71 |
+
with st.expander(f'π Analyze Feedback: "{feedback_text[:30]}..."'):
|
| 72 |
+
try:
|
| 73 |
+
# Sentiment Analysis
|
| 74 |
+
sentiment = distilbert_model.predict([feedback_text])
|
| 75 |
+
sentiment_result = 'π Positive' if sentiment == 1 else 'βΉοΈ Negative'
|
| 76 |
+
st.write(f"**Sentiment:** {sentiment_result}")
|
| 77 |
+
|
| 78 |
+
# Topic Prediction
|
| 79 |
+
topics = bert_topic_model.predict([feedback_text])
|
| 80 |
+
st.write(f"**Predicted Topic(s):** {topics}")
|
| 81 |
|
| 82 |
+
# Recommendations
|
| 83 |
+
recommendations = recommendation_model.predict([feedback_text])
|
| 84 |
+
st.write(f"**Recommended Actions:** {recommendations}")
|
| 85 |
|
| 86 |
+
except Exception as e:
|
| 87 |
+
st.error(f"Error analyzing feedback: {e}")
|
| 88 |
else:
|
| 89 |
+
st.error("β οΈ Unable to analyze feedback. Please check if models are correctly loaded.")
|
| 90 |
else:
|
| 91 |
+
st.info("π Please upload a feedback file to analyze.")
|