totoro74 commited on
Commit
f7e6017
·
verified ·
1 Parent(s): 78f9f72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -4,36 +4,38 @@ 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")
 
4
  import joblib
5
  import torch
6
  import os
7
+ from huggingface_hub import hf_hub_download
8
 
9
  # Function to load models safely
10
  def load_model(model_path, is_pytorch=False):
11
  """Loads a model based on the file type, ensuring safe execution."""
12
  try:
13
  if is_pytorch:
14
+ return torch.load(model_path, map_location=torch.device('cpu'))
15
  else:
16
  return joblib.load(model_path)
17
  except Exception as e:
18
  st.error(f"Error loading model {model_path}: {e}")
19
  return None
20
 
21
+ # Load models directly from Hugging Face
22
+ REPO_ID = "totoro74/Intelligent_Customer_Analyzer"
 
 
23
 
24
+ try:
25
+ # Load DistilBERT model
26
+ distilbert_model_path = hf_hub_download(repo_id=REPO_ID, filename="distilbert_model.pt")
27
+ distilbert_model = torch.load(distilbert_model_path, map_location=torch.device('cpu'))
 
 
 
28
 
29
+ # Load BERTopic model
30
+ bert_topic_model_path = hf_hub_download(repo_id=REPO_ID, filename="bertopic_model.joblib")
31
+ bert_topic_model = joblib.load(bert_topic_model_path)
32
+
33
+ # Load Recommendation model
34
+ recommendation_model_path = hf_hub_download(repo_id=REPO_ID, filename="recommendation_model.joblib")
35
+ recommendation_model = joblib.load(recommendation_model_path)
36
+
37
+ except Exception as e:
38
+ st.error(f"⚠️ Error loading models from Hugging Face: {e}")
39
 
40
  # Streamlit app layout
41
  st.title("📊 Intelligent Customer Feedback Analyzer")