Spaces:
Restarting
Restarting
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,56 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import os
|
4 |
import time
|
5 |
import requests
|
6 |
import pandas as pd
|
7 |
import gradio as gr
|
8 |
|
9 |
-
from
|
10 |
-
CodeAgent,
|
11 |
-
DuckDuckGoSearchTool,
|
12 |
-
PythonInterpreterTool,
|
13 |
-
InferenceClientModel
|
14 |
-
)
|
15 |
-
|
16 |
-
# --- Configuration ---
|
17 |
API_URL = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
|
18 |
-
SPACE_ID = os.getenv("SPACE_ID")
|
19 |
-
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
20 |
-
|
21 |
|
22 |
if not all([SPACE_ID, HF_TOKEN]):
|
23 |
raise RuntimeError(
|
24 |
-
"Please set
|
25 |
-
"
|
26 |
-
"
|
27 |
)
|
28 |
|
29 |
-
|
30 |
-
##
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
- You need **≥30%** to earn your Certificate of Completion. 🏅
|
35 |
-
- Submit your score and see how you stack up on the Student Leaderboard!
|
36 |
"""
|
37 |
|
38 |
-
# --- Agent
|
39 |
class GAIAAgent:
|
40 |
-
def __init__(self, model_id
|
41 |
-
|
42 |
-
self.
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
if task_file:
|
63 |
-
try:
|
64 |
-
with open(task_file, "r") as f:
|
65 |
-
content = f.read()
|
66 |
-
prompt += f"\n\nAttached file:\n```\n{content}\n```"
|
67 |
-
except:
|
68 |
-
pass
|
69 |
-
return self.agent.run(prompt)
|
70 |
-
|
71 |
-
# --- Runner & Submission ---
|
72 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
73 |
if profile is None:
|
74 |
return "⚠️ Please log in with your Hugging Face account.", pd.DataFrame()
|
75 |
-
|
76 |
username = profile.username
|
77 |
|
78 |
# 1) Fetch GAIA questions
|
@@ -80,36 +58,27 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
80 |
q_resp.raise_for_status()
|
81 |
questions = q_resp.json() or []
|
82 |
if not questions:
|
83 |
-
return "❌ No questions
|
84 |
|
85 |
-
# 2)
|
86 |
-
agent = GAIAAgent()
|
87 |
|
88 |
-
# 3)
|
89 |
-
results
|
|
|
90 |
for item in questions:
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
try:
|
96 |
-
|
97 |
except Exception as e:
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
"Question": question,
|
103 |
-
"Answer": answer
|
104 |
-
})
|
105 |
-
payload.append({
|
106 |
-
"task_id": task_id,
|
107 |
-
"submitted_answer": answer
|
108 |
-
})
|
109 |
-
|
110 |
-
time.sleep(0.5) # throttle requests
|
111 |
|
112 |
-
# 4) Submit
|
113 |
submission = {
|
114 |
"username": username,
|
115 |
"agent_code": f"https://huggingface.co/spaces/{SPACE_ID}/tree/main",
|
@@ -119,7 +88,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
119 |
s_resp.raise_for_status()
|
120 |
data = s_resp.json()
|
121 |
|
122 |
-
# 5) Build status
|
123 |
status = (
|
124 |
f"✅ **Submission Successful!**\n\n"
|
125 |
f"**User:** {data.get('username')}\n"
|
@@ -127,22 +96,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
127 |
f"({data.get('correct_count')}/{data.get('total_attempted')} correct)\n"
|
128 |
f"**Message:** {data.get('message')}"
|
129 |
)
|
130 |
-
|
131 |
return status, pd.DataFrame(results)
|
132 |
|
133 |
-
|
134 |
-
# --- Gradio Interface ---
|
135 |
with gr.Blocks() as demo:
|
136 |
-
gr.Markdown(
|
137 |
login = gr.LoginButton()
|
138 |
-
run_btn
|
139 |
-
|
140 |
-
|
141 |
|
142 |
run_btn.click(
|
143 |
fn=run_and_submit_all,
|
144 |
inputs=[login],
|
145 |
-
outputs=[
|
146 |
)
|
147 |
|
148 |
if __name__ == "__main__":
|
|
|
|
|
|
|
1 |
import os
|
2 |
import time
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
import gradio as gr
|
6 |
|
7 |
+
# --- Config from Env ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
API_URL = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
|
9 |
+
SPACE_ID = os.getenv("SPACE_ID")
|
10 |
+
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
11 |
+
MODEL_ID = os.getenv("MODEL_ID", "meta-llama/Llama-2-7b-instruct")
|
12 |
|
13 |
if not all([SPACE_ID, HF_TOKEN]):
|
14 |
raise RuntimeError(
|
15 |
+
"❌ Please set these in your Space Secrets:\n"
|
16 |
+
" • SPACE_ID (e.g. user/your-space)\n"
|
17 |
+
" • HUGGINGFACEHUB_API_TOKEN"
|
18 |
)
|
19 |
|
20 |
+
WELCOME = """
|
21 |
+
## GAIA Benchmark Runner 🎉
|
22 |
|
23 |
+
Build your agent, score **≥30%** to earn the Certificate of Completion,
|
24 |
+
and see where you land on the Student Leaderboard!
|
|
|
|
|
25 |
"""
|
26 |
|
27 |
+
# --- Simple HF-Inference Agent ---
|
28 |
class GAIAAgent:
|
29 |
+
def __init__(self, model_id: str, token: str):
|
30 |
+
self.model_id = model_id
|
31 |
+
self.headers = {"Authorization": f"Bearer {token}"}
|
32 |
+
|
33 |
+
def answer(self, prompt: str) -> str:
|
34 |
+
payload = {
|
35 |
+
"inputs": prompt,
|
36 |
+
"parameters": {
|
37 |
+
"max_new_tokens": 512,
|
38 |
+
"temperature": 0.2
|
39 |
+
}
|
40 |
+
}
|
41 |
+
url = f"https://api-inference.huggingface.co/models/{self.model_id}"
|
42 |
+
resp = requests.post(url, headers=self.headers, json=payload, timeout=60)
|
43 |
+
resp.raise_for_status()
|
44 |
+
data = resp.json()
|
45 |
+
# data is a list of {generated_text: "..."}
|
46 |
+
if isinstance(data, list) and data and "generated_text" in data[0]:
|
47 |
+
return data[0]["generated_text"].strip()
|
48 |
+
return str(data)
|
49 |
+
|
50 |
+
# --- Gradio callback ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
52 |
if profile is None:
|
53 |
return "⚠️ Please log in with your Hugging Face account.", pd.DataFrame()
|
|
|
54 |
username = profile.username
|
55 |
|
56 |
# 1) Fetch GAIA questions
|
|
|
58 |
q_resp.raise_for_status()
|
59 |
questions = q_resp.json() or []
|
60 |
if not questions:
|
61 |
+
return "❌ No questions found. Check your API_URL.", pd.DataFrame()
|
62 |
|
63 |
+
# 2) Init agent
|
64 |
+
agent = GAIAAgent(MODEL_ID, HF_TOKEN)
|
65 |
|
66 |
+
# 3) Answer each
|
67 |
+
results = []
|
68 |
+
payload = []
|
69 |
for item in questions:
|
70 |
+
tid = item.get("task_id")
|
71 |
+
qtxt = item.get("question","")
|
72 |
+
# Some tasks include a file path; you can fetch and append it if you like.
|
|
|
73 |
try:
|
74 |
+
ans = agent.answer(qtxt)
|
75 |
except Exception as e:
|
76 |
+
ans = f"ERROR: {e}"
|
77 |
+
results.append({"Task ID": tid, "Question": qtxt, "Answer": ans})
|
78 |
+
payload.append({"task_id": tid, "submitted_answer": ans})
|
79 |
+
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
# 4) Submit
|
82 |
submission = {
|
83 |
"username": username,
|
84 |
"agent_code": f"https://huggingface.co/spaces/{SPACE_ID}/tree/main",
|
|
|
88 |
s_resp.raise_for_status()
|
89 |
data = s_resp.json()
|
90 |
|
91 |
+
# 5) Build status text
|
92 |
status = (
|
93 |
f"✅ **Submission Successful!**\n\n"
|
94 |
f"**User:** {data.get('username')}\n"
|
|
|
96 |
f"({data.get('correct_count')}/{data.get('total_attempted')} correct)\n"
|
97 |
f"**Message:** {data.get('message')}"
|
98 |
)
|
|
|
99 |
return status, pd.DataFrame(results)
|
100 |
|
101 |
+
# --- Gradio UI ---
|
|
|
102 |
with gr.Blocks() as demo:
|
103 |
+
gr.Markdown(WELCOME)
|
104 |
login = gr.LoginButton()
|
105 |
+
run_btn = gr.Button("▶️ Run GAIA Benchmark")
|
106 |
+
status_md = gr.Markdown()
|
107 |
+
table_df = gr.Dataframe(headers=["Task ID","Question","Answer"], wrap=True)
|
108 |
|
109 |
run_btn.click(
|
110 |
fn=run_and_submit_all,
|
111 |
inputs=[login],
|
112 |
+
outputs=[status_md, table_df]
|
113 |
)
|
114 |
|
115 |
if __name__ == "__main__":
|