dlaima commited on
Commit
94feb70
·
verified ·
1 Parent(s): 8504f2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -67
app.py CHANGED
@@ -1,80 +1,36 @@
1
 
2
  import os
3
- import gradio as gr
4
  import requests
5
  import pandas as pd
 
 
 
6
 
7
- from smolagents import ToolCallingAgent, OpenAIServerModel
8
  from audio_transcriber import AudioTranscriptionTool
9
  from image_analyzer import ImageAnalysisTool
10
  from wikipedia_searcher import WikipediaSearcher
11
 
12
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
-
14
- SYSTEM_PROMPT = (
15
- "You are an agent solving the GAIA benchmark and must provide exact answers.\n"
16
- "Rules:\n"
17
- "1. Return only the exact requested answer: no explanation.\n"
18
- "2. For yes/no, return 'Yes' or 'No'.\n"
19
- "3. For dates, use the exact requested format.\n"
20
- "4. For numbers, use only the number.\n"
21
- "5. For names, use the exact name from sources.\n"
22
- "6. If the question has a file, download it using the task ID.\n"
23
- "Examples:\n"
24
- "- '42'\n"
25
- "- 'Arturo Nunez'\n"
26
- "- 'Yes'\n"
27
- "- 'October 5, 2001'\n"
28
- "- 'Buenos Aires'\n"
29
- "Never say 'the answer is...'. Only return the answer.\n"
30
- )
31
 
32
  class GaiaAgent:
33
  def __init__(self):
34
- print("Gaia Agent Initialized")
 
 
35
 
36
- # Initialize the OpenAI GPT-3.5-turbo model via smolagents OpenAIServerModel
37
- self.model = OpenAIServerModel(
38
- model_name="gpt-3.5-turbo",
39
- api_key=os.getenv("OPENAI_API_KEY") # Make sure you set this in your environment
40
  )
41
-
42
- # Initialize the tools
43
- self.tools = [
44
  AudioTranscriptionTool(),
45
  ImageAnalysisTool(),
46
  WikipediaSearcher()
47
  ]
 
48
 
49
- # Create the agent with tools and model
50
- self.agent = ToolCallingAgent(
51
- tools=self.tools,
52
- model=self.model
53
- )
54
-
55
- def __call__(self, question: str) -> str:
56
- print(f"Agent received question (first 50 chars): {question[:50]}...")
57
-
58
- full_prompt = f"{SYSTEM_PROMPT}\nQUESTION:\n{question}"
59
-
60
- try:
61
- result = self.agent.run(full_prompt)
62
- print(f"Raw result from agent: {result}")
63
-
64
- if isinstance(result, dict) and "answer" in result:
65
- return str(result["answer"]).strip()
66
- elif isinstance(result, str):
67
- return result.strip()
68
- elif isinstance(result, list):
69
- for item in reversed(result):
70
- if isinstance(item, dict) and item.get("role") == "assistant" and "content" in item:
71
- return item["content"].strip()
72
- return "ERROR: Unexpected list format"
73
- else:
74
- return "ERROR: Unexpected result type"
75
- except Exception as e:
76
- print(f"Exception during agent run: {e}")
77
- return f"AGENT ERROR: {e}"
78
 
79
  def run_and_submit_all(profile: gr.OAuthProfile | None):
80
  space_id = os.getenv("SPACE_ID")
@@ -119,7 +75,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
119
 
120
  question_text = item.get("question", "")
121
 
122
- # Download associated file if any (mp3 or jpeg) according to GAIA benchmark task
123
  file_url = item.get("file_url")
124
  local_file_path = None
125
  if file_url:
@@ -133,8 +88,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
133
  for chunk in r.iter_content(chunk_size=8192):
134
  f.write(chunk)
135
  print(f"Downloaded file for task {task_id} to {local_file_path}")
136
-
137
- # Append info about the file path to the question so the agent knows to use it
138
  question_text += f"\n\nFile path: {local_file_path}"
139
  except Exception as e:
140
  print(f"Failed to download file for task {task_id}: {e}")
