Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -334,7 +334,7 @@ def generate_story_endpoint():
|
|
| 334 |
return jsonify({'error': str(e)}), 500
|
| 335 |
|
| 336 |
# ---------- Video Generation Endpoint ----------
|
| 337 |
-
|
| 338 |
def generate_video_endpoint():
|
| 339 |
try:
|
| 340 |
# Verify user token
|
|
@@ -361,53 +361,33 @@ def generate_video_endpoint():
|
|
| 361 |
if not sections:
|
| 362 |
return jsonify({'error': 'No sections found in the story'}), 404
|
| 363 |
|
| 364 |
-
#
|
| 365 |
image_files = []
|
| 366 |
audio_files = []
|
| 367 |
-
temp_files = [] # Keep track of all files for cleanup
|
| 368 |
-
|
| 369 |
for section in sections:
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
else:
|
| 387 |
-
print(f"Warning: Failed to download image from {image_url}")
|
| 388 |
-
|
| 389 |
-
# Download and validate audio
|
| 390 |
-
if audio_url:
|
| 391 |
-
aud_resp = requests.get(audio_url, timeout=10)
|
| 392 |
-
if aud_resp.status_code == 200:
|
| 393 |
-
aud_path = f"/tmp/{uuid.uuid4().hex}.mp3"
|
| 394 |
-
with open(aud_path, "wb") as f:
|
| 395 |
-
f.write(aud_resp.content)
|
| 396 |
-
audio_files.append(aud_path)
|
| 397 |
-
temp_files.append(aud_path)
|
| 398 |
-
else:
|
| 399 |
-
print(f"Warning: Failed to download audio from {audio_url}")
|
| 400 |
-
|
| 401 |
-
except Exception as e:
|
| 402 |
-
print(f"Error processing section: {e}")
|
| 403 |
|
| 404 |
if not image_files:
|
| 405 |
-
return jsonify({'error': 'No
|
| 406 |
|
| 407 |
-
# Generate video
|
|
|
|
| 408 |
video_output_path = f"/tmp/{uuid.uuid4().hex}.mp4"
|
| 409 |
-
temp_files.append(video_output_path)
|
| 410 |
-
|
| 411 |
video_file = create_video(image_files, audio_files, output_path=video_output_path)
|
| 412 |
if not video_file:
|
| 413 |
return jsonify({'error': 'Video generation failed'}), 500
|
|
@@ -416,31 +396,27 @@ def generate_video_endpoint():
|
|
| 416 |
video_blob_name = f"stories/{uid}/{uuid.uuid4().hex}.mp4"
|
| 417 |
video_url = upload_to_storage(video_file, video_blob_name)
|
| 418 |
|
| 419 |
-
#
|
| 420 |
story_ref.update({"video_url": video_url})
|
| 421 |
|
| 422 |
-
|
| 423 |
user_ref = db.reference(f"users/{uid}")
|
| 424 |
user_data = user_ref.get() or {}
|
| 425 |
current_credits = user_data.get("credits", 0)
|
| 426 |
new_credits = max(0, current_credits - 5)
|
| 427 |
user_ref.update({"credits": new_credits})
|
| 428 |
|
| 429 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
|
|
|
| 431 |
except Exception as e:
|
| 432 |
-
print("Error generating video:", traceback.format_exc())
|
| 433 |
return jsonify({'error': str(e)}), 500
|
| 434 |
|
| 435 |
-
finally:
|
| 436 |
-
# Cleanup all temporary files
|
| 437 |
-
for file_path in temp_files:
|
| 438 |
-
try:
|
| 439 |
-
if os.path.exists(file_path):
|
| 440 |
-
os.remove(file_path)
|
| 441 |
-
except Exception as cleanup_error:
|
| 442 |
-
print(f"Failed to delete {file_path}: {cleanup_error}")
|
| 443 |
-
|
| 444 |
|
| 445 |
# ---------- Audio Generation Endpoint ----------
|
| 446 |
@app.route('/api/audio/generate', methods=['POST'])
|
|
|
|
| 334 |
return jsonify({'error': str(e)}), 500
|
| 335 |
|
| 336 |
# ---------- Video Generation Endpoint ----------
|
| 337 |
+
app.route('/api/video/generate', methods=['POST'])
|
| 338 |
def generate_video_endpoint():
|
| 339 |
try:
|
| 340 |
# Verify user token
|
|
|
|
| 361 |
if not sections:
|
| 362 |
return jsonify({'error': 'No sections found in the story'}), 404
|
| 363 |
|
| 364 |
+
# Download the image and audio files from their URLs.
|
| 365 |
image_files = []
|
| 366 |
audio_files = []
|
|
|
|
|
|
|
| 367 |
for section in sections:
|
| 368 |
+
image_url = section.get("image_url")
|
| 369 |
+
audio_url = section.get("audio_url")
|
| 370 |
+
# Download image
|
| 371 |
+
img_resp = requests.get(image_url)
|
| 372 |
+
if img_resp.status_code == 200:
|
| 373 |
+
img_path = f"/tmp/{uuid.uuid4().hex}.jpg"
|
| 374 |
+
with open(img_path, "wb") as f:
|
| 375 |
+
f.write(img_resp.content)
|
| 376 |
+
image_files.append(img_path)
|
| 377 |
+
# Download audio
|
| 378 |
+
aud_resp = requests.get(audio_url)
|
| 379 |
+
if aud_resp.status_code == 200:
|
| 380 |
+
aud_path = f"/tmp/{uuid.uuid4().hex}.mp3"
|
| 381 |
+
with open(aud_path, "wb") as f:
|
| 382 |
+
f.write(aud_resp.content)
|
| 383 |
+
audio_files.append(aud_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
if not image_files:
|
| 386 |
+
return jsonify({'error': 'No images available for video generation'}), 500
|
| 387 |
|
| 388 |
+
# Generate video using the video generation function (from video_gen.py)
|
| 389 |
+
from video_gen import create_video # from your video_gen.py
|
| 390 |
video_output_path = f"/tmp/{uuid.uuid4().hex}.mp4"
|
|
|
|
|
|
|
| 391 |
video_file = create_video(image_files, audio_files, output_path=video_output_path)
|
| 392 |
if not video_file:
|
| 393 |
return jsonify({'error': 'Video generation failed'}), 500
|
|
|
|
| 396 |
video_blob_name = f"stories/{uid}/{uuid.uuid4().hex}.mp4"
|
| 397 |
video_url = upload_to_storage(video_file, video_blob_name)
|
| 398 |
|
| 399 |
+
# Optionally, update the story record with the video URL.
|
| 400 |
story_ref.update({"video_url": video_url})
|
| 401 |
|
| 402 |
+
# --- Subtract 5 Credits from the User ---
|
| 403 |
user_ref = db.reference(f"users/{uid}")
|
| 404 |
user_data = user_ref.get() or {}
|
| 405 |
current_credits = user_data.get("credits", 0)
|
| 406 |
new_credits = max(0, current_credits - 5)
|
| 407 |
user_ref.update({"credits": new_credits})
|
| 408 |
|
| 409 |
+
# Clean up temporary local files.
|
| 410 |
+
for f in image_files:
|
| 411 |
+
os.remove(f)
|
| 412 |
+
for f in audio_files:
|
| 413 |
+
os.remove(f)
|
| 414 |
+
os.remove(video_file)
|
| 415 |
|
| 416 |
+
return jsonify({"video_url": video_url,"new_credits": new_credits})
|
| 417 |
except Exception as e:
|
|
|
|
| 418 |
return jsonify({'error': str(e)}), 500
|
| 419 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
# ---------- Audio Generation Endpoint ----------
|
| 422 |
@app.route('/api/audio/generate', methods=['POST'])
|