laverdes commited on
Commit
66dad6d
·
verified ·
1 Parent(s): a4ba7b0

fix: SyntaxError and improve readability

Browse files
Files changed (1) hide show
  1. app.py +19 -1
app.py CHANGED
@@ -43,9 +43,11 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
43
  # 1. Instantiate Agent ( modify this part to create your agent)
44
  try:
45
  agent = BasicAgent()
 
46
  except Exception as e:
47
  print(f"Error instantiating agent: {e}")
48
  return f"Error initializing agent: {e}", None
 
49
  # 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)
50
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
51
  print(agent_code)
@@ -57,17 +59,21 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
57
  response = requests.get(questions_url, timeout=15)
58
  response.raise_for_status()
59
  questions_data = response.json()
 
60
  if not questions_data:
61
  print("Fetched questions list is empty.")
62
  return "Fetched questions list is empty or invalid format.", None
63
  print(f"Fetched {len(questions_data)} questions.")
 
64
  except requests.exceptions.RequestException as e:
65
  print(f"Error fetching questions: {e}")
66
  return f"Error fetching questions: {e}", None
 
67
  except requests.exceptions.JSONDecodeError as e:
68
  print(f"Error decoding JSON response from questions endpoint: {e}")
69
  print(f"Response text: {response.text[:500]}")
70
  return f"Error decoding server response for questions: {e}", None
 
71
  except Exception as e:
72
  print(f"An unexpected error occurred fetching questions: {e}")
73
  return f"An unexpected error occurred fetching questions: {e}", None
@@ -77,16 +83,20 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
77
  results_log = []
78
  answers_payload = []
79
  print(f"Running agent on {len(questions_data)} questions...")
 
80
  for item in questions_data:
81
  task_id = item.get("task_id")
82
  question_text = item.get("question")
 
83
  if not task_id or question_text is None:
84
  print(f"Skipping item with missing task_id or question: {item}")
85
  continue
 
86
  try:
87
  submitted_answer = agent(question_text)
88
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
89
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
90
  except Exception as e:
91
  print(f"Error running agent on task {task_id}: {e}")
92
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
@@ -104,6 +114,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
104
 
105
  # 5. Submit
106
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
 
107
  try:
108
  response = requests.post(submit_url, json=submission_data, timeout=60)
109
  response.raise_for_status()
@@ -118,27 +129,34 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
118
  print("Submission successful.")
119
  results_df = pd.DataFrame(results_log)
120
  return final_status, results_df
 
121
  except requests.exceptions.HTTPError as e:
122
  error_detail = f"Server responded with status {e.response.status_code}."
 
123
  try:
124
  error_json = e.response.json()
125
  error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
 
126
  except requests.exceptions.JSONDecodeError:
127
  error_detail += f" Response: {e.response.text[:500]}"
 
128
  status_message = f"Submission Failed: {error_detail}"
129
  print(status_message)
130
  results_df = pd.DataFrame(results_log)
131
  return status_message, results_df
 
132
  except requests.exceptions.Timeout:
133
  status_message = "Submission Failed: The request timed out."
134
  print(status_message)
135
  results_df = pd.DataFrame(results_log)
136
  return status_message, results_df
 
137
  except requests.exceptions.RequestException as e:
138
  status_message = f"Submission Failed: Network error - {e}"
139
  print(status_message)
140
  results_df = pd.DataFrame(results_log)
141
  return status_message, results_df
 
142
  except Exception as e:
143
  status_message = f"An unexpected error occurred during submission: {e}"
144
  print(status_message)
@@ -158,7 +176,7 @@ with gr.Blocks() as demo:
158
 
159
  run_button = gr.Button("Run Evaluation & Submit All Answers")
160
 
161
- download_button = gr.Button("Download Evaluation Log", interactive=False))
162
 
163
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
164
 
 
43
  # 1. Instantiate Agent ( modify this part to create your agent)
44
  try:
45
  agent = BasicAgent()
46
+
47
  except Exception as e:
48
  print(f"Error instantiating agent: {e}")
49
  return f"Error initializing agent: {e}", None
50
+
51
  # 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)
52
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
53
  print(agent_code)
 
59
  response = requests.get(questions_url, timeout=15)
60
  response.raise_for_status()
61
  questions_data = response.json()
62
+
63
  if not questions_data:
64
  print("Fetched questions list is empty.")
65
  return "Fetched questions list is empty or invalid format.", None
66
  print(f"Fetched {len(questions_data)} questions.")
67
+
68
  except requests.exceptions.RequestException as e:
69
  print(f"Error fetching questions: {e}")
70
  return f"Error fetching questions: {e}", None
71
+
72
  except requests.exceptions.JSONDecodeError as e:
73
  print(f"Error decoding JSON response from questions endpoint: {e}")
74
  print(f"Response text: {response.text[:500]}")
75
  return f"Error decoding server response for questions: {e}", None
76
+
77
  except Exception as e:
78
  print(f"An unexpected error occurred fetching questions: {e}")
79
  return f"An unexpected error occurred fetching questions: {e}", None
 
83
  results_log = []
84
  answers_payload = []
85
  print(f"Running agent on {len(questions_data)} questions...")
86
+
87
  for item in questions_data:
88
  task_id = item.get("task_id")
89
  question_text = item.get("question")
90
+
91
  if not task_id or question_text is None:
92
  print(f"Skipping item with missing task_id or question: {item}")
93
  continue
94
+
95
  try:
96
  submitted_answer = agent(question_text)
97
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
98
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
99
+
100
  except Exception as e:
101
  print(f"Error running agent on task {task_id}: {e}")
102
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
114
 
115
  # 5. Submit
116
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
117
+
118
  try:
119
  response = requests.post(submit_url, json=submission_data, timeout=60)
120
  response.raise_for_status()
 
129
  print("Submission successful.")
130
  results_df = pd.DataFrame(results_log)
131
  return final_status, results_df
132
+
133
  except requests.exceptions.HTTPError as e:
134
  error_detail = f"Server responded with status {e.response.status_code}."
135
+
136
  try:
137
  error_json = e.response.json()
138
  error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
139
+
140
  except requests.exceptions.JSONDecodeError:
141
  error_detail += f" Response: {e.response.text[:500]}"
142
+
143
  status_message = f"Submission Failed: {error_detail}"
144
  print(status_message)
145
  results_df = pd.DataFrame(results_log)
146
  return status_message, results_df
147
+
148
  except requests.exceptions.Timeout:
149
  status_message = "Submission Failed: The request timed out."
150
  print(status_message)
151
  results_df = pd.DataFrame(results_log)
152
  return status_message, results_df
153
+
154
  except requests.exceptions.RequestException as e:
155
  status_message = f"Submission Failed: Network error - {e}"
156
  print(status_message)
157
  results_df = pd.DataFrame(results_log)
158
  return status_message, results_df
159
+
160
  except Exception as e:
161
  status_message = f"An unexpected error occurred during submission: {e}"
162
  print(status_message)
 
176
 
177
  run_button = gr.Button("Run Evaluation & Submit All Answers")
178
 
179
+ download_button = gr.Button("Download Evaluation Log", interactive=False)
180
 
181
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
182