AnseMin commited on
Commit
66c5a69
·
1 Parent(s): 13fd803

error handling, cancel button implementation

Browse files
Files changed (1) hide show
  1. src/ui.py +36 -24
src/ui.py CHANGED
@@ -7,6 +7,9 @@ from docling_chat import chat_with_document
7
  from parser_registry import ParserRegistry
8
 
9
 
 
 
 
10
  def format_markdown_content(content):
11
  if not content:
12
  return content
@@ -80,6 +83,9 @@ def create_ui():
80
  """) as demo:
81
  gr.Markdown("Doc2Md: Convert any documents to Markdown")
82
 
 
 
 
83
  with gr.Tabs():
84
  with gr.Tab("Upload and Convert"):
85
  file_input = gr.File(label="Upload PDF", type="filepath")
@@ -106,7 +112,7 @@ def create_ui():
106
  cancel_button = gr.Button("Cancel", variant="stop", visible=False)
107
 
108
  # Add a progress indicator
109
- progress = gr.Progress()
110
 
111
  with gr.Tab("Config ⚙️"):
112
  with gr.Group(elem_classes=["settings-group"]):
@@ -152,45 +158,51 @@ def create_ui():
152
  outputs=[ocr_dropdown]
153
  )
154
 
155
- # Update the convert button click handler to be cancellable and show/hide the cancel button
156
- def start_conversion(file_path, parser_name, ocr_method_name, output_format, progress=gr.Progress()):
157
- # Show cancel button when processing starts
158
- yield "", None, [], 1, "Processing...", gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
159
 
160
- # Simulate progress updates (this will be replaced by actual progress from converter)
161
- for i in range(10):
162
- # Check if the task has been cancelled
163
- if progress.cancelled:
164
- yield "Conversion cancelled.", None, [], 1, "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
165
- return
166
-
167
- progress(i/10, desc=f"Processing document ({i*10}%)")
168
- time.sleep(0.2) # Simulate work
169
 
170
- # Actual conversion
171
  try:
 
172
  content, download_file = convert_file(file_path, parser_name, ocr_method_name, output_format)
 
 
 
 
 
 
 
173
  pages = split_content_into_pages(str(content))
174
  page_info = f"Page 1/{len(pages)}"
175
 
176
  # Return results and update UI
177
- yield str(pages[0]) if pages else "", download_file, pages, 1, page_info, gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
 
178
  except Exception as e:
179
- yield f"Error: {str(e)}", None, [], 1, "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
 
 
 
 
 
 
180
 
181
  convert_button.click(
182
  fn=start_conversion,
183
- inputs=[file_input, provider_dropdown, ocr_dropdown, output_format],
184
- outputs=[file_display, file_download, content_pages, current_page, page_info, navigation_row, convert_button, cancel_button],
185
- cancellable=True, # Make this operation cancellable
186
  )
187
 
188
- # When cancel button is clicked, it should reset the UI
189
  cancel_button.click(
190
- fn=lambda: ("Conversion cancelled.", None, [], 1, "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)),
191
  inputs=[],
192
- outputs=[file_display, file_download, content_pages, current_page, page_info, navigation_row, convert_button, cancel_button],
193
- cancels=["convert"] # This will cancel the running convert task
194
  )
195
 
196
  prev_btn.click(
 
7
  from parser_registry import ParserRegistry
8
 
9
 
10
+ # Add a global variable to track cancellation state
11
+ conversion_cancelled = threading.Event()
12
+
13
  def format_markdown_content(content):
14
  if not content:
15
  return content
 
83
  """) as demo:
84
  gr.Markdown("Doc2Md: Convert any documents to Markdown")
85
 
86
+ # Add a state to track if conversion is in progress
87
+ is_converting = gr.State(False)
88
+
89
  with gr.Tabs():
90
  with gr.Tab("Upload and Convert"):
91
  file_input = gr.File(label="Upload PDF", type="filepath")
 
112
  cancel_button = gr.Button("Cancel", variant="stop", visible=False)
113
 
114
  # Add a progress indicator
115
+ progress_indicator = gr.Markdown("", visible=False)
116
 
117
  with gr.Tab("Config ⚙️"):
118
  with gr.Group(elem_classes=["settings-group"]):
 
158
  outputs=[ocr_dropdown]
159
  )
160
 
161
+ # Function to handle conversion with progress updates
162
+ def start_conversion(file_path, parser_name, ocr_method_name, output_format, converting):
163
+ global conversion_cancelled
 
164
 
165
+ # Reset cancellation flag
166
+ conversion_cancelled.clear()
167
+
168
+ # Show progress indicator and cancel button
169
+ yield "", None, [], 1, "", gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True, value="Processing document..."), True
 
 
 
 
170
 
 
171
  try:
172
+ # Actual conversion
173
  content, download_file = convert_file(file_path, parser_name, ocr_method_name, output_format)
174
+
175
+ # Check if cancelled during conversion
176
+ if conversion_cancelled.is_set():
177
+ yield "Conversion cancelled.", None, [], 1, "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), False
178
+ return
179
+
180
+ # Process results
181
  pages = split_content_into_pages(str(content))
182
  page_info = f"Page 1/{len(pages)}"
183
 
184
  # Return results and update UI
185
+ yield str(pages[0]) if pages else "", download_file, pages, 1, page_info, gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), False
186
+
187
  except Exception as e:
188
+ yield f"Error: {str(e)}", None, [], 1, "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), False
189
+
190
+ # Function to handle cancellation
191
+ def cancel_conversion():
192
+ global conversion_cancelled
193
+ conversion_cancelled.set()
194
+ return "Conversion cancelled.", None, [], 1, "", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), False
195
 
196
  convert_button.click(
197
  fn=start_conversion,
198
+ inputs=[file_input, provider_dropdown, ocr_dropdown, output_format, is_converting],
199
+ outputs=[file_display, file_download, content_pages, current_page, page_info, navigation_row, convert_button, cancel_button, progress_indicator, is_converting]
 
200
  )
201
 
 
202
  cancel_button.click(
203
+ fn=cancel_conversion,
204
  inputs=[],
205
+ outputs=[file_display, file_download, content_pages, current_page, page_info, navigation_row, convert_button, cancel_button, progress_indicator, is_converting]
 
206
  )
207
 
208
  prev_btn.click(