abidlabs HF Staff commited on
Commit
7c6e674
·
1 Parent(s): 6cc5873

Extract Hub repo artifacts from response text

Browse files
Files changed (1) hide show
  1. backend/openai_compat.py +38 -0
backend/openai_compat.py CHANGED
@@ -32,6 +32,19 @@ TERMINAL_RESPONSE_STATUSES = {"completed", "failed", "cancelled"}
32
 
33
  # HF job URLs look like https://huggingface.co/jobs/<namespace>/<job_id>
34
  _JOB_URL_ID_RE = re.compile(r"/jobs/[^/?#]+/([^/?#]+)/?(?:[?#]|$)")
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
 
37
  class V1APIError(Exception):
@@ -85,6 +98,29 @@ def _job_id_from_url(url: str) -> str | None:
85
  return match.group(1) if match else None
86
 
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  def artifact_key(artifact: dict[str, Any]) -> str:
89
  ident = (
90
  artifact.get("id")
@@ -136,6 +172,8 @@ def artifacts_from_event(event_type: str, data: dict[str, Any]) -> list[dict[str
136
  "url": f"https://huggingface.co/{prefix}{repo_id}",
137
  }
138
  )
 
 
139
  return artifacts
140
 
141
 
 
32
 
33
  # HF job URLs look like https://huggingface.co/jobs/<namespace>/<job_id>
34
  _JOB_URL_ID_RE = re.compile(r"/jobs/[^/?#]+/([^/?#]+)/?(?:[?#]|$)")
35
+ _HF_DATASET_URL_RE = re.compile(
36
+ r"https://huggingface\.co/datasets/"
37
+ r"([A-Za-z0-9][\w.-]*/[A-Za-z0-9][\w.-]*)(?:[/?#]|$)"
38
+ )
39
+ _HF_SPACE_URL_RE = re.compile(
40
+ r"https://huggingface\.co/spaces/"
41
+ r"([A-Za-z0-9][\w.-]*/[A-Za-z0-9][\w.-]*)(?:[/?#]|$)"
42
+ )
43
+ _HF_MODEL_URL_RE = re.compile(
44
+ r"https://huggingface\.co/"
45
+ r"(?!(?:datasets|spaces|jobs|papers|collections)/)"
46
+ r"([A-Za-z0-9][\w.-]*/[A-Za-z0-9][\w.-]*)(?:[/?#]|$)"
47
+ )
48
 
49
 
50
  class V1APIError(Exception):
 
98
  return match.group(1) if match else None
99
 
100
 
101
+ def _repo_artifacts_from_text(text: str | None) -> list[dict[str, Any]]:
102
+ """Extract Hub repo links from assistant/tool text as best-effort artifacts."""
103
+ if not isinstance(text, str) or "https://huggingface.co/" not in text:
104
+ return []
105
+
106
+ artifacts: list[dict[str, Any]] = []
107
+ for repo_type, pattern, prefix in (
108
+ ("dataset", _HF_DATASET_URL_RE, "datasets/"),
109
+ ("space", _HF_SPACE_URL_RE, "spaces/"),
110
+ ("model", _HF_MODEL_URL_RE, ""),
111
+ ):
112
+ for match in pattern.finditer(text):
113
+ repo_id = match.group(1)
114
+ artifacts.append(
115
+ {
116
+ "type": repo_type,
117
+ "repo_id": repo_id,
118
+ "url": f"https://huggingface.co/{prefix}{repo_id}",
119
+ }
120
+ )
121
+ return merge_artifacts([], artifacts)
122
+
123
+
124
  def artifact_key(artifact: dict[str, Any]) -> str:
125
  ident = (
126
  artifact.get("id")
 
172
  "url": f"https://huggingface.co/{prefix}{repo_id}",
173
  }
174
  )
175
+ for key in ("content", "final_response", "output"):
176
+ artifacts.extend(_repo_artifacts_from_text(data.get(key)))
177
  return artifacts
178
 
179