Martim-Ramos-Neural commited on
Commit
e7124b2
·
1 Parent(s): 7ca87ad
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -1,7 +1,6 @@
1
- import spaces
2
  import torch
3
  import imageio
4
- import os
5
  import gradio as gr
6
  from subprocess import getoutput
7
  from diffusers.schedulers import EulerAncestralDiscreteScheduler
@@ -9,53 +8,60 @@ from transformers import T5EncoderModel, T5Tokenizer
9
  from allegro.pipelines.pipeline_allegro import AllegroPipeline
10
  from allegro.models.vae.vae_allegro import AllegroAutoencoderKL3D
11
  from allegro.models.transformers.transformer_3d_allegro import AllegroTransformer3DModel
12
-
13
  from huggingface_hub import snapshot_download
14
  import spaces # Import ZeroGPU compatibility module
15
 
 
16
  weights_dir = './allegro_weights'
17
  os.makedirs(weights_dir, exist_ok=True)
18
 
19
- is_shared_ui = True if "rhymes-ai-Allegro-textToVideo" in os.environ.get('SPACE_ID', "") else False
 
20
  is_gpu_associated = torch.cuda.is_available()
21
 
 
22
  if not is_shared_ui:
23
  snapshot_download(
24
  repo_id='rhymes-ai/Allegro',
25
- allow_patterns=[
26
- 'scheduler/**',
27
- 'text_encoder/**',
28
- 'tokenizer/**',
29
- 'transformer/**',
30
- 'vae/**',
31
- ],
32
  local_dir=weights_dir,
 
33
  )
34
 
35
  if is_gpu_associated:
36
  gpu_info = getoutput('nvidia-smi')
 
 
 
 
 
 
 
37
 
38
- #@spaces.GPU(duration=120) # Request GPU for the entire process
39
- @spaces.GPU() # Request GPU for the entire process
40
  def process_pipeline(user_prompt, guidance_scale, num_sampling_steps, seed, enable_cpu_offload):
41
  # Define dtype
42
  dtype = torch.bfloat16
43
 
44
  # Load models
45
  vae = AllegroAutoencoderKL3D.from_pretrained(
46
- "./allegro_weights/vae/",
47
  torch_dtype=torch.float32
48
  ).cuda()
49
  vae.eval()
50
 
51
- text_encoder = T5EncoderModel.from_pretrained("./allegro_weights/text_encoder/", torch_dtype=dtype)
52
- text_encoder.eval()
 
 
53
 
54
- tokenizer = T5Tokenizer.from_pretrained("./allegro_weights/tokenizer/")
55
 
56
  scheduler = EulerAncestralDiscreteScheduler()
57
 
58
- transformer = AllegroTransformer3DModel.from_pretrained("./allegro_weights/transformer/", torch_dtype=dtype).cuda()
 
 
 
59
  transformer.eval()
60
 
61
  allegro_pipeline = AllegroPipeline(
@@ -105,13 +111,11 @@ def process_pipeline(user_prompt, guidance_scale, num_sampling_steps, seed, enab
105
 
106
  return save_path
107
 
108
-
109
  # Gradio interface function
110
  def run_inference(user_prompt, guidance_scale, num_sampling_steps, seed, enable_cpu_offload, progress=gr.Progress(track_tqdm=True)):
111
  result_path = process_pipeline(user_prompt, guidance_scale, num_sampling_steps, seed, enable_cpu_offload)
112
  return result_path
113
 
114
-
115
  css = """
116
  div#col-container {
117
  margin: 0 auto;
 
1
+ import os
2
  import torch
3
  import imageio
 
4
  import gradio as gr
5
  from subprocess import getoutput
6
  from diffusers.schedulers import EulerAncestralDiscreteScheduler
 
8
  from allegro.pipelines.pipeline_allegro import AllegroPipeline
9
  from allegro.models.vae.vae_allegro import AllegroAutoencoderKL3D
10
  from allegro.models.transformers.transformer_3d_allegro import AllegroTransformer3DModel
 
11
  from huggingface_hub import snapshot_download
12
  import spaces # Import ZeroGPU compatibility module
13
 
14
+ # Ensure the weights directory exists
15
  weights_dir = './allegro_weights'
16
  os.makedirs(weights_dir, exist_ok=True)
17
 
18
+ # Check if running in a shared UI environment
19
+ is_shared_ui = "rhymes-ai-Allegro-textToVideo" in os.environ.get('SPACE_ID', "")
20
  is_gpu_associated = torch.cuda.is_available()
21
 
22
+ # Download the necessary model files if not in shared UI
23
  if not is_shared_ui:
24
  snapshot_download(
25
  repo_id='rhymes-ai/Allegro',
 
 
 
 
 
 
 
26
  local_dir=weights_dir,
27
+ allow_patterns=['**'] # Download all required files
28
  )
29
 
30
  if is_gpu_associated:
31
  gpu_info = getoutput('nvidia-smi')
32
+ print(f"GPU Info: {gpu_info}")
33
+
34
+ # Check directory structure
35
+ required_dirs = ['vae', 'text_encoder', 'tokenizer', 'scheduler', 'transformer']
36
+ missing_dirs = [d for d in required_dirs if not os.path.exists(os.path.join(weights_dir, d))]
37
+ if missing_dirs:
38
+ raise FileNotFoundError(f"The following directories are missing in '{weights_dir}': {missing_dirs}")
39
 
40
+ @spaces.GPU(duration=120) # Request GPU for the entire process
 
41
  def process_pipeline(user_prompt, guidance_scale, num_sampling_steps, seed, enable_cpu_offload):
42
  # Define dtype
43
  dtype = torch.bfloat16
44
 
45
  # Load models
46
  vae = AllegroAutoencoderKL3D.from_pretrained(
47
+ os.path.join(weights_dir, 'vae/'),
48
  torch_dtype=torch.float32
49
  ).cuda()
50
  vae.eval()
51
 
52
+ text_encoder = T5EncoderModel.from_pretrained(
53
+ os.path.join(weights_dir, 'text_encoder/'),
54
+ torch_dtype=dtype
55
+ ).eval()
56
 
57
+ tokenizer = T5Tokenizer.from_pretrained(os.path.join(weights_dir, 'tokenizer/'))
58
 
59
  scheduler = EulerAncestralDiscreteScheduler()
60
 
61
+ transformer = AllegroTransformer3DModel.from_pretrained(
62
+ os.path.join(weights_dir, 'transformer/'),
63
+ torch_dtype=dtype
64
+ ).cuda()
65
  transformer.eval()
66
 
67
  allegro_pipeline = AllegroPipeline(
 
111
 
112
  return save_path
113
 
 
114
  # Gradio interface function
115
  def run_inference(user_prompt, guidance_scale, num_sampling_steps, seed, enable_cpu_offload, progress=gr.Progress(track_tqdm=True)):
116
  result_path = process_pipeline(user_prompt, guidance_scale, num_sampling_steps, seed, enable_cpu_offload)
117
  return result_path
118
 
 
119
  css = """
120
  div#col-container {
121
  margin: 0 auto;