jcleee's picture
Create submit.py
07a4cd3 verified
raw
history blame
1.78 kB
import requests
import os
# ✅ Your Hugging Face credentials
USERNAME = "jcleee"
AGENT_CODE_URL = "https://huggingface.co/spaces/jcleee/First_agent_template/tree/main"
BASE_URL = "https://agents-course-unit4-scoring.hf.space"
def download_files(task_id, filenames):
for fname in filenames:
url = f"{BASE_URL}/files/{task_id}"
response = requests.get(url)
os.makedirs("downloads", exist_ok=True)
with open(f"downloads/{fname}", "wb") as f:
f.write(response.content)
def load_questions():
response = requests.get(f"{BASE_URL}/questions")
response.raise_for_status()
return response.json()
def run_agent_on_question(question_text, agent):
output = agent.run(task=question_text, reset=True)
for step in output:
if hasattr(step, "final_answer"):
return step.final_answer
elif hasattr(step, "tool_calls"):
for call in step.tool_calls:
if call.name == "final_answer":
return call.arguments.get("answer")
return None
def submit_answers(agent):
questions = load_questions()
answers = []
for q in questions:
print(f"Running task: {q['task_id']}")
if q["files"]:
download_files(q["task_id"], q["files"])
answer = run_agent_on_question(q["question"], agent)
print("Answer:", answer)
answers.append({
"task_id": q["task_id"],
"submitted_answer": str(answer or "null")
})
payload = {
"username": USERNAME,
"agent_code": AGENT_CODE_URL,
"answers": answers
}
response = requests.post(f"{BASE_URL}/submit", json=payload)
print("Submitted! Status:", response.status_code)
print(response.json())