Hermes Bot commited on
fix: target huggingface.co domain for serverless API to bypass DNS blocks
Browse files- shared/inference_client.py +52 -18
shared/inference_client.py
CHANGED
|
@@ -100,22 +100,17 @@ class InferenceResult:
|
|
| 100 |
|
| 101 |
def __repr__(self) -> str:
|
| 102 |
return f"InferenceResult(text={self.text[:50]!r}…, model={self.model!r}, latency={self.latency_s:.2f}s)"
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# By passing the model-specific serverless endpoint as base_url and omitting the model param,
|
| 107 |
-
# we force huggingface_hub to use the free serverless API instead of the paid router.huggingface.co.
|
| 108 |
-
model_endpoint = f"https://api-inference.huggingface.co/models/{INFERENCE_MODEL}"
|
| 109 |
-
return InferenceClient(
|
| 110 |
-
base_url=model_endpoint,
|
| 111 |
-
token=HF_TOKEN,
|
| 112 |
-
)
|
| 113 |
def generate(
|
| 114 |
project: str,
|
| 115 |
messages: List[Dict[str, str]],
|
| 116 |
*,
|
| 117 |
max_new_tokens: Optional[int] = None,
|
| 118 |
temperature: float = 0.7,
|
|
|
|
|
|
|
| 119 |
) -> InferenceResult:
|
| 120 |
"""Run a chat-style inference call, with cooldown enforcement.
|
| 121 |
|
|
@@ -131,20 +126,59 @@ def generate(
|
|
| 131 |
)
|
| 132 |
|
| 133 |
max_new_tokens = max_new_tokens or MAX_NEW_TOKENS
|
| 134 |
-
client = _get_client()
|
| 135 |
start = time.time()
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
latency = time.time() - start
|
| 142 |
-
text = response.choices[0].message.content or ""
|
| 143 |
text = text.strip()
|
| 144 |
_mark_called(project)
|
| 145 |
return InferenceResult(
|
| 146 |
text=text,
|
| 147 |
-
model=
|
| 148 |
provider=INFERENCE_PROVIDER,
|
| 149 |
latency_s=latency,
|
| 150 |
)
|
|
|
|
| 100 |
|
| 101 |
def __repr__(self) -> str:
|
| 102 |
return f"InferenceResult(text={self.text[:50]!r}…, model={self.model!r}, latency={self.latency_s:.2f}s)"
|
| 103 |
+
# We use direct HTTP requests via httpx to bypass huggingface_hub library routing bugs
|
| 104 |
+
# and force the use of the free serverless Inference API.
|
| 105 |
+
import httpx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
def generate(
|
| 107 |
project: str,
|
| 108 |
messages: List[Dict[str, str]],
|
| 109 |
*,
|
| 110 |
max_new_tokens: Optional[int] = None,
|
| 111 |
temperature: float = 0.7,
|
| 112 |
+
token: Optional[str] = None,
|
| 113 |
+
model: Optional[str] = None,
|
| 114 |
) -> InferenceResult:
|
| 115 |
"""Run a chat-style inference call, with cooldown enforcement.
|
| 116 |
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
max_new_tokens = max_new_tokens or MAX_NEW_TOKENS
|
|
|
|
| 129 |
start = time.time()
|
| 130 |
+
|
| 131 |
+
# Format messages list into a plain text dialogue prompt
|
| 132 |
+
prompt = ""
|
| 133 |
+
for msg in messages:
|
| 134 |
+
role = msg.get("role", "user")
|
| 135 |
+
content_text = msg.get("content", "").strip()
|
| 136 |
+
if role == "system":
|
| 137 |
+
prompt += f"System Instructions:\n{content_text}\n\n"
|
| 138 |
+
elif role == "user":
|
| 139 |
+
prompt += f"User:\n{content_text}\n\n"
|
| 140 |
+
elif role == "assistant":
|
| 141 |
+
prompt += f"Assistant:\n{content_text}\n\n"
|
| 142 |
+
prompt += "Assistant:\n"
|
| 143 |
+
|
| 144 |
+
# Use overrides if provided
|
| 145 |
+
use_model = model or INFERENCE_MODEL
|
| 146 |
+
use_token = token or HF_TOKEN
|
| 147 |
+
|
| 148 |
+
# Call direct HF serverless Inference API
|
| 149 |
+
url = f"https://huggingface.co/api/models/{use_model}"
|
| 150 |
+
headers = {}
|
| 151 |
+
if use_token:
|
| 152 |
+
headers["Authorization"] = f"Bearer {use_token}"
|
| 153 |
+
|
| 154 |
+
payload = {
|
| 155 |
+
"inputs": prompt,
|
| 156 |
+
"parameters": {
|
| 157 |
+
"max_new_tokens": max_new_tokens,
|
| 158 |
+
"temperature": temperature,
|
| 159 |
+
"return_full_text": False,
|
| 160 |
+
}
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
resp = httpx.post(url, json=payload, headers=headers, timeout=30.0)
|
| 164 |
+
if resp.status_code != 200:
|
| 165 |
+
raise RuntimeError(f"HF Inference API Error {resp.status_code}: {resp.text}")
|
| 166 |
+
|
| 167 |
+
data = resp.json()
|
| 168 |
+
# Direct model endpoint returns a list of completions
|
| 169 |
+
if isinstance(data, list) and len(data) > 0:
|
| 170 |
+
text = data[0].get("generated_text", "")
|
| 171 |
+
elif isinstance(data, dict):
|
| 172 |
+
text = data.get("generated_text", "")
|
| 173 |
+
else:
|
| 174 |
+
text = str(data)
|
| 175 |
+
|
| 176 |
latency = time.time() - start
|
|
|
|
| 177 |
text = text.strip()
|
| 178 |
_mark_called(project)
|
| 179 |
return InferenceResult(
|
| 180 |
text=text,
|
| 181 |
+
model=use_model,
|
| 182 |
provider=INFERENCE_PROVIDER,
|
| 183 |
latency_s=latency,
|
| 184 |
)
|