akarshan11 commited on
Commit
f671129
·
verified ·
1 Parent(s): 86a1f8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -15,13 +15,13 @@ import gradio as gr
15
  # To set up the environment for this script, create a file named 'requirements.txt'
16
  # with the following content and run 'pip install -r requirements.txt':
17
  #
18
- # torch>=2.0.0
19
- # torchvision>=0.15.1
20
- # diffusers>=0.20.2
21
- # transformers>=4.30.2
22
- # accelerate>=0.21.0
23
- # gradio>=3.36.1
24
- # opencv-python-headless>=4.8.0.74
25
  # -----------------------------------------------------------------------------
26
 
27
  # --- Automatic Device Detection ---
@@ -54,12 +54,12 @@ except Exception as e:
54
 
55
  @torch.no_grad()
56
  def diffuse(
57
- pipe, cond_embeddings, cond_latents, num_inference_steps, guidance_scale, eta
58
  ):
59
- # This function remains the same, as it gets the device from the input tensors
60
- device = cond_latents.get_device()
61
  max_length = cond_embeddings.shape[1]
62
  uncond_input = pipe.tokenizer([""], padding="max_length", max_length=max_length, return_tensors="pt")
 
63
  uncond_embeddings = pipe.text_encoder(uncond_input.input_ids.to(device))[0]
64
  text_embeddings = torch.cat([uncond_embeddings, cond_embeddings])
65
 
@@ -142,14 +142,12 @@ def generate_dream_video(
142
  prompt_embeddings = []
143
  for prompt in prompts:
144
  text_input = pipe.tokenizer(prompt, padding="max_length", max_length=pipe.tokenizer.model_max_length, truncation=True, return_tensors="pt")
145
- # Move input_ids to the correct device before text encoding
146
  with torch.no_grad():
147
  embed = pipe.text_encoder(text_input.input_ids.to(torch_device))[0]
148
  prompt_embeddings.append(embed)
149
 
150
  prompt_embedding_a, prompt_embedding_b = prompt_embeddings
151
 
152
- # Use a device-specific generator for reproducibility
153
  generator_a = torch.Generator(device=torch_device).manual_seed(seeds[0])
154
  generator_b = torch.Generator(device=torch_device).manual_seed(seeds[1])
155
 
@@ -170,7 +168,8 @@ def generate_dream_video(
170
 
171
  # Use autocast only if on CUDA
172
  with autocast(torch_device) if torch_device == "cuda" else open(os.devnull, 'w') as f:
173
- image = diffuse(pipe, cond_embedding, init, num_inference_steps, guidance_scale, eta)
 
174
 
175
  im = Image.fromarray(image)
176
  outpath = os.path.join(outdir, f'frame{i:06d}.jpg')
 
15
  # To set up the environment for this script, create a file named 'requirements.txt'
16
  # with the following content and run 'pip install -r requirements.txt':
17
  #
18
+ # torch
19
+ # torchvision
20
+ # diffusers
21
+ # transformers
22
+ # accelerate
23
+ # gradio
24
+ # opencv-python-headless
25
  # -----------------------------------------------------------------------------
26
 
27
  # --- Automatic Device Detection ---
 
54
 
55
  @torch.no_grad()
56
  def diffuse(
57
+ pipe, cond_embeddings, cond_latents, num_inference_steps, guidance_scale, eta, device
58
  ):
59
+ # The 'device' is now passed explicitly to this function
 
60
  max_length = cond_embeddings.shape[1]
61
  uncond_input = pipe.tokenizer([""], padding="max_length", max_length=max_length, return_tensors="pt")
62
+ # Use the passed 'device' variable for all tensor placement
63
  uncond_embeddings = pipe.text_encoder(uncond_input.input_ids.to(device))[0]
64
  text_embeddings = torch.cat([uncond_embeddings, cond_embeddings])
65
 
 
142
  prompt_embeddings = []
143
  for prompt in prompts:
144
  text_input = pipe.tokenizer(prompt, padding="max_length", max_length=pipe.tokenizer.model_max_length, truncation=True, return_tensors="pt")
 
145
  with torch.no_grad():
146
  embed = pipe.text_encoder(text_input.input_ids.to(torch_device))[0]
147
  prompt_embeddings.append(embed)
148
 
149
  prompt_embedding_a, prompt_embedding_b = prompt_embeddings
150
 
 
151
  generator_a = torch.Generator(device=torch_device).manual_seed(seeds[0])
152
  generator_b = torch.Generator(device=torch_device).manual_seed(seeds[1])
153
 
 
168
 
169
  # Use autocast only if on CUDA
170
  with autocast(torch_device) if torch_device == "cuda" else open(os.devnull, 'w') as f:
171
+ # Pass the torch_device explicitly to the diffuse function
172
+ image = diffuse(pipe, cond_embedding, init, num_inference_steps, guidance_scale, eta, torch_device)
173
 
174
  im = Image.fromarray(image)
175
  outpath = os.path.join(outdir, f'frame{i:06d}.jpg')