fffiloni commited on
Commit
231c9f3
·
verified ·
1 Parent(s): 690ac59

Upload 6 files

Browse files
Files changed (1) hide show
  1. app.py +23 -15
app.py CHANGED
@@ -1,6 +1,7 @@
1
  from __future__ import annotations
2
 
3
  import json
 
4
  from pathlib import Path
5
  from typing import Any
6
 
@@ -143,22 +144,29 @@ def _event_key(event: dict[str, Any]) -> tuple[str, str, str, str]:
143
  )
144
 
145
 
 
 
 
146
  def _events_from_job_logs(log_text: str) -> list[dict[str, Any]]:
147
- """Extract worker append_event JSON lines from HF Job logs."""
 
 
 
 
 
 
148
  events: list[dict[str, Any]] = []
 
149
  for raw in (log_text or "").splitlines():
150
- line = raw.strip()
151
- start = line.find("{")
152
- if start > 0:
153
- line = line[start:]
154
- if not line.startswith("{"):
155
- continue
156
- try:
157
- payload = json.loads(line)
158
- except Exception:
159
- continue
160
- if isinstance(payload, dict) and payload.get("step") and payload.get("status"):
161
- events.append(payload)
162
  return events
163
 
164
 
@@ -213,7 +221,7 @@ def register_custom_routes(fastapi_app: FastAPI) -> None:
213
  return JSONResponse(
214
  {
215
  "name": "Agentic Space Factory",
216
- "version": "v66-clean-qwen-coder-path",
217
  "bucket_default": settings.bucket_name,
218
  "workflows": ["build_from_model_card", "validate_existing_space", "runs_explorer"],
219
  "custom_ui_status": "root_custom_ui",
@@ -504,7 +512,7 @@ def register_custom_routes(fastapi_app: FastAPI) -> None:
504
  if job_id:
505
  job_info = inspect_job_safe(job_id, token=ctx["token"])
506
  if include_job_logs:
507
- job_logs = fetch_recent_logs_safe(job_id, token=ctx["token"], max_lines=220)
508
  job_log_events = _events_from_job_logs(job_logs)
509
  job_stage = _normalize_job_stage(job_info.get("stage"))
510
  effective_state = {**launch, **state}
 
1
  from __future__ import annotations
2
 
3
  import json
4
+ import re
5
  from pathlib import Path
6
  from typing import Any
7
 
 
144
  )
145
 
146
 
147
+ ANSI_RE = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")
148
+
149
+
150
  def _events_from_job_logs(log_text: str) -> list[dict[str, Any]]:
151
+ """Extract worker append_event JSON objects from HF Job logs.
152
+
153
+ HF Job logs may prefix stdout lines with timestamps, may include ANSI
154
+ sequences, and may sometimes have text after a JSON object. Use
155
+ JSONDecoder.raw_decode from each opening brace instead of json.loads(line)
156
+ so event extraction is not fragile.
157
+ """
158
  events: list[dict[str, Any]] = []
159
+ decoder = json.JSONDecoder()
160
  for raw in (log_text or "").splitlines():
161
+ line = ANSI_RE.sub("", raw).strip()
162
+ for match in re.finditer(r"\{", line):
163
+ try:
164
+ payload, _ = decoder.raw_decode(line[match.start():])
165
+ except Exception:
166
+ continue
167
+ if isinstance(payload, dict) and payload.get("step") and payload.get("status"):
168
+ events.append(payload)
169
+ break
 
 
 
170
  return events
171
 
172
 
 
221
  return JSONResponse(
222
  {
223
  "name": "Agentic Space Factory",
224
+ "version": "v67-monotonic-progress-events",
225
  "bucket_default": settings.bucket_name,
226
  "workflows": ["build_from_model_card", "validate_existing_space", "runs_explorer"],
227
  "custom_ui_status": "root_custom_ui",
 
512
  if job_id:
513
  job_info = inspect_job_safe(job_id, token=ctx["token"])
514
  if include_job_logs:
515
+ job_logs = fetch_recent_logs_safe(job_id, token=ctx["token"], max_lines=1000)
516
  job_log_events = _events_from_job_logs(job_logs)
517
  job_stage = _normalize_job_stage(job_info.get("stage"))
518
  effective_state = {**launch, **state}