Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -365,19 +365,28 @@ def generate_video_endpoint():
|
|
| 365 |
auth_header = request.headers.get('Authorization', '')
|
| 366 |
if not auth_header.startswith('Bearer '):
|
| 367 |
logging.error("β ERROR: Missing or invalid token")
|
| 368 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
token = auth_header.split(' ')[1]
|
| 371 |
uid = verify_token(token)
|
| 372 |
if not uid:
|
| 373 |
logging.error("β ERROR: Invalid or expired token")
|
| 374 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 375 |
|
| 376 |
data = request.get_json()
|
| 377 |
story_id = data.get('story_id')
|
| 378 |
if not story_id:
|
| 379 |
logging.error("β ERROR: story_id is required")
|
| 380 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
logging.info(f"Fetching story {story_id} from Firebase...")
|
| 383 |
story_ref = db.reference(f"stories/{story_id}")
|
|
@@ -385,18 +394,25 @@ def generate_video_endpoint():
|
|
| 385 |
|
| 386 |
if not story_data:
|
| 387 |
logging.error("β ERROR: Story not found")
|
| 388 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 389 |
|
| 390 |
sections = story_data.get("sections", [])
|
| 391 |
if not sections:
|
| 392 |
logging.error("β ERROR: No sections found in the story")
|
| 393 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
image_files = []
|
| 396 |
audio_files = []
|
| 397 |
|
| 398 |
logging.info(f"Processing {len(sections)} sections...")
|
| 399 |
|
|
|
|
| 400 |
for section in sections:
|
| 401 |
image_url = section.get("image_url")
|
| 402 |
audio_url = section.get("audio_url")
|
|
@@ -406,6 +422,7 @@ def generate_video_endpoint():
|
|
| 406 |
if img_resp.status_code == 200:
|
| 407 |
img = Image.open(io.BytesIO(img_resp.content))
|
| 408 |
image_files.append(img)
|
|
|
|
| 409 |
else:
|
| 410 |
logging.error(f"β ERROR: Failed to download image {image_url}")
|
| 411 |
|
|
@@ -416,27 +433,61 @@ def generate_video_endpoint():
|
|
| 416 |
with open(aud_path, "wb") as f:
|
| 417 |
f.write(aud_resp.content)
|
| 418 |
audio_files.append(aud_path)
|
|
|
|
| 419 |
else:
|
| 420 |
logging.error(f"β ERROR: Failed to download audio {audio_url}")
|
| 421 |
|
| 422 |
if not image_files:
|
| 423 |
logging.error("β ERROR: No valid images found")
|
| 424 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 425 |
|
|
|
|
| 426 |
video_output_path = f"/tmp/{uuid.uuid4().hex}.mp4"
|
|
|
|
| 427 |
video_file = create_video(image_files, audio_files, output_path=video_output_path)
|
| 428 |
|
| 429 |
if not video_file:
|
| 430 |
logging.error("β ERROR: Video generation failed")
|
| 431 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
logging.info(f"β
Video generated successfully: {video_file}")
|
| 434 |
|
| 435 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
except Exception as e:
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
|
| 442 |
#----------Image Generation Endpoint ----------
|
|
|
|
| 365 |
auth_header = request.headers.get('Authorization', '')
|
| 366 |
if not auth_header.startswith('Bearer '):
|
| 367 |
logging.error("β ERROR: Missing or invalid token")
|
| 368 |
+
return jsonify({
|
| 369 |
+
'error': 'Missing or invalid token',
|
| 370 |
+
'log_url': upload_log() # Upload log file so you can see the error
|
| 371 |
+
}), 401
|
| 372 |
|
| 373 |
token = auth_header.split(' ')[1]
|
| 374 |
uid = verify_token(token)
|
| 375 |
if not uid:
|
| 376 |
logging.error("β ERROR: Invalid or expired token")
|
| 377 |
+
return jsonify({
|
| 378 |
+
'error': 'Invalid or expired token',
|
| 379 |
+
'log_url': upload_log()
|
| 380 |
+
}), 401
|
| 381 |
|
| 382 |
data = request.get_json()
|
| 383 |
story_id = data.get('story_id')
|
| 384 |
if not story_id:
|
| 385 |
logging.error("β ERROR: story_id is required")
|
| 386 |
+
return jsonify({
|
| 387 |
+
'error': 'story_id is required',
|
| 388 |
+
'log_url': upload_log()
|
| 389 |
+
}), 400
|
| 390 |
|
| 391 |
logging.info(f"Fetching story {story_id} from Firebase...")
|
| 392 |
story_ref = db.reference(f"stories/{story_id}")
|
|
|
|
| 394 |
|
| 395 |
if not story_data:
|
| 396 |
logging.error("β ERROR: Story not found")
|
| 397 |
+
return jsonify({
|
| 398 |
+
'error': 'Story not found',
|
| 399 |
+
'log_url': upload_log()
|
| 400 |
+
}), 404
|
| 401 |
|
| 402 |
sections = story_data.get("sections", [])
|
| 403 |
if not sections:
|
| 404 |
logging.error("β ERROR: No sections found in the story")
|
| 405 |
+
return jsonify({
|
| 406 |
+
'error': 'No sections found in the story',
|
| 407 |
+
'log_url': upload_log()
|
| 408 |
+
}), 404
|
| 409 |
|
| 410 |
image_files = []
|
| 411 |
audio_files = []
|
| 412 |
|
| 413 |
logging.info(f"Processing {len(sections)} sections...")
|
| 414 |
|
| 415 |
+
# Download each image and audio file
|
| 416 |
for section in sections:
|
| 417 |
image_url = section.get("image_url")
|
| 418 |
audio_url = section.get("audio_url")
|
|
|
|
| 422 |
if img_resp.status_code == 200:
|
| 423 |
img = Image.open(io.BytesIO(img_resp.content))
|
| 424 |
image_files.append(img)
|
| 425 |
+
logging.info("β
Image downloaded successfully.")
|
| 426 |
else:
|
| 427 |
logging.error(f"β ERROR: Failed to download image {image_url}")
|
| 428 |
|
|
|
|
| 433 |
with open(aud_path, "wb") as f:
|
| 434 |
f.write(aud_resp.content)
|
| 435 |
audio_files.append(aud_path)
|
| 436 |
+
logging.info("β
Audio downloaded successfully.")
|
| 437 |
else:
|
| 438 |
logging.error(f"β ERROR: Failed to download audio {audio_url}")
|
| 439 |
|
| 440 |
if not image_files:
|
| 441 |
logging.error("β ERROR: No valid images found")
|
| 442 |
+
return jsonify({
|
| 443 |
+
'error': 'No images available for video generation',
|
| 444 |
+
'log_url': upload_log()
|
| 445 |
+
}), 500
|
| 446 |
|
| 447 |
+
# Create the video
|
| 448 |
video_output_path = f"/tmp/{uuid.uuid4().hex}.mp4"
|
| 449 |
+
logging.info("Starting create_video...")
|
| 450 |
video_file = create_video(image_files, audio_files, output_path=video_output_path)
|
| 451 |
|
| 452 |
if not video_file:
|
| 453 |
logging.error("β ERROR: Video generation failed")
|
| 454 |
+
return jsonify({
|
| 455 |
+
'error': 'Video generation failed',
|
| 456 |
+
'log_url': upload_log()
|
| 457 |
+
}), 500
|
| 458 |
|
| 459 |
logging.info(f"β
Video generated successfully: {video_file}")
|
| 460 |
|
| 461 |
+
# Upload the video to Firebase Storage
|
| 462 |
+
logging.info("Uploading video to Firebase Storage...")
|
| 463 |
+
video_blob_name = f"stories/{uid}/{uuid.uuid4().hex}.mp4"
|
| 464 |
+
video_url = upload_to_storage(video_file, video_blob_name)
|
| 465 |
+
logging.info(f"β
Video uploaded to {video_url}")
|
| 466 |
+
|
| 467 |
+
# Update the story record with the video URL
|
| 468 |
+
story_ref.update({"video_url": video_url})
|
| 469 |
+
|
| 470 |
+
# Deduct 5 credits
|
| 471 |
+
user_ref = db.reference(f"users/{uid}")
|
| 472 |
+
user_data = user_ref.get() or {}
|
| 473 |
+
current_credits = user_data.get("credits", 0)
|
| 474 |
+
new_credits = max(0, current_credits - 5)
|
| 475 |
+
user_ref.update({"credits": new_credits})
|
| 476 |
+
logging.info(f"β
Deducted 5 credits. New credit balance: {new_credits}")
|
| 477 |
+
|
| 478 |
+
return jsonify({
|
| 479 |
+
"video_url": video_url,
|
| 480 |
+
"new_credits": new_credits,
|
| 481 |
+
"log_url": upload_log() # Upload the final log so you can inspect it
|
| 482 |
+
})
|
| 483 |
|
| 484 |
except Exception as e:
|
| 485 |
+
trace = traceback.format_exc()
|
| 486 |
+
logging.error(f"β EXCEPTION: {str(e)}\n{trace}")
|
| 487 |
+
return jsonify({
|
| 488 |
+
'error': str(e),
|
| 489 |
+
'log_url': upload_log()
|
| 490 |
+
}), 500
|
| 491 |
|
| 492 |
|
| 493 |
#----------Image Generation Endpoint ----------
|