philincloud commited on
Commit
c98850d
·
verified ·
1 Parent(s): 4861ba6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -33
app.py CHANGED
@@ -8,12 +8,9 @@ import re
8
  from langchain_core.messages import HumanMessage
9
  from langgraph_agent import build_graph
10
 
11
- # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
- # --- Basic Agent Definition ---
15
  class BasicAgent:
16
- """A langgraph agent."""
17
  def __init__(self):
18
  print("BasicAgent initialized.")
19
  self.graph = build_graph()
@@ -36,24 +33,21 @@ class BasicAgent:
36
 
37
  answer = answer.strip()
38
 
39
- # Extract final answer after "FINAL ANSWER:"
40
  match = re.search(r'FINAL ANSWER:\s*(.*)', answer, re.IGNORECASE | re.DOTALL)
41
  if match:
42
  final_answer = match.group(1).strip()
43
- # Remove surrounding quotes if present
44
  if (final_answer.startswith('"') and final_answer.endswith('"')) or \
45
  (final_answer.startswith("'") and final_answer.endswith("'")):
46
  final_answer = final_answer[1:-1].strip()
47
  answer = final_answer
48
  else:
49
- print("Warning: 'FINAL ANSWER:' not found in agent output; submitting full answer.")
50
 
51
  if not answer:
52
  answer = "I am unable to determine the information using the available tools."
53
 
54
  return answer
55
 
56
-
57
  def run_and_submit_all(profile: gr.OAuthProfile | None):
58
  space_id = os.getenv("SPACE_ID")
59
 
@@ -102,11 +96,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
102
  continue
103
  try:
104
  model_answer = agent(question_text, task_id=task_id)
 
 
 
 
 
 
 
 
 
 
 
105
  print(f"Answer for task {task_id}: '{model_answer}'")
106
  answers_payload.append({
107
  "task_id": task_id,
108
  "model_answer": model_answer,
109
- # "reasoning_trace": None # Add reasoning trace here if available
110
  })
