Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,21 +6,34 @@ import pandas as pd
|
|
6 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
7 |
from smolagents.models import OpenAIServerModel
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
# --- System prompt for the model ---
|
13 |
-
SYSTEM_PROMPT = """
|
14 |
-
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
15 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
16 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list
|
17 |
-
of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list,
|
18 |
-
|
19 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
22 |
"""
|
23 |
-
|
24 |
"""
|
25 |
space_id = os.getenv("SPACE_ID")
|
26 |
|
@@ -29,31 +42,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
29 |
print(f"User logged in: {username}")
|
30 |
else:
|
31 |
print("User not logged in.")
|
32 |
-
return "Please
|
33 |
|
34 |
api_url = DEFAULT_API_URL
|
35 |
questions_url = f"{api_url}/questions"
|
36 |
submit_url = f"{api_url}/submit"
|
37 |
|
38 |
-
# Instantiate Agent with fixed system_prompt keyword
|
39 |
try:
|
40 |
-
agent =
|
41 |
-
model=OpenAIServerModel(
|
42 |
-
model_id="gpt-4o-mini",
|
43 |
-
system_prompt=SYSTEM_PROMPT
|
44 |
-
),
|
45 |
-
tools=[DuckDuckGoSearchTool()]
|
46 |
-
)
|
47 |
-
print("Agent initialized successfully.")
|
48 |
except Exception as e:
|
49 |
print(f"Error initializing agent: {e}")
|
50 |
return f"Error initializing agent: {e}", None
|
51 |
|
52 |
-
# Link to code repo on Hugging Face
|
53 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
54 |
-
print(agent_code)
|
55 |
|
56 |
-
# Fetch questions
|
57 |
print(f"Fetching questions from: {questions_url}")
|
58 |
try:
|
59 |
response = requests.get(questions_url, timeout=15)
|
@@ -63,14 +66,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
63 |
print("Fetched questions list is empty.")
|
64 |
return "Fetched questions list is empty or invalid format.", None
|
65 |
print(f"Fetched {len(questions_data)} questions.")
|
66 |
-
except
|
67 |
print(f"Error fetching questions: {e}")
|
68 |
return f"Error fetching questions: {e}", None
|
69 |
-
except Exception as e:
|
70 |
-
print(f"Unexpected error fetching questions: {e}")
|
71 |
-
return f"Unexpected error fetching questions: {e}", None
|
72 |
|
73 |
-
# Run agent on questions
|
74 |
results_log = []
|
75 |
answers_payload = []
|
76 |
print(f"Running agent on {len(questions_data)} questions...")
|
@@ -78,37 +77,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
78 |
task_id = item.get("task_id")
|
79 |
question_text = item.get("question")
|
80 |
if not task_id or question_text is None:
|
81 |
-
print(f"Skipping item
|
82 |
continue
|
83 |
try:
|
84 |
submitted_answer = agent(question_text)
|
85 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
86 |
-
results_log.append({
|
87 |
-
"Task ID": task_id,
|
88 |
-
"Question": question_text,
|
89 |
-
"Submitted Answer": submitted_answer
|
90 |
-
})
|
91 |
except Exception as e:
|
92 |
print(f"Error running agent on task {task_id}: {e}")
|
93 |
-
results_log.append({
|
94 |
-
"Task ID": task_id,
|
95 |
-
"Question": question_text,
|
96 |
-
"Submitted Answer": f"AGENT ERROR: {e}"
|
97 |
-
})
|
98 |
|
99 |
if not answers_payload:
|
100 |
print("Agent did not produce any answers to submit.")
|
101 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
"username": username.strip(),
|
106 |
-
"agent_code": agent_code,
|
107 |
-
"answers": answers_payload
|
108 |
-
}
|
109 |
-
print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
|
110 |
-
|
111 |
-
# Submit answers
|
112 |
try:
|
113 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
114 |
response.raise_for_status()
|
@@ -132,51 +116,36 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
132 |
error_detail += f" Response: {e.response.text[:500]}"
|
133 |
status_message = f"Submission Failed: {error_detail}"
|
134 |
print(status_message)
|
135 |
-
|
136 |
-
return status_message, results_df
|
137 |
except requests.exceptions.Timeout:
|
138 |
status_message = "Submission Failed: The request timed out."
|
139 |
print(status_message)
|
140 |
-
|
141 |
-
return status_message, results_df
|
142 |
-
except requests.exceptions.RequestException as e:
|
143 |
-
status_message = f"Submission Failed: Network error - {e}"
|
144 |
-
print(status_message)
|
145 |
-
results_df = pd.DataFrame(results_log)
|
146 |
-
return status_message, results_df
|
147 |
except Exception as e:
|
148 |
-
status_message = f"
|
149 |
print(status_message)
|
150 |
-
|
151 |
-
return status_message, results_df
|
152 |
-
|
153 |
|
154 |
-
# --- Gradio Interface ---
|
155 |
with gr.Blocks() as demo:
|
156 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
157 |
gr.Markdown(
|
158 |
"""
|
159 |
**Instructions:**
|
160 |
-
1.
|
161 |
-
2. Log in to your Hugging Face account using the button below.
|
162 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see
|
163 |
-
|
164 |
-
**
|
165 |
-
Submission can take time depending on the number of questions and model latency.
|
166 |
-
This space provides a basic setup and encourages you to improve it further.
|
167 |
"""
|
168 |
)
|
169 |
|
170 |
-
|
171 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
172 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
173 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
174 |
|
175 |
-
run_button.click(
|
176 |
-
fn=run_and_submit_all,
|
177 |
-
inputs=[login_button],
|
178 |
-
outputs=[status_output, results_table]
|
179 |
-
)
|
180 |
|
181 |
if __name__ == "__main__":
|
182 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
@@ -187,14 +156,14 @@ if __name__ == "__main__":
|
|
187 |
print(f"✅ SPACE_HOST found: {space_host}")
|
188 |
print(f" Runtime URL should be: https://{space_host}.hf.space")
|
189 |
else:
|
190 |
-
print("ℹ️
|
191 |
|
192 |
if space_id:
|
193 |
print(f"✅ SPACE_ID found: {space_id}")
|
194 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
|
195 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id}/tree/main")
|
196 |
else:
|
197 |
-
print("ℹ️
|
198 |
|
199 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
200 |
|
|
|
6 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
7 |
from smolagents.models import OpenAIServerModel
|
8 |
|
9 |
+
# System prompt as per your instructions
|
10 |
+
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
11 |
+
Report your thoughts, and finish your answer with the following template:
|
|
|
|
|
|
|
12 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
13 |
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list
|
14 |
+
of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
15 |
+
|
16 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
17 |
+
|
18 |
+
class MyAgent:
|
19 |
+
def __init__(self):
|
20 |
+
# Initialize model with system prompt
|
21 |
+
self.model = OpenAIServerModel(
|
22 |
+
model_id="gpt-4",
|
23 |
+
system_message=SYSTEM_PROMPT
|
24 |
+
)
|
25 |
+
self.agent = CodeAgent(
|
26 |
+
tools=[DuckDuckGoSearchTool()],
|
27 |
+
model=self.model
|
28 |
+
)
|
29 |
+
|
30 |
+
def __call__(self, question: str) -> str:
|
31 |
+
# Run agent on the question
|
32 |
+
return self.agent.run(question)
|
33 |
|
34 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
35 |
"""
|
36 |
+
Fetches questions, runs the agent, submits answers, returns status and results table.
|
37 |
"""
|
38 |
space_id = os.getenv("SPACE_ID")
|
39 |
|
|
|
42 |
print(f"User logged in: {username}")
|
43 |
else:
|
44 |
print("User not logged in.")
|
45 |
+
return "Please Login to Hugging Face with the button.", None
|
46 |
|
47 |
api_url = DEFAULT_API_URL
|
48 |
questions_url = f"{api_url}/questions"
|
49 |
submit_url = f"{api_url}/submit"
|
50 |
|
|
|
51 |
try:
|
52 |
+
agent = MyAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
except Exception as e:
|
54 |
print(f"Error initializing agent: {e}")
|
55 |
return f"Error initializing agent: {e}", None
|
56 |
|
|
|
57 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
58 |
+
print(f"Agent code URL: {agent_code}")
|
59 |
|
|
|
60 |
print(f"Fetching questions from: {questions_url}")
|
61 |
try:
|
62 |
response = requests.get(questions_url, timeout=15)
|
|
|
66 |
print("Fetched questions list is empty.")
|
67 |
return "Fetched questions list is empty or invalid format.", None
|
68 |
print(f"Fetched {len(questions_data)} questions.")
|
69 |
+
except Exception as e:
|
70 |
print(f"Error fetching questions: {e}")
|
71 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
72 |
|
|
|
73 |
results_log = []
|
74 |
answers_payload = []
|
75 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
77 |
task_id = item.get("task_id")
|
78 |
question_text = item.get("question")
|
79 |
if not task_id or question_text is None:
|
80 |
+
print(f"Skipping invalid item: {item}")
|
81 |
continue
|
82 |
try:
|
83 |
submitted_answer = agent(question_text)
|
84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
85 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
86 |
except Exception as e:
|
87 |
print(f"Error running agent on task {task_id}: {e}")
|
88 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
|
|
|
|
|
89 |
|
90 |
if not answers_payload:
|
91 |
print("Agent did not produce any answers to submit.")
|
92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
93 |
|
94 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
95 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
try:
|
97 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
98 |
response.raise_for_status()
|
|
|
116 |
error_detail += f" Response: {e.response.text[:500]}"
|
117 |
status_message = f"Submission Failed: {error_detail}"
|
118 |
print(status_message)
|
119 |
+
return status_message, pd.DataFrame(results_log)
|
|
|
120 |
except requests.exceptions.Timeout:
|
121 |
status_message = "Submission Failed: The request timed out."
|
122 |
print(status_message)
|
123 |
+
return status_message, pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
except Exception as e:
|
125 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
126 |
print(status_message)
|
127 |
+
return status_message, pd.DataFrame(results_log)
|
|
|
|
|
128 |
|
|
|
129 |
with gr.Blocks() as demo:
|
130 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
131 |
gr.Markdown(
|
132 |
"""
|
133 |
**Instructions:**
|
134 |
+
1. Clone this space, modify code to define your agent's logic, tools, and packages.
|
135 |
+
2. Log in to your Hugging Face account using the button below.
|
136 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
|
137 |
+
|
138 |
+
**Note:** Submitting can take some time.
|
|
|
|
|
139 |
"""
|
140 |
)
|
141 |
|
142 |
+
gr.LoginButton()
|
143 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
144 |
+
|
145 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
146 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
147 |
|
148 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
149 |
|
150 |
if __name__ == "__main__":
|
151 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
156 |
print(f"✅ SPACE_HOST found: {space_host}")
|
157 |
print(f" Runtime URL should be: https://{space_host}.hf.space")
|
158 |
else:
|
159 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
160 |
|
161 |
if space_id:
|
162 |
print(f"✅ SPACE_ID found: {space_id}")
|
163 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
|
164 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id}/tree/main")
|
165 |
else:
|
166 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?).")
|
167 |
|
168 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
169 |
|