PaulJ00 commited on
Commit
23b52a2
·
verified ·
1 Parent(s): 92888f5
Files changed (1) hide show
  1. app.py +38 -71
app.py CHANGED
@@ -8,9 +8,12 @@ import gradio as gr
8
  import pandas as pd
9
  import requests
10
 
11
- from smolagents import CodeAgent, tool
12
- from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
13
- from smolagents import OpenAIServerModel
 
 
 
14
 
15
 
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -18,29 +21,11 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
18
 
19
  @tool
20
  def get_task_file_url(task_id: str) -> str:
21
- """
22
- Returns the download URL for the file attached to a GAIA task.
23
-
24
- Args:
25
- task_id: The GAIA task id.
26
-
27
- Returns:
28
- Direct URL to the attached file for this task.
29
- """
30
  return f"{DEFAULT_API_URL}/files/{task_id}"
31
 
32
 
33
  @tool
34
  def download_file(url: str) -> str:
35
- """
36
- Downloads a file from a URL to a temporary local path and returns that path.
37
-
38
- Args:
39
- url: The file URL.
40
-
41
- Returns:
42
- Local file path as a string.
43
- """
44
  suffix = ""
45
  match = re.search(r"\.([a-zA-Z0-9]{1,6})(?:\?|$)", url)
46
  if match:
@@ -60,16 +45,6 @@ def download_file(url: str) -> str:
60
 
61
  @tool
62
  def inspect_local_text_file(path: str, max_chars: int = 12000) -> str:
63
- """
64
- Reads a local text-like file and returns a preview.
65
-
66
- Args:
67
- path: Local file path.
68
- max_chars: Max number of characters to return.
69
-
70
- Returns:
71
- Text preview.
72
- """
73
  p = Path(path)
74
  try:
75
  return p.read_text(encoding="utf-8", errors="ignore")[:max_chars]
@@ -79,17 +54,6 @@ def inspect_local_text_file(path: str, max_chars: int = 12000) -> str:
79
 
80
  @tool
81
  def analyze_spreadsheet(path: str, instruction: str) -> str:
82
- """
83
- Loads an Excel/CSV spreadsheet with pandas and executes a constrained analysis
84
- instruction by giving the LLM a schema + preview.
85
-
86
- Args:
87
- path: Local spreadsheet path.
88
- instruction: What to compute.
89
-
90
- Returns:
91
- JSON string with columns, preview, dtypes, shape.
92
- """
93
  p = Path(path)
94
  if p.suffix.lower() in [".csv", ".tsv"]:
95
  sep = "\t" if p.suffix.lower() == ".tsv" else ","
@@ -113,15 +77,6 @@ def analyze_spreadsheet(path: str, instruction: str) -> str:
113
 
114
  @tool
115
  def python_compute(code: str) -> str:
116
- """
117
- Executes trusted short Python snippets for local analysis.
118
-
119
- Args:
120
- code: Python code. Must assign the final value to a variable named `result`.
121
-
122
- Returns:
123
- Stringified result.
124
- """
125
  local_vars = {}
126
  global_vars = {
127
  "pd": pd,
@@ -147,7 +102,6 @@ def clean_final_answer(text: str) -> str:
147
  answer = re.sub(r"^(FINAL ANSWER\s*:\s*)", "", answer, flags=re.IGNORECASE)
148
  answer = re.sub(r"^(Answer\s*:\s*)", "", answer, flags=re.IGNORECASE)
149
  answer = re.sub(r"[ \t]+", " ", answer).strip()
150
-
151
  return answer
152
 
153
 
@@ -162,8 +116,7 @@ Rules:
162
  - For exact-match scoring, your final output must be ONLY the answer and nothing else.
163
  - Do not include explanations in your final answer.
164
  - Do not include the words FINAL ANSWER.
165
- - When a question requests a format (comma-separated list, USD with two decimals, single word, date, etc.),
166
- obey that exact format.
167
  - If the answer is numeric and the prompt requests decimals, format correctly.
168
  """
169
 
@@ -184,22 +137,37 @@ class GaiaAgent:
184
  max_tokens=1200,
185
  )
186
 
187
- self.agent = CodeAgent(
188
- model=self.model,
189
- tools=[
190
- DuckDuckGoSearchTool(),
191
- VisitWebpageTool(),
192
- get_task_file_url,
193
- download_file,
194
- inspect_local_text_file,
195
- analyze_spreadsheet,
196
- python_compute,
197
- ],
198
- add_base_tools=False,
199
- max_steps=10,
200
- instructions=SYSTEM_PROMPT,
201
- )
202
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
 
204
  def __call__(self, question: str, task_id: str) -> str:
205
  full_prompt = f"""
@@ -316,6 +284,5 @@ with gr.Blocks() as demo:
316
  outputs=[status_output, results_table],
317
  )