111
  results_log.append({
112
  "Task ID": task_id,
@@ -118,7 +123,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
118
  answers_payload.append({
119
  "task_id": task_id,
120
  "model_answer": f"AGENT ERROR: {e}",
121
- # "reasoning_trace": None
122
  })
123
  results_log.append({
124
  "Task ID": task_id,
@@ -130,7 +135,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
130
  print("Agent did not produce any answers to submit.")
131
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
132
 
133
- # Serialize answers to JSON Lines format
134
  json_lines_str = "\n".join(json.dumps(ans, ensure_ascii=False) for ans in answers_payload)
135
  file_like = io.BytesIO(json_lines_str.encode("utf-8"))
136
  file_like.name = "submission.jsonl"
@@ -163,35 +168,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
163
  print("Submission successful.")
164
  results_df = pd.DataFrame(results_log)
165
  return final_status, results_df
166
- except requests.exceptions.HTTPError as e:
167
- error_detail = f"Server responded with status {e.response.status_code}."
168
- try:
169
- error_json = e.response.json()
170
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
171
- except requests.exceptions.JSONDecodeError:
172
- error_detail += f" Response: {e.response.text[:500]}"
173
- status_message = f"Submission Failed: {error_detail}"
174
- print(status_message)
175
- results_df = pd.DataFrame(results_log)
176
- return status_message, results_df
177
- except requests.exceptions.Timeout:
178
- status_message = "Submission Failed: The request timed out."
179
- print(status_message)
180
- results_df = pd.DataFrame(results_log)
181
- return status_message, results_df
182
- except requests.exceptions.RequestException as e:
183
- status_message = f"Submission Failed: Network error - {e}"
184
- print(status_message)
185
- results_df = pd.DataFrame(results_log)
186
- return status_message, results_df
187
  except Exception as e:
188
- status_message = f"An unexpected error occurred during submission: {e}"
189
  print(status_message)
190
  results_df = pd.DataFrame(results_log)
191
  return status_message, results_df
192
 
193
 
194
- # --- Build Gradio Interface using Blocks ---
195
  with gr.Blocks() as demo:
196
  gr.Markdown("# Basic Agent Evaluation Runner")
197
  gr.Markdown(
@@ -226,6 +209,7 @@ with gr.Blocks() as demo:
226
  outputs=[status_output, results_table]
227
  )
228
 
 
229
  if __name__ == "__main__":
230
  print("\n" + "-"*30 + " App Starting " + "-"*30)
231
  space_host_startup = os.getenv("SPACE_HOST")
 
8
  from langchain_core.messages import HumanMessage
9
  from langgraph_agent import build_graph
10
 
 
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
 
13
  class BasicAgent:
 
14
  def __init__(self):
15
  print("BasicAgent initialized.")
16
  self.graph = build_graph()
 
33
 
34
  answer = answer.strip()
35
 
 
36
  match = re.search(r'FINAL ANSWER:\s*(.*)', answer, re.IGNORECASE | re.DOTALL)
37
  if match:
38
  final_answer = match.group(1).strip()
 
39
  if (final_answer.startswith('"') and final_answer.endswith('"')) or \
40
  (final_answer.startswith("'") and final_answer.endswith("'")):
41
  final_answer = final_answer[1:-1].strip()
42
  answer = final_answer
43
  else:
44
+ print("Warning: 'FINAL ANSWER:' not found; submitting full answer.")
45
 
46
  if not answer:
47
  answer = "I am unable to determine the information using the available tools."
48
 
49
  return answer
50
 
 
51
  def run_and_submit_all(profile: gr.OAuthProfile | None):
52
  space_id = os.getenv("SPACE_ID")
53
 
 
96
  continue
97
  try:
98
  model_answer = agent(question_text, task_id=task_id)
99
+ model_answer = model_answer.strip()
100
+ # Clean trailing quotes/brackets if any
101
+ if model_answer.endswith("]'") or model_answer.endswith(']"'):
102
+ model_answer = model_answer[:-2].strip()
103
+ if (model_answer.startswith('"') and model_answer.endswith('"')) or \
104
+ (model_answer.startswith("'") and model_answer.endswith("'")):
105
+ model_answer = model_answer[1:-1].strip()
106
+
107
+ # Placeholder reasoning trace; replace with actual reasoning if available
108
+ reasoning_trace = f"Reasoning steps for task {task_id}."
109
+
110
  print(f"Answer for task {task_id}: '{model_answer}'")
111
  answers_payload.append({
112
  "task_id": task_id,
113
  "model_answer": model_answer,
114
+ "reasoning_trace": reasoning_trace
115
  })
116
  results_log.append({
117
  "Task ID": task_id,
 
123
  answers_payload.append({
124
  "task_id": task_id,
125
  "model_answer": f"AGENT ERROR: {e}",
126
+ "reasoning_trace": ""
127
  })
128
  results_log.append({
129
  "Task ID": task_id,
 
135
  print("Agent did not produce any answers to submit.")
136
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
137
 
138
+ # Serialize to JSON Lines string
139
  json_lines_str = "\n".join(json.dumps(ans, ensure_ascii=False) for ans in answers_payload)
140
  file_like = io.BytesIO(json_lines_str.encode("utf-8"))
141
  file_like.name = "submission.jsonl"
 
168
  print("Submission successful.")
169
  results_df = pd.DataFrame(results_log)
170
  return final_status, results_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  except Exception as e:
172
+ status_message = f"Submission Failed: {e}"
173
  print(status_message)
174
  results_df = pd.DataFrame(results_log)
175
  return status_message, results_df
176
 
177
 
 
178
  with gr.Blocks() as demo:
179
  gr.Markdown("# Basic Agent Evaluation Runner")
180
  gr.Markdown(
 
209
  outputs=[status_output, results_table]
210
  )
211
 
212
+
213
  if __name__ == "__main__":
214
  print("\n" + "-"*30 + " App Starting " + "-"*30)
215
  space_host_startup = os.getenv("SPACE_HOST")