tarrasyed19472007 commited on
Commit
5966f7d
·
verified ·
1 Parent(s): abc0dd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -19,10 +19,10 @@ st.subheader("Understand your emotions better with AI-powered predictions!")
19
  def load_emotion_model():
20
  try:
21
  st.info("⏳ Loading the emotion analysis model, please wait...")
22
- # Using a publicly available model
23
  emotion_analyzer = pipeline(
24
  "text-classification",
25
- model="bhadresh-savani/distilbert-base-uncased-emotion",
26
  device=0 if torch.cuda.is_available() else -1, # Use GPU if available
27
  )
28
  st.success("✅ Model loaded successfully!")
@@ -37,13 +37,15 @@ emotion_analyzer = load_emotion_model()
37
  # ---- Function for Predicting Emotion ----
38
  def predict_emotion(text):
39
  if emotion_analyzer is None:
40
- return {"Error": "Emotion analyzer model not initialized. Please reload the app."}
 
41
 
42
  try:
43
  # Analyze emotions
44
  result = emotion_analyzer([text])
45
  return {res["label"]: round(res["score"], 4) for res in result}
46
  except Exception as e:
 
47
  return {"Error": f"Prediction failed: {e}"}
48
 
49
  # ---- User Input Section ----
@@ -88,3 +90,11 @@ st.markdown(
88
  Designed for a fun and intuitive experience! 🌟
89
  """
90
  )
 
 
 
 
 
 
 
 
 
19
  def load_emotion_model():
20
  try:
21
  st.info("⏳ Loading the emotion analysis model, please wait...")
22
+ # Using a publicly available model for emotion analysis
23
  emotion_analyzer = pipeline(
24
  "text-classification",
25
+ model="bhadresh-savani/distilbert-base-uncased-emotion", # A valid public model
26
  device=0 if torch.cuda.is_available() else -1, # Use GPU if available
27
  )
28
  st.success("✅ Model loaded successfully!")
 
37
  # ---- Function for Predicting Emotion ----
38
  def predict_emotion(text):
39
  if emotion_analyzer is None:
40
+ st.error("⚠️ Model not loaded. Please reload the app.")
41
+ return {"Error": "Emotion analyzer model not initialized. Please try again later."}
42
 
43
  try:
44
  # Analyze emotions
45
  result = emotion_analyzer([text])
46
  return {res["label"]: round(res["score"], 4) for res in result}
47
  except Exception as e:
48
+ st.error(f"⚠️ Prediction failed: {e}")
49
  return {"Error": f"Prediction failed: {e}"}
50
 
51
  # ---- User Input Section ----
 
90
  Designed for a fun and intuitive experience! 🌟
91
  """
92
  )
93
+
94
+ # ---- Error Handling and User Suggestions ----
95
+ if emotion_analyzer is None:
96
+ st.error("⚠️ We couldn't load the emotion analysis model. Please check your internet connection or try again later.")
97
+ st.markdown("🔧 **Troubleshooting Steps:**")
98
+ st.markdown("1. Ensure you have a stable internet connection.")
99
+ st.markdown("2. If the issue persists, please refresh the page and try again.")
100
+ st.markdown("3. Check if the model has been updated or is temporarily unavailable.")