AdanAtif's picture
Update app.py
199a44c verified
Raw
History Blame
3.43 kB
import requests
import json
# Configuration
HF_USERNAME = "YOUR_HF_USERNAME" # Replace with your Hugging Face username
AGENT_CODE_URL = f"https://huggingface.co/spaces/{HF_USERNAME}/YOUR_SPACE_NAME/tree/main"
API_BASE_URL = "https://agents-course-unit4-scoring.hf.space"
def fetch_questions():
"""Retrieves the 20 GAIA level 1 validation questions from the API."""
print("Fetching questions from the API...")
response = requests.get(f"{API_BASE_URL}/questions")
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Failed to fetch questions: {response.text}")
def download_task_file(task_id):
"""Downloads associated files if the task requires it."""
# The API documentation specifies: GET /files/{task_id}
# Use this if your agent needs to read PDFs, images, or CSVs locally
url = f"{API_BASE_URL}/files/{task_id}"
response = requests.get(url)
if response.status_code == 200:
file_path = f"./{task_id}_file" # You'll want to parse the header for exact extension
with open(file_path, "wb") as f:
f.write(response.content)
return file_path
return None
def dummy_agent_fallback(question_text):
"""
Placeholder for your real agent call.
REPLACE THIS with your actual LLM/Agent call!
"""
# Strict prompt constraint to force exact matches
system_prompt = (
"You are a precise QA bot. Answer the question using your tools. "
"Output ONLY the final string or number representing the answer. "
"Do not include 'The answer is...', do not include 'FINAL ANSWER', "
"and do not include any conversational filler."
)
# Your agent logic goes here (e.g., agent.run(question_text))
return "example_answer"
def submit_answers(submission_payload):
"""Submits the payload to the final leaderboard API."""
print("Submitting answers to the leaderboard...")
headers = {"Content-Type": "application/json"}
response = requests.post(f"{API_BASE_URL}/submit", json=submission_payload, headers=headers)
if response.status_code == 200:
print("πŸŽ‰ Submission Successful!")
print("API Response:", response.json())
else:
print(f"❌ Submission Failed ({response.status_code}):", response.text)
def main():
questions = fetch_questions()
submitted_answers = []
# Loop through all 20 questions
for item in questions:
task_id = item.get("task_id")
question = item.get("question")
has_file = item.get("has_file", False)
print(f"\nProcessing Task ID: {task_id}")
# If the task has an associated file, download it first
if has_file:
file_path = download_task_file(task_id)
print(f"Downloaded file for task to: {file_path}")
# Run your agent framework here
answer = dummy_agent_fallback(question)
print(f"Agent Output: {answer}")
# Format for API specification
submitted_answers.append({
"task_id": task_id,
"submitted_answer": str(answer).strip()
})
# Build final payload
payload = {
"username": HF_USERNAME,
"agent_code": AGENT_CODE_URL,
"answers": submitted_answers
}
# Post results
submit_answers(payload)
if __name__ == "__main__":
main()