Spaces:
Restarting
Restarting
Update app.py
Browse files
app.py
CHANGED
@@ -19,13 +19,14 @@ Build your agent, score **≥30%** to earn your Certificate,
|
|
19 |
and see where you land on the Student Leaderboard!
|
20 |
"""
|
21 |
|
22 |
-
# --- Simple HF-Inference Agent ---
|
23 |
class GAIAAgent:
|
24 |
def __init__(self, model_id: str, token: str):
|
|
|
25 |
self.model_id = model_id
|
26 |
self.headers = {"Authorization": f"Bearer {token}"}
|
27 |
|
28 |
def answer(self, prompt: str) -> str:
|
|
|
29 |
payload = {
|
30 |
"inputs": prompt,
|
31 |
"parameters": {"max_new_tokens": 512, "temperature": 0.2}
|
@@ -34,18 +35,24 @@ class GAIAAgent:
|
|
34 |
resp = requests.post(url, headers=self.headers, json=payload, timeout=60)
|
35 |
resp.raise_for_status()
|
36 |
data = resp.json()
|
|
|
37 |
if isinstance(data, list) and data and "generated_text" in data[0]:
|
38 |
return data[0]["generated_text"].strip()
|
39 |
return str(data)
|
40 |
|
41 |
-
# --- Gradio callback ---
|
42 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
43 |
try:
|
|
|
44 |
if profile is None:
|
|
|
45 |
return ("⚠️ Please log in with your Hugging Face account.", pd.DataFrame())
|
|
|
46 |
username = profile.username
|
|
|
47 |
hf_token = HF_TOKEN_ENV or getattr(profile, "access_token", None)
|
|
|
48 |
if not hf_token:
|
|
|
49 |
return (
|
50 |
"❌ No Hugging Face token found.\n"
|
51 |
"Set HUGGINGFACEHUB_API_TOKEN in Secrets or log in via the button.",
|
@@ -53,9 +60,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
53 |
)
|
54 |
|
55 |
# 1) Fetch GAIA questions
|
|
|
56 |
q_resp = requests.get(f"{API_URL}/questions", timeout=15)
|
57 |
q_resp.raise_for_status()
|
58 |
questions = q_resp.json() or []
|
|
|
59 |
if not questions:
|
60 |
return ("❌ No questions found. Check your API_URL.", pd.DataFrame())
|
61 |
|
@@ -66,21 +75,25 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
66 |
results = []
|
67 |
payload = []
|
68 |
for item in questions:
|
|
|
69 |
tid = item.get("task_id")
|
70 |
qtxt = item.get("question", "")
|
71 |
try:
|
72 |
ans = agent.answer(qtxt)
|
73 |
except Exception as e:
|
74 |
ans = f"ERROR: {e}"
|
|
|
75 |
results.append({"Task ID": tid, "Question": qtxt, "Answer": ans})
|
76 |
payload.append({"task_id": tid, "submitted_answer": ans})
|
77 |
time.sleep(0.5)
|
78 |
|
79 |
# 4) Submit
|
|
|
80 |
submission = {"username": username, "answers": payload}
|
81 |
s_resp = requests.post(f"{API_URL}/submit", json=submission, timeout=60)
|
82 |
s_resp.raise_for_status()
|
83 |
data = s_resp.json()
|
|
|
84 |
|
85 |
# 5) Build status text
|
86 |
status = (
|
@@ -94,10 +107,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
94 |
|
95 |
except Exception as e:
|
96 |
tb = traceback.format_exc()
|
97 |
-
print("[ERROR]", tb)
|
98 |
return (f"❌ Unexpected error:\n{e}", pd.DataFrame())
|
99 |
|
100 |
-
# --- Gradio UI ---
|
101 |
with gr.Blocks() as demo:
|
102 |
gr.Markdown(WELCOME)
|
103 |
login = gr.LoginButton()
|
|
|
19 |
and see where you land on the Student Leaderboard!
|
20 |
"""
|
21 |
|
|
|
22 |
class GAIAAgent:
|
23 |
def __init__(self, model_id: str, token: str):
|
24 |
+
print(f"[DEBUG] Initializing GAIAAgent with model={model_id}") # debug
|
25 |
self.model_id = model_id
|
26 |
self.headers = {"Authorization": f"Bearer {token}"}
|
27 |
|
28 |
def answer(self, prompt: str) -> str:
|
29 |
+
print(f"[DEBUG] Sending prompt of length {len(prompt)} to HF Inference") # debug
|
30 |
payload = {
|
31 |
"inputs": prompt,
|
32 |
"parameters": {"max_new_tokens": 512, "temperature": 0.2}
|
|
|
35 |
resp = requests.post(url, headers=self.headers, json=payload, timeout=60)
|
36 |
resp.raise_for_status()
|
37 |
data = resp.json()
|
38 |
+
print(f"[DEBUG] Got response from model: {data!r}") # debug
|
39 |
if isinstance(data, list) and data and "generated_text" in data[0]:
|
40 |
return data[0]["generated_text"].strip()
|
41 |
return str(data)
|
42 |
|
|
|
43 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
44 |
try:
|
45 |
+
print("[DEBUG] run_and_submit_all called") # debug
|
46 |
if profile is None:
|
47 |
+
print("[DEBUG] No profile provided") # debug
|
48 |
return ("⚠️ Please log in with your Hugging Face account.", pd.DataFrame())
|
49 |
+
print(f"[DEBUG] Logged in as: {profile.username}") # debug
|
50 |
username = profile.username
|
51 |
+
|
52 |
hf_token = HF_TOKEN_ENV or getattr(profile, "access_token", None)
|
53 |
+
print(f"[DEBUG] Using HF token from {'env' if HF_TOKEN_ENV else 'profile'}") # debug
|
54 |
if not hf_token:
|
55 |
+
print("[DEBUG] No HF token found") # debug
|
56 |
return (
|
57 |
"❌ No Hugging Face token found.\n"
|
58 |
"Set HUGGINGFACEHUB_API_TOKEN in Secrets or log in via the button.",
|
|
|
60 |
)
|
61 |
|
62 |
# 1) Fetch GAIA questions
|
63 |
+
print(f"[DEBUG] Fetching questions from {API_URL}/questions") # debug
|
64 |
q_resp = requests.get(f"{API_URL}/questions", timeout=15)
|
65 |
q_resp.raise_for_status()
|
66 |
questions = q_resp.json() or []
|
67 |
+
print(f"[DEBUG] Received {len(questions)} questions") # debug
|
68 |
if not questions:
|
69 |
return ("❌ No questions found. Check your API_URL.", pd.DataFrame())
|
70 |
|
|
|
75 |
results = []
|
76 |
payload = []
|
77 |
for item in questions:
|
78 |
+
print(f"[DEBUG] Processing task_id={item.get('task_id')}") # debug
|
79 |
tid = item.get("task_id")
|
80 |
qtxt = item.get("question", "")
|
81 |
try:
|
82 |
ans = agent.answer(qtxt)
|
83 |
except Exception as e:
|
84 |
ans = f"ERROR: {e}"
|
85 |
+
print(f"[DEBUG] Error answering: {e}") # debug
|
86 |
results.append({"Task ID": tid, "Question": qtxt, "Answer": ans})
|
87 |
payload.append({"task_id": tid, "submitted_answer": ans})
|
88 |
time.sleep(0.5)
|
89 |
|
90 |
# 4) Submit
|
91 |
+
print(f"[DEBUG] Submitting payload with {len(payload)} answers") # debug
|
92 |
submission = {"username": username, "answers": payload}
|
93 |
s_resp = requests.post(f"{API_URL}/submit", json=submission, timeout=60)
|
94 |
s_resp.raise_for_status()
|
95 |
data = s_resp.json()
|
96 |
+
print(f"[DEBUG] Submission response: {data!r}") # debug
|
97 |
|
98 |
# 5) Build status text
|
99 |
status = (
|
|
|
107 |
|
108 |
except Exception as e:
|
109 |
tb = traceback.format_exc()
|
110 |
+
print("[ERROR] Unhandled exception:\n", tb)
|
111 |
return (f"❌ Unexpected error:\n{e}", pd.DataFrame())
|
112 |
|
|
|
113 |
with gr.Blocks() as demo:
|
114 |
gr.Markdown(WELCOME)
|
115 |
login = gr.LoginButton()
|