Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -33,14 +33,14 @@ llm = ChatGoogleGenerativeAI(
|
|
33 |
llm_with_tools = llm.bind_tools(toolset)
|
34 |
|
35 |
sys_prompt_file = open("sys_prompt.txt")
|
36 |
-
|
37 |
|
38 |
class AgentState(TypedDict):
|
39 |
messages: Annotated[list[AnyMessage], add_messages]
|
40 |
|
41 |
def assistant(state: AgentState):
|
42 |
return {
|
43 |
-
"messages": [llm_with_tools.invoke(state["messages"])],
|
44 |
}
|
45 |
|
46 |
builder = StateGraph(AgentState)
|
@@ -57,250 +57,141 @@ builder.add_edge("tools","assistant")
|
|
57 |
|
58 |
gaia_agent = builder.compile()
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
"""
|
62 |
-
Fetches all questions, runs the
|
63 |
-
and displays the results.
|
64 |
"""
|
65 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
66 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
|
|
67 |
if profile:
|
68 |
username= f"{profile.username}"
|
69 |
print(f"User logged in: {username}")
|
70 |
else:
|
71 |
print("User not logged in.")
|
72 |
return "Please Login to Hugging Face with the button.", None
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
try:
|
76 |
-
agent =
|
77 |
except Exception as e:
|
78 |
print(f"Error instantiating agent: {e}")
|
79 |
return f"Error initializing agent: {e}", None
|
80 |
-
|
81 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase (useful for others so please keep it public)
|
82 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
83 |
print(agent_code)
|
84 |
-
|
85 |
# 2. Fetch Questions
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
# 3. Run your Agent
|
89 |
results_log = []
|
90 |
answers_payload = []
|
91 |
print(f"Running agent on {len(questions_data)} questions...")
|
92 |
-
|
93 |
for item in questions_data:
|
94 |
task_id = item.get("task_id")
|
95 |
question_text = item.get("question")
|
96 |
-
|
97 |
if not task_id or question_text is None:
|
98 |
print(f"Skipping item with missing task_id or question: {item}")
|
99 |
continue
|
100 |
-
|
101 |
-
# 2.2 Handle attachment if present
|
102 |
-
attachment_info = None
|
103 |
-
if "file_name" in item and item["file_name"]:
|
104 |
-
file_name = item.get("file_name")
|
105 |
-
attachment_info = handle_attachment(task_id, file_name)
|
106 |
-
print(f"Attachment handling result: {attachment_info['status']}")
|
107 |
-
|
108 |
try:
|
109 |
-
|
110 |
-
messages = [
|
111 |
-
SystemMessage(content=SYSTEM_PROMPT),
|
112 |
-
SystemMessage(content=f"Current task id: {task_id}")
|
113 |
-
]
|
114 |
-
|
115 |
-
# If we have an attachment that Claude can process directly
|
116 |
-
if attachment_info and attachment_info["status"] == "success" and attachment_info["handling"] == "direct":
|
117 |
-
# Encode content for direct inclusion
|
118 |
-
encoded_content = base64.b64encode(attachment_info["raw_content"]).decode('utf-8')
|
119 |
-
content_type = attachment_info["content_type"]
|
120 |
-
|
121 |
-
# Create multimodal message
|
122 |
-
if content_type.startswith('image/'):
|
123 |
-
multimodal_content = [
|
124 |
-
{"type": "text", "text": question_text},
|
125 |
-
{
|
126 |
-
"type": "image",
|
127 |
-
"source": {
|
128 |
-
"type": "base64",
|
129 |
-
"media_type": content_type,
|
130 |
-
"data": encoded_content
|
131 |
-
}
|
132 |
-
}
|
133 |
-
]
|
134 |
-
elif content_type == "application/pdf" or "spreadsheet" in content_type or "excel" in content_type or "csv" in content_type:
|
135 |
-
multimodal_content = [
|
136 |
-
{"type": "text", "text": question_text},
|
137 |
-
{
|
138 |
-
"type": "file",
|
139 |
-
"source": {
|
140 |
-
"type": "base64",
|
141 |
-
"media_type": content_type,
|
142 |
-
"data": encoded_content
|
143 |
-
},
|
144 |
-
"name": attachment_info["file_name"]
|
145 |
-
}
|
146 |
-
]
|
147 |
-
|
148 |
-
messages.append(HumanMessage(content=multimodal_content))
|
149 |
-
|
150 |
-
# If we have an attachment that needs tool processing
|
151 |
-
elif attachment_info and attachment_info["status"] == "success" and attachment_info["handling"] == "tool":
|
152 |
-
# Add info about the file to the question
|
153 |
-
file_info = (
|
154 |
-
f"{question_text}\n\n"
|
155 |
-
f"Note: This task has an attached file that can be accessed at: {attachment_info['file_path']}\n"
|
156 |
-
f"File type: {attachment_info['content_type']}"
|
157 |
-
)
|
158 |
-
messages.append(HumanMessage(content=file_info))
|
159 |
-
|
160 |
-
# If no attachment or error with attachment
|
161 |
-
else:
|
162 |
-
messages.append(HumanMessage(content=question_text))
|
163 |
-
|
164 |
-
# Invoke the agent with the prepared messages
|
165 |
-
agent_answer = agent.invoke({"messages": messages},{"recursion_limit": 50})
|
166 |
-
submitted_answer = extract_final_answer(agent_answer['messages'][-1].content)
|
167 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
168 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
169 |
-
|
170 |
except Exception as e:
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
if not answers_payload:
|
175 |
print("Agent did not produce any answers to submit.")
|
176 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
177 |
-
|
178 |
# 4. Prepare Submission
|
179 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
180 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
181 |
print(status_update)
|
182 |
-
|
183 |
-
# 5. Submit
|
184 |
-
return submit(submission_data, results_log)
|
185 |
-
|
186 |
-
def run_and_submit_one( profile: gr.OAuthProfile | None):
|
187 |
-
# --- Determine HF Space Runtime URL and Repo URL ---
|
188 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
189 |
-
|
190 |
-
if profile:
|
191 |
-
username= f"{profile.username}"
|
192 |
-
print(f"User logged in: {username}")
|
193 |
-
else:
|
194 |
-
print("User not logged in.")
|
195 |
-
return "Please Login to Hugging Face with the button.", None
|
196 |
-
|
197 |
-
# 1. Instantiate Agent ( modify this part to create your agent)
|
198 |
-
try:
|
199 |
-
agent = gaia_agent
|
200 |
-
except Exception as e:
|
201 |
-
print(f"Error instantiating agent: {e}")
|
202 |
-
return f"Error initializing agent: {e}", None
|
203 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
204 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
205 |
-
print(agent_code)
|
206 |
-
|
207 |
-
# 2. Fetch Questions
|
208 |
-
questions_data = get_one_random_question()
|
209 |
-
print("questions_data:", questions_data)
|
210 |
-
|
211 |
-
# 2.2 Handle attachment if present
|
212 |
-
attachment_info = None
|
213 |
-
if "file_name" in questions_data and questions_data["file_name"]:
|
214 |
-
task_id = questions_data.get("task_id")
|
215 |
-
file_name = questions_data.get("file_name")
|
216 |
-
attachment_info = handle_attachment(task_id, file_name)
|
217 |
-
print(f"Attachment handling result: {attachment_info['status']}")
|
218 |
|
219 |
-
#
|
220 |
-
|
221 |
-
answers_payload = []
|
222 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
223 |
-
|
224 |
-
task_id = questions_data.get("task_id")
|
225 |
-
question_text = questions_data.get("question")
|
226 |
-
|
227 |
-
if not task_id or question_text is None:
|
228 |
-
print(f"Skipping item with missing task_id or question")
|
229 |
try:
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
},
|
265 |
-
"name": attachment_info["file_name"]
|
266 |
-
}
|
267 |
-
]
|
268 |
-
|
269 |
-
messages.append(HumanMessage(content=multimodal_content))
|
270 |
-
|
271 |
-
# If we have an attachment that needs tool processing
|
272 |
-
elif attachment_info and attachment_info["status"] == "success" and attachment_info["handling"] == "tool":
|
273 |
-
# Add info about the file to the question
|
274 |
-
file_info = (
|
275 |
-
f"{question_text}\n\n"
|
276 |
-
f"Note: This task has an attached file that can be accessed at: {attachment_info['file_path']}\n"
|
277 |
-
f"File type: {attachment_info['content_type']}"
|
278 |
-
)
|
279 |
-
messages.append(HumanMessage(content=file_info))
|
280 |
-
|
281 |
-
# If no attachment or error with attachment
|
282 |
-
else:
|
283 |
-
messages.append(HumanMessage(content=question_text))
|
284 |
-
|
285 |
-
# Invoke the agent with the prepared messages
|
286 |
-
agent_answer = agent.invoke({"messages": messages},{"recursion_limit": 50})
|
287 |
-
submitted_answer = extract_final_answer(agent_answer['messages'][-1].content)
|
288 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
289 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
290 |
except Exception as e:
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
print("Agent did not produce any answers to submit.")
|
296 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
297 |
|
298 |
-
# 4. Prepare Submission
|
299 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
300 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
301 |
-
print(status_update)
|
302 |
-
|
303 |
-
return submit(submission_data, results_log)
|
304 |
|
305 |
# --- Build Gradio Interface using Blocks ---
|
306 |
with gr.Blocks() as demo:
|
@@ -321,7 +212,6 @@ with gr.Blocks() as demo:
|
|
321 |
gr.LoginButton()
|
322 |
|
323 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
324 |
-
run_one_button = gr.Button("Run one question and submit")
|
325 |
|
326 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
327 |
# Removed max_rows=10 from DataFrame constructor
|
@@ -331,10 +221,6 @@ with gr.Blocks() as demo:
|
|
331 |
fn=run_and_submit_all,
|
332 |
outputs=[status_output, results_table]
|
333 |
)
|
334 |
-
run_one_button.click(
|
335 |
-
fn=run_and_submit_one,
|
336 |
-
outputs=[status_output, results_table]
|
337 |
-
)
|
338 |
|
339 |
if __name__ == "__main__":
|
340 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
33 |
llm_with_tools = llm.bind_tools(toolset)
|
34 |
|
35 |
sys_prompt_file = open("sys_prompt.txt")
|
36 |
+
sys_prompt = sys_prompt_file.read()
|
37 |
|
38 |
class AgentState(TypedDict):
|
39 |
messages: Annotated[list[AnyMessage], add_messages]
|
40 |
|
41 |
def assistant(state: AgentState):
|
42 |
return {
|
43 |
+
"messages": [llm_with_tools.invoke([sys_prompt]+state["messages"])],
|
44 |
}
|
45 |
|
46 |
builder = StateGraph(AgentState)
|
|
|
57 |
|
58 |
gaia_agent = builder.compile()
|
59 |
|
60 |
+
class BasicAgent:
|
61 |
+
"""A langgraph agent."""
|
62 |
+
def __init__(self):
|
63 |
+
print("BasicAgent initialized.")
|
64 |
+
self.graph = gaia_agent
|
65 |
+
|
66 |
+
def __call__(self, question: str) -> str:
|
67 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
68 |
+
# Wrap the question in a HumanMessage from langchain_core
|
69 |
+
messages = [HumanMessage(content=question)]
|
70 |
+
messages = self.graph.invoke({"messages": messages})
|
71 |
+
answer = messages['messages'][-1].content
|
72 |
+
return answer[14:]
|
73 |
+
|
74 |
+
|
75 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
76 |
"""
|
77 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
78 |
+
and displays the results.
|
79 |
"""
|
80 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
81 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
82 |
+
|
83 |
if profile:
|
84 |
username= f"{profile.username}"
|
85 |
print(f"User logged in: {username}")
|
86 |
else:
|
87 |
print("User not logged in.")
|
88 |
return "Please Login to Hugging Face with the button.", None
|
89 |
+
|
90 |
+
api_url = DEFAULT_API_URL
|
91 |
+
questions_url = f"{api_url}/questions"
|
92 |
+
submit_url = f"{api_url}/submit"
|
93 |
+
|
94 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
95 |
try:
|
96 |
+
agent = BasicAgent()
|
97 |
except Exception as e:
|
98 |
print(f"Error instantiating agent: {e}")
|
99 |
return f"Error initializing agent: {e}", None
|
100 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
|
|
101 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
102 |
print(agent_code)
|
103 |
+
|
104 |
# 2. Fetch Questions
|
105 |
+
print(f"Fetching questions from: {questions_url}")
|
106 |
+
try:
|
107 |
+
response = requests.get(questions_url, timeout=15)
|
108 |
+
response.raise_for_status()
|
109 |
+
questions_data = response.json()
|
110 |
+
if not questions_data:
|
111 |
+
print("Fetched questions list is empty.")
|
112 |
+
return "Fetched questions list is empty or invalid format.", None
|
113 |
+
print(f"Fetched {len(questions_data)} questions.")
|
114 |
+
except requests.exceptions.RequestException as e:
|
115 |
+
print(f"Error fetching questions: {e}")
|
116 |
+
return f"Error fetching questions: {e}", None
|
117 |
+
except requests.exceptions.JSONDecodeError as e:
|
118 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
119 |
+
print(f"Response text: {response.text[:500]}")
|
120 |
+
return f"Error decoding server response for questions: {e}", None
|
121 |
+
except Exception as e:
|
122 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
123 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
124 |
+
|
125 |
# 3. Run your Agent
|
126 |
results_log = []
|
127 |
answers_payload = []
|
128 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
129 |
for item in questions_data:
|
130 |
task_id = item.get("task_id")
|
131 |
question_text = item.get("question")
|
|
|
132 |
if not task_id or question_text is None:
|
133 |
print(f"Skipping item with missing task_id or question: {item}")
|
134 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
try:
|
136 |
+
submitted_answer = agent(question_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
138 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
139 |
except Exception as e:
|
140 |
+
print(f"Error running agent on task {task_id}: {e}")
|
141 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
142 |
+
|
143 |
if not answers_payload:
|
144 |
print("Agent did not produce any answers to submit.")
|
145 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
146 |
+
|
147 |
# 4. Prepare Submission
|
148 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
149 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
150 |
print(status_update)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
+
# 5. Submit
|
153 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
try:
|
155 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
156 |
+
response.raise_for_status()
|
157 |
+
result_data = response.json()
|
158 |
+
final_status = (
|
159 |
+
f"Submission Successful!\n"
|
160 |
+
f"User: {result_data.get('username')}\n"
|
161 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
162 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
163 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
164 |
+
)
|
165 |
+
print("Submission successful.")
|
166 |
+
results_df = pd.DataFrame(results_log)
|
167 |
+
return final_status, results_df
|
168 |
+
except requests.exceptions.HTTPError as e:
|
169 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
170 |
+
try:
|
171 |
+
error_json = e.response.json()
|
172 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
173 |
+
except requests.exceptions.JSONDecodeError:
|
174 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
175 |
+
status_message = f"Submission Failed: {error_detail}"
|
176 |
+
print(status_message)
|
177 |
+
results_df = pd.DataFrame(results_log)
|
178 |
+
return status_message, results_df
|
179 |
+
except requests.exceptions.Timeout:
|
180 |
+
status_message = "Submission Failed: The request timed out."
|
181 |
+
print(status_message)
|
182 |
+
results_df = pd.DataFrame(results_log)
|
183 |
+
return status_message, results_df
|
184 |
+
except requests.exceptions.RequestException as e:
|
185 |
+
status_message = f"Submission Failed: Network error - {e}"
|
186 |
+
print(status_message)
|
187 |
+
results_df = pd.DataFrame(results_log)
|
188 |
+
return status_message, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
except Exception as e:
|
190 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
191 |
+
print(status_message)
|
192 |
+
results_df = pd.DataFrame(results_log)
|
193 |
+
return status_message, results_df
|
|
|
|
|
194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
# --- Build Gradio Interface using Blocks ---
|
197 |
with gr.Blocks() as demo:
|
|
|
212 |
gr.LoginButton()
|
213 |
|
214 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
215 |
|
216 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
217 |
# Removed max_rows=10 from DataFrame constructor
|
|
|
221 |
fn=run_and_submit_all,
|
222 |
outputs=[status_output, results_table]
|
223 |
)
|
|
|
|
|
|
|
|
|
224 |
|
225 |
if __name__ == "__main__":
|
226 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|