Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,9 +14,18 @@ def run_and_submit_all(profile):
|
|
14 |
Fetch all questions, run the agent on them, submit all answers,
|
15 |
and return the status and results table.
|
16 |
"""
|
17 |
-
|
|
|
18 |
return "Please login to Hugging Face with the login button.", None
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Instantiate agent
|
22 |
try:
|
@@ -24,40 +33,49 @@ def run_and_submit_all(profile):
|
|
24 |
except Exception as e:
|
25 |
return f"Error initializing agent: {e}", None
|
26 |
|
27 |
-
agent_code = f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main"
|
28 |
-
|
29 |
# Fetch questions
|
30 |
try:
|
31 |
-
resp = requests.get(
|
32 |
resp.raise_for_status()
|
33 |
questions = resp.json()
|
34 |
except Exception as e:
|
35 |
return f"Error fetching questions: {e}", None
|
36 |
|
37 |
-
|
38 |
-
return "No questions fetched.", None
|
39 |
-
|
40 |
-
# Run agent on each question
|
41 |
results = []
|
42 |
-
|
43 |
-
for
|
44 |
-
|
45 |
-
|
46 |
-
if not
|
47 |
continue
|
48 |
try:
|
49 |
-
ans = agent.run(question=
|
50 |
except Exception as e:
|
51 |
ans = f"ERROR: {e}"
|
52 |
-
results.append({
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Submit answers
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
try:
|
58 |
-
|
59 |
-
|
60 |
-
data =
|
61 |
status = (
|
62 |
f"Submission Successful!\n"
|
63 |
f"User: {data.get('username')}\n"
|
@@ -72,17 +90,21 @@ def run_and_submit_all(profile):
|
|
72 |
|
73 |
def test_random_question(profile):
|
74 |
"""
|
75 |
-
Fetch a random question and return the
|
76 |
"""
|
77 |
-
if profile
|
78 |
return "Please login to Hugging Face with the login button.", ""
|
|
|
|
|
79 |
try:
|
80 |
q = fetch_random_question()
|
|
|
81 |
agent = create_agent()
|
82 |
-
|
83 |
-
return
|
84 |
except Exception as e:
|
85 |
-
return f"Error: {e}", ""
|
|
|
86 |
|
87 |
# --- Gradio Interface ---
|
88 |
with gr.Blocks() as demo:
|
@@ -90,28 +112,25 @@ with gr.Blocks() as demo:
|
|
90 |
gr.Markdown(
|
91 |
"""
|
92 |
**Instructions:**
|
93 |
-
1. Clone this space and
|
94 |
2. Log in with your Hugging Face account using the login button below.
|
95 |
-
3. Use
|
96 |
"""
|
97 |
)
|
98 |
|
99 |
login = gr.LoginButton()
|
100 |
-
state = gr.State(value=None)
|
101 |
-
# Store OAuthProfile in state when user logs in
|
102 |
-
login.click(fn=lambda profile: profile, inputs=[], outputs=[state])
|
103 |
|
104 |
-
|
105 |
-
|
106 |
|
107 |
status_output = gr.Textbox(label="Status / Result", lines=5, interactive=False)
|
108 |
-
results_table = gr.DataFrame(label="Results Table", wrap=True)
|
109 |
question_box = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
110 |
answer_box = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
111 |
|
112 |
-
|
113 |
-
|
|
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
demo.launch(debug=True, share=False)
|
117 |
-
|
|
|
14 |
Fetch all questions, run the agent on them, submit all answers,
|
15 |
and return the status and results table.
|
16 |
"""
|
17 |
+
# Check login
|
18 |
+
if not profile:
|
19 |
return "Please login to Hugging Face with the login button.", None
|
20 |
+
|
21 |
+
# Extract username (support both .username and .name)
|
22 |
+
username = getattr(profile, "username", None) or getattr(profile, "name", None)
|
23 |
+
if not username:
|
24 |
+
return "Login error: username not found.", None
|
25 |
+
|
26 |
+
# Build URLs
|
27 |
+
questions_url = f"{DEFAULT_API_URL}/questions"
|
28 |
+
submit_url = f"{DEFAULT_API_URL}/submit"
|
29 |
|
30 |
# Instantiate agent
|
31 |
try:
|
|
|
33 |
except Exception as e:
|
34 |
return f"Error initializing agent: {e}", None
|
35 |
|
|
|
|
|
36 |
# Fetch questions
|
37 |
try:
|
38 |
+
resp = requests.get(questions_url, timeout=15)
|
39 |
resp.raise_for_status()
|
40 |
questions = resp.json()
|
41 |
except Exception as e:
|
42 |
return f"Error fetching questions: {e}", None
|
43 |
|
44 |
+
# Run agent
|
|
|
|
|
|
|
45 |
results = []
|
46 |
+
answers_payload = []
|
47 |
+
for q in questions:
|
48 |
+
tid = q.get("task_id")
|
49 |
+
text = q.get("question")
|
50 |
+
if not tid or not text:
|
51 |
continue
|
52 |
try:
|
53 |
+
ans = agent.run(question=text)
|
54 |
except Exception as e:
|
55 |
ans = f"ERROR: {e}"
|
56 |
+
results.append({
|
57 |
+
"Task ID": tid,
|
58 |
+
"Question": text,
|
59 |
+
"Answer": ans
|
60 |
+
})
|
61 |
+
answers_payload.append({
|
62 |
+
"task_id": tid,
|
63 |
+
"submitted_answer": ans
|
64 |
+
})
|
65 |
+
|
66 |
+
if not answers_payload:
|
67 |
+
return "Agent returned no answers.", pd.DataFrame(results)
|
68 |
|
69 |
# Submit answers
|
70 |
+
payload = {
|
71 |
+
"username": username,
|
72 |
+
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
|
73 |
+
"answers": answers_payload
|
74 |
+
}
|
75 |
try:
|
76 |
+
resp = requests.post(submit_url, json=payload, timeout=60)
|
77 |
+
resp.raise_for_status()
|
78 |
+
data = resp.json()
|
79 |
status = (
|
80 |
f"Submission Successful!\n"
|
81 |
f"User: {data.get('username')}\n"
|
|
|
90 |
|
91 |
def test_random_question(profile):
|
92 |
"""
|
93 |
+
Fetch a random GAIA question and return the agent's answer.
|
94 |
"""
|
95 |
+
if not profile:
|
96 |
return "Please login to Hugging Face with the login button.", ""
|
97 |
+
|
98 |
+
# Get question and run agent
|
99 |
try:
|
100 |
q = fetch_random_question()
|
101 |
+
question = q.get("question", "")
|
102 |
agent = create_agent()
|
103 |
+
answer = agent.run(question=question)
|
104 |
+
return question, answer
|
105 |
except Exception as e:
|
106 |
+
return f"Error during test: {e}", ""
|
107 |
+
|
108 |
|
109 |
# --- Gradio Interface ---
|
110 |
with gr.Blocks() as demo:
|
|
|
112 |
gr.Markdown(
|
113 |
"""
|
114 |
**Instructions:**
|
115 |
+
1. Clone this space and define your agent logic in `agent.py`.
|
116 |
2. Log in with your Hugging Face account using the login button below.
|
117 |
+
3. Use **Run Evaluation & Submit All Answers** or **Test Random Question**.
|
118 |
"""
|
119 |
)
|
120 |
|
121 |
login = gr.LoginButton()
|
|
|
|
|
|
|
122 |
|
123 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
124 |
+
test_button = gr.Button("Test Random Question")
|
125 |
|
126 |
status_output = gr.Textbox(label="Status / Result", lines=5, interactive=False)
|
127 |
+
results_table = gr.DataFrame(label="Full Results Table", wrap=True)
|
128 |
question_box = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
129 |
answer_box = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
130 |
|
131 |
+
# Wire the login component directly into the callbacks
|
132 |
+
run_button.click(fn=run_and_submit_all, inputs=[login], outputs=[status_output, results_table])
|
133 |
+
test_button.click(fn=test_random_question, inputs=[login], outputs=[question_box, answer_box])
|
134 |
|
135 |
if __name__ == "__main__":
|
136 |
demo.launch(debug=True, share=False)
|
|