Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,41 @@
|
|
| 1 |
-
import torch, cv2, os, numpy as np
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
from
|
| 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 |
-
video_path = result[OutputKeys.OUTPUT_VIDEO]
|
| 44 |
-
os.rename(video_path, "realistic.mp4")
|
| 45 |
-
return "realistic.mp4"
|
| 46 |
-
|
| 47 |
-
# -------- Wrapper: Text -> Videos --------
|
| 48 |
-
def text_to_videos(prompt, ratio):
|
| 49 |
-
img = text_to_image(prompt, ratio)
|
| 50 |
-
vid1 = cinematic_video(img, ratio)
|
| 51 |
-
vid2 = realistic_video(img, ratio)
|
| 52 |
-
return vid1, vid2
|
| 53 |
-
|
| 54 |
-
# -------- Gradio UI --------
|
| 55 |
-
with gr.Blocks() as demo:
|
| 56 |
-
gr.Markdown("## π¬ Text β Video App (Cinematic + Realistic)")
|
| 57 |
-
prompt = gr.Textbox(label="Enter your text")
|
| 58 |
-
ratio = gr.Radio(["9:16", "16:9"], value="16:9", label="Aspect Ratio")
|
| 59 |
-
btn = gr.Button("Generate Videos")
|
| 60 |
-
out_vid1 = gr.Video(label="π₯ Cinematic Pan Motion")
|
| 61 |
-
out_vid2 = gr.Video(label="π₯ Realistic AnimateDiff")
|
| 62 |
-
|
| 63 |
-
btn.click(text_to_videos, [prompt, ratio], [out_vid1, out_vid2])
|
| 64 |
-
|
| 65 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline, StableVideoDiffusionPipeline
|
| 4 |
+
from diffusers.utils import export_to_video
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load models
|
| 8 |
+
text2img_pipe = StableDiffusionPipeline.from_pretrained(
|
| 9 |
+
"runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
|
| 10 |
+
).to("cuda")
|
| 11 |
+
text2img_pipe.enable_model_cpu_offload()
|
| 12 |
+
|
| 13 |
+
video_pipe = StableVideoDiffusionPipeline.from_pretrained(
|
| 14 |
+
"stabilityai/stable-video-diffusion-img2vid-xt", torch_dtype=torch.float16, variant="fp16"
|
| 15 |
+
).to("cuda")
|
| 16 |
+
video_pipe.enable_model_cpu_offload()
|
| 17 |
+
|
| 18 |
+
def generate_video(prompt, aspect_ratio):
|
| 19 |
+
# Generate image
|
| 20 |
+
image = text2img_pipe(prompt).images[0]
|
| 21 |
+
if aspect_ratio == "16:9":
|
| 22 |
+
image = image.resize((1024, 576))
|
| 23 |
+
else:
|
| 24 |
+
image = image.resize((576, 1024))
|
| 25 |
+
|
| 26 |
+
# Generate video
|
| 27 |
+
generator = torch.manual_seed(42)
|
| 28 |
+
frames = video_pipe(image, decode_chunk_size=8, generator=generator).frames[0]
|
| 29 |
+
export_to_video(frames, "output.mp4", fps=7)
|
| 30 |
+
return "output.mp4"
|
| 31 |
+
|
| 32 |
+
gr.Interface(
|
| 33 |
+
fn=generate_video,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Textbox(label="Scene Prompt", placeholder="e.g. A 3D Islamic city at sunset, 4K"),
|
| 36 |
+
gr.Radio(["16:9", "9:16"], label="Aspect Ratio", value="16:9")
|
| 37 |
+
],
|
| 38 |
+
outputs=gr.Video(label="Generated Video"),
|
| 39 |
+
title="Text β Image β Video Generator",
|
| 40 |
+
description="Enter a scene prompt and get a cinematic video. Powered by Stable Diffusion + Stable Video Diffusion."
|
| 41 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|