Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,36 +3,52 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
|
6 |
-
from
|
|
|
7 |
|
|
|
8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def run_and_submit_all(profile):
|
|
|
11 |
if not profile:
|
12 |
return "Please login to Hugging Face with the login button.", None
|
13 |
username = getattr(profile, "username", None) or getattr(profile, "name", None)
|
14 |
if not username:
|
15 |
return "Login error: username not found.", None
|
16 |
|
17 |
-
# Fetch questions
|
18 |
try:
|
19 |
-
|
20 |
-
|
21 |
-
questions =
|
22 |
except Exception as e:
|
23 |
return f"Error fetching questions: {e}", None
|
24 |
|
25 |
-
#
|
26 |
-
agent =
|
27 |
-
|
28 |
-
# Run and collect answers
|
29 |
results, payload = [], []
|
30 |
for q in questions:
|
31 |
-
tid, text = q
|
32 |
-
if not (tid and text):
|
33 |
-
continue
|
34 |
try:
|
35 |
-
ans = agent
|
36 |
except Exception as e:
|
37 |
ans = f"ERROR: {e}"
|
38 |
results.append({"Task ID": tid, "Question": text, "Answer": ans})
|
@@ -41,20 +57,21 @@ def run_and_submit_all(profile):
|
|
41 |
if not payload:
|
42 |
return "Agent returned no answers.", pd.DataFrame(results)
|
43 |
|
44 |
-
# Submit
|
45 |
submission = {
|
46 |
"username": username,
|
47 |
-
"agent_code": f"https://huggingface.co/spaces/{
|
48 |
"answers": payload
|
49 |
}
|
50 |
try:
|
51 |
-
|
52 |
-
|
53 |
-
data =
|
54 |
status = (
|
55 |
f"Submission Successful!\n"
|
56 |
f"User: {data['username']}\n"
|
57 |
-
f"Score: {data['score']}%
|
|
|
58 |
f"Message: {data['message']}"
|
59 |
)
|
60 |
except Exception as e:
|
@@ -62,27 +79,25 @@ def run_and_submit_all(profile):
|
|
62 |
|
63 |
return status, pd.DataFrame(results)
|
64 |
|
65 |
-
|
66 |
def test_random_question(profile):
|
67 |
if not profile:
|
68 |
return "Please login to Hugging Face with the login button.", ""
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
|
|
|
76 |
with gr.Blocks() as demo:
|
77 |
-
gr.Markdown("#
|
78 |
-
gr.Markdown(
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
"""
|
85 |
-
)
|
86 |
|
87 |
login = gr.LoginButton()
|
88 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
@@ -93,8 +108,8 @@ with gr.Blocks() as demo:
|
|
93 |
question_out = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
94 |
answer_out = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
95 |
|
96 |
-
run_btn.click(
|
97 |
-
test_btn.click(
|
98 |
|
99 |
if __name__ == "__main__":
|
100 |
demo.launch(debug=True, share=False)
|
|
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
|
6 |
+
from smolagents import CodeAgent, OpenAIServerModel
|
7 |
+
from tools import FinalAnswerTool
|
8 |
|
9 |
+
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
+
class BasicAgent:
|
13 |
+
def __init__(self):
|
14 |
+
|
15 |
+
model = OpenAIServerModel(model_id="gpt-4o")
|
16 |
+
final_tool = FinalAnswerTool()
|
17 |
+
self.agent = CodeAgent(
|
18 |
+
model=model,
|
19 |
+
tools=[final_tool],
|
20 |
+
# opzioni aggiuntive, se vuoi:
|
21 |
+
max_steps=3,
|
22 |
+
verbosity_level=1
|
23 |
+
)
|
24 |
+
|
25 |
+
def __call__(self, question: str) -> str:
|
26 |
+
|
27 |
+
return self.agent.run(question)
|
28 |
+
|
29 |
def run_and_submit_all(profile):
|
30 |
+
space_id = os.getenv("SPACE_ID")
|
31 |
if not profile:
|
32 |
return "Please login to Hugging Face with the login button.", None
|
33 |
username = getattr(profile, "username", None) or getattr(profile, "name", None)
|
34 |
if not username:
|
35 |
return "Login error: username not found.", None
|
36 |
|
37 |
+
# 1) Fetch questions
|
38 |
try:
|
39 |
+
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
40 |
+
resp.raise_for_status()
|
41 |
+
questions = resp.json()
|
42 |
except Exception as e:
|
43 |
return f"Error fetching questions: {e}", None
|
44 |
|
45 |
+
# 2) Run agent
|
46 |
+
agent = BasicAgent()
|
|
|
|
|
47 |
results, payload = [], []
|
48 |
for q in questions:
|
49 |
+
tid, text = q["task_id"], q["question"]
|
|
|
|
|
50 |
try:
|
51 |
+
ans = agent(text)
|
52 |
except Exception as e:
|
53 |
ans = f"ERROR: {e}"
|
54 |
results.append({"Task ID": tid, "Question": text, "Answer": ans})
|
|
|
57 |
if not payload:
|
58 |
return "Agent returned no answers.", pd.DataFrame(results)
|
59 |
|
60 |
+
# 3) Submit
|
61 |
submission = {
|
62 |
"username": username,
|
63 |
+
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
64 |
"answers": payload
|
65 |
}
|
66 |
try:
|
67 |
+
sub_resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
68 |
+
sub_resp.raise_for_status()
|
69 |
+
data = sub_resp.json()
|
70 |
status = (
|
71 |
f"Submission Successful!\n"
|
72 |
f"User: {data['username']}\n"
|
73 |
+
f"Score: {data['score']}% "
|
74 |
+
f"({data['correct_count']}/{data['total_attempted']})\n"
|
75 |
f"Message: {data['message']}"
|
76 |
)
|
77 |
except Exception as e:
|
|
|
79 |
|
80 |
return status, pd.DataFrame(results)
|
81 |
|
|
|
82 |
def test_random_question(profile):
|
83 |
if not profile:
|
84 |
return "Please login to Hugging Face with the login button.", ""
|
85 |
+
try:
|
86 |
+
q = requests.get(f"{DEFAULT_API_URL}/random-question", timeout=15).json()
|
87 |
+
ans = BasicAgent()(q["question"])
|
88 |
+
return q["question"], ans
|
89 |
+
except Exception as e:
|
90 |
+
return f"Error during test: {e}", ""
|
91 |
|
92 |
+
# --- Gradio UI ---
|
93 |
with gr.Blocks() as demo:
|
94 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
95 |
+
gr.Markdown("""
|
96 |
+
**Instructions:**
|
97 |
+
1. Clone this space and define your agent in `tools.py`.
|
98 |
+
2. Log in with your HF account.
|
99 |
+
3. Use **Run Evaluation & Submit All Answers** or **Test Random Question**.
|
100 |
+
""")
|
|
|
|
|
101 |
|
102 |
login = gr.LoginButton()
|
103 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
108 |
question_out = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
109 |
answer_out = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
110 |
|
111 |
+
run_btn.click(run_and_submit_all, inputs=[login], outputs=[status_out, table_out])
|
112 |
+
test_btn.click(test_random_question, inputs=[login], outputs=[question_out, answer_out])
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
demo.launch(debug=True, share=False)
|