YI Zhongyue commited on
Commit
7194b60
·
1 Parent(s): d142da5
Files changed (7) hide show
  1. .env.example +2 -0
  2. .gitignore +1 -0
  3. agent.py +51 -0
  4. app.py +4 -13
  5. prompt.py +39 -0
  6. pyproject.toml +1 -0
  7. uv.lock +45 -17
.env.example ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ OPENAI_API_KEY=
2
+ ANTHROPIC_API_KEY=
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
agent.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pathlib
3
+
4
+ import dotenv
5
+ from smolagents import (
6
+ CodeAgent,
7
+ OpenAIServerModel,
8
+ VisitWebpageTool,
9
+ WebSearchTool,
10
+ WikipediaSearchTool,
11
+ )
12
+
13
+ if pathlib.Path(".env").exists():
14
+ dotenv.load_dotenv(".env")
15
+
16
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", None)
17
+ ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY", None)
18
+
19
+ if not OPENAI_API_KEY and not ANTHROPIC_API_KEY:
20
+ raise ValueError(
21
+ "Please set either OPENAI_API_KEY or ANTHROPIC_API_KEY in your .env file or Huggingface Space settings."
22
+ )
23
+
24
+ model = OpenAIServerModel(
25
+ model_id="gpt-4.1",
26
+ api_key=OPENAI_API_KEY,
27
+ )
28
+
29
+ web_search_agent = CodeAgent(
30
+ model=model,
31
+ tools=[
32
+ WebSearchTool(),
33
+ VisitWebpageTool(),
34
+ WikipediaSearchTool(),
35
+ ],
36
+ )
37
+
38
+ calc_agent = CodeAgent(
39
+ model=model,
40
+ additional_authorized_imports=["pandas", "numpy"]
41
+ )
42
+
43
+ class Agent:
44
+ def __init__(self):
45
+ print("Agent initialized.")
46
+
47
+ def __call__(self, question: str) -> str:
48
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
49
+ fixed_answer = "This is a default answer."
50
+ print(f"Agent returning fixed answer: {fixed_answer}")
51
+ return fixed_answer
app.py CHANGED
@@ -5,6 +5,8 @@ import gradio as gr
5
  import pandas as pd
6
  import requests
7
 
 
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -12,20 +14,9 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
- class BasicAgent:
16
- def __init__(self):
17
- print("BasicAgent initialized.")
18
-
19
- def __call__(self, question: str) -> str:
20
- print(f"Agent received question (first 50 chars): {question[:50]}...")
21
- fixed_answer = "This is a default answer."
22
- print(f"Agent returning fixed answer: {fixed_answer}")
23
- return fixed_answer
24
-
25
-
26
  def run_and_submit_all(profile: gr.OAuthProfile | None):
27
  """
28
- Fetches all questions, runs the BasicAgent on them, submits all answers,
29
  and displays the results.
30
  """
31
  # --- Determine HF Space Runtime URL and Repo URL ---
@@ -44,7 +35,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
44
 
45
  # 1. Instantiate Agent ( modify this part to create your agent)
46
  try:
47
- agent = BasicAgent()
48
  except Exception as e:
49
  print(f"Error instantiating agent: {e}")
50
  return f"Error initializing agent: {e}", None
 
5
  import pandas as pd
6
  import requests
7
 
8
+ from agent import Agent
9
+
10
  # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
14
 
15
  # --- Basic Agent Definition ---
16
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
 
 
 
 
 
 
17
  def run_and_submit_all(profile: gr.OAuthProfile | None):
18
  """
19
+ Fetches all questions, runs the Agent on them, submits all answers,
20
  and displays the results.
21
  """
22
  # --- Determine HF Space Runtime URL and Repo URL ---
 
35
 
36
  # 1. Instantiate Agent ( modify this part to create your agent)
37
  try:
38
+ agent = Agent()
39
  except Exception as e:
40
  print(f"Error instantiating agent: {e}")
41
  return f"Error initializing agent: {e}", None
