Spaces:
Build error
Build error
Create submission.py
Browse files- submission.py +87 -0
submission.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
from huggingface_hub import HfApi
|
| 5 |
+
from gaia.loader import load_tasks
|
| 6 |
+
from agents import GaiaAgent, route_task, profiles
|
| 7 |
+
|
| 8 |
+
# API endpoint (can be overridden via env var)
|
| 9 |
+
DEFAULT_API_URL = os.getenv("GAIA_API_URL", "https://agents-course-unit4-scoring.hf.space")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def main():
|
| 13 |
+
# 1) Get Hugging Face username
|
| 14 |
+
api = HfApi()
|
| 15 |
+
user_info = api.whoami()
|
| 16 |
+
username = user_info.get("name")
|
| 17 |
+
if not username:
|
| 18 |
+
raise RuntimeError("Could not fetch Hugging Face username. Please login with 'huggingface-cli login'.")
|
| 19 |
+
|
| 20 |
+
# 2) Determine code link for verification
|
| 21 |
+
space_id = os.getenv("SPACE_ID")
|
| 22 |
+
if space_id:
|
| 23 |
+
code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 24 |
+
else:
|
| 25 |
+
code_link = input("Enter your Space code URL (e.g., https://huggingface.co/spaces/USER/REPO/tree/main): ")
|
| 26 |
+
|
| 27 |
+
print(f"Username: {username}\nCode Link: {code_link}")
|
| 28 |
+
|
| 29 |
+
# 3) Load GAIA tasks
|
| 30 |
+
print("Fetching GAIA tasks...")
|
| 31 |
+
tasks = load_tasks()
|
| 32 |
+
print(f"Loaded {len(tasks)} tasks.")
|
| 33 |
+
|
| 34 |
+
# 4) Instantiate agent
|
| 35 |
+
print("Initializing GaiaAgent...")
|
| 36 |
+
agent = GaiaAgent()
|
| 37 |
+
|
| 38 |
+
# 5) Run agent and collect answers
|
| 39 |
+
answers = []
|
| 40 |
+
for t in tasks:
|
| 41 |
+
task_id = t["task_id"]
|
| 42 |
+
prompt = t["prompt"]
|
| 43 |
+
kind = route_task(prompt)
|
| 44 |
+
# Merge default profile
|
| 45 |
+
profile = profiles.get(kind, {}).copy()
|
| 46 |
+
|
| 47 |
+
print(f"Running task {task_id} (type={kind})...")
|
| 48 |
+
try:
|
| 49 |
+
answer = agent.run(prompt, profile)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
answer = f"ERROR: {e}"
|
| 52 |
+
answers.append({
|
| 53 |
+
"task_id": task_id,
|
| 54 |
+
"submitted_answer": answer,
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
# 6) Build submission payload
|
| 58 |
+
submission = {
|
| 59 |
+
"username": username,
|
| 60 |
+
"agent_code": code_link,
|
| 61 |
+
"answers": answers,
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
# 7) Save to submission.json
|
| 65 |
+
out_path = "submission.json"
|
| 66 |
+
with open(out_path, "w") as f:
|
| 67 |
+
json.dump(submission, f, indent=2)
|
| 68 |
+
print(f"Saved submission to {out_path}.")
|
| 69 |
+
|
| 70 |
+
# 8) Optionally submit to scoring API
|
| 71 |
+
do_submit = input("Submit now to scoring API? (y/N): ").strip().lower() == 'y'
|
| 72 |
+
if do_submit:
|
| 73 |
+
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 74 |
+
print(f"Submitting to {submit_url}...")
|
| 75 |
+
resp = requests.post(submit_url, json=submission)
|
| 76 |
+
try:
|
| 77 |
+
resp.raise_for_status()
|
| 78 |
+
result = resp.json()
|
| 79 |
+
print("Submission result:", json.dumps(result, indent=2))
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print("Submission failed:", e, resp.text)
|
| 82 |
+
else:
|
| 83 |
+
print("Skipping submission. You can submit later by POSTing submission.json to /submit.")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
main()
|