Spaces:
Sleeping
Sleeping
Switch agent backend from Anthropic to HF Inference Providers
Browse filesUses smolagents InferenceClientModel with the logged-in user's own OAuth token (inference-api scope) instead of an Anthropic API key/Space secret.
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import spaces
|
|
| 8 |
|
| 9 |
from smolagents import (
|
| 10 |
CodeAgent,
|
| 11 |
-
|
| 12 |
WebSearchTool,
|
| 13 |
VisitWebpageTool,
|
| 14 |
WikipediaSearchTool,
|
|
@@ -22,7 +22,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 22 |
@spaces.GPU
|
| 23 |
def _zerogpu_startup_check():
|
| 24 |
# This Space runs on ZeroGPU hardware but the agent below only makes
|
| 25 |
-
# network calls (
|
| 26 |
# ZeroGPU requires at least one @spaces.GPU function to be declared,
|
| 27 |
# so this no-op satisfies that check without spending any GPU quota
|
| 28 |
# (it is never actually invoked).
|
|
@@ -40,13 +40,17 @@ If you are asked for a comma separated list, apply the above rules to each eleme
|
|
| 40 |
# --- Basic Agent Definition ---
|
| 41 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 42 |
class BasicAgent:
|
| 43 |
-
def __init__(self):
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
self.agent = CodeAgent(
|
| 51 |
model=self.model,
|
| 52 |
tools=[WebSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
|
|
@@ -76,7 +80,7 @@ class BasicAgent:
|
|
| 76 |
print(f"Agent returning answer: {answer}")
|
| 77 |
return answer
|
| 78 |
|
| 79 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 80 |
"""
|
| 81 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 82 |
and displays the results.
|
|
@@ -97,7 +101,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 97 |
|
| 98 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 99 |
try:
|
| 100 |
-
agent = BasicAgent()
|
| 101 |
except Exception as e:
|
| 102 |
print(f"Error instantiating agent: {e}")
|
| 103 |
return f"Error initializing agent: {e}", None
|
|
@@ -228,7 +232,7 @@ with gr.Blocks() as demo:
|
|
| 228 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 229 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 230 |
|
| 231 |
-
**Setup:** This agent calls
|
| 232 |
"""
|
| 233 |
)
|
| 234 |
|
|
|
|
| 8 |
|
| 9 |
from smolagents import (
|
| 10 |
CodeAgent,
|
| 11 |
+
InferenceClientModel,
|
| 12 |
WebSearchTool,
|
| 13 |
VisitWebpageTool,
|
| 14 |
WikipediaSearchTool,
|
|
|
|
| 22 |
@spaces.GPU
|
| 23 |
def _zerogpu_startup_check():
|
| 24 |
# This Space runs on ZeroGPU hardware but the agent below only makes
|
| 25 |
+
# network calls (HF Inference API, web search) and never touches CUDA.
|
| 26 |
# ZeroGPU requires at least one @spaces.GPU function to be declared,
|
| 27 |
# so this no-op satisfies that check without spending any GPU quota
|
| 28 |
# (it is never actually invoked).
|
|
|
|
| 40 |
# --- Basic Agent Definition ---
|
| 41 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 42 |
class BasicAgent:
|
| 43 |
+
def __init__(self, hf_token: str | None = None):
|
| 44 |
+
# Uses the logged-in user's own HF Inference Providers quota (via the
|
| 45 |
+
# "Sign in with Hugging Face" OAuth token, scope: inference-api).
|
| 46 |
+
# Falls back to the HF_TOKEN env var (e.g. for local testing) if no
|
| 47 |
+
# OAuth token is available.
|
| 48 |
+
model_kwargs = {"token": hf_token or os.getenv("HF_TOKEN"), "temperature": 0}
|
| 49 |
+
model_id = os.getenv("AGENT_MODEL_ID")
|
| 50 |
+
if model_id:
|
| 51 |
+
model_kwargs["model_id"] = model_id
|
| 52 |
+
|
| 53 |
+
self.model = InferenceClientModel(**model_kwargs)
|
| 54 |
self.agent = CodeAgent(
|
| 55 |
model=self.model,
|
| 56 |
tools=[WebSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
|
|
|
|
| 80 |
print(f"Agent returning answer: {answer}")
|
| 81 |
return answer
|
| 82 |
|
| 83 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None):
|
| 84 |
"""
|
| 85 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 86 |
and displays the results.
|
|
|
|
| 101 |
|
| 102 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 103 |
try:
|
| 104 |
+
agent = BasicAgent(hf_token=oauth_token.token if oauth_token else None)
|
| 105 |
except Exception as e:
|
| 106 |
print(f"Error instantiating agent: {e}")
|
| 107 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 232 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 233 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 234 |
|
| 235 |
+
**Setup:** This agent calls Hugging Face's Inference Providers via `smolagents`, using your own account's quota through the login below (no separate API key needed). No hardware/model access beyond your normal HF account is required.
|
| 236 |
"""
|
| 237 |
)
|
| 238 |
|