segovia commited on
Commit
119c0cc
·
1 Parent(s): 2704ee7

Some modifications

Browse files
Files changed (1) hide show
  1. app.py +111 -8
app.py CHANGED
@@ -2,6 +2,7 @@ from __future__ import annotations
2
 
3
  import os
4
  import csv
 
5
  import tempfile
6
  from pathlib import Path
7
 
@@ -36,7 +37,6 @@ RESULT_COLUMNS = [
36
  "confidence",
37
  ]
38
 
39
-
40
  def gpu_decorator(fn):
41
  if spaces is None:
42
  return fn
@@ -74,6 +74,27 @@ def rows_to_table(rows: list[dict]) -> list[list[object]]:
74
  return [[row.get(column) for column in RESULT_COLUMNS] for row in rows]
75
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  def build_csv_download(rows: list[dict]) -> str:
78
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", newline="", encoding="utf-8")
79
  with temp_file as csv_file:
@@ -108,6 +129,54 @@ def clear_results():
108
  return [], gr.update(value=[]), "Results cleared."
109
 
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  def build_app() -> gr.Blocks:
112
  default_model = os.getenv("OPENAI_MODEL", "gpt-4o")
113
 
@@ -120,19 +189,24 @@ def build_app() -> gr.Blocks:
120
 
121
  status = gr.Markdown("Ready.")
122
  results_state = gr.State([])
123
- results_grid = gr.Dataframe(
124
- label="Parsed documents",
125
- headers=RESULT_COLUMNS,
126
- value=[],
127
- interactive=False,
128
- wrap=True,
129
- )
130
 
131
  with gr.Row():
132
  parse_button = gr.Button("Parse documents", variant="primary")
133
  clear_button = gr.Button("Clear grid")
 
 
134
  download_button = gr.DownloadButton(label="Download CSV", variant="secondary", value=build_csv_download, inputs=[results_state])
135
 
 
 
 
 
 
 
 
 
136
  parse_button.click(
137
  process_files,
138
  inputs=[files, results_state, model],
@@ -145,6 +219,35 @@ def build_app() -> gr.Blocks:
145
  outputs=[results_state, results_grid, status],
146
  )
147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  return demo
149
 
150
 
 
2
 
3
  import os
4
  import csv
5
+ import json
6
  import tempfile
7
  from pathlib import Path
8
 
 
37
  "confidence",
38
  ]
39
 
 
40
  def gpu_decorator(fn):
41
  if spaces is None:
42
  return fn
 
74
  return [[row.get(column) for column in RESULT_COLUMNS] for row in rows]
75
 
76
 
77
+ def table_to_rows(table: object) -> list[dict]:
78
+ if table is None:
79
+ return []
80
+
81
+ if hasattr(table, "to_dict"):
82
+ try:
83
+ records = table.to_dict(orient="records")
84
+ return [{column: record.get(column) for column in RESULT_COLUMNS} for record in records]
85
+ except TypeError:
86
+ pass
87
+
88
+ rows: list[dict] = []
89
+ for raw_row in table: # type: ignore[assignment]
90
+ values = list(raw_row)
91
+ row = {}
92
+ for index, column in enumerate(RESULT_COLUMNS):
93
+ row[column] = values[index] if index < len(values) else None
94
+ rows.append(row)
95
+ return rows
96
+
97
+
98
  def build_csv_download(rows: list[dict]) -> str:
99
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", newline="", encoding="utf-8")
100
  with temp_file as csv_file:
 
129
  return [], gr.update(value=[]), "Results cleared."
130
 
131
 
132
+ def sync_grid_to_state(grid_value: object):
133
+ rows = table_to_rows(grid_value)
134
+ return rows, "Grid updated."
135
+
136
+
137
+ def select_grid_row(evt: gr.SelectData):
138
+ index = evt.index
139
+ if isinstance(index, (tuple, list)) and len(index) == 2:
140
+ row_index = index[0]
141
+ elif isinstance(index, int):
142
+ row_index = index
143
+ else:
144
+ return None, "Select a row first."
145
+
146
+ try:
147
+ row_index = int(row_index)
148
+ except (TypeError, ValueError):
149
+ return None, "Select a row first."
150
+
151
+ if row_index < 0:
152
+ return None, "Select a row first."
153
+
154
+ return row_index, f"Selected row {row_index + 1}."
155
+
156
+
157
+ def delete_selected_row(existing_rows: list[dict] | None, selected_row: int | None):
158
+ rows = list(existing_rows or [])
159
+ if selected_row is None:
160
+ return rows, gr.update(value=rows_to_table(rows)), "Select a row first."
161
+
162
+ if selected_row < 0 or selected_row >= len(rows):
163
+ return rows, gr.update(value=rows_to_table(rows)), "Selected row is out of range."
164
+
165
+ rows.pop(selected_row)
166
+ return rows, gr.update(value=rows_to_table(rows)), f"Deleted row {selected_row + 1}."
167
+
168
+
169
+ def send_selected_row(existing_rows: list[dict] | None, selected_row: int | None):
170
+ rows = list(existing_rows or [])
171
+ if selected_row is None:
172
+ return "Select a row first.", ""
173
+
174
+ if selected_row < 0 or selected_row >= len(rows):
175
+ return "Selected row is out of range.", ""
176
+
177
+ payload = json.dumps(rows[selected_row], ensure_ascii=False, indent=2)
178
+ return f"Sent row {selected_row + 1}.", payload
179
+
180
  def build_app() -> gr.Blocks:
181
  default_model = os.getenv("OPENAI_MODEL", "gpt-4o")
182
 
 
189
 
190
  status = gr.Markdown("Ready.")
191
  results_state = gr.State([])
192
+ selected_row_state = gr.State(None)
193
+ send_payload = gr.Textbox(visible=False)
 
 
 
 
 
194
 
195
  with gr.Row():
196
  parse_button = gr.Button("Parse documents", variant="primary")
197
  clear_button = gr.Button("Clear grid")
198
+ send_row_button = gr.Button("Send", variant="secondary")
199
+ delete_row_button = gr.Button("Delete row", variant="stop")
200
  download_button = gr.DownloadButton(label="Download CSV", variant="secondary", value=build_csv_download, inputs=[results_state])
201
 
202
+ results_grid = gr.Dataframe(
203
+ label="Parsed documents",
204
+ headers=RESULT_COLUMNS,
205
+ value=rows_to_table([]),
206
+ interactive=True,
207
+ wrap=True,
208
+ )
209
+
210
  parse_button.click(
211
  process_files,
212
  inputs=[files, results_state, model],
 
219
  outputs=[results_state, results_grid, status],
220
  )
221
 
222
+ results_grid.change(
223
+ sync_grid_to_state,
224
+ inputs=[results_grid],
225
+ outputs=[results_state, status],
226
+ )
227
+
228
+ results_grid.select(
229
+ select_grid_row,
230
+ inputs=[],
231
+ outputs=[selected_row_state, status],
232
+ )
233
+
234
+ delete_row_button.click(
235
+ delete_selected_row,
236
+ inputs=[results_state, selected_row_state],
237
+ outputs=[results_state, results_grid, status],
238
+ )
239
+
240
+ send_row_button.click(
241
+ send_selected_row,
242
+ inputs=[results_state, selected_row_state],
243
+ outputs=[status, send_payload],
244
+ ).then(
245
+ fn=None,
246
+ inputs=[send_payload],
247
+ outputs=[],
248
+ js="(payload) => { if (payload) { alert(payload); } }",
249
+ )
250
+
251
  return demo
252
 
253