Files changed (1) hide show
  1. app.py +29 -3
app.py CHANGED
@@ -4,6 +4,20 @@ import requests
4
  import inspect
5
  import pandas as pd
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -12,13 +26,25 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
  class BasicAgent:
14
  def __init__(self):
 
 
 
 
 
 
 
 
 
 
15
  print("BasicAgent initialized.")
16
  def __call__(self, question: str) -> str:
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
 
19
  print(f"Agent returning fixed answer: {fixed_answer}")
20
  return fixed_answer
21
 
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
24
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -81,6 +107,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
81
  continue
82
  try:
83
  submitted_answer = agent(question_text)
 
 
84
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
86
  except Exception as e:
@@ -146,11 +174,9 @@ with gr.Blocks() as demo:
146
  gr.Markdown(
147
  """
148
  **Instructions:**
149
-
150
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
151
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
152
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
153
-
154
  ---
155
  **Disclaimers:**
156
  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).
 
4
  import inspect
5
  import pandas as pd
6
 
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
+ import os
9
+
10
+ try:
11
+ from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool, InferenceClientModel
12
+ except ImportError as e:
13
+ from smolagents import OpenAIServerModel, CodeAgent, WikipediaSearchTool, InferenceClientModel
14
+ DuckDuckGoSearchTool = None
15
+ print(f"Warning: DuckDuckGoSearchTool unavailable ({e}). Install ddgs with `pip install ddgs`.")
16
+
17
+ api_key = os.getenv("HF_TOKEN")
18
+
19
+
20
+
21
  # (Keep Constants as is)
22
  # --- Constants ---
23
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
26
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
27
  class BasicAgent:
28
  def __init__(self):
29
+ openAI_model = OpenAIServerModel(model_id="gpt-4.1-mini", api_key="sk-proj--7IsHGYQG_dTB1Q9_wA")
30
+ tools = [WikipediaSearchTool()]
31
+ if DuckDuckGoSearchTool is not None:
32
+ tools.insert(0, DuckDuckGoSearchTool())
33
+
34
+ self.agent = CodeAgent(
35
+ model = openAI_model,
36
+ tools = tools,
37
+ add_base_tools = True,
38
+ )
39
  print("BasicAgent initialized.")
40
  def __call__(self, question: str) -> str:
41
  print(f"Agent received question (first 50 chars): {question[:50]}...")
42
+ # fixed_answer = "This is a default answer."
43
+ fixed_answer = self.agent.run(question)
44
  print(f"Agent returning fixed answer: {fixed_answer}")
45
  return fixed_answer
46
 
47
+
48
  def run_and_submit_all( profile: gr.OAuthProfile | None):
49
  """
50
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
107
  continue
108
  try:
109
  submitted_answer = agent(question_text)
110
+ if not isinstance(submitted_answer, str):
111
+ submitted_answer = str(submitted_answer)
112
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
113
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
114
  except Exception as e:
 
174
  gr.Markdown(
175
  """
176
  **Instructions:**
 
177
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
178
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
179
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
180
  ---
181
  **Disclaimers:**
182
  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).