Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -334,87 +334,87 @@ def generate_story_endpoint():
|
|
| 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
|
| 341 |
auth_header = request.headers.get('Authorization', '')
|
| 342 |
if not auth_header.startswith('Bearer '):
|
|
|
|
| 343 |
return jsonify({'error': 'Missing or invalid token'}), 401
|
|
|
|
| 344 |
token = auth_header.split(' ')[1]
|
| 345 |
uid = verify_token(token)
|
| 346 |
if not uid:
|
|
|
|
| 347 |
return jsonify({'error': 'Invalid or expired token'}), 401
|
| 348 |
|
| 349 |
data = request.get_json()
|
| 350 |
story_id = data.get('story_id')
|
| 351 |
if not story_id:
|
|
|
|
| 352 |
return jsonify({'error': 'story_id is required'}), 400
|
| 353 |
|
| 354 |
-
|
| 355 |
story_ref = db.reference(f"stories/{story_id}")
|
| 356 |
story_data = story_ref.get()
|
|
|
|
| 357 |
if not story_data:
|
|
|
|
| 358 |
return jsonify({'error': 'Story not found'}), 404
|
| 359 |
|
| 360 |
sections = story_data.get("sections", [])
|
| 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 |
-
|
|
|
|
| 371 |
img_resp = requests.get(image_url)
|
| 372 |
if img_resp.status_code == 200:
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
|
|
|
| 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
|
| 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 =
|
|
|
|
| 392 |
if not video_file:
|
|
|
|
| 393 |
return jsonify({'error': 'Video generation failed'}), 500
|
| 394 |
|
| 395 |
-
|
| 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 |
-
|
| 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 |
|
|
|
|
| 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 |
+
print("β‘οΈ Received video generation request...")
|
| 341 |
+
|
| 342 |
# Verify user token
|
| 343 |
auth_header = request.headers.get('Authorization', '')
|
| 344 |
if not auth_header.startswith('Bearer '):
|
| 345 |
+
print("β ERROR: Missing or invalid token")
|
| 346 |
return jsonify({'error': 'Missing or invalid token'}), 401
|
| 347 |
+
|
| 348 |
token = auth_header.split(' ')[1]
|
| 349 |
uid = verify_token(token)
|
| 350 |
if not uid:
|
| 351 |
+
print("β ERROR: Invalid or expired token")
|
| 352 |
return jsonify({'error': 'Invalid or expired token'}), 401
|
| 353 |
|
| 354 |
data = request.get_json()
|
| 355 |
story_id = data.get('story_id')
|
| 356 |
if not story_id:
|
| 357 |
+
print("β ERROR: story_id is required")
|
| 358 |
return jsonify({'error': 'story_id is required'}), 400
|
| 359 |
|
| 360 |
+
print(f"Fetching story {story_id} from Firebase...")
|
| 361 |
story_ref = db.reference(f"stories/{story_id}")
|
| 362 |
story_data = story_ref.get()
|
| 363 |
+
|
| 364 |
if not story_data:
|
| 365 |
+
print("β ERROR: Story not found")
|
| 366 |
return jsonify({'error': 'Story not found'}), 404
|
| 367 |
|
| 368 |
sections = story_data.get("sections", [])
|
| 369 |
if not sections:
|
| 370 |
+
print("β ERROR: No sections found in the story")
|
| 371 |
return jsonify({'error': 'No sections found in the story'}), 404
|
| 372 |
|
|
|
|
| 373 |
image_files = []
|
| 374 |
audio_files = []
|
| 375 |
+
|
| 376 |
+
print(f"Processing {len(sections)} sections...")
|
| 377 |
+
|
| 378 |
for section in sections:
|
| 379 |
image_url = section.get("image_url")
|
| 380 |
audio_url = section.get("audio_url")
|
| 381 |
+
|
| 382 |
+
print(f"β‘οΈ Downloading image from: {image_url}")
|
| 383 |
img_resp = requests.get(image_url)
|
| 384 |
if img_resp.status_code == 200:
|
| 385 |
+
img = Image.open(io.BytesIO(img_resp.content))
|
| 386 |
+
image_files.append(img)
|
| 387 |
+
else:
|
| 388 |
+
print(f"β ERROR: Failed to download image {image_url}")
|
| 389 |
+
|
| 390 |
+
print(f"β‘οΈ Downloading audio from: {audio_url}")
|
| 391 |
aud_resp = requests.get(audio_url)
|
| 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 |
+
else:
|
| 398 |
+
print(f"β ERROR: Failed to download audio {audio_url}")
|
| 399 |
|
| 400 |
if not image_files:
|
| 401 |
+
print("β ERROR: No valid images found")
|
| 402 |
return jsonify({'error': 'No images available for video generation'}), 500
|
| 403 |
|
| 404 |
+
# Generate video
|
|
|
|
| 405 |
video_output_path = f"/tmp/{uuid.uuid4().hex}.mp4"
|
| 406 |
+
video_file = create_silent_video(image_files, [5.0] * len(image_files), video_output_path)
|
| 407 |
+
|
| 408 |
if not video_file:
|
| 409 |
+
print("β ERROR: Video generation failed")
|
| 410 |
return jsonify({'error': 'Video generation failed'}), 500
|
| 411 |
|
| 412 |
+
print(f"β
Video generated successfully: {video_file}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
+
return jsonify({"video_url": video_file})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
|
|
|
|
| 416 |
except Exception as e:
|
| 417 |
+
print(f"β ERROR: {e}")
|
| 418 |
return jsonify({'error': str(e)}), 500
|
| 419 |
|
| 420 |
|