ayush2917 commited on
Commit
7cba04a
·
verified ·
1 Parent(s): 567ee21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -95
app.py CHANGED
@@ -282,108 +282,116 @@ def message():
282
  @app.route('/adventure', methods=['GET', 'POST'])
283
  def adventure():
284
  """Handle the interactive Vrindavan adventure game"""
285
- if request.method == 'POST':
286
- choice = request.form.get('choice', '')
287
- current_scene = request.form.get('current_scene', 'start')
288
- scenes = {
289
- 'start': {
290
- 'prompt': (
291
- "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
292
- "Start the adventure with a short scene (2-3 sentences) introducing Manavi to Vrindavan, and offer her two choices to continue the story. "
293
- "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)'"
294
- ),
295
- 'next_scenes': {'Chase peacocks': 'peacocks', 'Sneak butter': 'butter'}
296
- },
297
- 'peacocks': {
298
- 'prompt': (
299
- "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
300
- "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. "
301
- "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)'"
302
- ),
303
- 'next_scenes': {'Climb tree': 'tree', 'Play flute': 'flute'}
304
- },
305
- 'butter': {
306
- 'prompt': (
307
- "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
308
- "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. "
309
- "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)'"
310
- ),
311
- 'next_scenes': {'Hide by Yamuna': 'yamuna', 'Share with calves': 'calves'}
312
- },
313
- 'tree': {
314
- 'prompt': (
315
- "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
316
- "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. "
317
- "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!'"
318
- ),
319
- 'next_scenes': {}
320
- },
321
- 'flute': {
322
- 'prompt': (
323
- "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
324
- "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. "
325
- "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!'"
326
- ),
327
- 'next_scenes': {}
328
- },
329
- 'yamuna': {
330
- 'prompt': (
331
- "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
332
- "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. "
333
- "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!'"
334
- ),
335
- 'next_scenes': {}
336
- },
337
- 'calves': {
338
- 'prompt': (
339
- "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
340
- "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. "
341
- "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!'"
342
- ),
343
- 'next_scenes': {}
 
 
 
 
344
  }
345
- }
346
 
347
- scene = scenes.get(current_scene, scenes['start'])
348
- headers = {
349
- "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
350
- "Content-Type": "application/json"
351
- }
352
- payload = {
353
- "inputs": scene['prompt'],
354
- "parameters": {
355
- "max_length": 80,
356
- "temperature": 0.9,
357
- "top_p": 0.9,
358
- "top_k": 50
359
  }
360
- }
361
- response = make_api_request(
362
- "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-1.3B",
363
- headers=headers,
364
- payload=payload
365
- )
366
- if response:
367
- result = response.json()
368
- if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
369
- story_text = result[0]["generated_text"].strip()
370
- elif isinstance(result, dict) and "generated_text" in result:
371
- story_text = result["generated_text"].strip()
372
- elif isinstance(result, str):
373
- story_text = result.strip()
 
 
 
 
 
 
 
 
 
 
 
 
374
  else:
 
375
  story_text = scene['prompt'].split('Example: ')[1]
376
- else:
377
- logger.error("Failed to generate adventure scene after retries.")
378
- story_text = scene['prompt'].split('Example: ')[1]
379
 
380
- next_scene = scene['next_scenes'].get(choice, None)
381
- if next_scene:
382
- return jsonify({'scene': story_text, 'current_scene': next_scene, 'choices': list(scene['next_scenes'].keys())})
383
- else:
384
- return jsonify({'scene': story_text, 'current_scene': None, 'choices': []})
385
 
386
- return render_template('adventure.html')
 
 
 
387
 
388
  @app.route('/image', methods=['POST'])
389
  def image():
 
282
  @app.route('/adventure', methods=['GET', 'POST'])
283
  def adventure():
284
  """Handle the interactive Vrindavan adventure game"""
