Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline, DiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load Text β€ Image model
|
| 7 |
+
text2img = StableDiffusionPipeline.from_pretrained(
|
| 8 |
+
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
|
| 9 |
+
).to("cuda")
|
| 10 |
+
|
| 11 |
+
# Load Image β€ Video model
|
| 12 |
+
img2vid = DiffusionPipeline.from_pretrained(
|
| 13 |
+
"damo-vilab/image-to-video", torch_dtype=torch.float16, variant="fp16"
|
| 14 |
+
).to("cuda")
|
| 15 |
+
|
| 16 |
+
def generate_image(prompt):
|
| 17 |
+
image = text2img(prompt).images[0]
|
| 18 |
+
return image
|
| 19 |
+
|
| 20 |
+
def generate_video(image):
|
| 21 |
+
video_frames = img2vid(image).frames
|
| 22 |
+
return video_frames # Gradio will auto-render as video
|
| 23 |
+
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("## π¨ Text β€ Image β€ Video Generator")
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
prompt = gr.Textbox(label="π Enter Prompt")
|
| 29 |
+
img_output = gr.Image(label="πΌοΈ Generated Image")
|
| 30 |
+
btn_img = gr.Button("Generate Image")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
img_input = gr.Image(label="π₯ Drop Image Here")
|
| 34 |
+
vid_output = gr.Video(label="π¬ Generated Video")
|
| 35 |
+
btn_vid = gr.Button("Generate Video")
|
| 36 |
+
|
| 37 |
+
btn_img.click(fn=generate_image, inputs=prompt, outputs=img_output)
|
| 38 |
+
btn_vid.click(fn=generate_video, inputs=img_input, outputs=vid_output)
|
| 39 |
+
|
| 40 |
+
demo.launch()
|