File size: 3,706 Bytes
66ccc82
 
 
 
 
e082b74
66ccc82
 
d3bb50f
e082b74
d3bb50f
 
 
e082b74
 
d3bb50f
e082b74
 
 
 
 
 
 
d3bb50f
e082b74
 
 
d3bb50f
e082b74
 
66ccc82
e082b74
d3bb50f
e082b74
 
 
 
 
 
 
 
 
 
d3bb50f
 
e082b74
 
 
 
 
 
d3bb50f
e082b74
 
66ccc82
 
e082b74
66ccc82
e082b74
 
 
 
66ccc82
e082b74
 
 
 
 
 
 
 
 
 
 
66ccc82
 
e082b74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66ccc82
e082b74
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline
import moviepy.editor as mp
import os
from pathlib import Path

def generate_image(prompt):
    try:
        print("Generating image...")
        model_id = "runwayml/stable-diffusion-v1-5"
        pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
        pipe.to("cpu")
        
        # Generate image
        image = pipe(prompt).images[0]
        
        # Create output directory if it doesn't exist
        output_dir = Path("outputs")
        output_dir.mkdir(exist_ok=True)
        
        # Save image with timestamp to avoid conflicts
        image_path = output_dir / "generated_image.png"
        image.save(image_path)
        print(f"Image saved at {image_path}")
        return str(image_path)
        
    except Exception as e:
        print(f"Error generating image: {str(e)}")
        return None

def create_video(image_path, duration=5):
    try:
        if not image_path or not os.path.exists(image_path):
            return None
            
        print("Creating video...")
        
        # Create video path in outputs directory
        output_dir = Path("outputs")
        video_path = output_dir / "output_video.mp4"
        
        # Create video clip
        clip = mp.ImageClip(image_path, duration=duration)
        clip = clip.set_fps(24)
        clip.write_videofile(str(video_path), codec='libx264')
        clip.close()
        
        print(f"Video saved at {video_path}")
        return str(video_path)
        
    except Exception as e:
        print(f"Error creating video: {str(e)}")
        return None

def text_to_video(prompt):
    # Generate image
    image_path = generate_image(prompt)
    if not image_path:
        return "❌ Error generating image. Please try again with a different prompt."
    
    # Create video
    video_path = create_video(image_path)
    
    # Clean up image file
    try:
        if image_path and os.path.exists(image_path):
            os.remove(image_path)
    except Exception as e:
        print(f"Warning: Could not delete image file: {e}")
    
    if not video_path:
        return "❌ Error creating video. Please try again."
    
    return video_path

# Create outputs directory if it doesn't exist
Path("outputs").mkdir(exist_ok=True)

# Create the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("""
    # Text to AI Video Generator
    Create a video from text using Stable Diffusion and MoviePy
    """)
    
    with gr.Row():
        with gr.Column():
            prompt_input = gr.Textbox(
                label="Enter your prompt",
                placeholder="Describe the image you want to generate...",
                lines=3
            )
            generate_btn = gr.Button("Generate Video", variant="primary")
        
        with gr.Column():
            output_video = gr.Video(
                label="Generated Video",
                format="mp4",
                interactive=False
            )
            status = gr.Textbox(label="Status", interactive=False)
    
    # Examples
    gr.Examples(
        examples=[
            "A beautiful sunset over mountains",
            "An astronaut riding a horse on Mars",
            "A futuristic city with flying cars"
        ],
        inputs=prompt_input
    )
    
    # Button click handler
    generate_btn.click(
        fn=text_to_video,
        inputs=prompt_input,
        outputs=[output_video, status],
        api_name="generate"
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=True,
        debug=True
    )