muhqaxim commited on
Commit
c4de01b
·
1 Parent(s): 9e403e5

:fire:Initialized

Browse files
Files changed (7) hide show
  1. .gitattributes +8 -0
  2. .gitignore +13 -0
  3. .vscode/settings.json +7 -0
  4. app.py +113 -0
  5. requirements.txt +13 -0
  6. start.sh +7 -0
  7. utils/download_ckpts.py +16 -0
.gitattributes CHANGED
@@ -33,3 +33,11 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ # LFS track binary media
37
+ *.png filter=lfs diff=lfs merge=lfs -text
38
+ *.jpg filter=lfs diff=lfs merge=lfs -text
39
+ *.jpeg filter=lfs diff=lfs merge=lfs -text
40
+ *.mp4 filter=lfs diff=lfs merge=lfs -text
41
+ *.mov filter=lfs diff=lfs merge=lfs -text
42
+ *.pt filter=lfs diff=lfs merge=lfs -text
43
+ *.bin filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.pyc
4
+ .env
5
+ venv/
6
+ .venv/
7
+
8
+ # model checkpoints & outputs
9
+ ckpts/
10
+ samples/
11
+ outputs/
12
+ .cache/
13
+ hf_hub_download/
.vscode/settings.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "workbench.colorCustomizations": {
3
+ "activityBar.background": "#232D48",
4
+ "titleBar.activeBackground": "#303F65",
5
+ "titleBar.activeForeground": "#F8F9FC"
6
+ }
7
+ }
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import os
4
+ import glob
5
+ import time
6
+ from pathlib import Path
7
+ from huggingface_hub import snapshot_download
8
+
9
+ MODEL_REPO = "hpcai-tech/Open-Sora-v2"
10
+ CKPT_DIR = Path("ckpts")
11
+ SAMPLES_DIR = Path("samples")
12
+
13
+ def ensure_ckpts():
14
+ if CKPT_DIR.exists() and any(CKPT_DIR.iterdir()):
15
+ print("Found existing checkpoints in", CKPT_DIR)
16
+ return True
17
+ hf_token = os.environ.get("HUGGINGFACE_HUB_TOKEN") or os.environ.get("HF_TOKEN")
18
+ if not hf_token:
19
+ print("No HF token found in env. Cannot auto-download. Please add HUGGINGFACE_HUB_TOKEN or download ckpts manually.")
20
+ return False
21
+ print("Downloading model weights from HF... (this will take several minutes)")
22
+ try:
23
+ snapshot_download(repo_id=MODEL_REPO, local_dir=str(CKPT_DIR), local_dir_use_symlinks=False)
24
+ print("Download complete.")
25
+ return True
26
+ except Exception as e:
27
+ print("Error downloading checkpoints:", e)
28
+ return False
29
+
30
+ def find_latest_video():
31
+ SAMPLES_DIR.mkdir(exist_ok=True)
32
+ matches = list(SAMPLES_DIR.glob("*.mp4"))
33
+ if not matches:
34
+ return None
35
+ matches.sort(key=lambda p: p.stat().st_mtime, reverse=True)
36
+ return str(matches[0])
37
+
38
+ def run_torch_inference(config, prompt, ref_image=None, aspect_ratio=None, num_frames=None, offload=False):
39
+ SAMPLES_DIR.mkdir(exist_ok=True)
40
+ cmd = [
41
+ "torchrun", "--nproc_per_node", "1", "--standalone",
42
+ "scripts/diffusion/inference.py",
43
+ f"configs/diffusion/inference/{config}.py",
44
+ "--save-dir", str(SAMPLES_DIR),
45
+ "--prompt", prompt
46
+ ]
47
+ if ref_image:
48
+ cmd += ["--cond_type", "i2v_head", "--ref", ref_image]
49
+ if aspect_ratio:
50
+ cmd += ["--aspect_ratio", aspect_ratio]
51
+ if num_frames:
52
+ cmd += ["--num_frames", str(num_frames)]
53
+ if offload:
54
+ cmd += ["--offload", "True"]
55
+ print("Running command:", " ".join(cmd))
56
+ try:
57
+ subprocess.run(cmd, check=True, env=os.environ)
58
+ except subprocess.CalledProcessError as e:
59
+ print("Inference failed:", e)
60
+ raise
61
+
62
+ def generate_video(prompt, mode="t2i2v_256px", ref_image_path=None, aspect_ratio="16:9", num_frames=None, offload=False):
63
+ # mode options: t2i2v_256px, 256px (text2video direct), 768px...
64
+ ok = ensure_ckpts()
65
+ if not ok:
66
+ return "Model checkpoints not found and no HF token provided. Upload ckpts to ./ckpts or set HUGGINGFACE_HUB_TOKEN."
67
+
68
+ # Map UI selection to config file names
69
+ config_map = {
70
+ "256 (t2i2v)": "t2i2v_256px",
71
+ "256 (t2v)": "256px",
72
+ "768 (t2v)": "768px",
73
+ "768 (t2i2v)": "t2i2v_768px"
74
+ }
75
+ config = config_map.get(mode, "t2i2v_256px")
76
+ try:
77
+ run_torch_inference(config, prompt, ref_image=ref_image_path, aspect_ratio=aspect_ratio, num_frames=num_frames, offload=offload)
78
+ # wait for file to appear
79
+ for _ in range(120):
80
+ latest = find_latest_video()
81
+ if latest:
82
+ return latest
83
+ time.sleep(1)
84
+ return "No output video detected after inference."
85
+ except Exception as e:
86
+ return f"Error during generation: {str(e)}"
87
+
88
+ # Gradio UI
89
+ with gr.Blocks() as demo:
90
+ gr.Markdown("# 🎬 Open-Sora (Open-Sora-v2) — Text/Image to Video")
91
+ with gr.Row():
92
+ prompt = gr.Textbox(lines=3, label="Prompt", placeholder="A cinematic shot of ...")
93
+ with gr.Row():
94
+ mode = gr.Radio(["256 (t2i2v)", "256 (t2v)", "768 (t2v)", "768 (t2i2v)"], value="256 (t2i2v)", label="Generation Mode")
95
+ aspect_ratio = gr.Dropdown(["16:9","9:16","1:1","2.39:1"], value="16:9", label="Aspect Ratio")
96
+ num_frames = gr.Number(value=17, label="Frames (use 4k+1 rules)", precision=0)
97
+ with gr.Row():
98
+ ref_image = gr.Image(type="filepath", label="Reference image (optional, for I2V)")
99
+ offload = gr.Checkbox(label="Memory offload (slower but uses less GPU memory)", value=False)
100
+ generate_btn = gr.Button("Generate Video")
101
+ output_video = gr.Video(label="Generated Video")
102
+ status = gr.Textbox(label="Status/Logs", interactive=False)
103
+
104
+ def on_generate(prompt_text, mode_val, ar, nf, ref_img, off):
105
+ status_text = "Starting..."
106
+ status.update(status_text)
107
+ res = generate_video(prompt_text, mode_val, ref_image_path=ref_img, aspect_ratio=ar, num_frames=int(nf) if nf else None, offload=off)
108
+ return res, f"Completed: {res}"
109
+
110
+ generate_btn.click(on_generate, inputs=[prompt, mode, aspect_ratio, num_frames, ref_image, offload], outputs=[output_video, status])
111
+
112
+ if __name__ == "__main__":
113
+ demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # core
2
+ torch>=2.4.0
3
+ transformers
4
+ diffusers
5
+ xformers==0.0.27.post2
6
+ flash-attn
7
+ huggingface_hub[cli]
8
+ gradio
9
+ accelerate
10
+ numpy
11
+ opencv-python-headless
12
+ tqdm
13
+ omegaconf
start.sh ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # create venv and install (if needed)
3
+ python -m venv .venv
4
+ source .venv/bin/activate
5
+ pip install -r requirements.txt
6
+ # run app (Gradio)
7
+ python app.py
utils/download_ckpts.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Small utility to download model files using huggingface_hub snapshot_download
2
+ from huggingface_hub import snapshot_download
3
+ import os
4
+
5
+ MODEL_REPO = "hpcai-tech/Open-Sora-v2"
6
+ OUT_DIR = "ckpts"
7
+
8
+ def download_ckpts():
9
+ if not os.path.exists(OUT_DIR):
10
+ os.makedirs(OUT_DIR, exist_ok=True)
11
+ print(f"Downloading {MODEL_REPO} to {OUT_DIR} (this may take a while)...")
12
+ snapshot_download(repo_id=MODEL_REPO, local_dir=OUT_DIR, local_dir_use_symlinks=False)
13
+ print("Download complete.")
14
+
15
+ if __name__ == "__main__":
16
+ download_ckpts()