rahul7star commited on
Commit
8ab0463
Β·
verified Β·
1 Parent(s): 74ff47f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -71
app.py CHANGED
@@ -1,82 +1,29 @@
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()
 
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()