Update app.py
Browse files
app.py
CHANGED
@@ -116,6 +116,45 @@ def generate_daily_quote():
|
|
116 |
logger.error(f"Error generating daily quote: {str(e)}")
|
117 |
return "Hare Manavi! Let your heart bloom like the lotus in the Yamuna, for love is the sweetest melody of Vrindavan!"
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
def generate_birthday_message():
|
120 |
"""Generate a unique birthday message for Manavi using an AI model."""
|
121 |
headers = {
|
@@ -156,12 +195,62 @@ def generate_birthday_message():
|
|
156 |
logger.error(f"Error generating birthday message: {str(e)}")
|
157 |
return "Hare Manavi! On your special day, I’ve brought the sweetest butter from Vrindavan and a flute melody to make your heart dance—happy birthday, my dear gopi!"
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
@app.route('/')
|
160 |
def home():
|
161 |
-
"""Render the home page with daily message, blessing, and
|
162 |
daily_data = get_daily_message_and_blessing()
|
163 |
countdown_days = get_countdown()
|
164 |
daily_quote = generate_daily_quote()
|
|
|
165 |
|
166 |
return render_template('home.html',
|
167 |
daily_message=daily_data['message'],
|
@@ -169,6 +258,7 @@ def home():
|
|
169 |
is_special=daily_data['is_special'],
|
170 |
daily_blessing=daily_data['blessing'],
|
171 |
daily_quote=daily_quote,
|
|
|
172 |
countdown=countdown_days)
|
173 |
|
174 |
@app.route('/chat', methods=['GET', 'POST'])
|
@@ -185,14 +275,124 @@ def chat():
|
|
185 |
|
186 |
@app.route('/message')
|
187 |
def message():
|
188 |
-
"""Render the birthday message page with an AI-generated message and
|
189 |
birthday_message = generate_birthday_message()
|
|
|
190 |
# Generate a birthday image for Manavi
|
191 |
image_prompt = "Little Krishna celebrating Manavi’s birthday in Vrindavan, with peacocks, butter pots, and a festive atmosphere, cartoon style"
|
192 |
birthday_image_url = generate_krishna_image(image_prompt)
|
193 |
if not birthday_image_url:
|
194 |
birthday_image_url = "/static/assets/krishna.png" # Fallback image
|
195 |
-
return render_template('message.html', birthday_message=birthday_message, birthday_image_url=birthday_image_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
@app.route('/image', methods=['POST'])
|
198 |
def image():
|
@@ -250,17 +450,34 @@ def story():
|
|
250 |
logger.error(f"Error generating story: {str(e)}")
|
251 |
return jsonify({'story': f"Hare Manavi! One day, I hid the gopis’ {theme} in a peacock’s nest, and they danced with me to get it back—what a Vrindavan adventure!"})
|
252 |
|
253 |
-
@app.route('/
|
254 |
-
def
|
255 |
-
"""
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
-
# Add to app.py
|
264 |
@app.route('/riddle', methods=['GET'])
|
265 |
def riddle():
|
266 |
"""Generate a Krishna-themed riddle using an AI model."""
|
@@ -300,4 +517,14 @@ def riddle():
|
|
300 |
return jsonify({'riddle': "Hare Manavi! I play a tune that makes the gopis dance, but I’m not a drum—what am I? (Answer: Flute)"})
|
301 |
except Exception as e:
|
302 |
logger.error(f"Error generating riddle: {str(e)}")
|
303 |
-
return jsonify({'riddle': "Hare Manavi! I play a tune that makes the gopis dance, but I’m not a drum—what am I? (Answer: Flute)"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
logger.error(f"Error generating daily quote: {str(e)}")
|
117 |
return "Hare Manavi! Let your heart bloom like the lotus in the Yamuna, for love is the sweetest melody of Vrindavan!"
|
118 |
|
119 |
+
def generate_horoscope():
|
120 |
+
"""Generate a personalized daily horoscope for Manavi in Krishna's tone."""
|
121 |
+
headers = {
|
122 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
123 |
+
"Content-Type": "application/json"
|
124 |
+
}
|
125 |
+
prompt = (
|
126 |
+
"You are Little Krishna, a wise and playful cowherd from Vrindavan, speaking to Manavi on April 13, 2025. "
|
127 |
+
"Generate a short, personalized daily horoscope (1-2 sentences) for Manavi, reflecting her interests in art, nature, and spirituality, in a wise yet playful tone, using Vrindavan imagery. "
|
128 |
+
"Example: 'Hare Manavi! Today, the Yamuna’s gentle waves inspire your art—paint with the colors of Vrindavan’s flowers and let your spirit dance like the peacocks!'"
|
129 |
+
)
|
130 |
+
payload = {
|
131 |
+
"inputs": prompt,
|
132 |
+
"parameters": {
|
133 |
+
"max_length": 60,
|
134 |
+
"temperature": 0.9,
|
135 |
+
"top_p": 0.9,
|
136 |
+
"top_k": 50
|
137 |
+
}
|
138 |
+
}
|
139 |
+
try:
|
140 |
+
response = requests.post(
|
141 |
+
"https://api-inference.huggingface.co/models/bigscience/bloom-560m",
|
142 |
+
headers=headers,
|
143 |
+
json=payload
|
144 |
+
)
|
145 |
+
if response.status_code == 200:
|
146 |
+
result = response.json()
|
147 |
+
if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
|
148 |
+
return result[0]["generated_text"].strip()
|
149 |
+
elif isinstance(result, dict) and "generated_text" in result:
|
150 |
+
return result["generated_text"].strip()
|
151 |
+
elif isinstance(result, str):
|
152 |
+
return result.strip()
|
153 |
+
return "Hare Manavi! Today, the Yamuna’s gentle waves inspire your art—paint with the colors of Vrindavan’s flowers and let your spirit dance like the peacocks!"
|
154 |
+
except Exception as e:
|
155 |
+
logger.error(f"Error generating horoscope: {str(e)}")
|
156 |
+
return "Hare Manavi! Today, the Yamuna’s gentle waves inspire your art—paint with the colors of Vrindavan’s flowers and let your spirit dance like the peacocks!"
|
157 |
+
|
158 |
def generate_birthday_message():
|
159 |
"""Generate a unique birthday message for Manavi using an AI model."""
|
160 |
headers = {
|
|
|
195 |
logger.error(f"Error generating birthday message: {str(e)}")
|
196 |
return "Hare Manavi! On your special day, I’ve brought the sweetest butter from Vrindavan and a flute melody to make your heart dance—happy birthday, my dear gopi!"
|
197 |
|
198 |
+
def generate_gift_suggestions():
|
199 |
+
"""Generate personalized gift suggestions for Manavi in Krishna's tone."""
|
200 |
+
headers = {
|
201 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
202 |
+
"Content-Type": "application/json"
|
203 |
+
}
|
204 |
+
prompt = (
|
205 |
+
"You are Little Krishna, a playful cowherd from Vrindavan, speaking to Manavi on her birthday, April 19, 2025. "
|
206 |
+
"Ayush has created this chatbot as a surprise for her. "
|
207 |
+
"Suggest three personalized gifts for Manavi that reflect her interests in art, nature, and spirituality, in a playful Krishna tone. "
|
208 |
+
"Keep each suggestion short (1 sentence) and use Vrindavan imagery. "
|
209 |
+
"Example: 'Hare Manavi! A peacock feather paintbrush to create art as vibrant as Vrindavan’s flowers!'"
|
210 |
+
)
|
211 |
+
payload = {
|
212 |
+
"inputs": prompt,
|
213 |
+
"parameters": {
|
214 |
+
"max_length": 100,
|
215 |
+
"temperature": 0.9,
|
216 |
+
"top_p": 0.9,
|
217 |
+
"top_k": 50
|
218 |
+
}
|
219 |
+
}
|
220 |
+
try:
|
221 |
+
response = requests.post(
|
222 |
+
"https://api-inference.huggingface.co/models/google/gemma-2b",
|
223 |
+
headers=headers,
|
224 |
+
json=payload
|
225 |
+
)
|
226 |
+
if response.status_code == 200:
|
227 |
+
result = response.json()
|
228 |
+
if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
|
229 |
+
return result[0]["generated_text"].strip().split('\n')
|
230 |
+
elif isinstance(result, dict) and "generated_text" in result:
|
231 |
+
return result["generated_text"].strip().split('\n')
|
232 |
+
elif isinstance(result, str):
|
233 |
+
return result.strip().split('\n')
|
234 |
+
return [
|
235 |
+
"Hare Manavi! A peacock feather paintbrush to create art as vibrant as Vrindavan’s flowers!",
|
236 |
+
"Hare Manavi! A lotus-shaped journal to capture your thoughts by the Yamuna’s serene banks!",
|
237 |
+
"Hare Manavi! A flute pendant to carry Vrindavan’s spiritual melody close to your heart!"
|
238 |
+
]
|
239 |
+
except Exception as e:
|
240 |
+
logger.error(f"Error generating gift suggestions: {str(e)}")
|
241 |
+
return [
|
242 |
+
"Hare Manavi! A peacock feather paintbrush to create art as vibrant as Vrindavan’s flowers!",
|
243 |
+
"Hare Manavi! A lotus-shaped journal to capture your thoughts by the Yamuna’s serene banks!",
|
244 |
+
"Hare Manavi! A flute pendant to carry Vrindavan’s spiritual melody close to your heart!"
|
245 |
+
]
|
246 |
+
|
247 |
@app.route('/')
|
248 |
def home():
|
249 |
+
"""Render the home page with daily message, blessing, quote, and horoscope"""
|
250 |
daily_data = get_daily_message_and_blessing()
|
251 |
countdown_days = get_countdown()
|
252 |
daily_quote = generate_daily_quote()
|
253 |
+
daily_horoscope = generate_horoscope()
|
254 |
|
255 |
return render_template('home.html',
|
256 |
daily_message=daily_data['message'],
|
|
|
258 |
is_special=daily_data['is_special'],
|
259 |
daily_blessing=daily_data['blessing'],
|
260 |
daily_quote=daily_quote,
|
261 |
+
daily_horoscope=daily_horoscope,
|
262 |
countdown=countdown_days)
|
263 |
|
264 |
@app.route('/chat', methods=['GET', 'POST'])
|
|
|
275 |
|
276 |
@app.route('/message')
|
277 |
def message():
|
278 |
+
"""Render the birthday message page with an AI-generated message, image, and gift suggestions"""
|
279 |
birthday_message = generate_birthday_message()
|
280 |
+
gift_suggestions = generate_gift_suggestions()
|
281 |
# Generate a birthday image for Manavi
|
282 |
image_prompt = "Little Krishna celebrating Manavi’s birthday in Vrindavan, with peacocks, butter pots, and a festive atmosphere, cartoon style"
|
283 |
birthday_image_url = generate_krishna_image(image_prompt)
|
284 |
if not birthday_image_url:
|
285 |
birthday_image_url = "/static/assets/krishna.png" # Fallback image
|
286 |
+
return render_template('message.html', birthday_message=birthday_message, birthday_image_url=birthday_image_url, gift_suggestions=gift_suggestions)
|
287 |
+
|
288 |
+
@app.route('/adventure', methods=['GET', 'POST'])
|
289 |
+
def adventure():
|
290 |
+
"""Handle the interactive Vrindavan adventure game"""
|
291 |
+
if request.method == 'POST':
|
292 |
+
choice = request.form.get('choice', '')
|
293 |
+
current_scene = request.form.get('current_scene', 'start')
|
294 |
+
scenes = {
|
295 |
+
'start': {
|
296 |
+
'prompt': (
|
297 |
+
"You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
|
298 |
+
"Start the adventure with a short scene (2-3 sentences) introducing Manavi to Vrindavan, and offer her two choices to continue the story. "
|
299 |
+
"Example: 'Hare Manavi! We’re standing by the Yamuna’s sparkling waters in Vrindavan—shall we chase the peacocks dancing nearby or sneak some butter from the gopis’ pots? (Choices: Chase peacocks, Sneak butter)'"
|
300 |
+
),
|
301 |
+
'next_scenes': {'Chase peacocks': 'peacocks', 'Sneak butter': 'butter'}
|
302 |
+
},
|
303 |
+
'peacocks': {
|
304 |
+
'prompt': (
|
305 |
+
"You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
|
306 |
+
"Manavi chose to chase peacocks. Describe a short scene (2-3 sentences) where Manavi chases peacocks with you, and offer her two new choices to continue the story. "
|
307 |
+
"Example: 'Hare Manavi! We chased the peacocks through Vrindavan’s fields, their feathers shimmering like rainbows—now, shall we climb the kadamba tree to see them better or play a tune on my flute to call them back? (Choices: Climb tree, Play flute)'"
|
308 |
+
),
|
309 |
+
'next_scenes': {'Climb tree': 'tree', 'Play flute': 'flute'}
|
310 |
+
},
|
311 |
+
'butter': {
|
312 |
+
'prompt': (
|
313 |
+
"You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
|
314 |
+
"Manavi chose to sneak butter. Describe a short scene (2-3 sentences) where Manavi sneaks butter with you, and offer her two new choices to continue the story. "
|
315 |
+
"Example: 'Hare Manavi! We sneaked some butter from the gopis’ pots, giggling as they chased us—shall we hide by the Yamuna to enjoy our treat or share it with the calves? (Choices: Hide by Yamuna, Share with calves)'"
|
316 |
+
),
|
317 |
+
'next_scenes': {'Hide by Yamuna': 'yamuna', 'Share with calves': 'calves'}
|
318 |
+
},
|
319 |
+
'tree': {
|
320 |
+
'prompt': (
|
321 |
+
"You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
|
322 |
+
"Manavi chose to climb the kadamba tree. Describe a short scene (2-3 sentences) where Manavi climbs the tree with you, and end the story with a playful conclusion. "
|
323 |
+
"Example: 'Hare Manavi! We climbed the kadamba tree, watching the peacocks dance below, and I played a flute tune to make them twirl even faster—what a magical Vrindavan day!'"
|
324 |
+
),
|
325 |
+
'next_scenes': {}
|
326 |
+
},
|
327 |
+
'flute': {
|
328 |
+
'prompt': (
|
329 |
+
"You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
|
330 |
+
"Manavi chose to play the flute. Describe a short scene (2-3 sentences) where Manavi listens to your flute, and end the story with a playful conclusion. "
|
331 |
+
"Example: 'Hare Manavi! I played a sweet flute tune, and the peacocks gathered around us, dancing to the melody—what a joyful Vrindavan memory we’ve made!'"
|
332 |
+
),
|
333 |
+
'next_scenes': {}
|
334 |
+
},
|
335 |
+
'yamuna': {
|
336 |
+
'prompt': (
|
337 |
+
"You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
|
338 |
+
"Manavi chose to hide by the Yamuna. Describe a short scene (2-3 sentences) where Manavi hides by the Yamuna with you, and end the story with a playful conclusion. "
|
339 |
+
"Example: 'Hare Manavi! We hid by the Yamuna, sharing the butter as the river sparkled, and the gopis laughed when they found us—what a mischievous Vrindavan adventure!'"
|
340 |
+
),
|
341 |
+
'next_scenes': {}
|
342 |
+
},
|
343 |
+
'calves': {
|
344 |
+
'prompt': (
|
345 |
+
"You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
|
346 |
+
"Manavi chose to share the butter with the calves. Describe a short scene (2-3 sentences) where Manavi shares the butter with the calves, and end the story with a playful conclusion. "
|
347 |
+
"Example: 'Hare Manavi! We shared the butter with the calves, who mooed happily, and the gopis smiled at our kindness—what a sweet Vrindavan moment!'"
|
348 |
+
),
|
349 |
+
'next_scenes': {}
|
350 |
+
}
|
351 |
+
}
|
352 |
+
|
353 |
+
scene = scenes.get(current_scene, scenes['start'])
|
354 |
+
headers = {
|
355 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
356 |
+
"Content-Type": "application/json"
|
357 |
+
}
|
358 |
+
payload = {
|
359 |
+
"inputs": scene['prompt'],
|
360 |
+
"parameters": {
|
361 |
+
"max_length": 80,
|
362 |
+
"temperature": 0.9,
|
363 |
+
"top_p": 0.9,
|
364 |
+
"top_k": 50
|
365 |
+
}
|
366 |
+
}
|
367 |
+
try:
|
368 |
+
response = requests.post(
|
369 |
+
"https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-1.3B",
|
370 |
+
headers=headers,
|
371 |
+
json=payload
|
372 |
+
)
|
373 |
+
if response.status_code == 200:
|
374 |
+
result = response.json()
|
375 |
+
if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
|
376 |
+
story_text = result[0]["generated_text"].strip()
|
377 |
+
elif isinstance(result, dict) and "generated_text" in result:
|
378 |
+
story_text = result["generated_text"].strip()
|
379 |
+
elif isinstance(result, str):
|
380 |
+
story_text = result.strip()
|
381 |
+
else:
|
382 |
+
story_text = scene['prompt'].split('Example: ')[1]
|
383 |
+
else:
|
384 |
+
story_text = scene['prompt'].split('Example: ')[1]
|
385 |
+
except Exception as e:
|
386 |
+
logger.error(f"Error generating adventure scene: {str(e)}")
|
387 |
+
story_text = scene['prompt'].split('Example: ')[1]
|
388 |
+
|
389 |
+
next_scene = scene['next_scenes'].get(choice, None)
|
390 |
+
if next_scene:
|
391 |
+
return jsonify({'scene': story_text, 'current_scene': next_scene, 'choices': list(scene['next_scenes'].keys())})
|
392 |
+
else:
|
393 |
+
return jsonify({'scene': story_text, 'current_scene': None, 'choices': []})
|
394 |
+
|
395 |
+
return render_template('adventure.html')
|
396 |
|
397 |
@app.route('/image', methods=['POST'])
|
398 |
def image():
|
|
|
450 |
logger.error(f"Error generating story: {str(e)}")
|
451 |
return jsonify({'story': f"Hare Manavi! One day, I hid the gopis’ {theme} in a peacock’s nest, and they danced with me to get it back—what a Vrindavan adventure!"})
|
452 |
|
453 |
+
@app.route('/story_audio', methods=['POST'])
|
454 |
+
def story_audio():
|
455 |
+
"""Generate audio narration for a story using text-to-speech."""
|
456 |
+
story_text = request.json.get('story_text', '')
|
457 |
+
headers = {
|
458 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
459 |
+
"Content-Type": "application/json"
|
460 |
+
}
|
461 |
+
payload = {
|
462 |
+
"inputs": story_text
|
463 |
+
}
|
464 |
+
try:
|
465 |
+
response = requests.post(
|
466 |
+
"https://api-inference.huggingface.co/models/facebook/tts-transformer-en-ljspeech",
|
467 |
+
headers=headers,
|
468 |
+
json=payload
|
469 |
+
)
|
470 |
+
if response.status_code == 200:
|
471 |
+
# Save the audio file temporarily
|
472 |
+
audio_path = "static/audio/story_audio.mp3"
|
473 |
+
with open(audio_path, "wb") as f:
|
474 |
+
f.write(response.content)
|
475 |
+
return jsonify({'audio_url': f"/{audio_path}"})
|
476 |
+
return jsonify({'error': 'Failed to generate audio'}), 500
|
477 |
+
except Exception as e:
|
478 |
+
logger.error(f"Error generating story audio: {str(e)}")
|
479 |
+
return jsonify({'error': 'Failed to generate audio'}), 500
|
480 |
|
|
|
481 |
@app.route('/riddle', methods=['GET'])
|
482 |
def riddle():
|
483 |
"""Generate a Krishna-themed riddle using an AI model."""
|
|
|
517 |
return jsonify({'riddle': "Hare Manavi! I play a tune that makes the gopis dance, but I’m not a drum—what am I? (Answer: Flute)"})
|
518 |
except Exception as e:
|
519 |
logger.error(f"Error generating riddle: {str(e)}")
|
520 |
+
return jsonify({'riddle': "Hare Manavi! I play a tune that makes the gopis dance, but I’m not a drum—what am I? (Answer: Flute)"})
|
521 |
+
|
522 |
+
@app.route('/countdown')
|
523 |
+
def countdown():
|
524 |
+
"""Return the number of days until Manavi's birthday"""
|
525 |
+
days_left = get_countdown()
|
526 |
+
return jsonify({'days': days_left})
|
527 |
+
|
528 |
+
if __name__ == '__main__':
|
529 |
+
port = int(os.environ.get('PORT', 5000))
|
530 |
+
app.run(host='0.0.0.0', port=port, debug=False)
|