AnseMin commited on
Commit
13fd803
·
1 Parent(s): 45cd93b

adding a cancel button

Browse files
Files changed (1) hide show
  1. src/ui.py +45 -36
src/ui.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
2
  import markdown
 
 
3
  from converter import convert_file
4
  from docling_chat import chat_with_document
5
  from parser_registry import ParserRegistry
@@ -74,8 +76,7 @@ def create_ui():
74
  .page-navigation { text-align: center; margin-top: 1rem; }
75
  .page-navigation button { margin: 0 0.5rem; }
76
  .page-info { display: inline-block; margin: 0 1rem; }
77
- .processing-indicator { display: flex; align-items: center; gap: 10px; }
78
- .cancel-btn { background-color: #ff4d4f; color: white; }
79
  """) as demo:
80
  gr.Markdown("Doc2Md: Convert any documents to Markdown")
81
 
@@ -98,14 +99,14 @@ def create_ui():
98
 
99
  file_download = gr.File(label="Download File")
100
 
101
- # Add processing indicator and cancel button
102
- with gr.Row(visible=False) as processing_row:
103
- with gr.Column(scale=3):
104
- processing_indicator = gr.Markdown("Processing document... This may take a while.", elem_classes=["processing-indicator"])
105
- with gr.Column(scale=1):
106
- cancel_btn = gr.Button("Cancel", elem_classes=["cancel-btn"])
107
 
108
- convert_button = gr.Button("Convert")
 
109
 
110
  with gr.Tab("Config ⚙️"):
111
  with gr.Group(elem_classes=["settings-group"]):
@@ -151,37 +152,45 @@ def create_ui():
151
  outputs=[ocr_dropdown]
152
  )
153
 
154
- def start_processing():
155
- return gr.update(visible=True), gr.update(interactive=False)
156
-
157
- def end_processing():
158
- return gr.update(visible=False), gr.update(interactive=True)
159
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  convert_button.click(
161
- fn=start_processing,
162
- inputs=None,
163
- outputs=[processing_row, convert_button],
164
- queue=False
165
- ).then(
166
- fn=handle_convert,
167
  inputs=[file_input, provider_dropdown, ocr_dropdown, output_format],
168
- outputs=[file_display, file_download, content_pages, current_page, page_info, navigation_row]
169
- ).then(
170
- fn=end_processing,
171
- inputs=None,
172
- outputs=[processing_row, convert_button]
173
  )
174
 
175
- # Add cancel button functionality
176
- cancel_btn.click(
177
- fn=lambda: None,
178
- inputs=None,
179
- outputs=None,
180
- cancels=[convert_button]
181
- ).then(
182
- fn=end_processing,
183
- inputs=None,
184
- outputs=[processing_row, convert_button]
185
  )
186
 
187
  prev_btn.click(
 
1
  import gradio as gr
2
  import markdown
3
+ import threading
4
+ import time
5
  from converter import convert_file
6
  from docling_chat import chat_with_document
7
  from parser_registry import ParserRegistry
 
76
  .page-navigation { text-align: center; margin-top: 1rem; }
77
  .page-navigation button { margin: 0 0.5rem; }
78
  .page-info { display: inline-block; margin: 0 1rem; }
79
+ .processing-controls { display: flex; justify-content: center; gap: 10px; margin-top: 10px; }
 
80
  """) as demo:
81
  gr.Markdown("Doc2Md: Convert any documents to Markdown")
82
 
 
99
 
100
  file_download = gr.File(label="Download File")
101
 
102
+ # Processing controls row
103
+ with gr.Row(elem_classes=["processing-controls"]) as processing_controls:
104
+ convert_button = gr.Button("Convert", variant="primary")
105
+ # Cancel button that will only be visible during processing
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
  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(