prompt.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents.agents import (
2
+ FinalAnswerPromptTemplate,
3
+ PlanningPromptTemplate,
4
+ PromptTemplates,
5
+ )
6
+
7
+ web_search_agent_prompt = PromptTemplates(
8
+ system_prompt="""You are a web search agent. Your task is to find information on the web to answer the user's question.
9
+ You can use web search and visit webpages to gather information. Plan your actions carefully to find the
10
+ most relevant information.
11
+ """,
12
+ planning=PlanningPromptTemplate(
13
+ "You are a web search agent. Your task is to find information on the web to answer the user's question. "
14
+ "You can use web search and visit webpages to gather information. "
15
+ "Plan your actions carefully to find the most relevant information.",
16
+ max_steps=5,
17
+ ),
18
+ final_answer=FinalAnswerPromptTemplate(
19
+ "Based on the information you found, provide a final answer to the user's question. "
20
+ "Make sure your answer is clear and concise. "
21
+ "If you couldn't find enough information, explain why and what you would do next.",
22
+ max_steps=3,
23
+ ),
24
+ )
25
+
26
+ calc_agent_prompt = PromptTemplates(
27
+ planning=PlanningPromptTemplate(
28
+ "You are a calculator agent. Your task is to perform calculations or data analysis based on "
29
+ "the user's question. You can use libraries like pandas and numpy to assist you. "
30
+ "Plan your actions carefully to ensure accurate results.",
31
+ max_steps=5,
32
+ ),
33
+ final_answer=FinalAnswerPromptTemplate(
34
+ "Based on your calculations or data analysis, provide a final answer to the user's question. "
35
+ "Make sure your answer is clear and concise. "
36
+ "If you encountered any issues, explain what they were and how you would resolve them.",
37
+ max_steps=3,
38
+ ),
39
+ )
pyproject.toml CHANGED
@@ -7,5 +7,6 @@ requires-python = ">=3.11"
7
  dependencies = [
8
  "gradio>=5.34.0",
9
  "requests>=2.32.4",
 
10
  "types-requests>=2.32.4.20250611",
11
  ]
 
7
  dependencies = [
8
  "gradio>=5.34.0",
9
  "requests>=2.32.4",
10
+ "smolagents>=1.18.0",
11
  "types-requests>=2.32.4.20250611",
12
  ]
uv.lock CHANGED
@@ -7,6 +7,25 @@ resolution-markers = [
7
  "python_full_version < '3.12'",
8
  ]
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  [[package]]
11
  name = "aiofiles"
12
  version = "24.1.0"
@@ -189,23 +208,6 @@ wheels = [
189
  { url = "https://files.pythonhosted.org/packages/4d/36/2a115987e2d8c300a974597416d9de88f2444426de9571f4b59b2cca3acc/filelock-3.18.0-py3-none-any.whl", hash = "sha256:c401f4f8377c4464e6db25fff06205fd89bdd83b65eb0488ed1b160f780e21de", size = 16215, upload-time = "2025-03-14T07:11:39.145Z" },
190
  ]
191
 
192
- [[package]]
193
- name = "final-assignment-template"
194
- version = "0.1.0"
195
- source = { virtual = "." }
196
- dependencies = [
197
- { name = "gradio" },
198
- { name = "requests" },
199
- { name = "types-requests" },
200
- ]
201
-
202
- [package.metadata]
203
- requires-dist = [
204
- { name = "gradio", specifier = ">=5.34.0" },
205
- { name = "requests", specifier = ">=2.32.4" },
206
- { name = "types-requests", specifier = ">=2.32.4.20250611" },
207
- ]
208
-
209
  [[package]]
210
  name = "fsspec"
211
  version = "2025.5.1"
@@ -772,6 +774,15 @@ wheels = [
772
  { url = "https://files.pythonhosted.org/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427", size = 229892, upload-time = "2024-03-01T18:36:18.57Z" },
773
  ]
774
 
 
 
 
 
 
 
 
 
 
775
  [[package]]
776
  name = "python-multipart"
777
  version = "0.0.20"
@@ -917,6 +928,23 @@ wheels = [
917
  { url = "https://files.pythonhosted.org/packages/b7/ce/149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e/six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274", size = 11050, upload-time = "2024-12-04T17:35:26.475Z" },
918
  ]
919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
920
  [[package]]
921
  name = "sniffio"
922
  version = "1.3.1"
 
7
  "python_full_version < '3.12'",
8
  ]
9
 
