aquibmoin commited on
Commit
4d1254b
1 Parent(s): 9ef9a98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -195,10 +195,10 @@ def extract_table_from_response(gpt_response):
195
  # Split the response into lines
196
  lines = gpt_response.strip().split("\n")
197
 
198
- # Find where the table starts and ends (based on the presence of pipes `|`)
199
- table_lines = [line for line in lines if '|' in line]
200
 
201
- # If no table is found, return an empty string
202
  if not table_lines:
203
  return None
204
 
@@ -215,15 +215,22 @@ def gpt_response_to_dataframe(gpt_response):
215
  # Extract the table text from the GPT response
216
  table_lines = extract_table_from_response(gpt_response)
217
 
218
- # If no table found, return None or an empty DataFrame
219
- if table_lines is None:
220
  return pd.DataFrame()
221
 
222
- # Find the separator line (line with dashes) to determine columns
223
- sep_line_index = next(i for i, line in enumerate(table_lines) if set(line.strip()) == {'|'})
 
 
 
 
 
224
 
225
- # Extract headers and rows
226
  headers = [h.strip() for h in table_lines[sep_line_index - 1].split('|')[1:-1]]
 
 
227
  rows = [
228
  [cell.strip() for cell in row.split('|')[1:-1]]
229
  for row in table_lines[sep_line_index + 1:]
 
195
  # Split the response into lines
196
  lines = gpt_response.strip().split("\n")
197
 
198
+ # Find where the table starts and ends (based on the presence of pipes `|` and at least 3 columns)
199
+ table_lines = [line for line in lines if '|' in line and len(line.split('|')) > 3]
200
 
201
+ # If no table is found, return None or an empty string
202
  if not table_lines:
203
  return None
204
 
 
215
  # Extract the table text from the GPT response
216
  table_lines = extract_table_from_response(gpt_response)
217
 
218
+ # If no table found, return an empty DataFrame
219
+ if table_lines is None or len(table_lines) == 0:
220
  return pd.DataFrame()
221
 
222
+ # Find the header and row separator (assume it's a line with dashes like |---|)
223
+ try:
224
+ # The separator line (contains dashes separating headers and rows)
225
+ sep_line_index = next(i for i, line in enumerate(table_lines) if set(line.strip()) == {'|', '-'})
226
+ except StopIteration:
227
+ # If no separator line is found, return an empty DataFrame
228
+ return pd.DataFrame()
229
 
230
+ # Extract headers (the line before the separator) and rows (lines after the separator)
231
  headers = [h.strip() for h in table_lines[sep_line_index - 1].split('|')[1:-1]]
232
+
233
+ # Extract rows (each line after the separator)
234
  rows = [
235
  [cell.strip() for cell in row.split('|')[1:-1]]
236
  for row in table_lines[sep_line_index + 1:]