318
 
319
-
320
  if __name__ == "__main__":
321
  demo.launch(debug=True)
 
8
  import pandas as pd
9
  import requests
10
 
11
+ from smolagents import CodeAgent, tool, DuckDuckGoSearchTool, VisitWebpageTool
12
+
13
+ try:
14
+ from smolagents import OpenAIServerModel
15
+ except ImportError:
16
+ from smolagents import OpenAIModel as OpenAIServerModel
17
 
18
 
19
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
21
 
22
  @tool
23
  def get_task_file_url(task_id: str) -> str:
 
 
 
 
 
 
 
 
 
24
  return f"{DEFAULT_API_URL}/files/{task_id}"
25
 
26
 
27
  @tool
28
  def download_file(url: str) -> str:
 
 
 
 
 
 
 
 
 
29
  suffix = ""
30
  match = re.search(r"\.([a-zA-Z0-9]{1,6})(?:\?|$)", url)
31
  if match:
 
45
 
46
  @tool
47
  def inspect_local_text_file(path: str, max_chars: int = 12000) -> str:
 
 
 
 
 
 
 
 
 
 
48
  p = Path(path)
49
  try:
50
  return p.read_text(encoding="utf-8", errors="ignore")[:max_chars]
 
54
 
55
  @tool
56
  def analyze_spreadsheet(path: str, instruction: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
57
  p = Path(path)
58
  if p.suffix.lower() in [".csv", ".tsv"]:
59
  sep = "\t" if p.suffix.lower() == ".tsv" else ","
 
77
 
78
  @tool
79
  def python_compute(code: str) -> str:
 
 
 
 
 
 
 
 
 
80
  local_vars = {}
81
  global_vars = {
82
  "pd": pd,
 
102
  answer = re.sub(r"^(FINAL ANSWER\s*:\s*)", "", answer, flags=re.IGNORECASE)
103
  answer = re.sub(r"^(Answer\s*:\s*)", "", answer, flags=re.IGNORECASE)
104
  answer = re.sub(r"[ \t]+", " ", answer).strip()
 
105
  return answer
106
 
107
 
 
116
  - For exact-match scoring, your final output must be ONLY the answer and nothing else.
117
  - Do not include explanations in your final answer.
118
  - Do not include the words FINAL ANSWER.
119
+ - When a question requests a format, obey that exact format.
 
120
  - If the answer is numeric and the prompt requests decimals, format correctly.
121
  """
122
 
 
137
  max_tokens=1200,
138
  )
139
 
140
+ try:
141
+ self.agent = CodeAgent(
142
+ model=self.model,
143
+ tools=[
144
+ DuckDuckGoSearchTool(),
145
+ VisitWebpageTool(),
146
+ get_task_file_url,
147
+ download_file,
148
+ inspect_local_text_file,
149
+ analyze_spreadsheet,
150
+ python_compute,
151
+ ],
152
+ add_base_tools=False,
153
+ max_steps=10,
154
+ instructions=SYSTEM_PROMPT,
155
+ )
156
+ except TypeError:
157
+ self.agent = CodeAgent(
158
+ model=self.model,
159
+ tools=[
160
+ DuckDuckGoSearchTool(),
161
+ VisitWebpageTool(),
162
+ get_task_file_url,
163
+ download_file,
164
+ inspect_local_text_file,
165
+ analyze_spreadsheet,
166
+ python_compute,
167
+ ],
168
+ add_base_tools=False,
169
+ max_steps=10,
170
+ )
171
 
172
  def __call__(self, question: str, task_id: str) -> str:
173
  full_prompt = f"""
 
284
  outputs=[status_output, results_table],
285
  )
286
 
 
287
  if __name__ == "__main__":
288
  demo.launch(debug=True)