Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -417,50 +417,51 @@ def combine_video_with_audio(video_path, audio_path, output_path):
|
|
417 |
|
418 |
# Main processing function
|
419 |
def create_story_video(prompt, progress=gr.Progress()):
|
420 |
-
# Input validation
|
421 |
if not prompt or len(prompt.strip()) < 5:
|
422 |
return "Please enter a longer prompt (at least 5 characters).", None, None
|
423 |
|
424 |
try:
|
425 |
-
|
426 |
progress(0, desc="Starting story generation...")
|
427 |
story = generate_story(prompt)
|
|
|
428 |
progress(20, desc="Story generated successfully!")
|
429 |
|
430 |
-
|
431 |
progress(25, desc="Creating video animation (this may take several minutes)...")
|
432 |
video_path = generate_video(story)
|
|
|
433 |
progress(60, desc="Video created successfully!")
|
434 |
|
435 |
-
|
436 |
progress(65, desc="Creating audio summary...")
|
437 |
audio_summary = summary_of_summary(story, video_path)
|
|
|
438 |
progress(80, desc="Creating audio narration...")
|
439 |
|
440 |
-
|
441 |
try:
|
442 |
-
# Set up event loop handling
|
443 |
try:
|
444 |
loop = asyncio.get_event_loop()
|
445 |
except RuntimeError:
|
446 |
loop = asyncio.new_event_loop()
|
447 |
asyncio.set_event_loop(loop)
|
448 |
-
|
449 |
audio_file = loop.run_until_complete(
|
450 |
generate_audio_with_sentiment(audio_summary, sentiment_analyzer)
|
451 |
)
|
|
|
452 |
progress(90, desc="Audio created successfully!")
|
453 |
except Exception as e:
|
454 |
print(f"Audio generation error: {str(e)}")
|
455 |
return story, None, f"Audio generation failed: {str(e)}"
|
456 |
|
457 |
-
|
458 |
progress(95, desc="Combining video and audio...")
|
459 |
output_path = 'final_video_with_audio.mp4'
|
460 |
combine_video_with_audio(video_path, audio_file, output_path)
|
461 |
-
|
462 |
progress(100, desc="Process complete!")
|
463 |
-
return story, output_path,
|
464 |
|
465 |
except Exception as e:
|
466 |
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
@@ -481,7 +482,6 @@ with gr.Blocks(title="AI Story Video Generator", theme=gr.themes.Soft()) as demo
|
|
481 |
gr.Markdown("# 🎬 AI Story Video Generator")
|
482 |
gr.Markdown("Enter a one-sentence prompt to generate a complete story with video and narration.")
|
483 |
|
484 |
-
# Input section
|
485 |
with gr.Row():
|
486 |
prompt_input = gr.Textbox(
|
487 |
label="Your Story Idea",
|
@@ -489,10 +489,7 @@ with gr.Blocks(title="AI Story Video Generator", theme=gr.themes.Soft()) as demo
|
|
489 |
lines=2
|
490 |
)
|
491 |
|
492 |
-
# Example prompts section
|
493 |
gr.Markdown("### Try these example prompts:")
|
494 |
-
|
495 |
-
# Create examples using Gradio's examples feature
|
496 |
with gr.Row():
|
497 |
examples = gr.Examples(
|
498 |
examples=[[prompt] for prompt in EXAMPLE_PROMPTS],
|
@@ -504,10 +501,8 @@ with gr.Blocks(title="AI Story Video Generator", theme=gr.themes.Soft()) as demo
|
|
504 |
generate_button = gr.Button("Generate Story Video", variant="primary")
|
505 |
clear_button = gr.Button("Clear", variant="secondary")
|
506 |
|
507 |
-
# Status indicator
|
508 |
status_indicator = gr.Markdown("Ready to generate your story video...")
|
509 |
|
510 |
-
# Output section with tabs
|
511 |
with gr.Tabs():
|
512 |
with gr.TabItem("Results"):
|
513 |
with gr.Row():
|
@@ -515,61 +510,36 @@ with gr.Blocks(title="AI Story Video Generator", theme=gr.themes.Soft()) as demo
|
|
515 |
video_output = gr.Video(label="Generated Video with Narration")
|
516 |
with gr.Column(scale=1):
|
517 |
story_output = gr.TextArea(label="Generated Story", lines=15, max_lines=30)
|
518 |
-
|
519 |
|
520 |
with gr.TabItem("Help & Information"):
|
521 |
gr.Markdown("""
|
522 |
## How to use this tool
|
523 |
-
|
524 |
1. Enter a creative one-sentence story idea in the input box
|
525 |
2. Click "Generate Story Video" and wait for processing to complete
|
526 |
-
3. View your
|
527 |
-
|
528 |
## Processing Steps
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
4. **Final Compilation**: Video and audio are combined into your final story
|
534 |
-
|
535 |
-
## Tips for Great Results
|
536 |
-
|
537 |
-
- Use clear, specific prompts that suggest a narrative arc
|
538 |
-
- Include interesting characters, settings, or situations
|
539 |
-
- Make your prompt realistic but with potential for development
|
540 |
-
- Try to suggest a potential conflict or discovery
|
541 |
-
|
542 |
-
## Note on Processing Time
|
543 |
-
|
544 |
-
For faster testing, the app currently processes only the first 5 sentences of the story.
|
545 |
-
In a production environment, this limit would be removed.
|
546 |
-
|
547 |
-
## Troubleshooting
|
548 |
-
|
549 |
-
If you encounter errors:
|
550 |
-
- Try a different prompt
|
551 |
-
- Ensure your prompt is clear and specific
|
552 |
-
- Check that all required models are properly loaded
|
553 |
""")
|
554 |
|
555 |
-
# Handle clearing
|
556 |
def clear_outputs():
|
557 |
-
return "", None,
|
558 |
|
559 |
-
# Connect interface elements
|
560 |
generate_button.click(
|
561 |
fn=create_story_video,
|
562 |
inputs=prompt_input,
|
563 |
-
outputs=[story_output, video_output,
|
564 |
api_name="generate"
|
565 |
)
|
566 |
|
567 |
clear_button.click(
|
568 |
fn=clear_outputs,
|
569 |
inputs=None,
|
570 |
-
outputs=[story_output, video_output,
|
571 |
)
|
572 |
|
573 |
-
# Launch the app
|
574 |
if __name__ == "__main__":
|
575 |
demo.launch()
|
|
|
417 |
|
418 |
# Main processing function
|
419 |
def create_story_video(prompt, progress=gr.Progress()):
|
|
|
420 |
if not prompt or len(prompt.strip()) < 5:
|
421 |
return "Please enter a longer prompt (at least 5 characters).", None, None
|
422 |
|
423 |
try:
|
424 |
+
print("Step 1: Generating story...")
|
425 |
progress(0, desc="Starting story generation...")
|
426 |
story = generate_story(prompt)
|
427 |
+
print("Story generation complete.")
|
428 |
progress(20, desc="Story generated successfully!")
|
429 |
|
430 |
+
print("Step 2: Generating video...")
|
431 |
progress(25, desc="Creating video animation (this may take several minutes)...")
|
432 |
video_path = generate_video(story)
|
433 |
+
print("Video generation complete.")
|
434 |
progress(60, desc="Video created successfully!")
|
435 |
|
436 |
+
print("Step 3: Summarizing for audio...")
|
437 |
progress(65, desc="Creating audio summary...")
|
438 |
audio_summary = summary_of_summary(story, video_path)
|
439 |
+
print("Audio summary complete.")
|
440 |
progress(80, desc="Creating audio narration...")
|
441 |
|
442 |
+
print("Step 4: Generating audio...")
|
443 |
try:
|
|
|
444 |
try:
|
445 |
loop = asyncio.get_event_loop()
|
446 |
except RuntimeError:
|
447 |
loop = asyncio.new_event_loop()
|
448 |
asyncio.set_event_loop(loop)
|
|
|
449 |
audio_file = loop.run_until_complete(
|
450 |
generate_audio_with_sentiment(audio_summary, sentiment_analyzer)
|
451 |
)
|
452 |
+
print(f"Audio generated at: {audio_file}")
|
453 |
progress(90, desc="Audio created successfully!")
|
454 |
except Exception as e:
|
455 |
print(f"Audio generation error: {str(e)}")
|
456 |
return story, None, f"Audio generation failed: {str(e)}"
|
457 |
|
458 |
+
print("Step 5: Combining video and audio...")
|
459 |
progress(95, desc="Combining video and audio...")
|
460 |
output_path = 'final_video_with_audio.mp4'
|
461 |
combine_video_with_audio(video_path, audio_file, output_path)
|
462 |
+
print("Combination complete.")
|
463 |
progress(100, desc="Process complete!")
|
464 |
+
return story, output_path, audio_file # Return audio file path instead of summary
|
465 |
|
466 |
except Exception as e:
|
467 |
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
|
|
482 |
gr.Markdown("# 🎬 AI Story Video Generator")
|
483 |
gr.Markdown("Enter a one-sentence prompt to generate a complete story with video and narration.")
|
484 |
|
|
|
485 |
with gr.Row():
|
486 |
prompt_input = gr.Textbox(
|
487 |
label="Your Story Idea",
|
|
|
489 |
lines=2
|
490 |
)
|
491 |
|
|
|
492 |
gr.Markdown("### Try these example prompts:")
|
|
|
|
|
493 |
with gr.Row():
|
494 |
examples = gr.Examples(
|
495 |
examples=[[prompt] for prompt in EXAMPLE_PROMPTS],
|
|
|
501 |
generate_button = gr.Button("Generate Story Video", variant="primary")
|
502 |
clear_button = gr.Button("Clear", variant="secondary")
|
503 |
|
|
|
504 |
status_indicator = gr.Markdown("Ready to generate your story video...")
|
505 |
|
|
|
506 |
with gr.Tabs():
|
507 |
with gr.TabItem("Results"):
|
508 |
with gr.Row():
|
|
|
510 |
video_output = gr.Video(label="Generated Video with Narration")
|
511 |
with gr.Column(scale=1):
|
512 |
story_output = gr.TextArea(label="Generated Story", lines=15, max_lines=30)
|
513 |
+
audio_output = gr.Audio(label="Audio Narration") # Changed to Audio
|
514 |
|
515 |
with gr.TabItem("Help & Information"):
|
516 |
gr.Markdown("""
|
517 |
## How to use this tool
|
|
|
518 |
1. Enter a creative one-sentence story idea in the input box
|
519 |
2. Click "Generate Story Video" and wait for processing to complete
|
520 |
+
3. View your story, narration audio, and final video
|
|
|
521 |
## Processing Steps
|
522 |
+
- Story Generation: Expands your idea into a 15-20 sentence story
|
523 |
+
- Video Creation: Visualizes sentences with AI animation
|
524 |
+
- Audio Narration: Creates a voiceover with sentiment analysis
|
525 |
+
- Final Compilation: Combines video and audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
526 |
""")
|
527 |
|
|
|
528 |
def clear_outputs():
|
529 |
+
return "", None, None
|
530 |
|
|
|
531 |
generate_button.click(
|
532 |
fn=create_story_video,
|
533 |
inputs=prompt_input,
|
534 |
+
outputs=[story_output, video_output, audio_output], # Updated to audio_output
|
535 |
api_name="generate"
|
536 |
)
|
537 |
|
538 |
clear_button.click(
|
539 |
fn=clear_outputs,
|
540 |
inputs=None,
|
541 |
+
outputs=[story_output, video_output, audio_output]
|
542 |
)
|
543 |
|
|
|
544 |
if __name__ == "__main__":
|
545 |
demo.launch()
|