Lucasstranger1 commited on
Commit
d78bf26
1 Parent(s): dc26248
Files changed (1) hide show
  1. app.py +7 -2
app.py CHANGED
@@ -29,12 +29,17 @@ def query_emotion(filename):
29
  # Function to generate a joke or uplifting text based on the mood
30
  def generate_text_based_on_mood(emotion):
31
  try:
32
- prompt = f"Generate a light-hearted joke or uplifting message for someone who is feeling {emotion}."
 
33
 
34
  for attempt in range(5): # Retry up to 5 times
35
  response = requests.post(text_model_url, headers=headers, json={"inputs": prompt})
36
  if response.status_code == 200:
37
- return response.json()[0]['generated_text']
 
 
 
 
38
  elif response.status_code == 503: # Service Unavailable
39
  st.warning("Model is loading, retrying...")
40
  time.sleep(5) # Wait before retrying
 
29
  # Function to generate a joke or uplifting text based on the mood
30
  def generate_text_based_on_mood(emotion):
31
  try:
32
+ # Define a clear and directive prompt
33
+ prompt = f"Please tell a funny joke that would cheer someone up who is feeling {emotion}. Make it light-hearted and fun."
34
 
35
  for attempt in range(5): # Retry up to 5 times
36
  response = requests.post(text_model_url, headers=headers, json={"inputs": prompt})
37
  if response.status_code == 200:
38
+ generated_text = response.json()[0]['generated_text']
39
+ if generated_text.strip(): # Ensure the response is not empty
40
+ return generated_text
41
+ else:
42
+ st.warning("Received an empty joke, retrying...")
43
  elif response.status_code == 503: # Service Unavailable
44
  st.warning("Model is loading, retrying...")
45
  time.sleep(5) # Wait before retrying