Spaces:
Running
Running
Create submit.py
Browse files
submit.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# ✅ Your Hugging Face credentials
|
| 5 |
+
USERNAME = "jcleee"
|
| 6 |
+
AGENT_CODE_URL = "https://huggingface.co/spaces/jcleee/First_agent_template/tree/main"
|
| 7 |
+
BASE_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
+
|
| 9 |
+
def download_files(task_id, filenames):
|
| 10 |
+
for fname in filenames:
|
| 11 |
+
url = f"{BASE_URL}/files/{task_id}"
|
| 12 |
+
response = requests.get(url)
|
| 13 |
+
os.makedirs("downloads", exist_ok=True)
|
| 14 |
+
with open(f"downloads/{fname}", "wb") as f:
|
| 15 |
+
f.write(response.content)
|
| 16 |
+
|
| 17 |
+
def load_questions():
|
| 18 |
+
response = requests.get(f"{BASE_URL}/questions")
|
| 19 |
+
response.raise_for_status()
|
| 20 |
+
return response.json()
|
| 21 |
+
|
| 22 |
+
def run_agent_on_question(question_text, agent):
|
| 23 |
+
output = agent.run(task=question_text, reset=True)
|
| 24 |
+
for step in output:
|
| 25 |
+
if hasattr(step, "final_answer"):
|
| 26 |
+
return step.final_answer
|
| 27 |
+
elif hasattr(step, "tool_calls"):
|
| 28 |
+
for call in step.tool_calls:
|
| 29 |
+
if call.name == "final_answer":
|
| 30 |
+
return call.arguments.get("answer")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
def submit_answers(agent):
|
| 34 |
+
questions = load_questions()
|
| 35 |
+
answers = []
|
| 36 |
+
|
| 37 |
+
for q in questions:
|
| 38 |
+
print(f"Running task: {q['task_id']}")
|
| 39 |
+
|
| 40 |
+
if q["files"]:
|
| 41 |
+
download_files(q["task_id"], q["files"])
|
| 42 |
+
|
| 43 |
+
answer = run_agent_on_question(q["question"], agent)
|
| 44 |
+
print("Answer:", answer)
|
| 45 |
+
|
| 46 |
+
answers.append({
|
| 47 |
+
"task_id": q["task_id"],
|
| 48 |
+
"submitted_answer": str(answer or "null")
|
| 49 |
+
})
|
| 50 |
+
|
| 51 |
+
payload = {
|
| 52 |
+
"username": USERNAME,
|
| 53 |
+
"agent_code": AGENT_CODE_URL,
|
| 54 |
+
"answers": answers
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
response = requests.post(f"{BASE_URL}/submit", json=payload)
|
| 58 |
+
print("Submitted! Status:", response.status_code)
|
| 59 |
+
print(response.json())
|