initial version
Browse files- agent.py +13 -0
- app.py +20 -29
- requirements.txt +2 -1
agent.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, InferenceClientModel, WikipediaSearchTool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class GAIAAgent:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
self.agent = CodeAgent(
|
| 7 |
+
tools=[WikipediaSearchTool()],
|
| 8 |
+
model=InferenceClientModel(),
|
| 9 |
+
add_base_tools=True
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def __call__(self, question: str) -> str:
|
| 13 |
+
return self.agent.run(question)
|
app.py
CHANGED
|
@@ -1,34 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
class BasicAgent:
|
| 14 |
-
def __init__(self):
|
| 15 |
-
print("BasicAgent initialized.")
|
| 16 |
-
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
fixed_answer = "This is a default answer."
|
| 19 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
-
return fixed_answer
|
| 21 |
-
|
| 22 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 28 |
-
space_id = os.getenv("SPACE_ID")
|
| 29 |
|
| 30 |
if profile:
|
| 31 |
-
username= f"{profile.username}"
|
| 32 |
print(f"User logged in: {username}")
|
| 33 |
else:
|
| 34 |
print("User not logged in.")
|
|
@@ -38,9 +29,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 38 |
questions_url = f"{api_url}/questions"
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
-
# 1. Instantiate Agent (
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
@@ -55,16 +46,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 55 |
response.raise_for_status()
|
| 56 |
questions_data = response.json()
|
| 57 |
if not questions_data:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
print(f"Fetched {len(questions_data)} questions.")
|
| 61 |
except requests.exceptions.RequestException as e:
|
| 62 |
print(f"Error fetching questions: {e}")
|
| 63 |
return f"Error fetching questions: {e}", None
|
| 64 |
except requests.exceptions.JSONDecodeError as e:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
except Exception as e:
|
| 69 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 70 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -84,8 +75,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
except Exception as e:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
if not answers_payload:
|
| 91 |
print("Agent did not produce any answers to submit.")
|
|
@@ -172,10 +163,10 @@ with gr.Blocks() as demo:
|
|
| 172 |
)
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 176 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 177 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 178 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 179 |
|
| 180 |
if space_host_startup:
|
| 181 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
@@ -183,14 +174,14 @@ if __name__ == "__main__":
|
|
| 183 |
else:
|
| 184 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 185 |
|
| 186 |
-
if space_id_startup:
|
| 187 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 188 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 189 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 190 |
else:
|
| 191 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 192 |
|
| 193 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
|
| 195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
from agent import GAIAAgent
|
| 7 |
+
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
|
| 13 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 16 |
and displays the results.
|
| 17 |
"""
|
| 18 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 19 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 20 |
|
| 21 |
if profile:
|
| 22 |
+
username = f"{profile.username}"
|
| 23 |
print(f"User logged in: {username}")
|
| 24 |
else:
|
| 25 |
print("User not logged in.")
|
|
|
|
| 29 |
questions_url = f"{api_url}/questions"
|
| 30 |
submit_url = f"{api_url}/submit"
|
| 31 |
|
| 32 |
+
# 1. Instantiate Agent (modify this part to create your agent)
|
| 33 |
try:
|
| 34 |
+
agent = GAIAAgent()
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error instantiating agent: {e}")
|
| 37 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 46 |
response.raise_for_status()
|
| 47 |
questions_data = response.json()
|
| 48 |
if not questions_data:
|
| 49 |
+
print("Fetched questions list is empty.")
|
| 50 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 51 |
print(f"Fetched {len(questions_data)} questions.")
|
| 52 |
except requests.exceptions.RequestException as e:
|
| 53 |
print(f"Error fetching questions: {e}")
|
| 54 |
return f"Error fetching questions: {e}", None
|
| 55 |
except requests.exceptions.JSONDecodeError as e:
|
| 56 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 57 |
+
print(f"Response text: {response.text[:500]}")
|
| 58 |
+
return f"Error decoding server response for questions: {e}", None
|
| 59 |
except Exception as e:
|
| 60 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 61 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 75 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 76 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 77 |
except Exception as e:
|
| 78 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 79 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 80 |
|
| 81 |
if not answers_payload:
|
| 82 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 163 |
)
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
| 167 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 168 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 169 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 170 |
|
| 171 |
if space_host_startup:
|
| 172 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
|
| 174 |
else:
|
| 175 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 176 |
|
| 177 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 178 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 179 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 180 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 181 |
else:
|
| 182 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 183 |
|
| 184 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 185 |
|
| 186 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 187 |
+
demo.launch(debug=True, share=False)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
requests
|
|
|
|
| 1 |
+
smolagents==1.19.0
|
| 2 |
+
gradio==5.35.0
|
| 3 |
requests
|