tarrasyed19472007 commited on
Commit
49b9656
·
verified ·
1 Parent(s): 3692333

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -8
app.py CHANGED
@@ -2,15 +2,13 @@ import streamlit as st
2
  from transformers import pipeline
3
  import torch
4
 
5
- # Check for device (GPU or CPU) availability
6
- device = 0 if torch.cuda.is_available() else -1 # Use GPU if available, else use CPU
7
-
8
- # Function to load the model with enhanced error handling
9
  @st.cache_resource
10
  def load_model():
11
  try:
 
12
  st.write("Attempting to load the emotion analysis model...")
13
- emotion_analyzer = pipeline("text-classification", model="suryakiran786/T5-emotion", device=device)
14
  st.write("Model loaded successfully!")
15
  return emotion_analyzer
16
  except Exception as e:
@@ -20,7 +18,7 @@ def load_model():
20
  # Initialize the model (with caching to prevent reloads)
21
  emotion_analyzer = load_model()
22
 
23
- # Check if the model is loaded successfully
24
  if emotion_analyzer is None:
25
  st.warning("The emotion analysis model could not be loaded. Please try again.")
26
  else:
@@ -33,7 +31,7 @@ def predict_emotion_single(response):
33
  return {"Error": "Emotion analyzer model not initialized. Please check model loading."}
34
 
35
  try:
36
- response = response.strip()
37
  result = emotion_analyzer([response])
38
  return {res["label"]: round(res["score"], 4) for res in result}
39
  except Exception as e:
@@ -78,4 +76,4 @@ if st.button("Submit Responses"):
78
  st.write(f"Your Response: {response}")
79
  st.write(f"Emotion Analysis: {analysis}")
80
  else:
81
- st.write("Please answer all the questions before submitting.")
 
2
  from transformers import pipeline
3
  import torch
4
 
5
+ # Function to load the model with error handling and ensure it's available
 
 
 
6
  @st.cache_resource
7
  def load_model():
8
  try:
9
+ # Attempt to load a model
10
  st.write("Attempting to load the emotion analysis model...")
11
+ emotion_analyzer = pipeline("text-classification", model="bhadresh-savani/bert-base-uncased-emotion", device=0 if torch.cuda.is_available() else -1)
12
  st.write("Model loaded successfully!")
13
  return emotion_analyzer
14
  except Exception as e:
 
18
  # Initialize the model (with caching to prevent reloads)
19
  emotion_analyzer = load_model()
20
 
21
+ # Check if the model is loaded
22
  if emotion_analyzer is None:
23
  st.warning("The emotion analysis model could not be loaded. Please try again.")
24
  else:
 
31
  return {"Error": "Emotion analyzer model not initialized. Please check model loading."}
32
 
33
  try:
34
+ # Analyze the emotion of the response
35
  result = emotion_analyzer([response])
36
  return {res["label"]: round(res["score"], 4) for res in result}
37
  except Exception as e:
 
76
  st.write(f"Your Response: {response}")
77
  st.write(f"Emotion Analysis: {analysis}")
78
  else:
79
+ st.write("Please answer all the questions before submitting.")