Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,36 +19,65 @@ def answer_question(question):
|
|
| 19 |
|
| 20 |
def submit_to_gaia(username, space_link):
|
| 21 |
GAIA_API_URL = "https://huggingface.co/api/gaia"
|
| 22 |
-
|
| 23 |
try:
|
| 24 |
-
# 1
|
| 25 |
response = requests.get(f"{GAIA_API_URL}/questions")
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# 2. Generate answers using your agent
|
| 29 |
answers = []
|
| 30 |
-
for q in questions:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
payload = {
|
| 41 |
-
"username": username,
|
| 42 |
-
"agent_code": space_link,
|
| 43 |
"answers": answers
|
| 44 |
}
|
|
|
|
|
|
|
| 45 |
submit_response = requests.post(f"{GAIA_API_URL}/submit", json=payload)
|
| 46 |
-
result = submit_response.json()
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
return f"β Submission failed
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
with gr.Blocks() as demo:
|
|
|
|
| 19 |
|
| 20 |
def submit_to_gaia(username, space_link):
|
| 21 |
GAIA_API_URL = "https://huggingface.co/api/gaia"
|
| 22 |
+
|
| 23 |
try:
|
| 24 |
+
# Step 1: Fetch questions
|
| 25 |
response = requests.get(f"{GAIA_API_URL}/questions")
|
| 26 |
+
if response.status_code != 200:
|
| 27 |
+
return f"β Failed to fetch tasks. HTTP {response.status_code}: {response.text}"
|
| 28 |
+
|
| 29 |
+
questions = response.json()
|
| 30 |
+
if not isinstance(questions, list):
|
| 31 |
+
return f"β Unexpected response format: {questions}"
|
| 32 |
|
|
|
|
| 33 |
answers = []
|
| 34 |
+
for idx, q in enumerate(questions):
|
| 35 |
+
try:
|
| 36 |
+
task_id = q["task_id"]
|
| 37 |
+
question_text = q["question"]
|
| 38 |
+
print(f"β‘οΈ [{idx+1}/{len(questions)}] Answering: {question_text}")
|
| 39 |
+
|
| 40 |
+
agent_answer = agent.search_advice(question_text)
|
| 41 |
+
cleaned_answer = (agent_answer or "").strip()
|
| 42 |
+
|
| 43 |
+
if not cleaned_answer:
|
| 44 |
+
print(f"β οΈ Warning: Empty answer for task {task_id}")
|
| 45 |
+
|
| 46 |
+
answers.append({
|
| 47 |
+
"task_id": task_id,
|
| 48 |
+
"submitted_answer": cleaned_answer
|
| 49 |
+
})
|
| 50 |
+
|
| 51 |
+
except Exception as inner_e:
|
| 52 |
+
print(f"β Error on question {idx+1}: {inner_e}")
|
| 53 |
+
continue
|
| 54 |
+
|
| 55 |
+
if not answers:
|
| 56 |
+
return "β No answers were collected. All tasks failed or returned empty."
|
| 57 |
+
|
| 58 |
payload = {
|
| 59 |
+
"username": username.strip(),
|
| 60 |
+
"agent_code": space_link.strip(),
|
| 61 |
"answers": answers
|
| 62 |
}
|
| 63 |
+
|
| 64 |
+
print("π Submitting answers to GAIA...")
|
| 65 |
submit_response = requests.post(f"{GAIA_API_URL}/submit", json=payload)
|
|
|
|
| 66 |
|
| 67 |
+
try:
|
| 68 |
+
result = submit_response.json()
|
| 69 |
+
except Exception:
|
| 70 |
+
return f"β Failed to parse submission response:\n{submit_response.text}"
|
| 71 |
+
|
| 72 |
+
return f"""β
Submission complete!
|
| 73 |
+
Score: {result.get('score', 'N/A')}
|
| 74 |
+
Status: {result.get('status', 'N/A')}
|
| 75 |
+
Message: {result.get('message', 'No message')}
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
except Exception as e:
|
| 79 |
+
return f"β Submission failed: {str(e)}"
|
| 80 |
+
|
| 81 |
|
| 82 |
|
| 83 |
with gr.Blocks() as demo:
|