@@ -155,7 +108,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
155
  "Submitted Answer": error_msg
156
  })
157
 
158
- # Cleanup downloaded file
159
  if local_file_path:
160
  try:
161
  os.remove(local_file_path)
@@ -196,7 +148,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
196
  except Exception as e:
197
  return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
198
 
199
- # Gradio UI
200
  with gr.Blocks() as demo:
201
  gr.Markdown("# Basic Agent Evaluation Runner")
202
  gr.Markdown("""
@@ -220,16 +172,16 @@ if __name__ == "__main__":
220
  space_id = os.getenv("SPACE_ID")
221
 
222
  if space_host:
223
- print(f" SPACE_HOST found: {space_host}")
224
  print(f" Runtime URL should be: https://{space_host}.hf.space")
225
  else:
226
- print("ℹ️ SPACE_HOST not found.")
227
 
228
  if space_id:
229
- print(f" SPACE_ID found: {space_id}")
230
  print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
231
  else:
232
- print("ℹ️ SPACE_ID not found.")
233
 
234
  print("-"*(60 + len(" App Starting ")) + "\n")
235
  demo.launch(debug=True, share=False)
 
1
 
2
  import os
 
3
  import requests
4
  import pandas as pd
5
+ import gradio as gr
6
+ from smolagents import ToolCallingAgent
7
+ from smolagents.models import OpenAIServerModel
8
 
 
9
  from audio_transcriber import AudioTranscriptionTool
10
  from image_analyzer import ImageAnalysisTool
11
  from wikipedia_searcher import WikipediaSearcher
12
 
13
+ DEFAULT_API_URL = "https://gaia-benchmark.com/api"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  class GaiaAgent:
16
  def __init__(self):
17
+ api_key = os.getenv("OPENAI_API_KEY")
18
+ if not api_key:
19
+ raise EnvironmentError("OPENAI_API_KEY not found in environment variables.")
20
 
21
+ model = OpenAIServerModel(
22
+ model_id="gpt-3.5-turbo",
23
+ api_key=api_key
 
24
  )
25
+ tools = [
 
 
26
  AudioTranscriptionTool(),
27
  ImageAnalysisTool(),
28
  WikipediaSearcher()
29
  ]
30
+ self.agent = ToolCallingAgent(model=model, tools=tools)
31
 
32
+ def __call__(self, prompt: str) -> str:
33
+ return self.agent.run([{"role": "user", "content": prompt}])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  def run_and_submit_all(profile: gr.OAuthProfile | None):
36
  space_id = os.getenv("SPACE_ID")
 
75
 
76
  question_text = item.get("question", "")
77
 
 
78
  file_url = item.get("file_url")
79
  local_file_path = None
80
  if file_url:
 
88
  for chunk in r.iter_content(chunk_size=8192):
89
  f.write(chunk)
90
  print(f"Downloaded file for task {task_id} to {local_file_path}")
 
 
91
  question_text += f"\n\nFile path: {local_file_path}"
92
  except Exception as e:
93
  print(f"Failed to download file for task {task_id}: {e}")
 
108
  "Submitted Answer": error_msg
109
  })
110
 
 
111
  if local_file_path:
112
  try:
113
  os.remove(local_file_path)
 
148
  except Exception as e:
149
  return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
150
 
151
+
152
  with gr.Blocks() as demo:
153
  gr.Markdown("# Basic Agent Evaluation Runner")
154
  gr.Markdown("""
 
172
  space_id = os.getenv("SPACE_ID")
173
 
174
  if space_host:
175
+ print(f"\u2705 SPACE_HOST found: {space_host}")
176
  print(f" Runtime URL should be: https://{space_host}.hf.space")
177
  else:
178
+ print("\u2139\ufe0f SPACE_HOST not found.")
179
 
180
  if space_id:
181
+ print(f"\u2705 SPACE_ID found: {space_id}")
182
  print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
183
  else:
184
+ print("\u2139\ufe0f SPACE_ID not found.")
185
 
186
  print("-"*(60 + len(" App Starting ")) + "\n")
187
  demo.launch(debug=True, share=False)