lucaslingle commited on
Commit
9f3acfe
·
verified ·
1 Parent(s): f4196dd

Update app.py

Browse files

see if this runs

Files changed (1) hide show
  1. app.py +50 -56
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, FinalAnswerTool, InferenceClientModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
@@ -11,18 +11,15 @@ import gradio as gr
11
  import inspect
12
  import pandas as pd
13
 
14
- # (Keep Constants as is)
15
- # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
 
 
 
 
 
18
 
19
- # --- Tools ---
20
- # search_tool = GoogleSearchTool()
21
- search_tool = DuckDuckGoSearchTool()
22
- visit_webpage_tool = VisitWebpageTool()
23
- final_answer_tool = FinalAnswerTool()
24
-
25
- def download_file(file_name: str) -> None:
26
  if not os.path.exists(file_name):
27
  url = f"{DEFAULT_API_URL}/files/{file_name.split('.')[-2]}"
28
  r = requests.get(url)
@@ -30,7 +27,7 @@ def download_file(file_name: str) -> None:
30
  f.write(r.content)
31
 
32
  @tool
33
- def read_file_tool(file_name: str) -> str:
34
  """
35
  Opens a file and returns its content as readable text.
36
  Supports 'txt', 'json', 'csv', 'xlsx', and 'mp3' (for mp3, it transcribes speech to text).
@@ -39,7 +36,7 @@ def read_file_tool(file_name: str) -> str:
39
  Returns:
40
  str: The content of the file as text, or transcribed speech if 'mp3'.
41
  """
42
- download_file(file_name)
43
  file_type = file_name.split(".")[-1]
44
  try:
45
  if file_type in {"txt", "py"}:
@@ -72,9 +69,8 @@ def read_file_tool(file_name: str) -> str:
72
  except Exception as e:
73
  return f"Error opening file '{file_name}': {str(e)}"
74
 
75
-
76
  @tool
77
- def reverse_string_tool(text: str) -> str:
78
  """
79
  Reverses the input text.
80
  Args:
@@ -85,52 +81,50 @@ def reverse_string_tool(text: str) -> str:
85
  return text[::-1]
86
 
87
 
88
- # --- Agent ---
89
- # MODEL_ID = "Qwen/Qwen3-8B"
90
- MODEL_ID = "Qwen/Qwen3-235B-A22B-Thinking-2507"
91
- # MODEL_ID = "deepseek-ai/DeepSeek-R1"
92
- PROVIDER = "auto"
93
- TOOLS = [read_file_tool, reverse_string_tool, search_tool, visit_webpage_tool, final_answer_tool]
94
- AUTORIZED_IMPORTS = ["pandas", "numpy", "datetime", "json", "re", "math", "os", "requests", "csv", "urllib"]
95
-
96
- AGENT = CodeAgent(
97
- model=InferenceClientModel(model_id=MODEL_ID, provider=PROVIDER, token=os.getenv("HF_TOKEN")),
98
- tools=TOOLS,
99
- additional_authorized_imports=AUTORIZED_IMPORTS,
100
- max_steps=5,
101
- )
102
 
103
- class PromptedAgent:
104
- def __init__(self):
105
- self.agent = AGENT
106
  def __call__(self, question_text, file_name) -> str:
107
- full_prompt = f"""You are a highly precise answering agent.
108
- When given a question:
109
- - If necessary, perform a web search using the tool `search_tool` to find possible sources of information.
110
- - If the web search only returns titles and short snippets, you MUST visit the actual webpage to read the full content before answering.
111
- - If the task requires reading, listening, or analyzing a file, you must use the file specified after the question, NOT the file name mentioned casually inside the question text.
112
- - Comma separated lists MUST contain a single space after each comma.
113
- - If you are asked for a number, don't use comma to write your number, nor use units such as $ or percent sign unless specified otherwise.
114
- - If you are asked for a string, don't use articles, nor abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
115
- - If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
116
- - Only answer after you have gathered enough information by reading the actual page contents.
117
- - Once you have the final answer, you must call `final_answer_tool("your_answer")`
118
- immediately after printing it.
119
- - Do not retry or execute anything else after calling `final_answer_tool`.
120
- - `final_answer_tool` must wrap the exact printed value.
121
- - Provide ONLY the precise answer requested.
122
- - Do not include explanations, steps, reasoning, or additional text.
123
- - Be direct and specific. GAIA benchmark requires exact matching answers.
124
-
125
- Based on the above guidelines, answer the following question:
126
- -- beginning of question --
127
- {question_text}
128
- -- end of question --
129
- If the questions mentions the need to use a file, use the following `file_name` value as the `file_name` parameter in any function calls:
130
- file_name: {file_name}"""
131
- response = self.agent.run(full_prompt)
132
  return response
133
 
 
134
  # --- submission code ---
135
  def run_and_submit_all( profile: gr.OAuthProfile | None):
136
  """
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, FinalAnswerTool, InferenceClientModel, tool
2
  import datetime
