dlaima commited on
Commit
c2f416b
·
verified ·
1 Parent(s): 87850bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -32
app.py CHANGED
@@ -4,17 +4,17 @@ import gradio as gr
4
  import requests
5
  import pandas as pd
6
 
7
- #from smolagents.agent import CodeAgent
8
  #from smolagents.models import HfApiModel
9
- from smolagents import Tool
10
- from smolagents import CodeAgent, HfApiModel
11
 
12
  from audio_transcriber import AudioTranscriptionTool
13
  from image_analyzer import ImageAnalysisTool
14
  from wikipedia_searcher import WikipediaSearcher
15
 
 
 
16
 
17
- # System prompt
18
  SYSTEM_PROMPT = """You are an agent solving the GAIA benchmark and you are required to provide exact answers.
19
  Rules to follow:
20
  1. Return only the exact requested answer: no explanation and no reasoning.
@@ -31,11 +31,9 @@ Examples of good responses:
31
  Never include phrases like "the answer is..." or "Based on my research".
32
  Only return the exact answer."""
33
 
34
-
35
- # Tool definitions
36
  audio_tool = AudioTranscriptionTool()
37
  image_tool = ImageAnalysisTool()
38
-
39
  wiki_tool = Tool.from_function(
40
  name="wikipedia_search",
41
  description="Search for facts using Wikipedia.",
@@ -46,20 +44,16 @@ wiki_tool = Tool.from_function(
46
 
47
  tools = [audio_tool, image_tool, wiki_tool]
48
 
49
-
50
- # Agent factory
51
- def MyAgent():
52
- return CodeAgent(
53
- tools=tools,
54
- system_prompt=SYSTEM_PROMPT,
55
- model=HfApiModel(
56
- api_url="https://api-inference.huggingface.com/models/HuggingFaceH4/zephyr-7b-beta",
57
  api_key=os.getenv("HF_API_TOKEN")
58
  )
59
- )
60
-
61
 
62
- # Main run and submission logic
63
  def run_and_submit_all(profile: gr.OAuthProfile | None):
64
  space_id = os.getenv("SPACE_ID")
65
 
@@ -70,7 +64,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
70
  print("User not logged in.")
71
  return "Please Login to Hugging Face with the button.", None
72
 
73
- api_url = os.getenv("GAIA_API_URL", "https://gaia-benchmark.com/api")
74
  questions_url = f"{api_url}/questions"
75
  submit_url = f"{api_url}/submit"
76
 
@@ -97,7 +91,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
97
  results_log = []
98
  answers_payload = []
99
  print(f"Running agent on {len(questions_data)} questions...")
100
-
101
  for item in questions_data:
102
  task_id = item.get("task_id")
103
  if not task_id:
@@ -152,16 +145,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
152
  except Exception as e:
153
  return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
154
 
155
-
156
- # Gradio UI setup
157
  with gr.Blocks() as demo:
158
  gr.Markdown("# Basic Agent Evaluation Runner")
159
  gr.Markdown("""
160
  **Instructions:**
161
- 1. Clone this space, modify code to define your agent's logic, tools, and packages.
162
  2. Log in to your Hugging Face account using the button below.
163
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
164
- **Note:** Submitting can take some time.
165
  """)
166
 
167
  gr.LoginButton()
@@ -172,9 +163,8 @@ with gr.Blocks() as demo:
172
 
173
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
174
 
175
- # App startup logs
176
  if __name__ == "__main__":
177
- print("\n" + "-" * 30 + " App Starting " + "-" * 30)
178
  space_host = os.getenv("SPACE_HOST")
179
  space_id = os.getenv("SPACE_ID")
180
 
@@ -182,17 +172,16 @@ if __name__ == "__main__":
182
  print(f"✅ SPACE_HOST found: {space_host}")
183
  print(f" Runtime URL should be: https://{space_host}.hf.space")
184
  else:
185
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
186
 
187
  if space_id:
188
  print(f"✅ SPACE_ID found: {space_id}")
189
  print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
190
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id}/tree/main")
191
  else:
192
- print("ℹ️ SPACE_ID environment variable not found (running locally?).")
193
 
194
- print("-" * (60 + len(" App Starting ")) + "\n")
195
- print("Launching Gradio Interface for Basic Agent Evaluation...")
196
  demo.launch(debug=True, share=False)
197
 
198
 
 
 
4
  import requests
5
  import pandas as pd
6
 
7
+ from smolagents import Tool, CodeAgent, HfApiModel
8
  #from smolagents.models import HfApiModel
 
 
9
 
10
  from audio_transcriber import AudioTranscriptionTool
11
  from image_analyzer import ImageAnalysisTool
12
  from wikipedia_searcher import WikipediaSearcher
13
 
14
+ # GAIA scoring endpoint
15
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
+ # Static system prompt for GAIA
18
  SYSTEM_PROMPT = """You are an agent solving the GAIA benchmark and you are required to provide exact answers.
19
  Rules to follow:
20
  1. Return only the exact requested answer: no explanation and no reasoning.
 
31
  Never include phrases like "the answer is..." or "Based on my research".
32
  Only return the exact answer."""
33
 
34
+ # Define agent tools
 
35
  audio_tool = AudioTranscriptionTool()
36
  image_tool = ImageAnalysisTool()
 
37
  wiki_tool = Tool.from_function(
38
  name="wikipedia_search",
39
  description="Search for facts using Wikipedia.",
 
44
 
45
  tools = [audio_tool, image_tool, wiki_tool]
46
 
47
+ # Define the custom agent using Mixtral on Hugging Face
48
+ class MyAgent(CodeAgent):
49
+ def __init__(self):
50
+ model = HfApiModel(
51
+ api_url="https://api-inference.huggingface.com/models/mistralai/Mixtral-8x7B-Instruct-v0.1",
 
 
 
52
  api_key=os.getenv("HF_API_TOKEN")
53
  )
54
+ super().__init__(model=model, tools=tools, system_prompt=SYSTEM_PROMPT)
 
55
 
56
+ # Evaluation + Submission function
57
  def run_and_submit_all(profile: gr.OAuthProfile | None):
58
  space_id = os.getenv("SPACE_ID")
59
 
 
64
  print("User not logged in.")
65
  return "Please Login to Hugging Face with the button.", None
66
 
67
+ api_url = DEFAULT_API_URL
68
  questions_url = f"{api_url}/questions"
69
  submit_url = f"{api_url}/submit"
70
 
 
91
  results_log = []
92
  answers_payload = []
93
  print(f"Running agent on {len(questions_data)} questions...")
 
94
  for item in questions_data:
95
  task_id = item.get("task_id")
96
  if not task_id:
 
145
  except Exception as e:
146
  return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
147
 
148
+ # Gradio UI
 
149
  with gr.Blocks() as demo:
150
  gr.Markdown("# Basic Agent Evaluation Runner")
151
  gr.Markdown("""
152
  **Instructions:**
153
+ 1. Clone this space and define your agent and tools.
154
  2. Log in to your Hugging Face account using the button below.
155
+ 3. Click 'Run Evaluation & Submit All Answers' to test your agent and submit results.
 
156
  """)
157
 
158
  gr.LoginButton()
 
163
 
164
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
165
 
 
166
  if __name__ == "__main__":
167
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
168
  space_host = os.getenv("SPACE_HOST")
169
  space_id = os.getenv("SPACE_ID")
170
 
 
172
  print(f"✅ SPACE_HOST found: {space_host}")
173
  print(f" Runtime URL should be: https://{space_host}.hf.space")
174
  else:
175
+ print("ℹ️ SPACE_HOST not found.")
176
 
177
  if space_id:
178
  print(f"✅ SPACE_ID found: {space_id}")
179
  print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
 
180
  else:
181
+ print("ℹ️ SPACE_ID not found.")
182
 
183
+ print("-"*(60 + len(" App Starting ")) + "\n")
 
184
  demo.launch(debug=True, share=False)
185
 
186
 
187
+