285
+ try:
286
+ if request.method == 'POST':
287
+ choice = request.form.get('choice', '')
288
+ current_scene = request.form.get('current_scene', 'start')
289
+ logger.info(f"Adventure POST request: current_scene={current_scene}, choice={choice}")
290
+
291
+ scenes = {
292
+ 'start': {
293
+ 'prompt': (
294
+ "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
295
+ "Start the adventure with a short scene (2-3 sentences) introducing Manavi to Vrindavan, and offer her two choices to continue the story. "
296
+ "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)'"
297
+ ),
298
+ 'next_scenes': {'Chase peacocks': 'peacocks', 'Sneak butter': 'butter'}
299
+ },
300
+ 'peacocks': {
301
+ 'prompt': (
302
+ "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
303
+ "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. "
304
+ "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)'"
305
+ ),
306
+ 'next_scenes': {'Climb tree': 'tree', 'Play flute': 'flute'}
307
+ },
308
+ 'butter': {
309
+ 'prompt': (
310
+ "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
311
+ "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. "
312
+ "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)'"
313
+ ),
314
+ 'next_scenes': {'Hide by Yamuna': 'yamuna', 'Share with calves': 'calves'}
315
+ },
316
+ 'tree': {
317
+ 'prompt': (
318
+ "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
319
+ "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. "
320
+ "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!'"
321
+ ),
322
+ 'next_scenes': {}
323
+ },
324
+ 'flute': {
325
+ 'prompt': (
326
+ "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
327
+ "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. "
328
+ "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!'"
329
+ ),
330
+ 'next_scenes': {}
331
+ },
332
+ 'yamuna': {
333
+ 'prompt': (
334
+ "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
335
+ "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. "
336
+ "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!'"
337
+ ),
338
+ 'next_scenes': {}
339
+ },
340
+ 'calves': {
341
+ 'prompt': (
342
+ "You are Little Krishna, a playful cowherd from Vrindavan, guiding Manavi on an interactive adventure in Vrindavan. "
343
+ "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. "
344
+ "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!'"
345
+ ),
346
+ 'next_scenes': {}
347
+ }
348
  }
 
349
 
350
+ scene = scenes.get(current_scene, scenes['start'])
351
+ headers = {
352
+ "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
353
+ "Content-Type": "application/json"
 
 
 
 
 
 
 
 
354
  }
355
+ payload = {
356
+ "inputs": scene['prompt'],
357
+ "parameters": {
358
+ "max_length": 80,
359
+ "temperature": 0.9,
360
+ "top_p": 0.9,
361
+ "top_k": 50
362
+ }
363
+ }
364
+ response = make_api_request(
365
+ "https://api-inference.huggingface.co/models/EleutherAI/gpt-neo-1.3B",
366
+ headers=headers,
367
+ payload=payload
368
+ )
369
+ if response and response.status_code == 200:
370
+ result = response.json()
371
+ logger.info(f"API response: {result}")
372
+ if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
373
+ story_text = result[0]["generated_text"].strip()
374
+ elif isinstance(result, dict) and "generated_text" in result:
375
+ story_text = result["generated_text"].strip()
376
+ elif isinstance(result, str):
377
+ story_text = result.strip()
378
+ else:
379
+ logger.error("Unexpected API response format.")
380
+ story_text = scene['prompt'].split('Example: ')[1]
381
  else:
382
+ logger.error("Failed to generate adventure scene after retries.")
383
  story_text = scene['prompt'].split('Example: ')[1]
 
 
 
384
 
385
+ next_scene = scene['next_scenes'].get(choice, None)
386
+ if next_scene:
387
+ return jsonify({'scene': story_text, 'current_scene': next_scene, 'choices': list(scene['next_scenes'].keys())})
388
+ else:
389
+ return jsonify({'scene': story_text, 'current_scene': None, 'choices': []})
390
 
391
+ return render_template('adventure.html')
392
+ except Exception as e:
393
+ logger.error(f"Error in /adventure route: {str(e)}")
394
+ return jsonify({'error': 'Internal Server Error: Please try again later.'}), 500
395
 
396
  @app.route('/image', methods=['POST'])
397
  def image():