anas31 commited on
Commit
3ffd377
·
verified ·
1 Parent(s): fd8c6f0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import DiffusionPipeline
4
+ from diffusers.utils import load_image, export_to_video
5
+ import tempfile
6
+ import os
7
+
8
+ # Load the model (we'll initialize it when first used to save resources)
9
+ pipe = None
10
+
11
+ def generate_video(image, prompt, seed=42):
12
+ global pipe
13
+
14
+ # Initialize the model if not already loaded
15
+ if pipe is None:
16
+ pipe = DiffusionPipeline.from_pretrained(
17
+ "Wan-AI/Wan2.1-VACE-14B",
18
+ torch_dtype=torch.float16
19
+ )
20
+ pipe.to("cuda")
21
+
22
+ # Set the seed for reproducibility
23
+ torch.manual_seed(seed)
24
+
25
+ # Generate the video frames
26
+ output = pipe(image=image, prompt=prompt).frames[0]
27
+
28
+ # Save to temporary file
29
+ temp_dir = tempfile.mkdtemp()
30
+ output_path = os.path.join(temp_dir, "output.mp4")
31
+ export_to_video(output, output_path)
32
+
33
+ return output_path
34
+
35
+ # Create Gradio interface
36
+ with gr.Blocks(title="Wan2.1 Video Generation") as demo:
37
+ gr.Markdown("# Wan2.1-VACE-14B Video Generation")
38
+ gr.Markdown("Generate videos from images and prompts using Wan2.1 model")
39
+
40
+ with gr.Row():
41
+ with gr.Column():
42
+ input_image = gr.Image(label="Input Image", type="pil")
43
+ prompt = gr.Textbox(label="Prompt", placeholder="Describe the video you want to generate...")
44
+ seed = gr.Number(label="Seed", value=42, precision=0)
45
+ generate_btn = gr.Button("Generate Video")
46
+
47
+ with gr.Column():
48
+ output_video = gr.Video(label="Generated Video")
49
+
50
+ # Example inputs
51
+ gr.Examples(
52
+ examples=[
53
+ [
54
+ "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/guitar-man.png",
55
+ "A man with short gray hair plays a red electric guitar.",
56
+ 42
57
+ ],
58
+ [
59
+ "https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Sunflower_from_Silesia2.jpg/1200px-Sunflower_from_Silesia2.jpg",
60
+ "A sunflower slowly blooming in the sunlight",
61
+ 123
62
+ ]
63
+ ],
64
+ inputs=[input_image, prompt, seed],
65
+ outputs=output_video,
66
+ fn=generate_video,
67
+ cache_examples=True
68
+ )
69
+
70
+ generate_btn.click(
71
+ fn=generate_video,
72
+ inputs=[input_image, prompt, seed],
73
+ outputs=output_video
74
+ )
75
+
76
+ demo.launch()