kevalfst commited on
Commit
a046610
·
verified ·
1 Parent(s): 357e4ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -30
app.py CHANGED
@@ -8,18 +8,14 @@ from diffusers import (
8
  )
9
  from diffusers.utils import export_to_video, load_image
10
 
11
- # Detect device & dtype
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
  dtype = torch.float16 if device == "cuda" else torch.float32
14
 
15
- # Factory to load & offload a pipeline
16
  def make_pipe(cls, model_id, **kwargs):
17
  pipe = cls.from_pretrained(model_id, torch_dtype=dtype, **kwargs)
18
- # Enables CPU offload of model parts not in use
19
  pipe.enable_model_cpu_offload()
20
  return pipe
21
 
22
- # Hold pipelines in globals but don’t load yet
23
  TXT2IMG_PIPE = None
24
  IMG2IMG_PIPE = None
25
  TXT2VID_PIPE = None
@@ -52,7 +48,7 @@ def generate_video_from_text(prompt):
52
  "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
53
  ).to(device)
54
  frames = TXT2VID_PIPE(prompt=prompt, num_frames=12).frames[0]
55
- return export_to_video(frames, "wan_video.mp4", fps=8)
56
 
57
  def generate_video_from_image(image):
58
  global IMG2VID_PIPE
@@ -60,38 +56,38 @@ def generate_video_from_image(image):
60
  IMG2VID_PIPE = make_pipe(
61
  StableVideoDiffusionPipeline,
62
  "stabilityai/stable-video-diffusion-img2vid-xt",
63
- variant="fp16" if dtype==torch.float16 else None
64
  ).to(device)
65
  image = load_image(image).resize((512, 288))
66
  frames = IMG2VID_PIPE(image, num_inference_steps=16).frames[0]
67
- return export_to_video(frames, "svd_video.mp4", fps=8)
 
 
 
68
 
69
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
- gr.Markdown("# 🧠 Lightweight Any‑to‑Any AI Playground")
71
-
72
  with gr.Tab("Text → Image"):
73
- inp = gr.Textbox(label="Prompt")
74
- out = gr.Image()
75
- btn = gr.Button("Generate")
76
- btn.click(generate_image_from_text, inp, out)
77
-
78
  with gr.Tab("Image → Image"):
79
- img = gr.Image(label="Input Image")
80
- prm = gr.Textbox(label="Edit Prompt")
81
- out2 = gr.Image()
82
- btn2 = gr.Button("Generate")
83
- btn2.click(generate_image_from_image_and_prompt, [img, prm], out2)
84
-
85
  with gr.Tab("Text → Video"):
86
- inp2 = gr.Textbox(label="Prompt")
87
- out_vid = gr.Video()
88
- btn3 = gr.Button("Generate")
89
- btn3.click(generate_video_from_text, inp2, out_vid)
90
-
91
  with gr.Tab("Image → Video"):
92
- img2 = gr.Image(label="Input Image")
93
- out_vid2 = gr.Video()
94
- btn4 = gr.Button("Animate")
95
- btn4.click(generate_video_from_image, img2, out_vid2)
96
 
97
  demo.launch()
 
8
  )
9
  from diffusers.utils import export_to_video, load_image
10
 
 
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  dtype = torch.float16 if device == "cuda" else torch.float32
13
 
 
14
  def make_pipe(cls, model_id, **kwargs):
15
  pipe = cls.from_pretrained(model_id, torch_dtype=dtype, **kwargs)
 
16
  pipe.enable_model_cpu_offload()
17
  return pipe
18
 
 
19
  TXT2IMG_PIPE = None
20
  IMG2IMG_PIPE = None
21
  TXT2VID_PIPE = None
 
48
  "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
49
  ).to(device)
50
  frames = TXT2VID_PIPE(prompt=prompt, num_frames=12).frames[0]
51
+ return export_to_video(frames, "/tmp/wan_video.mp4", fps=8)
52
 
53
  def generate_video_from_image(image):
54
  global IMG2VID_PIPE
 
56
  IMG2VID_PIPE = make_pipe(
57
  StableVideoDiffusionPipeline,
58
  "stabilityai/stable-video-diffusion-img2vid-xt",
59
+ variant="fp16" if dtype == torch.float16 else None
60
  ).to(device)
61
  image = load_image(image).resize((512, 288))
62
  frames = IMG2VID_PIPE(image, num_inference_steps=16).frames[0]
63
+ return export_to_video(frames, "/tmp/svd_video.mp4", fps=8)
64
+
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("## 🧠 Lightweight Any-to-Any AI Playground")
67
 
 
 
 
68
  with gr.Tab("Text → Image"):
69
+ text_input = gr.Textbox(label="Prompt")
70
+ image_output = gr.Image(label="Generated Image")
71
+ generate_button = gr.Button("Generate")
72
+ generate_button.click(generate_image_from_text, inputs=text_input, outputs=image_output)
73
+
74
  with gr.Tab("Image → Image"):
75
+ input_image = gr.Image(label="Input Image")
76
+ prompt_input = gr.Textbox(label="Edit Prompt")
77
+ output_image = gr.Image(label="Edited Image")
78
+ edit_button = gr.Button("Generate")
79
+ edit_button.click(generate_image_from_image_and_prompt, inputs=[input_image, prompt_input], outputs=output_image)
80
+
81
  with gr.Tab("Text → Video"):
82
+ video_prompt = gr.Textbox(label="Prompt")
83
+ video_output = gr.Video(label="Generated Video")
84
+ video_button = gr.Button("Generate")
85
+ video_button.click(generate_video_from_text, inputs=video_prompt, outputs=video_output)
86
+
87
  with gr.Tab("Image → Video"):
88
+ anim_image = gr.Image(label="Input Image")
89
+ anim_video_output = gr.Video(label="Animated Video")
90
+ anim_button = gr.Button("Animate")
91
+ anim_button.click(generate_video_from_image, inputs=anim_image, outputs=anim_video_output)
92
 
93
  demo.launch()