Spaces:
Running
Running
Migrate upstream client to HuggingFace inference router
Browse filesSwitch config.py from the HY_API_KEY/HY_BASE_URL/HY_MODEL env vars to a
fixed HF router endpoint and a hardcoded model id (tencent/Hy3:deepinfra),
authenticating with HF_TOKEN. Stream-based chat path is unchanged.
Co-Authored-By: Claude <noreply@anthropic.com>
config.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
-
"""Runtime configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
Required:
|
| 4 |
-
|
| 5 |
-
HY_BASE_URL OpenAI-compatible base URL of the upstream endpoint.
|
| 6 |
-
HY_MODEL Model name passed to chat.completions.create.
|
| 7 |
|
| 8 |
Optional:
|
| 9 |
HY_LOG_LEVEL One of DEBUG / INFO / WARNING / ERROR (default: WARNING).
|
|
@@ -17,9 +19,7 @@ import os
|
|
| 17 |
from openai import OpenAI
|
| 18 |
|
| 19 |
|
| 20 |
-
|
| 21 |
-
BASE_URL = os.environ.get("HY_BASE_URL", "").strip()
|
| 22 |
-
MODEL = os.environ.get("HY_MODEL", "").strip()
|
| 23 |
LOG_LEVEL = os.environ.get("HY_LOG_LEVEL", "WARNING").upper()
|
| 24 |
|
| 25 |
|
|
@@ -37,34 +37,19 @@ if not logging.getLogger().handlers:
|
|
| 37 |
logger = logging.getLogger("Hy3")
|
| 38 |
|
| 39 |
|
| 40 |
-
# Surface missing
|
| 41 |
-
#
|
| 42 |
-
#
|
| 43 |
-
#
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
_missing = [
|
| 47 |
-
name
|
| 48 |
-
for name, val in (
|
| 49 |
-
("HY_API_KEY", API_KEY),
|
| 50 |
-
("HY_BASE_URL", BASE_URL),
|
| 51 |
-
("HY_MODEL", MODEL),
|
| 52 |
-
)
|
| 53 |
-
if not val
|
| 54 |
-
]
|
| 55 |
-
if _missing:
|
| 56 |
logger.warning(
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
-
"configured (e.g. as HuggingFace Space secrets / variables).",
|
| 60 |
-
", ".join(_missing),
|
| 61 |
)
|
| 62 |
|
| 63 |
|
| 64 |
-
# ``base_url=""`` would be passed through to httpx and produce undefined
|
| 65 |
-
# request URLs. ``None`` makes the OpenAI SDK fall back to its default
|
| 66 |
-
# (api.openai.com), which is at least a well-defined behaviour.
|
| 67 |
client = OpenAI(
|
| 68 |
-
|
| 69 |
-
|
| 70 |
)
|
|
|
|
| 1 |
+
"""Runtime configuration for the upstream OpenAI-compatible endpoint.
|
| 2 |
+
|
| 3 |
+
The model is served via the HuggingFace inference router. The only secret
|
| 4 |
+
we need is ``HF_TOKEN`` (a HuggingFace access token); the base URL and model
|
| 5 |
+
name are fixed.
|
| 6 |
|
| 7 |
Required:
|
| 8 |
+
HF_TOKEN HuggingFace access token (read access to the model).
|
|
|
|
|
|
|
| 9 |
|
| 10 |
Optional:
|
| 11 |
HY_LOG_LEVEL One of DEBUG / INFO / WARNING / ERROR (default: WARNING).
|
|
|
|
| 19 |
from openai import OpenAI
|
| 20 |
|
| 21 |
|
| 22 |
+
MODEL = "tencent/Hy3:deepinfra"
|
|
|
|
|
|
|
| 23 |
LOG_LEVEL = os.environ.get("HY_LOG_LEVEL", "WARNING").upper()
|
| 24 |
|
| 25 |
|
|
|
|
| 37 |
logger = logging.getLogger("Hy3")
|
| 38 |
|
| 39 |
|
| 40 |
+
# Surface a missing HF_TOKEN loudly at import time. We don't raise — that
|
| 41 |
+
# would prevent the Gradio server from even starting up (and HuggingFace
|
| 42 |
+
# Spaces would just show a blank build-failed page), making it harder to
|
| 43 |
+
# debug. Instead: warn now, and the per-request code paths will still fail
|
| 44 |
+
# cleanly with the upstream API's own 401 error message.
|
| 45 |
+
if not os.environ.get("HF_TOKEN", "").strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
logger.warning(
|
| 47 |
+
"HF_TOKEN is not set. The app will boot but every chat request will "
|
| 48 |
+
"fail until it is configured (e.g. as a HuggingFace Space secret)."
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
client = OpenAI(
|
| 53 |
+
base_url="https://router.huggingface.co/v1",
|
| 54 |
+
api_key=os.environ["HF_TOKEN"],
|
| 55 |
)
|