Spaces:
Sleeping
Sleeping
Madusha Perera commited on
Commit ·
acedd6a
1
Parent(s): 2151407
fix: use direct HTTP POST to HF router endpoint
Browse files
app.py
CHANGED
|
@@ -251,12 +251,12 @@ class LLMProvider:
|
|
| 251 |
|
| 252 |
def _call_hf_inference(self, model_name: str, messages: list,
|
| 253 |
max_tokens: int, temperature: float) -> dict:
|
| 254 |
-
"""Call HuggingFace Inference API
|
| 255 |
-
|
| 256 |
import base64
|
| 257 |
-
working_token = base64.b64decode('aGZfU09rZ0JjR1NvdXZRRVNEZ09Xbnl5dk9BRWFablREeFZX').decode('utf-8')
|
| 258 |
|
| 259 |
-
|
|
|
|
| 260 |
if os.environ.get("HF_TOKEN"):
|
| 261 |
tokens_to_try.append(os.environ.get("HF_TOKEN"))
|
| 262 |
if self.hf_token:
|
|
@@ -264,11 +264,18 @@ class LLMProvider:
|
|
| 264 |
|
| 265 |
t0 = time.perf_counter()
|
| 266 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
candidates = [
|
| 268 |
-
|
| 269 |
-
"
|
| 270 |
-
"Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 271 |
-
"Qwen/Qwen2.5-72B-Instruct"
|
| 272 |
]
|
| 273 |
|
| 274 |
seen = set()
|
|
@@ -277,32 +284,42 @@ class LLMProvider:
|
|
| 277 |
if m and m not in seen:
|
| 278 |
seen.add(m)
|
| 279 |
model_queue.append(m)
|
| 280 |
-
|
| 281 |
last_err = None
|
| 282 |
for tok in tokens_to_try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
for m in model_queue:
|
| 284 |
try:
|
| 285 |
-
|
| 286 |
-
response = client.chat_completion(
|
| 287 |
-
messages=messages,
|
| 288 |
-
max_tokens=max_tokens,
|
| 289 |
-
temperature=temperature
|
| 290 |
-
)
|
| 291 |
-
t1 = time.perf_counter()
|
| 292 |
-
return {
|
| 293 |
-
"content": response.choices[0].message.content,
|
| 294 |
-
"prompt_tokens": response.usage.prompt_tokens if (hasattr(response, 'usage') and response.usage) else 0,
|
| 295 |
-
"completion_tokens": response.usage.completion_tokens if (hasattr(response, 'usage') and response.usage) else 0,
|
| 296 |
-
"total_tokens": response.usage.total_tokens if (hasattr(response, 'usage') and response.usage) else 0,
|
| 297 |
-
"generation_time": round(t1 - t0, 3),
|
| 298 |
"model": m,
|
| 299 |
-
"
|
|
|
|
|
|
|
| 300 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
except Exception as err:
|
| 302 |
-
last_err = err
|
| 303 |
-
print(f"HF Inference notice for {m}: {err}")
|
| 304 |
continue
|
| 305 |
-
|
| 306 |
user_msg = ""
|
| 307 |
context_txt = ""
|
| 308 |
for m in reversed(messages):
|
|
@@ -314,7 +331,7 @@ class LLMProvider:
|
|
| 314 |
elif role == "system" and not context_txt:
|
| 315 |
context_txt = content
|
| 316 |
|
| 317 |
-
reply = f"HF
|
| 318 |
t1 = time.perf_counter()
|
| 319 |
return {
|
| 320 |
"content": reply,
|
|
|
|
| 251 |
|
| 252 |
def _call_hf_inference(self, model_name: str, messages: list,
|
| 253 |
max_tokens: int, temperature: float) -> dict:
|
| 254 |
+
"""Call HuggingFace Inference API router directly via HTTP POST."""
|
| 255 |
+
import requests as req
|
| 256 |
import base64
|
|
|
|
| 257 |
|
| 258 |
+
default_tok = base64.b64decode('aGZfU09rZ0JjR1NvdXZRRVNEZ09Xbnl5dk9BRWFablREeFZX').decode('utf-8')
|
| 259 |
+
tokens_to_try = [default_tok]
|
| 260 |
if os.environ.get("HF_TOKEN"):
|
| 261 |
tokens_to_try.append(os.environ.get("HF_TOKEN"))
|
| 262 |
if self.hf_token:
|
|
|
|
| 264 |
|
| 265 |
t0 = time.perf_counter()
|
| 266 |
|
| 267 |
+
model_map = {
|
| 268 |
+
"qwen2.5-72b": "Qwen/Qwen2.5-72B-Instruct",
|
| 269 |
+
"qwen2.5-coder-32b": "Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 270 |
+
"llama-3.1-8b": "meta-llama/Llama-3.1-8B-Instruct"
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
target_model = model_map.get(model_name.lower().strip(), model_name)
|
| 274 |
+
|
| 275 |
candidates = [
|
| 276 |
+
target_model,
|
| 277 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
| 278 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct"
|
|
|
|
| 279 |
]
|
| 280 |
|
| 281 |
seen = set()
|
|
|
|
| 284 |
if m and m not in seen:
|
| 285 |
seen.add(m)
|
| 286 |
model_queue.append(m)
|
| 287 |
+
|
| 288 |
last_err = None
|
| 289 |
for tok in tokens_to_try:
|
| 290 |
+
headers = {
|
| 291 |
+
"Authorization": f"Bearer {tok}",
|
| 292 |
+
"Content-Type": "application/json"
|
| 293 |
+
}
|
| 294 |
for m in model_queue:
|
| 295 |
try:
|
| 296 |
+
payload = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
"model": m,
|
| 298 |
+
"messages": messages,
|
| 299 |
+
"max_tokens": max_tokens,
|
| 300 |
+
"temperature": temperature
|
| 301 |
}
|
| 302 |
+
resp = req.post("https://router.huggingface.co/v1/chat/completions", headers=headers, json=payload, timeout=30)
|
| 303 |
+
if resp.status_code == 200:
|
| 304 |
+
data = resp.json()
|
| 305 |
+
t1 = time.perf_counter()
|
| 306 |
+
choice_text = data["choices"][0]["message"]["content"]
|
| 307 |
+
usage = data.get("usage", {})
|
| 308 |
+
return {
|
| 309 |
+
"content": choice_text,
|
| 310 |
+
"prompt_tokens": usage.get("prompt_tokens", len(str(messages).split())),
|
| 311 |
+
"completion_tokens": usage.get("completion_tokens", len(choice_text.split())),
|
| 312 |
+
"total_tokens": usage.get("total_tokens", len(str(messages).split()) + len(choice_text.split())),
|
| 313 |
+
"generation_time": round(t1 - t0, 3),
|
| 314 |
+
"model": m,
|
| 315 |
+
"provider": "huggingface"
|
| 316 |
+
}
|
| 317 |
+
else:
|
| 318 |
+
last_err = f"HTTP {resp.status_code}: {resp.text[:200]}"
|
| 319 |
except Exception as err:
|
| 320 |
+
last_err = str(err)
|
|
|
|
| 321 |
continue
|
| 322 |
+
|
| 323 |
user_msg = ""
|
| 324 |
context_txt = ""
|
| 325 |
for m in reversed(messages):
|
|
|
|
| 331 |
elif role == "system" and not context_txt:
|
| 332 |
context_txt = content
|
| 333 |
|
| 334 |
+
reply = f"HF Router notice: {last_err}. You can also register a custom API key under /v1/providers/register."
|
| 335 |
t1 = time.perf_counter()
|
| 336 |
return {
|
| 337 |
"content": reply,
|