rahul7star commited on
Commit
19a018b
Β·
verified Β·
1 Parent(s): 4cf9808

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -54
app.py CHANGED
@@ -1,66 +1,55 @@
1
  import os
2
  import sys
3
  import subprocess
4
- from huggingface_hub import hf_hub_download, hf_hub_list
5
  from huggingface_hub import list_repo_files
6
- from pathlib import Path
7
- import spaces
8
  MODEL_REPO = "tencent/HunyuanVideo-Avatar"
9
- BASE_DIR = Path.cwd()
10
- WEIGHTS_DIR = BASE_DIR / "weights"
11
- CKPTS_DIR = WEIGHTS_DIR / "ckpts"
12
 
13
- # The root folder in the repo you want to download files from
14
- REPO_ROOT_FOLDER = "ckpts"
 
15
 
16
- # List all files under the folder 'ckpts' in the repo (recursively)
17
  def list_ckpt_files():
18
  print(f"πŸ” Listing files under '{REPO_ROOT_FOLDER}' in repo {MODEL_REPO}...")
19
- files = hf_hub_list(repo_id=MODEL_REPO, repo_type="model", revision="main", allow_regex=f"^{REPO_ROOT_FOLDER}/.*")
 
20
  return files
21
 
22
- # Download all files in the list into weights/ckpts preserving folder structure
23
- def download_ckpts(files):
24
- print(f"⬇️ Downloading {len(files)} files under '{REPO_ROOT_FOLDER}' ...")
25
- for file in files:
26
- # Target local path preserving folder structure after 'ckpts/'
27
- relative_path = Path(file).relative_to(REPO_ROOT_FOLDER)
28
- local_path = CKPTS_DIR / relative_path
29
-
30
- # Make sure directory exists
31
- local_path.parent.mkdir(parents=True, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Download the file if not present
34
- if not local_path.is_file():
35
- print(f"Downloading {file} -> {local_path}")
36
- hf_hub_download(
37
- repo_id=MODEL_REPO,
38
- filename=file,
39
- repo_type="model",
40
- cache_dir=str(WEIGHTS_DIR),
41
- local_dir=str(WEIGHTS_DIR),
42
- local_dir_use_symlinks=False,
43
- force_filename=str(local_path.name),
44
- revision="main",
45
- )
46
- else:
47
- print(f"File {local_path} already exists, skipping download.")
48
- print("βœ… All checkpoint files downloaded.")
49
-
50
- @spaces.GPU(duration=1000)
51
  def run_sample_gpu_poor():
52
- checkpoint_fp8 = CKPTS_DIR / "hunyuan-video-t2v-720p/transformers/mp_rank_00_model_states_fp8.pt"
53
- if not checkpoint_fp8.is_file():
54
- print(f"❌ Checkpoint file {checkpoint_fp8} not found!")
55
  sys.exit(1)
56
 
57
- OUTPUT_BASEPATH = BASE_DIR / "results-poor"
58
- OUTPUT_BASEPATH.mkdir(parents=True, exist_ok=True)
59
-
60
  cmd = [
61
  "python3", "hymm_sp/sample_gpu_poor.py",
62
  "--input", "assets/test.csv",
63
- "--ckpt", str(checkpoint_fp8),
64
  "--sample-n-frames", "129",
65
  "--seed", "128",
66
  "--image-size", "704",
@@ -68,7 +57,7 @@ def run_sample_gpu_poor():
68
  "--infer-steps", "50",
69
  "--use-deepcache", "1",
70
  "--flow-shift-eval-video", "5.0",
71
- "--save-path", str(OUTPUT_BASEPATH),
72
  "--use-fp8",
73
  "--cpu-offload",
74
  "--infer-min"
@@ -76,11 +65,11 @@ def run_sample_gpu_poor():
76
 
77
  env = os.environ.copy()
78
  env["PYTHONPATH"] = "./"
79
- env["MODEL_BASE"] = str(WEIGHTS_DIR)
80
  env["CPU_OFFLOAD"] = "1"
81
  env["CUDA_VISIBLE_DEVICES"] = "0"
82
 
83
- print("🎬 Running sample_gpu_poor.py...")
84
  proc = subprocess.run(cmd, env=env)
85
  if proc.returncode != 0:
86
  print("❌ sample_gpu_poor.py failed.")
@@ -88,12 +77,7 @@ def run_sample_gpu_poor():
88
  print("βœ… sample_gpu_poor.py completed successfully.")
89
 
90
  def main():
91
- files = list_ckpt_files()
92
- if not files:
93
- print("❌ No checkpoint files found in repo under ckpts folder.")
94
- sys.exit(1)
95
-
96
- download_ckpts(files)
97
  run_sample_gpu_poor()
98
 
99
  if __name__ == "__main__":
 
1
  import os
2
  import sys
3
  import subprocess
 
4
  from huggingface_hub import list_repo_files
5
+
 
6
  MODEL_REPO = "tencent/HunyuanVideo-Avatar"
7
+ REPO_ROOT_FOLDER = "ckpts" # folder inside repo to download
 
 
8
 
9
+ BASE_DIR = os.getcwd()
10
+ WEIGHTS_DIR = os.path.join(BASE_DIR, "weights")
11
+ OUTPUT_BASEPATH = os.path.join(BASE_DIR, "results-poor")
12
 
 
13
  def list_ckpt_files():
14
  print(f"πŸ” Listing files under '{REPO_ROOT_FOLDER}' in repo {MODEL_REPO}...")
15
+ all_files = list_repo_files(repo_id=MODEL_REPO, repo_type="model", revision="main")
16
+ files = [f for f in all_files if f.startswith(REPO_ROOT_FOLDER + "/")]
17
  return files
18
 
19
+ def download_ckpts():
20
+ os.makedirs(WEIGHTS_DIR, exist_ok=True)
21
+ for filepath in list_ckpt_files():
22
+ # Target path relative to weights directory
23
+ relative_path = os.path.relpath(filepath, REPO_ROOT_FOLDER)
24
+ target_path = os.path.join(WEIGHTS_DIR, REPO_ROOT_FOLDER, relative_path)
25
+
26
+ # Skip if already exists
27
+ if os.path.exists(target_path):
28
+ print(f"βœ… File exists: {target_path}")
29
+ continue
30
+
31
+ # Create dirs if needed
32
+ os.makedirs(os.path.dirname(target_path), exist_ok=True)
33
+
34
+ # Download the file blob from HF repo using huggingface_hub api (requires v0.14+)
35
+ print(f"⬇️ Downloading {filepath} to {target_path} ...")
36
+ try:
37
+ from huggingface_hub import hf_hub_download
38
+ hf_hub_download(repo_id=MODEL_REPO, filename=filepath, local_dir=os.path.dirname(target_path), local_dir_use_symlinks=False)
39
+ except ImportError:
40
+ print("⚠️ hf_hub_download not available, please upgrade huggingface_hub")
41
+ sys.exit(1)
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  def run_sample_gpu_poor():
44
+ checkpoint_fp8 = os.path.join(WEIGHTS_DIR, "ckpts", "hunyuan-video-t2v-720p", "transformers", "mp_rank_00_model_states_fp8.pt")
45
+ if not os.path.isfile(checkpoint_fp8):
46
+ print(f"❌ Checkpoint file not found at {checkpoint_fp8}. Cannot run sampling.")
47
  sys.exit(1)
48
 
 
 
 
49
  cmd = [
50
  "python3", "hymm_sp/sample_gpu_poor.py",
51
  "--input", "assets/test.csv",
52
+ "--ckpt", checkpoint_fp8,
53
  "--sample-n-frames", "129",
54
  "--seed", "128",
55
  "--image-size", "704",
 
57
  "--infer-steps", "50",
58
  "--use-deepcache", "1",
59
  "--flow-shift-eval-video", "5.0",
60
+ "--save-path", OUTPUT_BASEPATH,
61
  "--use-fp8",
62
  "--cpu-offload",
63
  "--infer-min"
 
65
 
66
  env = os.environ.copy()
67
  env["PYTHONPATH"] = "./"
68
+ env["MODEL_BASE"] = WEIGHTS_DIR
69
  env["CPU_OFFLOAD"] = "1"
70
  env["CUDA_VISIBLE_DEVICES"] = "0"
71
 
72
+ print("🎬 Running sample_gpu_poor.py ...")
73
  proc = subprocess.run(cmd, env=env)
74
  if proc.returncode != 0:
75
  print("❌ sample_gpu_poor.py failed.")
 
77
  print("βœ… sample_gpu_poor.py completed successfully.")
78
 
79
  def main():
80
+ download_ckpts()
 
 
 
 
 
81
  run_sample_gpu_poor()
82
 
83
  if __name__ == "__main__":