FD900's picture
Update app.py
6d03c10 verified
raw
history blame
2.21 kB
import gradio as gr
import json
import os
import requests
from agent import solve_task, load_tasks
from mistral_hf_wrapper import MistralInference
API_URL = os.getenv("HF_MISTRAL_ENDPOINT")
API_TOKEN = os.getenv("HF_TOKEN")
USERNAME = os.getenv("HF_USERNAME")
CODE_LINK = os.getenv("HF_CODE_LINK")
def run_and_submit_all():
model = MistralInference(api_url=API_URL, api_token=API_TOKEN)
tasks = load_tasks()
if not tasks:
return "No tasks loaded from metadata.jsonl. Make sure the file exists and is valid."
print(f"[INFO] Loaded {len(tasks)} tasks from metadata.jsonl")
answers = []
for i, task in enumerate(tasks):
print(f"[INFO] Solving task {i+1}/{len(tasks)}: {task.get('question_id', 'N/A')}")
try:
result = solve_task(task, model)
if not result.get("submitted_answer"): # Check empty responses
print("[WARN] Empty model response detected")
result["submitted_answer"] = "ERROR: Empty model response"
else:
print(f"Answer: {result['submitted_answer'][:100]}...")
answers.append(result)
except Exception as e:
print(f"[ERROR] Task failed: {e}")
answers.append({
"question_id": task.get("question_id", "UNKNOWN"),
"submitted_answer": f"ERROR: {str(e)}"
})
if not answers:
return "No answers generated. Check model response."
print("[INFO] Submitting answers to GAIA benchmark API...")
res = requests.post(
"https://agents-course-unit4-scoring.hf.space/submit",
headers={"Content-Type": "application/json"},
json={
"username": USERNAME,
"agent_code": CODE_LINK,
"answers": answers
},
)
if res.ok:
print(" Submission successful.")
return json.dumps(res.json(), indent=2)
print(f"[ERROR] Submission failed: {res.status_code} - {res.text}")
return f"Error submitting: {res.status_code} - {res.text}"
# Gradio interface
gr.Interface(
fn=run_and_submit_all,
inputs=[],
outputs="textbox",
title="GAIA Benchmark Agent Submission"
).launch()