Update app.py
Browse files
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 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
}
|
| 345 |
-
}
|
| 346 |
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 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 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
| 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():
|