Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,82 @@
|
|
1 |
import torch
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
)
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import imageio
|
4 |
+
import os
|
5 |
+
import requests
|
6 |
+
from safetensors.torch import load_file
|
7 |
+
from torchvision import transforms
|
8 |
+
from PIL import Image
|
9 |
+
import numpy as np
|
10 |
+
import random
|
11 |
+
|
12 |
+
# Define model URL and local path
|
13 |
+
MODEL_URL = "https://huggingface.co/sarthak247/Wan2.1-T2V-1.3B-nf4/resolve/main/diffusion_pytorch_model.safetensors"
|
14 |
+
MODEL_FILE = "diffusion_pytorch_model.safetensors"
|
15 |
+
|
16 |
+
# Function to download model if not present
|
17 |
+
def download_model():
|
18 |
+
if not os.path.exists(MODEL_FILE):
|
19 |
+
print("Downloading model...")
|
20 |
+
response = requests.get(MODEL_URL, stream=True)
|
21 |
+
if response.status_code == 200:
|
22 |
+
with open(MODEL_FILE, "wb") as f:
|
23 |
+
for chunk in response.iter_content(chunk_size=8192):
|
24 |
+
f.write(chunk)
|
25 |
+
print("Download complete!")
|
26 |
+
else:
|
27 |
+
raise RuntimeError(f"Failed to download model: {response.status_code}")
|
28 |
+
|
29 |
+
# Load model weights manually
|
30 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
31 |
+
print(f"Loading model on {device}...")
|
32 |
+
|
33 |
+
try:
|
34 |
+
download_model()
|
35 |
+
model_weights = load_file(MODEL_FILE, device=device)
|
36 |
+
print("Model loaded successfully!")
|
37 |
+
except Exception as e:
|
38 |
+
print(f"Error loading model: {e}")
|
39 |
+
model_weights = None
|
40 |
+
|
41 |
+
# Function to generate video using the model
|
42 |
+
def generate_video(prompt):
|
43 |
+
"""
|
44 |
+
Generates a video using the model based on the provided text prompt.
|
45 |
+
"""
|
46 |
+
if model_weights is None:
|
47 |
+
return "Model failed to load. Please check the logs."
|
48 |
+
|
49 |
+
# Placeholder - actual inference logic should be implemented here
|
50 |
+
# Example of using the model to generate an image from a prompt
|
51 |
+
# For now, we'll create a random color image as a placeholder.
|
52 |
+
|
53 |
+
# Assuming the model generates an image based on the prompt (modify with actual logic)
|
54 |
+
width, height = 512, 512
|
55 |
+
img = Image.new("RGB", (width, height),
|
56 |
+
color=(random.randint(0, 255),
|
57 |
+
random.randint(0, 255),
|
58 |
+
random.randint(0, 255))) # Random color
|
59 |
+
|
60 |
+
# Transform the image to a tensor and convert it to a numpy array
|
61 |
+
transform = transforms.ToTensor()
|
62 |
+
frame = (transform(img).permute(1, 2, 0).numpy() * 255).astype(np.uint8)
|
63 |
+
|
64 |
+
# Create a fake video with repeated frames (replace with actual frame generation)
|
65 |
+
frames = [frame] * 16 # 16 repeated frames (replace with actual video frames from the model)
|
66 |
+
output_path = "output.mp4"
|
67 |
+
|
68 |
+
# Save frames as a video with 8 fps
|
69 |
+
imageio.mimsave(output_path, frames, fps=8)
|
70 |
+
|
71 |
+
return output_path
|
72 |
+
|
73 |
+
# Gradio UI
|
74 |
+
iface = gr.Interface(
|
75 |
+
fn=generate_video,
|
76 |
+
inputs=gr.Textbox(label="Enter Text Prompt"),
|
77 |
+
outputs=gr.Video(label="Generated Video"),
|
78 |
+
title="Wan2.1-T2V-1.3B Video Generation",
|
79 |
+
description="This app loads the model manually and generates text-to-video output."
|
80 |
+
)
|
81 |
+
|
82 |
+
iface.launch()
|