Update app.py
Browse filesSe ha agregado una barra de progreso a app.py.
app.py
CHANGED
@@ -181,10 +181,68 @@ with gr.Blocks(css=custom_css) as app:
|
|
181 |
elem_classes="gr-padded"
|
182 |
)
|
183 |
|
|
|
|
|
|
|
184 |
convert_button.click(
|
185 |
-
fn=lambda file, format, method: convert_document_docling(file, format) if method == "llama-index-readers-docling" else convert_document_original(file, format),
|
186 |
inputs=[file_input, format_input, method_input],
|
187 |
-
outputs=[output_text, output_metadata, download_button, download_button, status_message]
|
188 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
app.launch(debug=True, share=True)
|
|
|
|
181 |
elem_classes="gr-padded"
|
182 |
)
|
183 |
|
184 |
+
with gr.Row():
|
185 |
+
progress = gr.Slider(minimum=0, maximum=100, value=0, label="Progress", interactive=False)
|
186 |
+
|
187 |
convert_button.click(
|
188 |
+
fn=lambda file, format, method: convert_document_docling(file, format, progress) if method == "llama-index-readers-docling" else convert_document_original(file, format, progress),
|
189 |
inputs=[file_input, format_input, method_input],
|
190 |
+
outputs=[output_text, output_metadata, download_button, download_button, status_message, progress]
|
191 |
)
|
192 |
+
|
193 |
+
def update_progress(current, total):
|
194 |
+
return (current / total) * 100
|
195 |
+
|
196 |
+
def convert_document_docling(file, output_format, progress):
|
197 |
+
try:
|
198 |
+
reader = DoclingReader(export_type=DoclingReader.ExportType.JSON if output_format == "JSON" else DoclingReader.ExportType.MARKDOWN)
|
199 |
+
docs = reader.load_data(file_path=file.name)
|
200 |
+
converted_text = docs[0].text
|
201 |
+
temp_dir = tempfile.gettempdir()
|
202 |
+
output_filename = os.path.splitext(os.path.basename(file.name))[0] + (".json" if output_format == "JSON" else ".md")
|
203 |
+
output_path = os.path.join(temp_dir, output_filename)
|
204 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
205 |
+
f.write(converted_text)
|
206 |
+
metadata = {
|
207 |
+
"Filename": file.name,
|
208 |
+
"File Size": f"{os.path.getsize(file.name) / 1024:.2f} KB",
|
209 |
+
"Output Format": output_format,
|
210 |
+
"Conversion Status": "Success",
|
211 |
+
"Method": "llama-index-readers-docling"
|
212 |
+
}
|
213 |
+
progress(100) # Set progress to 100% once complete
|
214 |
+
return converted_text, metadata, output_path, gr.update(visible=True), "✅ Document converted successfully!", 100
|
215 |
+
except Exception as e:
|
216 |
+
error_metadata = {"Error": str(e), "Status": "Failed"}
|
217 |
+
return "", error_metadata, None, gr.update(visible=False), "❌ Error during conversion", 0
|
218 |
+
|
219 |
+
def convert_document_original(file, output_format, progress):
|
220 |
+
try:
|
221 |
+
converter = DocumentConverter()
|
222 |
+
result = converter.convert(file.name)
|
223 |
+
temp_dir = tempfile.gettempdir()
|
224 |
+
if output_format == "Markdown":
|
225 |
+
converted_text = result.document.export_to_markdown()
|
226 |
+
file_extension = ".md"
|
227 |
+
else:
|
228 |
+
converted_text = result.document.export_to_json()
|
229 |
+
file_extension = ".json"
|
230 |
+
output_filename = os.path.splitext(os.path.basename(file.name))[0] + file_extension
|
231 |
+
output_path = os.path.join(temp_dir, output_filename)
|
232 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
233 |
+
f.write(converted_text)
|
234 |
+
metadata = {
|
235 |
+
"Filename": file.name,
|
236 |
+
"File Size": f"{os.path.getsize(file.name) / 1024:.2f} KB",
|
237 |
+
"Output Format": output_format,
|
238 |
+
"Conversion Status": "Success",
|
239 |
+
"Method": "docling"
|
240 |
+
}
|
241 |
+
progress(100) # Set progress to 100% once complete
|
242 |
+
return converted_text, metadata, output_path, gr.update(visible=True), "✅ Document converted successfully!", 100
|
243 |
+
except Exception as e:
|
244 |
+
error_metadata = {"Error": str(e), "Status": "Failed"}
|
245 |
+
return "", error_metadata, None, gr.update(visible=False), "❌ Error during conversion", 0
|
246 |
|
247 |
app.launch(debug=True, share=True)
|
248 |
+
|