Walidb commited on
Commit
f4a92a2
·
1 Parent(s): cbf4814

try to submit only final answer

Browse files
Files changed (1) hide show
  1. app.py +51 -15
app.py CHANGED
@@ -18,19 +18,49 @@ class WalidzAgent:
18
 
19
  def __call__(self, question: str) -> str:
20
  print(f"Agent received question (first 50 chars): {question[:50]}...")
21
- # Wrap the question in a HumanMessage from langchain_core
22
- messages = [HumanMessage(content=question)]
23
- messages = self.graph.invoke({"messages": messages})
24
- answer = messages['messages'][-1].content
25
- # Find the last occurrence of "FINAL ANSWER:" in the response
26
- if "FINAL ANSWER:" in answer:
27
- # Split by "FINAL ANSWER:" and take the last part
28
- final_answer_parts = answer.split("FINAL ANSWER:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  answer = final_answer_parts[-1].strip()
30
  else:
31
- # If "FINAL ANSWER:" is not found, return the original answer
32
- print("Warning: 'FINAL ANSWER:' not found in response")
33
- print(answer)
 
 
34
  return answer
35
 
36
  # (Keep Constants as is)
@@ -99,7 +129,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
99
  print(f"Skipping item with missing task_id or question: {item}")
100
  continue
101
  try:
102
- submitted_answer = agent(question_text)
 
 
 
 
 
 
 
103
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
104
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
105
  except Exception as e:
@@ -222,9 +259,8 @@ def run_evaluation_only(profile: gr.OAuthProfile | None):
222
  "messages": [human_msg],
223
  "question_text": question_text
224
  }
225
-
226
  submitted_answer = agent(input_state)
227
- #submitted_answer = agent(question_text)
228
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
229
  except Exception as e:
230
  print(f"Error running agent on task {task_id}: {e}")
@@ -235,7 +271,7 @@ def run_evaluation_only(profile: gr.OAuthProfile | None):
235
 
236
  # --- Build Gradio Interface using Blocks ---
237
  with gr.Blocks() as demo:
238
- gr.Markdown("# Walid's Basic Agent Evaluation Runner (v4)")
239
  gr.Markdown(
240
  """
241
  **Instructions:**
 
18
 
19
  def __call__(self, question: str) -> str:
20
  print(f"Agent received question (first 50 chars): {question[:50]}...")
21
+
22
+ # Handle both string and dict input formats
23
+ if isinstance(question, dict) and "messages" in question and "question_text" in question:
24
+ # We got a properly formatted input
25
+ input_state = question
26
+ else:
27
+ # Create a proper input state from the question string
28
+ human_msg = HumanMessage(content=question)
29
+ input_state = {
30
+ "messages": [human_msg],
31
+ "question_text": question
32
+ }
33
+
34
+ # Invoke the graph
35
+ messages = self.graph.invoke(input_state)
36
+
37
+ # Extract the answer based on message structure
38
+ if "assistant" in messages and "messages" in messages["assistant"]:
39
+ content = messages["assistant"]["messages"].content
40
+ elif "messages" in messages and len(messages["messages"]) > 0:
41
+ content = messages["messages"][-1].content
42
+ else:
43
+ print("Warning: Unexpected message structure")
44
+ return "Error: Could not extract answer due to unexpected message structure"
45
+
46
+ # First try to extract from XML tags
47
+ import re
48
+ xml_pattern = re.compile(r'<FINAL_ANSWER>(.*?)</FINAL_ANSWER>', re.DOTALL)
49
+ match = xml_pattern.search(content)
50
+
51
+ if match:
52
+ # Found XML tags
53
+ answer = match.group(1).strip()
54
+ elif "FINAL ANSWER:" in content:
55
+ # Fall back to "FINAL ANSWER:" format
56
+ final_answer_parts = content.split("FINAL ANSWER:")
57
  answer = final_answer_parts[-1].strip()
58
  else:
59
+ # If no explicit final answer format is found, use the original content
60
+ print("Warning: No final answer format found in response")
61
+ answer = content
62
+
63
+ print(f"Extracted final answer: {answer}")
64
  return answer
65
 
66
  # (Keep Constants as is)
 
129
  print(f"Skipping item with missing task_id or question: {item}")
130
  continue
131
  try:
132
+ # Prepare input state
133
+ human_msg = HumanMessage(content=question_text)
134
+ input_state = {
135
+ "messages": [human_msg],
136
+ "question_text": question_text
137
+ }
138
+
139
+ submitted_answer = agent(input_state)
140
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
141
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
142
  except Exception as e:
 
259
  "messages": [human_msg],
260
  "question_text": question_text
261
  }
262
+
263
  submitted_answer = agent(input_state)
 
264
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
265
  except Exception as e:
266
  print(f"Error running agent on task {task_id}: {e}")
 
271
 
272
  # --- Build Gradio Interface using Blocks ---
273
  with gr.Blocks() as demo:
274
+ gr.Markdown("# Walid's Basic Agent Evaluation Runner (v5)")
275
  gr.Markdown(
276
  """
277
  **Instructions:**