File size: 4,902 Bytes
64e02cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a19757
 
 
 
 
 
 
 
64e02cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import argparse
from pathlib import Path

from huggingface_hub import HfApi
from huggingface_hub.errors import HfHubHTTPError


DEFAULT_SPACE_REPO_ID = "bykhan/wan13-zero-video"
DEFAULT_HARDWARE = "zero-a10g"
DEFAULT_SLEEP_TIME = 60


def read_env_file(file_path: Path) -> dict[str, str]:
    values: dict[str, str] = {}

    for raw_line in file_path.read_text(encoding="utf-8").splitlines():
        line = raw_line.strip()
        if not line or line.startswith("#") or "=" not in line:
            continue

        key, value = line.split("=", 1)
        values[key.strip()] = value.strip()

    return values


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Create or update a Hugging Face Gradio ZeroGPU Space for the Wan backend.")
    parser.add_argument("--repo-id", default=DEFAULT_SPACE_REPO_ID)
    parser.add_argument("--hardware", default=DEFAULT_HARDWARE)
    parser.add_argument("--sleep-time", type=int, default=DEFAULT_SLEEP_TIME)
    parser.add_argument("--private", action="store_true")
    parser.add_argument("--skip-hardware-request", action="store_true")
    parser.add_argument("--dry-run", action="store_true")
    return parser.parse_args()


def build_space_url(repo_id: str) -> str:
    owner, name = repo_id.split("/", 1)
    return f"https://{owner}-{name}.hf.space"


def main() -> int:
    args = parse_args()
    project_root = Path(__file__).resolve().parents[1]
    env_values = read_env_file(project_root / ".env")
    token = env_values.get("HF_TOKEN", "")

    if not token:
        raise SystemExit(".env icinde HF_TOKEN bulunamadi.")

    space_variables = [
        {"key": "WAN_VACE_CONFIG_REPO", "value": "Wan-AI/Wan2.1-VACE-1.3B-diffusers"},
        {"key": "WAN_VACE_GGUF_REPO", "value": "bykhan/wan-1.3b-gguf"},
        {"key": "WAN_VACE_GGUF_FILE", "value": "wan2.1-vace-1.3b-q4_0.gguf"},
        {"key": "WAN_TEXT_ENCODER_REPO", "value": "bykhan/wan-1.3b-gguf"},
        {"key": "WAN_TEXT_ENCODER_GGUF_FILE", "value": "umt5-xxl-encoder-q4_k_m.gguf"},
        {"key": "WAN_ENABLE_CPU_OFFLOAD", "value": "1"},
        {"key": "WAN_ENABLE_VAE_TILING", "value": "1"},
        {"key": "WAN_FLOW_SHIFT", "value": "3.0"},
        {"key": "WAN_DEFAULT_HEIGHT", "value": "480"},
        {"key": "WAN_DEFAULT_WIDTH", "value": "832"},
        {"key": "WAN_DEFAULT_NUM_FRAMES", "value": "81"},
        {"key": "WAN_DEFAULT_NUM_STEPS", "value": "30"},
        {"key": "WAN_DEFAULT_GUIDANCE", "value": "5.0"},
        {"key": "WAN_DEFAULT_CONDITIONING_SCALE", "value": "1.0"},
        {"key": "WAN_DEFAULT_FPS", "value": "16"},
        {"key": "WAN_RESULTS_REPO_ID", "value": env_values.get("WAN_RESULTS_REPO_ID", "bykhan/generated-videos")},
        {"key": "WAN_RESULTS_REPO_TYPE", "value": env_values.get("WAN_RESULTS_REPO_TYPE", "dataset")},
        {"key": "WAN_RESULTS_PATH_PREFIX", "value": env_values.get("WAN_RESULTS_PATH_PREFIX", "generated-videos")},
    ]
    space_secrets = [{"key": "HF_TOKEN", "value": token}]

    if args.dry_run:
        print(f"repo_id={args.repo_id}")
        print(f"hardware={args.hardware}")
        print(f"sleep_time={args.sleep_time}")
        print(f"space_url={build_space_url(args.repo_id)}")
        return 0

    api = HfApi(token=token)
    api.create_repo(
        repo_id=args.repo_id,
        repo_type="space",
        space_sdk="gradio",
        private=args.private,
        exist_ok=True,
        space_secrets=space_secrets,
        space_variables=space_variables,
        space_sleep_time=args.sleep_time,
    )

    api.upload_folder(
        repo_id=args.repo_id,
        repo_type="space",
        folder_path=Path(__file__).resolve().parent,
        commit_message="Deploy Wan ZeroGPU Space",
        ignore_patterns=["__pycache__/*", "*.pyc", ".env", ".env.*"],
    )

    runtime = api.get_space_runtime(args.repo_id)

    if not args.skip_hardware_request:
        try:
            request_kwargs = {
                "repo_id": args.repo_id,
                "hardware": args.hardware,
            }
            if args.hardware != "zero-a10g":
                request_kwargs["sleep_time"] = args.sleep_time

            runtime = api.request_space_hardware(**request_kwargs)
        except HfHubHTTPError as error:
            print(f"space_url={build_space_url(args.repo_id)}")
            print(f"stage={runtime.stage}")
            print("hardware_request=blocked")
            print(f"reason={error}")
            return 2

    print(f"space_url={build_space_url(args.repo_id)}")
    print(f"stage={runtime.stage}")
    print(f"hardware={getattr(runtime, 'hardware', None)}")
    print(f"requested_hardware={getattr(runtime, 'requested_hardware', None)}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())