fffiloni commited on
Commit
13dcf63
·
verified ·
1 Parent(s): 9c6bd0f

Upload 6 files

Browse files
Files changed (2) hide show
  1. src/jobs.py +45 -226
  2. src/worker_payload.py +0 -0
src/jobs.py CHANGED
@@ -8,14 +8,8 @@ from huggingface_hub import Volume, fetch_job_logs, inspect_job, run_job
8
  from .config import settings
9
  from .runs import make_run_id, utc_now_iso, validate_run_id
10
  from .worker_payload import (
11
- encoded_create_space_worker_script,
12
- encoded_pi_gist_worker_script,
13
- encoded_pi_model_card_worker_script,
14
- encoded_runtime_recommender_worker_script,
15
- encoded_longcat_article_worker_script,
16
  encoded_universal_model_card_worker_script,
17
- encoded_pi_space_worker_script,
18
- encoded_worker_script,
19
  python_decode_and_run_command,
20
  )
21
 
@@ -61,201 +55,32 @@ def _job_result(job: Any, *, run_id: str, kind: str, extra: dict[str, Any] | Non
61
  return payload
62
 
63
 
64
- def launch_hello_job(*, token: str, username: str, run_id: str | None = None) -> dict[str, Any]:
65
- """Launch the Phase 1 HF Job that only writes state/events/report to the bucket."""
66
- if not token:
67
- raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
68
-
69
- safe_run_id = validate_run_id(run_id) if run_id else make_run_id("hello")
70
- env = _base_env(run_id=safe_run_id, username=username, worker_script_b64=encoded_worker_script())
71
- job = _launch_job(token=token, env=env)
72
- return _job_result(job, run_id=safe_run_id, kind="hello_job")
73
-
74
-
75
  def normalize_target_space(*, username: str, target_slug: str | None, run_id: str) -> str:
76
- """Return `username/slug`, constrained to the signed-in user's namespace for V2."""
77
  slug = (target_slug or "").strip()
78
  if not slug:
79
  slug = f"space-factory-{run_id}".lower()[:80]
80
- # If user pasted a full repo id, only allow their own namespace in Phase 2.
81
  if "/" in slug:
82
  namespace, repo = slug.split("/", 1)
83
  if namespace != username:
84
- raise ValueError("For Phase 2, the target Space must be created in your own namespace.")
85
  slug = repo
86
  if not SPACE_SLUG_RE.match(slug):
87
  raise ValueError("Invalid target Space name. Use letters, numbers, dots, underscores, or dashes.")
88
  return f"{username}/{slug}"
89
 
90
 
91
- def launch_create_private_space_job(
92
- *,
93
- token: str,
94
- username: str,
95
- target_slug: str | None = None,
96
- run_id: str | None = None,
97
- ) -> dict[str, Any]:
98
- """Launch the Phase 2 Job: create a private target Gradio Space and validate it live."""
99
- if not token:
100
- raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
101
- safe_run_id = validate_run_id(run_id) if run_id else make_run_id("space")
102
- target_space_id = normalize_target_space(username=username, target_slug=target_slug, run_id=safe_run_id)
103
-
104
- env = _base_env(
105
- run_id=safe_run_id,
106
- username=username,
107
- worker_script_b64=encoded_create_space_worker_script(),
108
- )
109
- env["TARGET_SPACE_ID"] = target_space_id
110
- job = _launch_job(token=token, env=env)
111
- return _job_result(
112
- job,
113
- run_id=safe_run_id,
114
- kind="create_private_space",
115
- extra={"target_space": target_space_id, "target_space_url": f"https://huggingface.co/spaces/{target_space_id}"},
116
- )
117
-
118
-
119
- def launch_pi_space_smoke_job(
120
- *,
121
- token: str,
122
- username: str,
123
- target_slug: str | None = None,
124
- pi_model: str | None = None,
125
- run_id: str | None = None,
126
- ) -> dict[str, Any]:
127
- """Launch the Phase 3 Job: install Pi, let Pi modify a minimal app, then create/validate a private Space."""
128
- if not token:
129
- raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
130
- safe_run_id = validate_run_id(run_id) if run_id else make_run_id("pi")
131
- target_space_id = normalize_target_space(username=username, target_slug=target_slug, run_id=safe_run_id)
132
-
133
- env = _base_env(
134
- run_id=safe_run_id,
135
- username=username,
136
- worker_script_b64=encoded_pi_space_worker_script(),
137
- )
138
- env["TARGET_SPACE_ID"] = target_space_id
139
- env["PI_MODEL"] = (pi_model or "Qwen/Qwen3-Coder-Next").strip()
140
- job = _launch_job(token=token, env=env)
141
- return _job_result(
142
- job,
143
- run_id=safe_run_id,
144
- kind="pi_space_smoke",
145
- extra={
146
- "target_space": target_space_id,
147
- "target_space_url": f"https://huggingface.co/spaces/{target_space_id}",
148
- "pi_model": env["PI_MODEL"],
149
- },
150
- )
151
-
152
-
153
-
154
- def launch_pi_gist_recipe_job(
155
- *,
156
- token: str,
157
- username: str,
158
- target_slug: str | None = None,
159
- pi_model: str | None = None,
160
- run_id: str | None = None,
161
- ) -> dict[str, Any]:
162
- """Launch the Phase 4 Job: Pi applies the HF Spaces gist recipe and uses hf CLI to create/validate a private Space."""
163
- if not token:
164
- raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
165
- safe_run_id = validate_run_id(run_id) if run_id else make_run_id("gist")
166
- target_space_id = normalize_target_space(username=username, target_slug=target_slug, run_id=safe_run_id)
167
-
168
- env = _base_env(
169
- run_id=safe_run_id,
170
- username=username,
171
- worker_script_b64=encoded_pi_gist_worker_script(),
172
- )
173
- env["TARGET_SPACE_ID"] = target_space_id
174
- env["PI_MODEL"] = (pi_model or "Qwen/Qwen3-Coder-Next").strip()
175
- job = _launch_job(token=token, env=env)
176
- return _job_result(
177
- job,
178
- run_id=safe_run_id,
179
- kind="pi_gist_recipe",
180
- extra={
181
- "target_space": target_space_id,
182
- "target_space_url": f"https://huggingface.co/spaces/{target_space_id}",
183
- "pi_model": env["PI_MODEL"],
184
- },
185
- )
186
-
187
-
188
- def launch_pi_model_card_job(
189
- *,
190
- token: str,
191
- username: str,
192
- target_slug: str | None = None,
193
- model_id: str | None = None,
194
- pi_model: str | None = None,
195
- run_id: str | None = None,
196
- ) -> dict[str, Any]:
197
- """Launch the Phase 5 Job: analyze a model card, let Pi adapt a template, create/validate a private Space."""
198
- if not token:
199
- raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
200
- safe_run_id = validate_run_id(run_id) if run_id else make_run_id("model")
201
- target_space_id = normalize_target_space(username=username, target_slug=target_slug, run_id=safe_run_id)
202
- clean_model_id = (model_id or "").strip().replace("https://huggingface.co/", "").strip("/")
203
- if "/" not in clean_model_id:
204
- raise ValueError("Model ID must look like owner/model-name.")
205
-
206
- env = _base_env(
207
- run_id=safe_run_id,
208
- username=username,
209
- worker_script_b64=encoded_pi_model_card_worker_script(),
210
- )
211
- env["TARGET_SPACE_ID"] = target_space_id
212
- env["MODEL_ID"] = clean_model_id
213
- env["PI_MODEL"] = (pi_model or "Qwen/Qwen3-Coder-Next").strip()
214
- job = _launch_job(token=token, env=env)
215
- return _job_result(
216
- job,
217
- run_id=safe_run_id,
218
- kind="pi_model_card",
219
- extra={
220
- "target_space": target_space_id,
221
- "target_space_url": f"https://huggingface.co/spaces/{target_space_id}",
222
- "model_id": clean_model_id,
223
- "pi_model": env["PI_MODEL"],
224
- },
225
- )
226
-
227
-
228
-
229
- def launch_runtime_recommender_job(
230
- *,
231
- token: str,
232
- username: str,
233
- model_id: str | None = None,
234
- run_id: str | None = None,
235
- ) -> dict[str, Any]:
236
- """Launch the Phase 6 Job: analyze model metadata and recommend safe runtime/hardware before building."""
237
- if not token:
238
- raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
239
- safe_run_id = validate_run_id(run_id) if run_id else make_run_id("runtime")
240
- clean_model_id = (model_id or "").strip().replace("https://huggingface.co/", "").strip("/")
241
- if "/" not in clean_model_id:
242
- raise ValueError("Model ID must look like owner/model-name.")
243
- env = _base_env(
244
- run_id=safe_run_id,
245
- username=username,
246
- worker_script_b64=encoded_runtime_recommender_worker_script(),
247
- )
248
- env["MODEL_ID"] = clean_model_id
249
- job = _launch_job(token=token, env=env)
250
- return _job_result(
251
- job,
252
- run_id=safe_run_id,
253
- kind="runtime_recommender",
254
- extra={"model_id": clean_model_id},
255
- )
256
 
257
 
258
- def launch_longcat_article_job(
259
  *,
260
  token: str,
261
  username: str,
@@ -268,19 +93,17 @@ def launch_longcat_article_job(
268
  implementation_mode: str | None = None,
269
  run_id: str | None = None,
270
  ) -> dict[str, Any]:
271
- """Launch Phase 9: strict LongCat full-inference gate with blocker reporting."""
272
  if not token:
273
  raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
274
- safe_run_id = validate_run_id(run_id) if run_id else make_run_id("longcat")
275
  target_space_id = normalize_target_space(username=username, target_slug=target_slug, run_id=safe_run_id)
276
- clean_model_id = (model_id or "meituan-longcat/LongCat-Video-Avatar-1.5").strip().replace("https://huggingface.co/", "").strip("/")
277
- if "/" not in clean_model_id:
278
- raise ValueError("Model ID must look like owner/model-name.")
279
 
280
  env = _base_env(
281
  run_id=safe_run_id,
282
  username=username,
283
- worker_script_b64=encoded_longcat_article_worker_script(),
284
  )
285
  env["TARGET_SPACE_ID"] = target_space_id
286
  env["MODEL_ID"] = clean_model_id
@@ -293,7 +116,7 @@ def launch_longcat_article_job(
293
  return _job_result(
294
  job,
295
  run_id=safe_run_id,
296
- kind="longcat_full_inference_gate",
297
  extra={
298
  "target_space": target_space_id,
299
  "target_space_url": f"https://huggingface.co/spaces/{target_space_id}",
@@ -307,57 +130,53 @@ def launch_longcat_article_job(
307
  )
308
 
309
 
310
- def launch_universal_model_card_job(
311
  *,
312
  token: str,
313
  username: str,
314
- target_slug: str | None = None,
315
- model_id: str | None = None,
316
- pi_model: str | None = None,
317
- preferred_space_hardware: str | None = None,
318
- fallback_space_hardware: str | None = None,
319
- allow_fixed_gpu_fallback: bool = True,
320
- implementation_mode: str | None = None,
321
  run_id: str | None = None,
322
  ) -> dict[str, Any]:
323
- """Launch Phase 10: universal model-card private Space builder with Pi and gated validation."""
324
  if not token:
325
  raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
326
- safe_run_id = validate_run_id(run_id) if run_id else make_run_id("universal")
327
- target_space_id = normalize_target_space(username=username, target_slug=target_slug, run_id=safe_run_id)
328
- clean_model_id = (model_id or "").strip().replace("https://huggingface.co/", "").strip("/")
329
- if "/" not in clean_model_id:
330
- raise ValueError("Model ID must look like owner/model-name or a Hugging Face model URL.")
331
-
332
  env = _base_env(
333
  run_id=safe_run_id,
334
  username=username,
335
- worker_script_b64=encoded_universal_model_card_worker_script(),
336
  )
337
- env["TARGET_SPACE_ID"] = target_space_id
338
- env["MODEL_ID"] = clean_model_id
339
- env["PI_MODEL"] = (pi_model or "Qwen/Qwen3-Coder-Next").strip()
340
- env["PREFERRED_SPACE_HARDWARE"] = (preferred_space_hardware or "cpu-basic").strip()
341
- env["FALLBACK_SPACE_HARDWARE"] = (fallback_space_hardware or "l40sx1").strip()
342
- env["ALLOW_FIXED_GPU_FALLBACK"] = "true" if allow_fixed_gpu_fallback else "false"
343
- env["IMPLEMENTATION_MODE"] = (implementation_mode or "full-inference-gated").strip()
344
  job = _launch_job(token=token, env=env, timeout="60m")
345
  return _job_result(
346
  job,
347
  run_id=safe_run_id,
348
- kind="universal_model_card_builder",
349
  extra={
350
- "target_space": target_space_id,
351
- "target_space_url": f"https://huggingface.co/spaces/{target_space_id}",
352
- "model_id": clean_model_id,
353
- "pi_model": env["PI_MODEL"],
354
- "preferred_space_hardware": env["PREFERRED_SPACE_HARDWARE"],
355
- "fallback_space_hardware": env["FALLBACK_SPACE_HARDWARE"],
356
- "allow_fixed_gpu_fallback": allow_fixed_gpu_fallback,
357
- "implementation_mode": env["IMPLEMENTATION_MODE"],
358
  },
359
  )
360
 
 
361
  def inspect_job_safe(job_id: str, token: str | None = None) -> dict[str, Any]:
362
  if not job_id:
363
  return {"error": "Missing job_id"}
 
8
  from .config import settings
9
  from .runs import make_run_id, utc_now_iso, validate_run_id
10
  from .worker_payload import (
 
 
 
 
 
11
  encoded_universal_model_card_worker_script,
12
+ encoded_validate_existing_space_worker_script,
 
13
  python_decode_and_run_command,
14
  )
15
 
 
55
  return payload
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
58
  def normalize_target_space(*, username: str, target_slug: str | None, run_id: str) -> str:
59
+ """Return `username/slug`, constrained to the signed-in user's namespace."""
60
  slug = (target_slug or "").strip()
61
  if not slug:
62
  slug = f"space-factory-{run_id}".lower()[:80]
 
63
  if "/" in slug:
64
  namespace, repo = slug.split("/", 1)
65
  if namespace != username:
66
+ raise ValueError("The target Space must be created in your own namespace.")
67
  slug = repo
68
  if not SPACE_SLUG_RE.match(slug):
69
  raise ValueError("Invalid target Space name. Use letters, numbers, dots, underscores, or dashes.")
70
  return f"{username}/{slug}"
71
 
72
 
73
+ def _clean_repo_id(value: str | None, *, repo_kind: str) -> str:
74
+ cleaned = (value or "").strip()
75
+ cleaned = cleaned.replace("https://huggingface.co/spaces/", "")
76
+ cleaned = cleaned.replace("https://huggingface.co/", "")
77
+ cleaned = cleaned.strip("/")
78
+ if "/" not in cleaned:
79
+ raise ValueError(f"{repo_kind} must look like owner/name or a Hugging Face URL.")
80
+ return cleaned
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
 
83
+ def launch_universal_model_card_job(
84
  *,
85
  token: str,
86
  username: str,
 
93
  implementation_mode: str | None = None,
94
  run_id: str | None = None,
95
  ) -> dict[str, Any]:
96
+ """Launch the public product builder: model card private Space attempt."""
97
  if not token:
98
  raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
99
+ safe_run_id = validate_run_id(run_id) if run_id else make_run_id("universal")
100
  target_space_id = normalize_target_space(username=username, target_slug=target_slug, run_id=safe_run_id)
101
+ clean_model_id = _clean_repo_id(model_id, repo_kind="Model ID")
 
 
102
 
103
  env = _base_env(
104
  run_id=safe_run_id,
105
  username=username,
106
+ worker_script_b64=encoded_universal_model_card_worker_script(),
107
  )
108
  env["TARGET_SPACE_ID"] = target_space_id
109
  env["MODEL_ID"] = clean_model_id
 
116
  return _job_result(
117
  job,
118
  run_id=safe_run_id,
119
+ kind="universal_model_card_builder",
120
  extra={
121
  "target_space": target_space_id,
122
  "target_space_url": f"https://huggingface.co/spaces/{target_space_id}",
 
130
  )
131
 
132
 
133
+ def launch_validate_existing_space_job(
134
  *,
135
  token: str,
136
  username: str,
137
+ target_space_id: str,
138
+ api_name: str | None = None,
139
+ test_args_json: str | None = None,
140
+ test_kwargs_json: str | None = None,
141
+ expected_output_type: str | None = None,
142
+ live_timeout_seconds: int = 1800,
 
143
  run_id: str | None = None,
144
  ) -> dict[str, Any]:
145
+ """Launch the public product validator for an existing generated Space."""
146
  if not token:
147
  raise ValueError("Missing OAuth token. Please sign in with Hugging Face first.")
148
+ safe_run_id = validate_run_id(run_id) if run_id else make_run_id("validate")
149
+ target = _clean_repo_id(target_space_id, repo_kind="Target Space")
150
+ namespace, _ = target.split("/", 1)
151
+ if namespace != username:
152
+ raise ValueError("For this version, target Space validation is limited to your own namespace.")
 
153
  env = _base_env(
154
  run_id=safe_run_id,
155
  username=username,
156
+ worker_script_b64=encoded_validate_existing_space_worker_script(),
157
  )
158
+ env["TARGET_SPACE_ID"] = target
159
+ env["API_NAME"] = (api_name or "/generate").strip()
160
+ env["TEST_ARGS_JSON"] = (test_args_json or '["a cinematic robot cat astronaut, detailed, studio lighting"]').strip()
161
+ env["TEST_KWARGS_JSON"] = (test_kwargs_json or "{}").strip()
162
+ env["EXPECTED_OUTPUT_TYPE"] = (expected_output_type or "image").strip()
163
+ env["LIVE_TIMEOUT_SECONDS"] = str(int(live_timeout_seconds or 1800))
 
164
  job = _launch_job(token=token, env=env, timeout="60m")
165
  return _job_result(
166
  job,
167
  run_id=safe_run_id,
168
+ kind="validate_existing_space",
169
  extra={
170
+ "target_space": target,
171
+ "target_space_url": f"https://huggingface.co/spaces/{target}",
172
+ "api_name": env["API_NAME"],
173
+ "expected_output_type": env["EXPECTED_OUTPUT_TYPE"],
174
+ "test_args_json": env["TEST_ARGS_JSON"],
175
+ "test_kwargs_json": env["TEST_KWARGS_JSON"],
 
 
176
  },
177
  )
178
 
179
+
180
  def inspect_job_safe(job_id: str, token: str | None = None) -> dict[str, Any]:
181
  if not job_id:
182
  return {"error": "Missing job_id"}
src/worker_payload.py CHANGED
The diff for this file is too large to render. See raw diff