Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,36 +6,53 @@ from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepSchedu
|
|
6 |
import os
|
7 |
from uuid import uuid4
|
8 |
|
|
|
|
|
|
|
|
|
9 |
# Load model on startup
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
pipe.
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Define the generation function
|
23 |
def generate_video(prompt, negative_prompt="", height=720, width=1280, num_frames=81, guidance_scale=5.0):
|
24 |
-
|
25 |
-
prompt
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
# Gradio Interface with API support
|
41 |
iface = gr.Interface(
|
@@ -54,4 +71,9 @@ iface = gr.Interface(
|
|
54 |
api=True # This enables the API
|
55 |
)
|
56 |
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import os
|
7 |
from uuid import uuid4
|
8 |
|
9 |
+
# Check if CUDA is available and set device accordingly
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
print(f"Using device: {device}") # Print device information
|
12 |
+
|
13 |
# Load model on startup
|
14 |
+
try:
|
15 |
+
print("Loading model...")
|
16 |
+
model_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"
|
17 |
+
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
|
18 |
+
scheduler = UniPCMultistepScheduler(
|
19 |
+
prediction_type='flow_prediction',
|
20 |
+
use_flow_sigmas=True,
|
21 |
+
num_train_timesteps=1000,
|
22 |
+
flow_shift=5.0
|
23 |
+
)
|
24 |
+
pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16)
|
25 |
+
pipe.scheduler = scheduler
|
26 |
+
pipe.to(device) # Move model to CUDA if available, otherwise CPU
|
27 |
+
print("Model loaded successfully.")
|
28 |
+
except Exception as e:
|
29 |
+
print(f"Error loading model: {e}")
|
30 |
+
raise e
|
31 |
|
32 |
# Define the generation function
|
33 |
def generate_video(prompt, negative_prompt="", height=720, width=1280, num_frames=81, guidance_scale=5.0):
|
34 |
+
try:
|
35 |
+
print(f"Generating video for prompt: {prompt}")
|
36 |
+
output = pipe(
|
37 |
+
prompt=prompt,
|
38 |
+
negative_prompt=negative_prompt,
|
39 |
+
height=height,
|
40 |
+
width=width,
|
41 |
+
num_frames=num_frames,
|
42 |
+
guidance_scale=guidance_scale,
|
43 |
+
).frames[0]
|
44 |
+
|
45 |
+
output_filename = f"{uuid4()}.mp4"
|
46 |
+
output_path = os.path.join("outputs", output_filename)
|
47 |
+
os.makedirs("outputs", exist_ok=True)
|
48 |
+
export_to_video(output, output_path, fps=16)
|
49 |
+
|
50 |
+
print(f"Video generated successfully: {output_path}")
|
51 |
+
return output_path # Gradio returns this as a downloadable file/video
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
print(f"Error generating video: {e}")
|
55 |
+
return None # Return None in case of error
|
56 |
|
57 |
# Gradio Interface with API support
|
58 |
iface = gr.Interface(
|
|
|
71 |
api=True # This enables the API
|
72 |
)
|
73 |
|
74 |
+
try:
|
75 |
+
print("Launching Gradio interface...")
|
76 |
+
iface.launch(share=True) # `share=True` will allow others to access your app via a public link
|
77 |
+
print("Gradio interface launched successfully.")
|
78 |
+
except Exception as e:
|
79 |
+
print(f"Error launching Gradio interface: {e}")
|