Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,17 +7,47 @@ import requests
|
|
7 |
|
8 |
converter = DocumentConverter()
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def convert_input(pdf_file, pdf_url):
|
11 |
-
|
12 |
|
13 |
try:
|
14 |
-
if pdf_file:
|
15 |
-
with
|
16 |
-
|
17 |
-
|
18 |
-
source = temp_pdf_path
|
19 |
elif pdf_url:
|
20 |
source = pdf_url
|
|
|
21 |
else:
|
22 |
raise gr.Error("Please upload a PDF or provide a URL.")
|
23 |
|
@@ -29,13 +59,11 @@ def convert_input(pdf_file, pdf_url):
|
|
29 |
tmp_md.write(markdown)
|
30 |
markdown_path = tmp_md.name
|
31 |
|
32 |
-
return
|
33 |
|
34 |
except Exception as e:
|
35 |
return None, f"Error: {str(e)}", None
|
36 |
-
|
37 |
-
if temp_pdf_path and os.path.exists(temp_pdf_path):
|
38 |
-
os.remove(temp_pdf_path)
|
39 |
|
40 |
with gr.Blocks() as demo:
|
41 |
gr.Markdown("# π PDF to Markdown with `docling`")
|
|
|
7 |
|
8 |
converter = DocumentConverter()
|
9 |
|
10 |
+
# def convert_input(pdf_file, pdf_url):
|
11 |
+
# temp_pdf_path = None
|
12 |
+
|
13 |
+
# try:
|
14 |
+
# if pdf_file:
|
15 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
16 |
+
# tmp.write(pdf_file.read())
|
17 |
+
# temp_pdf_path = tmp.name
|
18 |
+
# source = temp_pdf_path
|
19 |
+
# elif pdf_url:
|
20 |
+
# source = pdf_url
|
21 |
+
# else:
|
22 |
+
# raise gr.Error("Please upload a PDF or provide a URL.")
|
23 |
+
|
24 |
+
# doc = converter.convert(source).document
|
25 |
+
# markdown = doc.export_to_markdown()
|
26 |
+
|
27 |
+
# # Save markdown to a file
|
28 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".md", mode="w", encoding="utf-8") as tmp_md:
|
29 |
+
# tmp_md.write(markdown)
|
30 |
+
# markdown_path = tmp_md.name
|
31 |
+
|
32 |
+
# return pdf_file.name if pdf_file else None, markdown, markdown_path
|
33 |
+
|
34 |
+
# except Exception as e:
|
35 |
+
# return None, f"Error: {str(e)}", None
|
36 |
+
# finally:
|
37 |
+
# if temp_pdf_path and os.path.exists(temp_pdf_path):
|
38 |
+
# os.remove(temp_pdf_path)
|
39 |
+
|
40 |
def convert_input(pdf_file, pdf_url):
|
41 |
+
source = None
|
42 |
|
43 |
try:
|
44 |
+
if pdf_file is not None:
|
45 |
+
# pdf_file is a NamedString object with .name being the local path
|
46 |
+
source = pdf_file.name
|
47 |
+
pdf_preview_path = source
|
|
|
48 |
elif pdf_url:
|
49 |
source = pdf_url
|
50 |
+
pdf_preview_path = None # No local file to preview
|
51 |
else:
|
52 |
raise gr.Error("Please upload a PDF or provide a URL.")
|
53 |
|
|
|
59 |
tmp_md.write(markdown)
|
60 |
markdown_path = tmp_md.name
|
61 |
|
62 |
+
return pdf_preview_path, markdown, markdown_path
|
63 |
|
64 |
except Exception as e:
|
65 |
return None, f"Error: {str(e)}", None
|
66 |
+
|
|
|
|
|
67 |
|
68 |
with gr.Blocks() as demo:
|
69 |
gr.Markdown("# π PDF to Markdown with `docling`")
|