Spaces:
Paused
Paused
adding a cancel button
Browse files
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-
|
| 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 |
-
#
|
| 102 |
-
with gr.Row(
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
cancel_btn = gr.Button("Cancel", elem_classes=["cancel-btn"])
|
| 107 |
|
| 108 |
-
|
|
|
|
| 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 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
convert_button.click(
|
| 161 |
-
fn=
|
| 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 |
-
|
| 170 |
-
fn=end_processing,
|
| 171 |
-
inputs=None,
|
| 172 |
-
outputs=[processing_row, convert_button]
|
| 173 |
)
|
| 174 |
|
| 175 |
-
#
|
| 176 |
-
|
| 177 |
-
fn=lambda: None,
|
| 178 |
-
inputs=
|
| 179 |
-
outputs=
|
| 180 |
-
cancels=[
|
| 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(
|