rahul7star commited on
Commit
aab7d3b
Β·
verified Β·
1 Parent(s): 4b2a462

try anbiter way

Browse files
Files changed (1) hide show
  1. app.py +21 -68
app.py CHANGED
@@ -1,82 +1,35 @@
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
+ # Load model manually from Hugging Face model hub or your uploaded files
6
+ model_path = "sarthak247/Wan2.1-T2V-1.3B-nf4" # Replace with your model path
7
+ pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
8
+ pipe.to("cuda") # If running on GPU
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def generate_video(prompt):
11
  """
12
+ Generates a video from the provided prompt using the pre-loaded model.
13
  """
14
+ try:
15
+ # Generate video using the model pipeline
16
+ video = pipe(prompt).videos[0] # Assuming output is a video tensor
17
+
18
+ # Return the generated video
19
+ return video
20
+
21
+ except Exception as e:
22
+ print(f"Error during video generation: {e}")
23
+ return "Error generating video"
24
+
25
+ # Gradio UI for video generation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  iface = gr.Interface(
27
  fn=generate_video,
28
  inputs=gr.Textbox(label="Enter Text Prompt"),
29
  outputs=gr.Video(label="Generated Video"),
30
+ title="Text-to-Video Generation with Wan2.1-T2V",
31
+ description="This app generates a video based on the text prompt using the Wan2.1-T2V model."
32
  )
33
 
34
+ # Launch the Gradio app
35
  iface.launch()