10
+ [[package]]
11
+ name = "agents-course-final-assignment"
12
+ version = "0.1.0"
13
+ source = { virtual = "." }
14
+ dependencies = [
15
+ { name = "gradio" },
16
+ { name = "requests" },
17
+ { name = "smolagents" },
18
+ { name = "types-requests" },
19
+ ]
20
+
21
+ [package.metadata]
22
+ requires-dist = [
23
+ { name = "gradio", specifier = ">=5.34.0" },
24
+ { name = "requests", specifier = ">=2.32.4" },
25
+ { name = "smolagents", specifier = ">=1.18.0" },
26
+ { name = "types-requests", specifier = ">=2.32.4.20250611" },
27
+ ]
28
+
29
  [[package]]
30
  name = "aiofiles"
31
  version = "24.1.0"
 
208
  { url = "https://files.pythonhosted.org/packages/4d/36/2a115987e2d8c300a974597416d9de88f2444426de9571f4b59b2cca3acc/filelock-3.18.0-py3-none-any.whl", hash = "sha256:c401f4f8377c4464e6db25fff06205fd89bdd83b65eb0488ed1b160f780e21de", size = 16215, upload-time = "2025-03-14T07:11:39.145Z" },
209
  ]
210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  [[package]]
212
  name = "fsspec"
213
  version = "2025.5.1"
 
774
  { url = "https://files.pythonhosted.org/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427", size = 229892, upload-time = "2024-03-01T18:36:18.57Z" },
775
  ]
776
 
777
+ [[package]]
778
+ name = "python-dotenv"
779
+ version = "1.1.0"
780
+ source = { registry = "https://pypi.org/simple" }
781
+ sdist = { url = "https://files.pythonhosted.org/packages/88/2c/7bb1416c5620485aa793f2de31d3df393d3686aa8a8506d11e10e13c5baf/python_dotenv-1.1.0.tar.gz", hash = "sha256:41f90bc6f5f177fb41f53e87666db362025010eb28f60a01c9143bfa33a2b2d5", size = 39920, upload-time = "2025-03-25T10:14:56.835Z" }
782
+ wheels = [
783
+ { url = "https://files.pythonhosted.org/packages/1e/18/98a99ad95133c6a6e2005fe89faedf294a748bd5dc803008059409ac9b1e/python_dotenv-1.1.0-py3-none-any.whl", hash = "sha256:d7c01d9e2293916c18baf562d95698754b0dbbb5e74d457c45d4f6561fb9d55d", size = 20256, upload-time = "2025-03-25T10:14:55.034Z" },
784
+ ]
785
+
786
  [[package]]
787
  name = "python-multipart"
788
  version = "0.0.20"
 
928
  { url = "https://files.pythonhosted.org/packages/b7/ce/149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e/six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274", size = 11050, upload-time = "2024-12-04T17:35:26.475Z" },
929
  ]
930
 
931
+ [[package]]
932
+ name = "smolagents"
933
+ version = "1.18.0"
934
+ source = { registry = "https://pypi.org/simple" }
935
+ dependencies = [
936
+ { name = "huggingface-hub" },
937
+ { name = "jinja2" },
938
+ { name = "pillow" },
939
+ { name = "python-dotenv" },
940
+ { name = "requests" },
941
+ { name = "rich" },
942
+ ]
943
+ sdist = { url = "https://files.pythonhosted.org/packages/00/31/f4aa75afd9a98724932fef9b6f1356e9305a22359a0a32eff48e0ee6cb42/smolagents-1.18.0.tar.gz", hash = "sha256:2a44bd9ac85a2c47102bf30c1bcf32e5d399e1f9d66bb9518cc5fcb30879660a", size = 181958, upload-time = "2025-06-10T14:14:34.633Z" }
944
+ wheels = [
945
+ { url = "https://files.pythonhosted.org/packages/d3/28/493fcf6fb5f7c4ae21b9541fd588fbc69a22c22d3d9477771554a0779006/smolagents-1.18.0-py3-none-any.whl", hash = "sha256:c8f6d0c37808090213f45ec742be7f818e7f4772516f6e776bf991936ecabaf8", size = 136036, upload-time = "2025-06-10T14:14:32.949Z" },
946
+ ]
947
+
948
  [[package]]
949
  name = "sniffio"
950
  version = "1.3.1"