Karim0111 commited on
Commit
a1c3e17
·
verified ·
1 Parent(s): c23b39d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -89
app.py CHANGED
@@ -35,28 +35,13 @@ class BasicAgent:
35
  Process a GAIA benchmark question and return the answer
36
  """
37
  try:
38
- # Ensure question is a string
39
- if not isinstance(question, str):
40
- question = str(question)
41
-
42
  if self.verbose:
43
  print(f"Processing question: {question}")
44
- print(f"Task file path type: {type(task_file_path)}, value: {task_file_path}")
45
-
46
- # CRITICAL FIX: Ensure task_file_path is a string or None
47
- if task_file_path is not None:
48
- if isinstance(task_file_path, list):
49
- task_file_path = task_file_path[0] if len(task_file_path) > 0 else None
50
- print(f"Converted list to string: {task_file_path}")
51
- elif not isinstance(task_file_path, str):
52
- task_file_path = str(task_file_path)
53
- print(f"Converted to string: {task_file_path}")
54
-
55
- if self.verbose and task_file_path:
56
- print(f"With associated file: {task_file_path}")
57
 
58
  # Create a context with file information if available
59
- context = str(question) # Ensure it's a string
60
 
61
  # If there's a file, read it and include its content in the context
62
  if task_file_path:
@@ -77,10 +62,10 @@ You can still try to answer the question based on the information provided.
77
  """
78
 
79
  # Check for special cases that need specific formatting
80
- if str(question).startswith(".") or ".rewsna eht sa" in str(question):
81
  context = f"""
82
  This question appears to be in reversed text. Here's the reversed version:
83
- {str(question)[::-1]}
84
  Now answer the question above. Remember to format your answer exactly as requested.
85
  """
86
 
@@ -92,48 +77,16 @@ Be direct and specific. GAIA benchmark requires exact matching answers.
92
  For example, if asked "What is the capital of France?", respond simply with "Paris".
93
  """
94
 
95
- # Ensure full_prompt is definitely a string
96
- full_prompt = str(full_prompt)
97
-
98
- if self.verbose:
99
- print(f"Full prompt type: {type(full_prompt)}")
100
- print(f"Full prompt length: {len(full_prompt)}")
101
-
102
- # *** FIXED AGENT CALL - Pass string to CodeAgent ***
103
  try:
104
- # CodeAgent.run expects a textual task (string), not a list of messages
105
- # Passing a list causes internal `.strip()` calls on a list, which raises:
106
- # "AttributeError: 'list' object has no attribute 'strip'"
107
  raw_response = self.agent.run(full_prompt)
108
 
109
  if self.verbose:
110
  print(f"Raw response type: {type(raw_response)}")
111
  print(f"Raw response: {raw_response}")
112
 
113
- # Handle ALL possible response formats
114
- if isinstance(raw_response, dict):
115
- answer = raw_response.get('choices', [{}])[0].get('message', {}).get('content', str(raw_response))
116
- elif isinstance(raw_response, list):
117
- if len(raw_response) > 0:
118
- if isinstance(raw_response[0], dict):
119
- # Common format: [{"role": "assistant", "content": "..."}]
120
- answer = raw_response[0].get('content', str(raw_response[0]))
121
- elif isinstance(raw_response[0], list):
122
- # Nested list - dig deeper
123
- nested = raw_response[0]
124
- if isinstance(nested, list) and len(nested) > 0:
125
- if isinstance(nested[0], dict):
126
- answer = nested[0].get('content', str(nested[0]))
127
- else:
128
- answer = str(nested[0])
129
- else:
130
- answer = str(raw_response[0])
131
- else:
132
- answer = str(raw_response[0])
133
- else:
134
- answer = "No response"
135
- else:
136
- answer = str(raw_response)
137
 
138
  if self.verbose:
139
  print(f"Extracted answer type: {type(answer)}")
@@ -145,13 +98,7 @@ For example, if asked "What is the capital of France?", respond simply with "Par
145
  return f"Agent error: {agent_error}"
146
 
147
  # Clean the answer
148
- try:
149
- answer = self._clean_answer(answer)
150
- except Exception as clean_error:
151
- print(f"Error cleaning answer: {clean_error}")
152
- traceback.print_exc()
153
- # Just return the raw answer as string if cleaning fails
154
- answer = str(answer) if answer else "Error cleaning answer"
155
 
156
  if self.verbose:
157
  print(f"Generated answer: {answer}")
@@ -165,28 +112,71 @@ For example, if asked "What is the capital of France?", respond simply with "Par
165
  traceback.print_exc()
166
  return error_msg
167
 
168
- def _clean_answer(self, answer: any) -> str:
169
  """
