Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,8 +14,26 @@
|
|
| 14 |
# ------------------------------------------------------------------------
|
| 15 |
"""URSA TI2V application."""
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
import sys
|
| 20 |
|
| 21 |
import gradio as gr
|
|
@@ -66,16 +84,6 @@ _patch_diffnext_qwen3()
|
|
| 66 |
# =========================
|
| 67 |
|
| 68 |
|
| 69 |
-
# ✅ HF Token(从环境变量读取,不写死)
|
| 70 |
-
os.environ["HF_TOKEN"] = os.environ.get("HF_TOKEN", "")
|
| 71 |
-
os.environ["HUGGINGFACE_HUB_TOKEN"] = os.environ.get("HF_TOKEN", "")
|
| 72 |
-
os.environ["HF_HUB_ENABLE_HUGGINGFACE_TRANSFER"] = "1"
|
| 73 |
-
os.environ["HF_HUB_DOWNLOAD_TIMEOUT"] = "300"
|
| 74 |
-
|
| 75 |
-
os.environ["TOKENIZERS_PARALLELISM"] = "true"
|
| 76 |
-
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 77 |
-
|
| 78 |
-
|
| 79 |
def parse_args():
|
| 80 |
parser = argparse.ArgumentParser(description="Serve URSA TI2V application")
|
| 81 |
parser.add_argument("--model", default="BAAI/URSA-0.6B-FSQ320", help="model path")
|
|
@@ -98,6 +106,25 @@ def crop_image(image, target_h, target_w):
|
|
| 98 |
return np.array(image.resize((target_w, target_h), PIL.Image.Resampling.BILINEAR))
|
| 99 |
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def generate_image(
|
| 102 |
prompt,
|
| 103 |
negative_prompt,
|
|
@@ -114,6 +141,7 @@ def generate_image(
|
|
| 114 |
return [export_to_image(image, quality=95) for image in images] + [seed]
|
| 115 |
|
| 116 |
|
|
|
|
| 117 |
def generate_video(
|
| 118 |
prompt,
|
| 119 |
negative_prompt,
|
|
@@ -161,20 +189,6 @@ examples = [list(x) for x in zip(prompts, motion_scores)]
|
|
| 161 |
|
| 162 |
|
| 163 |
if __name__ == "__main__":
|
| 164 |
-
args = parse_args()
|
| 165 |
-
|
| 166 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu", args.device)
|
| 167 |
-
model_args = {
|
| 168 |
-
"torch_dtype": getattr(torch, args.precision.lower()),
|
| 169 |
-
"trust_remote_code": True,
|
| 170 |
-
}
|
| 171 |
-
|
| 172 |
-
pipe = URSAPipeline.from_pretrained(
|
| 173 |
-
args.model,
|
| 174 |
-
**model_args,
|
| 175 |
-
resume_download=True,
|
| 176 |
-
).to(device)
|
| 177 |
-
|
| 178 |
# Application
|
| 179 |
app = gr.Blocks(css=css, theme="origin").__enter__()
|
| 180 |
container = gr.Column(elem_id="col-container").__enter__()
|
|
@@ -244,4 +258,7 @@ if __name__ == "__main__":
|
|
| 244 |
],
|
| 245 |
outputs=[result, seed],
|
| 246 |
)
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# ------------------------------------------------------------------------
|
| 15 |
"""URSA TI2V application."""
|
| 16 |
|
| 17 |
+
# =========================
|
| 18 |
+
# ✅ 改 1:环境变量必须最先设置(早于 torch / spaces / diffnext)
|
| 19 |
+
# =========================
|
| 20 |
import os
|
| 21 |
+
|
| 22 |
+
# 只在 token 存在时设置,避免把 HF_TOKEN 污染成空字符串
|
| 23 |
+
if os.environ.get("HF_TOKEN"):
|
| 24 |
+
os.environ.setdefault("HUGGINGFACE_HUB_TOKEN", os.environ["HF_TOKEN"])
|
| 25 |
+
os.environ["HF_HUB_ENABLE_HUGGINGFACE_TRANSFER"] = "1"
|
| 26 |
+
os.environ["HF_HUB_DOWNLOAD_TIMEOUT"] = "300"
|
| 27 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "true"
|
| 28 |
+
# 必须在 CUDA 初始化前设置,否则 expandable_segments 不生效
|
| 29 |
+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 30 |
+
|
| 31 |
+
# =========================
|
| 32 |
+
# ✅ 改 2:import spaces 必须早于 torch(ZeroGPU 硬性要求)
|
| 33 |
+
# =========================
|
| 34 |
+
import spaces
|
| 35 |
+
|
| 36 |
+
import argparse
|
| 37 |
import sys
|
| 38 |
|
| 39 |
import gradio as gr
|
|
|
|
| 84 |
# =========================
|
| 85 |
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def parse_args():
|
| 88 |
parser = argparse.ArgumentParser(description="Serve URSA TI2V application")
|
| 89 |
parser.add_argument("--model", default="BAAI/URSA-0.6B-FSQ320", help="model path")
|
|
|
|
| 106 |
return np.array(image.resize((target_w, target_h), PIL.Image.Resampling.BILINEAR))
|
| 107 |
|
| 108 |
|
| 109 |
+
# =========================
|
| 110 |
+
# ✅ 改 3:模型加载放在【模块顶层】,不能放进 if __name__ == "__main__"
|
| 111 |
+
# ZeroGPU 只在模块级启用 CUDA 模拟并打包权重
|
| 112 |
+
# =========================
|
| 113 |
+
args = parse_args()
|
| 114 |
+
|
| 115 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu", args.device)
|
| 116 |
+
model_args = {
|
| 117 |
+
"torch_dtype": getattr(torch, args.precision.lower()),
|
| 118 |
+
"trust_remote_code": True,
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
pipe = URSAPipeline.from_pretrained(args.model, **model_args).to(device)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# =========================
|
| 125 |
+
# ✅ 改 4:给真正跑推理的函数加 @spaces.GPU
|
| 126 |
+
# =========================
|
| 127 |
+
@spaces.GPU(duration=60)
|
| 128 |
def generate_image(
|
| 129 |
prompt,
|
| 130 |
negative_prompt,
|
|
|
|
| 141 |
return [export_to_image(image, quality=95) for image in images] + [seed]
|
| 142 |
|
| 143 |
|
| 144 |
+
@spaces.GPU(duration=180)
|
| 145 |
def generate_video(
|
| 146 |
prompt,
|
| 147 |
negative_prompt,
|
|
|
|
| 189 |
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
# Application
|
| 193 |
app = gr.Blocks(css=css, theme="origin").__enter__()
|
| 194 |
container = gr.Column(elem_id="col-container").__enter__()
|
|
|
|
| 258 |
],
|
| 259 |
outputs=[result, seed],
|
| 260 |
)
|
| 261 |
+
# ✅ 改 5:加 queue(default_concurrency_limit=1),避免多请求抢同一张卡
|
| 262 |
+
app.__exit__(), app.queue(default_concurrency_limit=1).launch(
|
| 263 |
+
share=False, ssr_mode=False, max_threads=1
|
| 264 |
+
)
|