Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,10 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
# -------------------------------
|
| 7 |
-
# Constants
|
| 8 |
-
# -------------------------------
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
# -------------------------------
|
| 12 |
-
# Basic
|
| 13 |
# -------------------------------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
|
@@ -17,70 +14,53 @@ class BasicAgent:
|
|
| 17 |
|
| 18 |
def __call__(self, question: str) -> str:
|
| 19 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 20 |
-
# TODO: 這裡改成你的 GAIA Agent 邏輯
|
| 21 |
return "This is a default answer."
|
| 22 |
|
| 23 |
# -------------------------------
|
| 24 |
# Run & Submit Function
|
| 25 |
# -------------------------------
|
| 26 |
def run_and_submit_all(profile=None):
|
| 27 |
-
|
| 28 |
-
Fetch questions, run the agent, submit answers, return results
|
| 29 |
-
profile: Either HF profile (from LoginButton) or mock_profile dict
|
| 30 |
-
"""
|
| 31 |
-
# --- HF Username ---
|
| 32 |
if profile and hasattr(profile, "username"):
|
| 33 |
username = profile.username
|
| 34 |
-
elif isinstance(profile, dict) and "username" in profile:
|
| 35 |
-
username = profile["username"]
|
| 36 |
else:
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
# --- Agent & Code Link ---
|
| 40 |
agent = BasicAgent()
|
| 41 |
space_id = os.getenv("SPACE_ID", "unknown")
|
| 42 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 43 |
|
| 44 |
-
#
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
except Exception as e:
|
| 50 |
return f"❌ Failed to fetch questions: {e}", None
|
| 51 |
|
| 52 |
-
if not
|
| 53 |
return "❌ No questions fetched.", None
|
| 54 |
|
| 55 |
-
#
|
| 56 |
results_log = []
|
| 57 |
answers_payload = []
|
| 58 |
-
for item in
|
| 59 |
task_id = item.get("task_id")
|
| 60 |
question_text = item.get("question")
|
| 61 |
if not task_id or not question_text:
|
| 62 |
continue
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
results_log.append({
|
| 73 |
-
"Task ID": task_id,
|
| 74 |
-
"Question": question_text,
|
| 75 |
-
"Submitted Answer": f"AGENT ERROR: {e}"
|
| 76 |
-
})
|
| 77 |
-
|
| 78 |
-
if not answers_payload:
|
| 79 |
-
return "❌ Agent did not produce any answers.", pd.DataFrame(results_log)
|
| 80 |
-
|
| 81 |
-
# --- Submit Answers ---
|
| 82 |
submission_data = {
|
| 83 |
-
"username": username
|
| 84 |
"agent_code": agent_code,
|
| 85 |
"answers": answers_payload
|
| 86 |
}
|
|
@@ -107,26 +87,23 @@ with gr.Blocks() as demo:
|
|
| 107 |
gr.Markdown("# Basic GAIA Agent Evaluation Runner")
|
| 108 |
gr.Markdown(
|
| 109 |
"""
|
| 110 |
-
|
|
|
|
| 111 |
2. Click 'Run Evaluation & Submit All Answers'.
|
| 112 |
"""
|
| 113 |
)
|
| 114 |
|
| 115 |
-
# HF LoginButton only works in HF Space
|
| 116 |
login_btn = gr.LoginButton()
|
| 117 |
-
|
| 118 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 119 |
-
|
| 120 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 121 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 122 |
|
| 123 |
-
#
|
| 124 |
run_button.click(
|
| 125 |
-
fn=lambda profile: run_and_submit_all(profile
|
| 126 |
inputs=login_btn,
|
| 127 |
outputs=[status_output, results_table]
|
| 128 |
)
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
| 131 |
-
print("Launching Gradio Interface...")
|
| 132 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 7 |
|
| 8 |
# -------------------------------
|
| 9 |
+
# Basic GAIA Agent
|
| 10 |
# -------------------------------
|
| 11 |
class BasicAgent:
|
| 12 |
def __init__(self):
|
|
|
|
| 14 |
|
| 15 |
def __call__(self, question: str) -> str:
|
| 16 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 17 |
return "This is a default answer."
|
| 18 |
|
| 19 |
# -------------------------------
|
| 20 |
# Run & Submit Function
|
| 21 |
# -------------------------------
|
| 22 |
def run_and_submit_all(profile=None):
|
| 23 |
+
# HF Space Login Profile or local mock
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if profile and hasattr(profile, "username"):
|
| 25 |
username = profile.username
|
|
|
|
|
|
|
| 26 |
else:
|
| 27 |
+
# 本地測試用 mock profile
|
| 28 |
+
username = "local_user"
|
| 29 |
|
|
|
|
| 30 |
agent = BasicAgent()
|
| 31 |
space_id = os.getenv("SPACE_ID", "unknown")
|
| 32 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 33 |
|
| 34 |
+
# Fetch Questions
|
| 35 |
try:
|
| 36 |
+
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
| 37 |
+
resp.raise_for_status()
|
| 38 |
+
questions = resp.json()
|
| 39 |
except Exception as e:
|
| 40 |
return f"❌ Failed to fetch questions: {e}", None
|
| 41 |
|
| 42 |
+
if not questions:
|
| 43 |
return "❌ No questions fetched.", None
|
| 44 |
|
| 45 |
+
# Run Agent
|
| 46 |
results_log = []
|
| 47 |
answers_payload = []
|
| 48 |
+
for item in questions:
|
| 49 |
task_id = item.get("task_id")
|
| 50 |
question_text = item.get("question")
|
| 51 |
if not task_id or not question_text:
|
| 52 |
continue
|
| 53 |
+
ans = agent(question_text)
|
| 54 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": ans})
|
| 55 |
+
results_log.append({
|
| 56 |
+
"Task ID": task_id,
|
| 57 |
+
"Question": question_text,
|
| 58 |
+
"Submitted Answer": ans
|
| 59 |
+
})
|
| 60 |
+
|
| 61 |
+
# Submit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
submission_data = {
|
| 63 |
+
"username": username,
|
| 64 |
"agent_code": agent_code,
|
| 65 |
"answers": answers_payload
|
| 66 |
}
|
|
|
|
| 87 |
gr.Markdown("# Basic GAIA Agent Evaluation Runner")
|
| 88 |
gr.Markdown(
|
| 89 |
"""
|
| 90 |
+
**Instructions:**
|
| 91 |
+
1. Log in with Hugging Face (Space only) or test locally with mock user.
|
| 92 |
2. Click 'Run Evaluation & Submit All Answers'.
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
|
|
|
|
| 96 |
login_btn = gr.LoginButton()
|
|
|
|
| 97 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 98 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 99 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 100 |
|
| 101 |
+
# HF Space or local: 使用 lambda 包裝 profile
|
| 102 |
run_button.click(
|
| 103 |
+
fn=lambda profile: run_and_submit_all(profile),
|
| 104 |
inputs=login_btn,
|
| 105 |
outputs=[status_output, results_table]
|
| 106 |
)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
|
|
|
| 109 |
demo.launch(debug=True, share=False)
|