170
- Ultra-safe answer extraction and cleaning
 
171
  """
172
- # Force to string immediately with extra safety
173
- try:
174
- if answer is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  return ""
176
- if isinstance(answer, list):
177
- # If it's a list, try to extract meaningful content
178
- if len(answer) == 0:
179
- return ""
180
- # Try to get content from first element
181
- answer = answer[0] if len(answer) > 0 else ""
182
- if isinstance(answer, dict):
183
- # If it's a dict, try to get 'content' or convert to string
184
- answer = answer.get('content', str(answer))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  if not isinstance(answer, str):
186
  answer = str(answer)
 
187
  except Exception as e:
188
  print(f"Error in initial conversion: {e}")
189
- return str(answer) if answer else ""
 
 
 
 
190
 
191
  # Now answer should definitely be a string
192
  try:
@@ -211,23 +201,21 @@ For example, if asked "What is the capital of France?", respond simply with "Par
211
 
212
  return answer
213
  except Exception as e:
214
- print(f"Error in answer cleaning: {e}, returning raw string")
215
- return str(answer) if answer else ""
 
 
 
 
216
 
217
  def run_and_submit_all(profile: gr.OAuthProfile | None):
218
  """
219
  Fetches all questions, runs the BasicAgent on them, submits all answers,
220
  and displays the results.
221
  """
222
- space_id = os.getenv("SPACE_ID")
223
-
224
- if profile:
225
- username = f"{profile.username}"
226
- print(f"User logged in: {username}")
227
- else:
228
- print("User not logged in.")
229
- return "Please Login to Hugging Face with the button.", None
230
-
231
  api_url = DEFAULT_API_URL
232
  questions_url = f"{api_url}/questions"
233
  submit_url = f"{api_url}/submit"
@@ -286,6 +274,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
286
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
287
  except Exception as e:
288
  print(f"Error running agent on task {task_id}: {e}")
 
289
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
290
 
291
  if not answers_payload:
@@ -337,6 +326,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
337
  except Exception as e:
338
  status_message = f"An unexpected error occurred during submission: {e}"
339
  print(status_message)
 
340
  results_df = pd.DataFrame(results_log)
341
  return status_message, results_df
342
 
@@ -392,7 +382,7 @@ class Tee:
392
 
393
  if __name__ == "__main__":
394
  # Redirect stdout and stderr
395
- log_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
396
  log_file = f"./logs/output_{log_timestamp}.log"
397
  tee = Tee(log_file)
398
  sys.stdout = tee
 
35
  Process a GAIA benchmark question and return the answer
36
  """
37
  try:
 
 
 
 
38
  if self.verbose:
39
  print(f"Processing question: {question}")
40
+ if task_file_path:
41
+ print(f"With associated file: {task_file_path}")
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Create a context with file information if available
44
+ context = question
45
 
46
  # If there's a file, read it and include its content in the context
47
  if task_file_path:
 
62
  """
63
 
64
  # Check for special cases that need specific formatting
65
+ if question.startswith(".") or ".rewsna eht sa" in question:
66
  context = f"""
67
  This question appears to be in reversed text. Here's the reversed version:
68
+ {question[::-1]}
69
  Now answer the question above. Remember to format your answer exactly as requested.
70
  """
71
 
 
77
  For example, if asked "What is the capital of France?", respond simply with "Paris".
78
  """
79
 
80
+ # *** FIXED AGENT CALL - Handles all response formats ***
 
 
 
 
 
 
 
81
  try:
 
 
 
82
  raw_response = self.agent.run(full_prompt)
83
 
84
  if self.verbose:
85
  print(f"Raw response type: {type(raw_response)}")
86
  print(f"Raw response: {raw_response}")
87
 