3
  import requests
4
  import pytz
 
11
  import inspect
12
  import pandas as pd
13
 
14
+ # --- constants ---
 
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
+ # --- tools ---
18
+ web_search = DuckDuckGoSearchTool()
19
+ visit_webpage = VisitWebpageTool()
20
+ final_answer = FinalAnswerTool()
21
 
22
+ def _download_file(file_name: str) -> None:
 
 
 
 
 
 
23
  if not os.path.exists(file_name):
24
  url = f"{DEFAULT_API_URL}/files/{file_name.split('.')[-2]}"
25
  r = requests.get(url)
 
27
  f.write(r.content)
28
 
29
  @tool
30
+ def read_file_as_text(file_name: str) -> str:
31
  """
32
  Opens a file and returns its content as readable text.
33
  Supports 'txt', 'json', 'csv', 'xlsx', and 'mp3' (for mp3, it transcribes speech to text).
 
36
  Returns:
37
  str: The content of the file as text, or transcribed speech if 'mp3'.
38
  """
39
+ _download_file(file_name)
40
  file_type = file_name.split(".")[-1]
41
  try:
42
  if file_type in {"txt", "py"}:
 
69
  except Exception as e:
70
  return f"Error opening file '{file_name}': {str(e)}"
71
 
 
72
  @tool
73
+ def reverse_string(text: str) -> str:
74
  """
75
  Reverses the input text.
76
  Args:
 
81
  return text[::-1]
82
 
83
 
84
+ # --- agent ---
85
+ class Agent:
86
+ def __init__(self, model_id, provider, tools, auth_imports):
87
+ self.model_id = = "Qwen/Qwen3-235B-A22B-Thinking-2507"
88
+ self.provider = "auto"
89
+ self.tools = [read_file_as_text, reverse_string, web_search, visit_webpage, final_answer]
90
+ self.auth_imports = ["pandas", "numpy", "datetime", "json", "re", "math", "os", "requests", "csv", "urllib"]
91
+ self.max_steps = 5
92
+ self.agent = CodeAgent(
93
+ model=InferenceClientModel(model_id=self.model_id, provider=self.provider, token=os.getenv("HF_TOKEN")),
94
+ tools=self.tools,
95
+ additional_authorized_imports=self.auth_imports,
96
+ max_steps=self.max_steps,
97
+ )
98
 
 
 
 
99
  def __call__(self, question_text, file_name) -> str:
100
+ template = t"""You are a highly precise question-answering agent.
101
+ When given a question:
102
+ - If necessary, perform a web search using the tool `web_search` to find possible sources of information.
103
+ - If the web search only returns titles and short snippets, you MUST visit the actual webpage using the `visit_webpage` tool to read the full content before answering.
104
+ - If the task requires reading, listening, or analyzing a file, you must use the file specified after the question, NOT the file name mentioned casually inside the question text.
105
+ - Comma separated lists MUST contain a single space after each comma.
106
+ - If you are asked for a number, don't use comma to write your number, nor use units such as $ or percent sign unless specified otherwise.
107
+ - If you are asked for a string, don't use articles, nor abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
108
+ - If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
109
+ - Only answer after you have gathered enough information by reading the actual page contents.
110
+ - Once you have the final answer, you must call `final_answer("your_answer")` immediately after printing it.
111
+ - Do not retry or execute anything else after calling `final_answer`.
112
+ - `final_answer` must wrap the exact printed value.
113
+ - Provide ONLY the precise answer requested.
114
+ - Do not include explanations, steps, reasoning, or additional text when calling `final_answer`.
115
+ - Be direct and specific. The GAIA benchmark requires exactly matching answers.
116
+
117
+ Based on the above guidelines, answer the following question:
118
+ -- beginning of question --
119
+ {question_text}
120
+ -- end of question --
121
+ If the questions mentions the need to use a file, use the following `file_name` value below as the `file_name` parameter in any function calls:
122
+ file_name: {file_name}"""
123
+ enhanced_question = template.substitute(question_text=question_text, file_name=file_name)
124
+ response = self.agent.run(enhanced_question)
125
  return response
126
 
127
+
128
  # --- submission code ---
129
  def run_and_submit_all( profile: gr.OAuthProfile | None):
130
  """