maodd commited on
Commit
4b6e05e
·
verified ·
1 Parent(s): d1c8c67

Switch agent backend from Anthropic to HF Inference Providers

Browse files

Uses smolagents InferenceClientModel with the logged-in user's own OAuth token (inference-api scope) instead of an Anthropic API key/Space secret.

Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -8,7 +8,7 @@ import spaces
8
 
9
  from smolagents import (
10
  CodeAgent,
11
- LiteLLMModel,
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 (Claude 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,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
- model_id = os.getenv("AGENT_MODEL_ID", "anthropic/claude-sonnet-4-5")
45
- api_key = os.getenv("ANTHROPIC_API_KEY")
46
- if not api_key:
47
- print("Warning: ANTHROPIC_API_KEY is not set - the agent will fail to call the model.")
48
-
49
- self.model = LiteLLMModel(model_id=model_id, api_key=api_key, temperature=0)
 
 
 
 
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 Anthropic's Claude via `smolagents`. Set the `ANTHROPIC_API_KEY` secret in this Space's settings before running.
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