88
+ # Handle ALL possible response formats with recursive flattening
89
+ answer = self._extract_answer(raw_response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  if self.verbose:
92
  print(f"Extracted answer type: {type(answer)}")
 
98
  return f"Agent error: {agent_error}"
99
 
100
  # Clean the answer
101
+ answer = self._clean_answer(answer)
 
 
 
 
 
 
102
 
103
  if self.verbose:
104
  print(f"Generated answer: {answer}")
 
112
  traceback.print_exc()
113
  return error_msg
114
 
115
+ def _extract_answer(self, response):
116
  """
117
+ Recursively extract the answer from any nested structure.
118
+ This handles dict, list, and nested combinations.
119
  """
120
+ if response is None:
121
+ return ""
122
+
123
+ # If it's already a string, return it
124
+ if isinstance(response, str):
125
+ return response
126
+
127
+ # If it's a dict, try to get 'content' or 'message'
128
+ if isinstance(response, dict):
129
+ # Try common keys
130
+ for key in ['content', 'message', 'text', 'answer']:
131
+ if key in response:
132
+ return self._extract_answer(response[key])
133
+ # Try OpenAI-style format
134
+ if 'choices' in response:
135
+ choices = response['choices']
136
+ if choices and len(choices) > 0:
137
+ return self._extract_answer(choices[0])
138
+ # If nothing found, convert to string
139
+ return str(response)
140
+
141
+ # If it's a list, recursively process elements
142
+ if isinstance(response, list):
143
+ if len(response) == 0:
144
  return ""
145
+ # Try to extract from first element
146
+ first_element = self._extract_answer(response[0])
147
+ # If first element is empty or None, try other elements
148
+ if not first_element and len(response) > 1:
149
+ for element in response[1:]:
150
+ extracted = self._extract_answer(element)
151
+ if extracted:
152
+ return extracted
153
+ return first_element
154
+
155
+ # For any other type, convert to string
156
+ return str(response)
157
+
158
+ def _clean_answer(self, answer) -> str:
159
+ """
160
+ Ultra-safe answer extraction and cleaning.
161
+ Ensures the answer is always a string.
162
+ """
163
+ # First, ensure we have a string
164
+ try:
165
+ # Recursively extract if needed
166
+ if not isinstance(answer, str):
167
+ answer = self._extract_answer(answer)
168
+
169
+ # At this point, answer should be a string
170
  if not isinstance(answer, str):
171
  answer = str(answer)
172
+
173
  except Exception as e:
174
  print(f"Error in initial conversion: {e}")
175
+ traceback.print_exc()
176
+ try:
177
+ return str(answer) if answer else ""
178
+ except:
179
+ return ""
180
 
181
  # Now answer should definitely be a string
182
  try:
 
201
 
202
  return answer
203
  except Exception as e:
204
+ print(f"Error in answer cleaning: {e}")
205
+ traceback.print_exc()
206
+ try:
207
+ return str(answer) if answer else ""
208
+ except:
209
+ return ""
210
 
211
  def run_and_submit_all(profile: gr.OAuthProfile | None):
212
  """
213
  Fetches all questions, runs the BasicAgent on them, submits all answers,
214
  and displays the results.
215
  """
216
+ if profile is None:
217
+ return "Please log in with your Hugging Face account first.", None
218
+ username = profile.username
 
 
 
 
 
 
219
  api_url = DEFAULT_API_URL
220
  questions_url = f"{api_url}/questions"
221
  submit_url = f"{api_url}/submit"
 
274
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
275
  except Exception as e:
276
  print(f"Error running agent on task {task_id}: {e}")
277
+ traceback.print_exc()
278
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
279
 
280
  if not answers_payload:
 
326
  except Exception as e:
327
  status_message = f"An unexpected error occurred during submission: {e}"
328
  print(status_message)
329
+ traceback.print_exc()
330
  results_df = pd.DataFrame(results_log)
331
  return status_message, results_df
332
 
 
382
 
383
  if __name__ == "__main__":
384
  # Redirect stdout and stderr
385
+ log_timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
386
  log_file = f"./logs/output_{log_timestamp}.log"
387
  tee = Tee(log_file)
388
  sys.stdout = tee