Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,15 +3,15 @@ import gradio as gr
|
|
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-3.5-turbo")
|
16 |
final_tool = FinalAnswerTool()
|
17 |
self.agent = CodeAgent(
|
@@ -22,18 +22,31 @@ class BasicAgent:
|
|
22 |
)
|
23 |
|
24 |
def __call__(self, question: str) -> str:
|
25 |
-
|
26 |
return self.agent.run(question)
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
if not profile:
|
31 |
-
return
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
if not username:
|
34 |
-
return "
|
35 |
|
36 |
-
#
|
37 |
try:
|
38 |
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
39 |
resp.raise_for_status()
|
@@ -41,26 +54,30 @@ def run_and_submit_all(profile):
|
|
41 |
except Exception as e:
|
42 |
return f"Error fetching questions: {e}", None
|
43 |
|
44 |
-
#
|
45 |
agent = BasicAgent()
|
46 |
-
results
|
|
|
47 |
for q in questions:
|
48 |
-
tid
|
|
|
|
|
|
|
49 |
try:
|
50 |
ans = agent(text)
|
51 |
except Exception as e:
|
52 |
ans = f"ERROR: {e}"
|
53 |
-
results.append({
|
54 |
-
payload.append({
|
55 |
|
56 |
if not payload:
|
57 |
return "Agent returned no answers.", pd.DataFrame(results)
|
58 |
|
59 |
-
#
|
60 |
submission = {
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
}
|
65 |
try:
|
66 |
sub_resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
@@ -68,47 +85,51 @@ def run_and_submit_all(profile):
|
|
68 |
data = sub_resp.json()
|
69 |
status = (
|
70 |
f"Submission Successful!\n"
|
71 |
-
f"User: {data
|
72 |
-
f"Score: {data
|
73 |
-
f"
|
74 |
-
f"Message: {data['message']}"
|
75 |
)
|
76 |
except Exception as e:
|
77 |
status = f"Submission Failed: {e}"
|
78 |
|
79 |
return status, pd.DataFrame(results)
|
80 |
|
|
|
81 |
def test_random_question(profile):
|
82 |
-
|
|
|
|
|
|
|
|
|
83 |
return "Please login to Hugging Face with the login button.", ""
|
84 |
try:
|
85 |
q = requests.get(f"{DEFAULT_API_URL}/random-question", timeout=15).json()
|
86 |
-
ans = BasicAgent()(q
|
87 |
-
return q
|
88 |
except Exception as e:
|
89 |
return f"Error during test: {e}", ""
|
90 |
|
91 |
# --- Gradio UI ---
|
92 |
with gr.Blocks() as demo:
|
93 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
94 |
-
gr.Markdown(
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
101 |
login = gr.LoginButton()
|
102 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
103 |
test_btn = gr.Button("Test Random Question")
|
104 |
-
|
105 |
status_out = gr.Textbox(label="Status / Result", lines=5, interactive=False)
|
106 |
table_out = gr.DataFrame(label="Full Results Table", wrap=True)
|
107 |
question_out = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
108 |
answer_out = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
109 |
|
110 |
-
run_btn.click(run_and_submit_all, inputs=[login], outputs=[status_out, table_out])
|
111 |
-
test_btn.click(test_random_question, inputs=[login], outputs=[question_out, answer_out])
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
demo.launch(debug=True, share=False)
|
|
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
|
|
|
6 |
from tools import FinalAnswerTool
|
7 |
+
from smolagents import CodeAgent, OpenAIServerModel
|
8 |
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
class BasicAgent:
|
13 |
def __init__(self):
|
14 |
+
# Use GPT-3.5-turbo for quota-friendly operation
|
15 |
model = OpenAIServerModel(model_id="gpt-3.5-turbo")
|
16 |
final_tool = FinalAnswerTool()
|
17 |
self.agent = CodeAgent(
|
|
|
22 |
)
|
23 |
|
24 |
def __call__(self, question: str) -> str:
|
|
|
25 |
return self.agent.run(question)
|
26 |
|
27 |
+
|
28 |
+
def extract_username(profile):
|
29 |
+
"""
|
30 |
+
Extract username from the Hugging Face OAuthProfile or dict.
|
31 |
+
"""
|
32 |
if not profile:
|
33 |
+
return None
|
34 |
+
# If profile is a dict (Gradio may return dict), extract keys
|
35 |
+
if isinstance(profile, dict):
|
36 |
+
return profile.get('username') or profile.get('name') or profile.get('login') or profile.get('id')
|
37 |
+
# Otherwise, assume object with attributes
|
38 |
+
return getattr(profile, 'username', None) or getattr(profile, 'name', None) or getattr(profile, 'login', None)
|
39 |
+
|
40 |
+
|
41 |
+
def run_and_submit_all(profile):
|
42 |
+
"""
|
43 |
+
Fetch all questions, run the BasicAgent, submit answers, and return status and DataFrame.
|
44 |
+
"""
|
45 |
+
username = extract_username(profile)
|
46 |
if not username:
|
47 |
+
return "Please login to Hugging Face with the login button.", None
|
48 |
|
49 |
+
# Fetch questions
|
50 |
try:
|
51 |
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
52 |
resp.raise_for_status()
|
|
|
54 |
except Exception as e:
|
55 |
return f"Error fetching questions: {e}", None
|
56 |
|
57 |
+
# Run agent on each question
|
58 |
agent = BasicAgent()
|
59 |
+
results = []
|
60 |
+
payload = []
|
61 |
for q in questions:
|
62 |
+
tid = q.get('task_id')
|
63 |
+
text = q.get('question')
|
64 |
+
if not tid or not text:
|
65 |
+
continue
|
66 |
try:
|
67 |
ans = agent(text)
|
68 |
except Exception as e:
|
69 |
ans = f"ERROR: {e}"
|
70 |
+
results.append({'Task ID': tid, 'Question': text, 'Answer': ans})
|
71 |
+
payload.append({'task_id': tid, 'submitted_answer': ans})
|
72 |
|
73 |
if not payload:
|
74 |
return "Agent returned no answers.", pd.DataFrame(results)
|
75 |
|
76 |
+
# Prepare and submit
|
77 |
submission = {
|
78 |
+
'username': username,
|
79 |
+
'agent_code': f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
|
80 |
+
'answers': payload
|
81 |
}
|
82 |
try:
|
83 |
sub_resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
|
|
85 |
data = sub_resp.json()
|
86 |
status = (
|
87 |
f"Submission Successful!\n"
|
88 |
+
f"User: {data.get('username')}\n"
|
89 |
+
f"Score: {data.get('score')}% ({data.get('correct_count')}/{data.get('total_attempted')})\n"
|
90 |
+
f"Message: {data.get('message')}"
|
|
|
91 |
)
|
92 |
except Exception as e:
|
93 |
status = f"Submission Failed: {e}"
|
94 |
|
95 |
return status, pd.DataFrame(results)
|
96 |
|
97 |
+
|
98 |
def test_random_question(profile):
|
99 |
+
"""
|
100 |
+
Fetch a random question and return question and agent answer.
|
101 |
+
"""
|
102 |
+
username = extract_username(profile)
|
103 |
+
if not username:
|
104 |
return "Please login to Hugging Face with the login button.", ""
|
105 |
try:
|
106 |
q = requests.get(f"{DEFAULT_API_URL}/random-question", timeout=15).json()
|
107 |
+
ans = BasicAgent()(q.get('question', ''))
|
108 |
+
return q.get('question', ''), ans
|
109 |
except Exception as e:
|
110 |
return f"Error during test: {e}", ""
|
111 |
|
112 |
# --- Gradio UI ---
|
113 |
with gr.Blocks() as demo:
|
114 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
115 |
+
gr.Markdown(
|
116 |
+
"""
|
117 |
+
**Instructions:**
|
118 |
+
1. Clone this space and define your agent in `tools.py`.
|
119 |
+
2. Log in with your Hugging Face account.
|
120 |
+
3. Use **Run Evaluation & Submit All Answers** or **Test Random Question**.
|
121 |
+
"""
|
122 |
+
)
|
123 |
login = gr.LoginButton()
|
124 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
125 |
test_btn = gr.Button("Test Random Question")
|
|
|
126 |
status_out = gr.Textbox(label="Status / Result", lines=5, interactive=False)
|
127 |
table_out = gr.DataFrame(label="Full Results Table", wrap=True)
|
128 |
question_out = gr.Textbox(label="Random Question", lines=3, interactive=False)
|
129 |
answer_out = gr.Textbox(label="Agent Answer", lines=3, interactive=False)
|
130 |
|
131 |
+
run_btn.click(fn=run_and_submit_all, inputs=[login], outputs=[status_out, table_out])
|
132 |
+
test_btn.click(fn=test_random_question, inputs=[login], outputs=[question_out, answer_out])
|
133 |
|
134 |
if __name__ == "__main__":
|
135 |
demo.launch(debug=True, share=False)
|