Update hf_client.py
Browse files- hf_client.py +48 -15
hf_client.py
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
from typing import Optional
|
|
@@ -6,39 +14,64 @@ from typing import Optional
|
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
from tavily import TavilyClient
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 12 |
|
| 13 |
-
def get_inference_client(model_id: str, provider: str = "auto", user_token: Optional[str] = None):
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
# Determine which token to use for the API call
|
| 20 |
token_to_use = user_token or HF_TOKEN
|
| 21 |
-
|
|
|
|
| 22 |
if not token_to_use:
|
| 23 |
-
raise ValueError(
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
if model_id == "moonshotai/Kimi-K2-Instruct":
|
| 26 |
provider = "groq"
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
# The Hugging Face Hub automatically bills the account associated with the
|
| 30 |
-
#
|
| 31 |
return InferenceClient(
|
| 32 |
provider=provider,
|
| 33 |
api_key=token_to_use
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
TAVILY_API_KEY = os.getenv('TAVILY_API_KEY')
|
| 38 |
tavily_client = None
|
|
|
|
| 39 |
if TAVILY_API_KEY:
|
| 40 |
try:
|
| 41 |
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
| 42 |
except Exception as e:
|
| 43 |
-
|
|
|
|
| 44 |
tavily_client = None
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This module handles the creation of API clients for the application.
|
| 3 |
+
It includes the logic for instantiating the Hugging Face InferenceClient
|
| 4 |
+
and the Tavily Search client.
|
| 5 |
+
|
| 6 |
+
The get_inference_client function is critical for enabling the "user-pays"
|
| 7 |
+
model in a Hugging Face Space. It prioritizes the API token of the logged-in
|
| 8 |
+
user, ensuring their account is billed for inference costs.
|
| 9 |
+
"""
|
| 10 |
|
| 11 |
import os
|
| 12 |
from typing import Optional
|
|
|
|
| 14 |
from huggingface_hub import InferenceClient
|
| 15 |
from tavily import TavilyClient
|
| 16 |
|
| 17 |
+
# --- Hugging Face Inference Client ---
|
| 18 |
+
|
| 19 |
+
# This is the Space owner's token, loaded from environment secrets.
|
| 20 |
+
# It serves as a fallback for local development or when a user-provided token is not available.
|
| 21 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 22 |
|
| 23 |
+
def get_inference_client(model_id: str, provider: str = "auto", user_token: Optional[str] = None) -> InferenceClient:
|
| 24 |
"""
|
| 25 |
+
Creates and returns a Hugging Face InferenceClient.
|
| 26 |
+
|
| 27 |
+
This function implements the "user-pays" logic. It prioritizes using the token
|
| 28 |
+
provided by the logged-in user (`user_token`). If that is not available,
|
| 29 |
+
it falls back to the Space owner's token (`HF_TOKEN`).
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
model_id (str): The ID of the model to be used (e.g., "mistralai/Mistral-7B-Instruct-v0.2").
|
| 33 |
+
provider (str): The specific inference provider (e.g., "groq"). Defaults to "auto".
|
| 34 |
+
user_token (Optional[str]): The API token of the logged-in user, passed from the Gradio app.
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
InferenceClient: An initialized client ready for making API calls.
|
| 38 |
+
|
| 39 |
+
Raises:
|
| 40 |
+
ValueError: If no API token can be found (neither from the user nor the environment).
|
| 41 |
"""
|
| 42 |
+
# 1. Determine which token to use for the API call. The user's token takes precedence.
|
| 43 |
token_to_use = user_token or HF_TOKEN
|
| 44 |
+
|
| 45 |
+
# 2. Validate that we have a token. If not, the application cannot make API calls.
|
| 46 |
if not token_to_use:
|
| 47 |
+
raise ValueError(
|
| 48 |
+
"Cannot proceed without an API token. "
|
| 49 |
+
"Please log into Hugging Face, or ensure the HF_TOKEN environment secret is set for this Space."
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
# 3. Handle any model-specific provider logic.
|
| 53 |
if model_id == "moonshotai/Kimi-K2-Instruct":
|
| 54 |
provider = "groq"
|
| 55 |
|
| 56 |
+
# 4. Instantiate and return the client.
|
| 57 |
+
# The Hugging Face Hub automatically bills the account associated with the provided `api_key`.
|
| 58 |
+
# The `bill_to` parameter is NOT needed or used for this user-pays scenario.
|
| 59 |
return InferenceClient(
|
| 60 |
provider=provider,
|
| 61 |
api_key=token_to_use
|
| 62 |
)
|
| 63 |
|
| 64 |
+
|
| 65 |
+
# --- Tavily Search Client ---
|
| 66 |
+
|
| 67 |
+
# This client uses the Space owner's TAVILY_API_KEY, as this is a backend service.
|
| 68 |
TAVILY_API_KEY = os.getenv('TAVILY_API_KEY')
|
| 69 |
tavily_client = None
|
| 70 |
+
|
| 71 |
if TAVILY_API_KEY:
|
| 72 |
try:
|
| 73 |
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
| 74 |
except Exception as e:
|
| 75 |
+
# Log an error if the client fails to initialize, but don't crash the app.
|
| 76 |
+
print(f"Warning: Failed to initialize Tavily client. Web search will be unavailable. Error: {e}")
|
| 77 |
tavily_client = None
|