Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,29 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
-
import
|
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 |
-
#
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
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 |
-
|
30 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
31 |
-
print(f"Loading model on {device}...")
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
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=
|
76 |
-
inputs=gr.Textbox(label="Enter
|
77 |
-
outputs=gr.
|
78 |
-
title="Wan2.1
|
79 |
-
description="
|
80 |
)
|
81 |
|
82 |
-
iface.launch()
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
+
from diffusers import StableDiffusionPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Model path from Hugging Face
|
6 |
+
MODEL_NAME = "Evados/DiffSynth-Studio-Lora-Wan2.1-ComfyUI"
|
7 |
+
LORA_FILE = "Wan2.1-1.3b-lora-aesthetics-v1_new.safetensors"
|
8 |
|
9 |
+
def load_model():
|
10 |
+
pipe = StableDiffusionPipeline.from_pretrained(MODEL_NAME, torch_dtype=torch.float16).to("cuda")
|
11 |
+
pipe.load_lora_weights(f"{MODEL_NAME}/{LORA_FILE}")
|
12 |
+
return pipe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
pipe = load_model()
|
|
|
|
|
15 |
|
16 |
+
def generate_image(prompt):
|
17 |
+
image = pipe(prompt).images[0]
|
18 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Gradio UI
|
21 |
iface = gr.Interface(
|
22 |
+
fn=generate_image,
|
23 |
+
inputs=gr.Textbox(label="Enter your prompt"),
|
24 |
+
outputs=gr.Image(label="Generated Image"),
|
25 |
+
title="Wan2.1 LoRA Image Generator",
|
26 |
+
description="Generate images using the Wan2.1 LoRA model. Enter a prompt to begin."
|
27 |
)
|
28 |
|
29 |
+
iface.launch()
|