Sabbirr12 commited on
Commit
44dff12
Β·
verified Β·
1 Parent(s): e2bcf6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -30
app.py CHANGED
@@ -1,40 +1,65 @@
 
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()
 
1
+ import torch, cv2, os, numpy as np
2
  import gradio as gr
3
+ from diffusers import StableDiffusionPipeline
4
+ from modelscope.pipelines import pipeline
5
+ from modelscope.outputs import OutputKeys
6
 
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
8
 
9
+ # -------- Text -> Image --------
10
+ def text_to_image(prompt, ratio="9:16"):
11
+ pipe = StableDiffusionPipeline.from_pretrained(
12
+ "stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16
13
+ ).to(device)
14
 
15
+ width, height = (576, 1024) if ratio=="9:16" else (1024, 576)
16
+ image = pipe(prompt, width=width, height=height).images[0]
17
+ filename = "generated.png"
18
+ image.save(filename)
19
+ return filename
20
 
21
+ # -------- Cinematic Pan Video --------
22
+ def cinematic_video(image, ratio="9:16"):
23
+ size = (576, 1024) if ratio=="9:16" else (1024, 576)
24
+ img = cv2.imread(image)
25
+ img = cv2.resize(img, size)
26
+ frames = []
27
+ for i in range(40):
28
+ dx, dy = int(i*0.5), int(i*0.3)
29
+ M = np.float32([[1, 0, dx], [0, 1, dy]])
30
+ frame = cv2.warpAffine(img, M, size)
31
+ frames.append(frame)
32
+ out_video = "cinematic_pan.mp4"
33
+ out = cv2.VideoWriter(out_video, cv2.VideoWriter_fourcc(*'mp4v'), 12, size)
34
+ for f in frames:
35
+ out.write(f)
36
+ out.release()
37
+ return out_video
38
 
39
+ # -------- Realistic AnimateDiff Video --------
40
+ def realistic_video(image, ratio="9:16"):
41
+ text2video = pipeline('image-to-video', model='damo-vilab/modelscope-damo-text-to-video-synthesis', device=device)
42
+ result = text2video({'image': image})
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()