ayush2917 commited on
Commit
d46c0ad
·
verified ·
1 Parent(s): 2c32ee2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -87,8 +87,8 @@ def get_daily_message_and_blessing():
87
  "blessing": "Hare Manavi! May Vrindavan’s joy fill your heart today!"
88
  }
89
 
90
- @backoff.on_exception(backoff.expo, (requests.exceptions.RequestException, requests.exceptions.HTTPError), max_tries=10, max_time=600)
91
- def make_api_request(url, headers, payload, timeout=120):
92
  """Helper function to make API requests with exponential backoff."""
93
  try:
94
  response = requests.post(url, headers=headers, json=payload, timeout=timeout)
@@ -154,7 +154,10 @@ def generate_gift_suggestions():
154
  "Ayush has created this chatbot as a surprise for her. "
155
  "Suggest three personalized gifts for Manavi that reflect her interests in art, nature, and spirituality, in a playful Krishna tone. "
156
  "Keep each suggestion short (1 sentence) and use Vrindavan imagery. "
157
- "Example: 'Hare Manavi! A peacock feather paintbrush to create art as vibrant as Vrindavan’s flowers!'"
 
 
 
158
  )
159
  payload = {
160
  "inputs": prompt,
@@ -173,11 +176,14 @@ def generate_gift_suggestions():
173
  if response:
174
  result = response.json()
175
  if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
176
- return result[0]["generated_text"].strip().split('\n')
 
177
  elif isinstance(result, dict) and "generated_text" in result:
178
- return result["generated_text"].strip().split('\n')
 
179
  elif isinstance(result, str):
180
- return result.strip().split('\n')
 
181
  logger.warning("Returning fallback gift suggestions due to API failure.")
182
  return [
183
  "Hare Manavi! A peacock feather paintbrush to create art as vibrant as Vrindavan’s flowers!",
@@ -401,11 +407,11 @@ def adventure():
401
 
402
  @app.route('/message')
403
  def message():
404
- """Render the birthday message page with an AI-generated message, image, and gift suggestions."""
405
  try:
406
  birthday_message = generate_birthday_message()
407
  gift_suggestions = generate_gift_suggestions()
408
- birthday_image_url = "/static/assets/krishna.png" # Use fallback image due to API issues
409
  return render_template('message.html',
410
  birthday_message=birthday_message,
411
  birthday_image_url=birthday_image_url,
 
87
  "blessing": "Hare Manavi! May Vrindavan’s joy fill your heart today!"
88
  }
89
 
90
+ @backoff.on_exception(backoff.expo, (requests.exceptions.RequestException, requests.exceptions.HTTPError), max_tries=12, max_time=900)
91
+ def make_api_request(url, headers, payload, timeout=180):
92
  """Helper function to make API requests with exponential backoff."""
93
  try:
94
  response = requests.post(url, headers=headers, json=payload, timeout=timeout)
 
154
  "Ayush has created this chatbot as a surprise for her. "
155
  "Suggest three personalized gifts for Manavi that reflect her interests in art, nature, and spirituality, in a playful Krishna tone. "
156
  "Keep each suggestion short (1 sentence) and use Vrindavan imagery. "
157
+ "Return the suggestions as a numbered list (1., 2., 3.). "
158
+ "Example: '1. Hare Manavi! A peacock feather paintbrush to create art as vibrant as Vrindavan’s flowers!\n"
159
+ "2. A lotus-shaped journal to capture your thoughts by the Yamuna’s serene banks!\n"
160
+ "3. A flute pendant to carry Vrindavan’s spiritual melody close to your heart!'"
161
  )
162
  payload = {
163
  "inputs": prompt,
 
176
  if response:
177
  result = response.json()
178
  if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
179
+ suggestions = result[0]["generated_text"].strip().split('\n')
180
+ return [s.strip() for s in suggestions if s.strip()] # Clean up empty lines
181
  elif isinstance(result, dict) and "generated_text" in result:
182
+ suggestions = result["generated_text"].strip().split('\n')
183
+ return [s.strip() for s in suggestions if s.strip()]
184
  elif isinstance(result, str):
185
+ suggestions = result.strip().split('\n')
186
+ return [s.strip() for s in suggestions if s.strip()]
187
  logger.warning("Returning fallback gift suggestions due to API failure.")
188
  return [
189
  "Hare Manavi! A peacock feather paintbrush to create art as vibrant as Vrindavan’s flowers!",
 
407
 
408
  @app.route('/message')
409
  def message():
410
+ """Render the birthday message page with an AI-generated message and gift suggestions."""
411
  try:
412
  birthday_message = generate_birthday_message()
413
  gift_suggestions = generate_gift_suggestions()
414
+ birthday_image_url = "/static/assets/krishna.png" # Use fallback image
415
  return render_template('message.html',
416
  birthday_message=birthday_message,
417
  birthday_image_url=birthday_image_url,