Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,54 +16,46 @@ def convert(file, out_format):
|
|
| 16 |
# Convert the file
|
| 17 |
doc = converter.convert(file.name)
|
| 18 |
|
| 19 |
-
# Get the base filename without extension
|
| 20 |
-
base_filename = os.path.splitext(os.path.basename(file.name))[0]
|
| 21 |
-
|
| 22 |
# Export based on the selected format
|
| 23 |
if out_format == "Markdown":
|
| 24 |
# Save as Markdown file
|
| 25 |
-
output_filename = f"{base_filename}.md"
|
| 26 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as temp_file:
|
| 27 |
temp_filename = temp_file.name
|
| 28 |
with open(temp_filename, 'w') as f:
|
| 29 |
f.write(doc.document.export_to_markdown())
|
| 30 |
-
return temp_filename
|
| 31 |
|
| 32 |
elif out_format == "HTML":
|
| 33 |
# Save as HTML file
|
| 34 |
-
output_filename = f"{base_filename}.html"
|
| 35 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as temp_file:
|
| 36 |
temp_filename = temp_file.name
|
| 37 |
with open(temp_filename, 'w') as f:
|
| 38 |
f.write(doc.document.export_to_html())
|
| 39 |
-
return temp_filename
|
| 40 |
|
| 41 |
elif out_format == "JSON":
|
| 42 |
# Save as JSON file (lossless serialization)
|
| 43 |
-
output_filename = f"{base_filename}.json"
|
| 44 |
doctags = doc.document.export_to_dict() # Correct method for JSON export
|
| 45 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".json") as temp_file:
|
| 46 |
temp_filename = temp_file.name
|
| 47 |
with open(temp_filename, 'w') as f:
|
| 48 |
json.dump(doctags, f, indent=4)
|
| 49 |
-
return temp_filename
|
| 50 |
|
| 51 |
elif out_format == "Text":
|
| 52 |
# Save as Text file (plain text)
|
| 53 |
-
output_filename = f"{base_filename}.txt"
|
| 54 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as temp_file:
|
| 55 |
temp_filename = temp_file.name
|
| 56 |
with open(temp_filename, 'w') as f:
|
| 57 |
f.write(doc.document.export_to_text()) # Correct method for plain text extraction
|
| 58 |
-
return temp_filename
|
| 59 |
|
| 60 |
elif out_format == "Doctags":
|
| 61 |
# Save as Doctags file
|
| 62 |
-
output_filename = f"{base_filename}.doctags"
|
| 63 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".doctags") as temp_file:
|
| 64 |
temp_filename = temp_file.name
|
| 65 |
doc.document.save_as_doctags(temp_filename)
|
| 66 |
-
return temp_filename
|
| 67 |
|
| 68 |
else:
|
| 69 |
return "Unsupported output format"
|
|
@@ -114,12 +106,8 @@ with gr.Blocks() as demo:
|
|
| 114 |
out = gr.File(label="📄 Download Output File")
|
| 115 |
|
| 116 |
# Wire it up
|
| 117 |
-
|
| 118 |
-
output_file, output_filename = convert(file, fmt)
|
| 119 |
-
return output_file # Return file path for download
|
| 120 |
-
|
| 121 |
-
btn.click(on_convert, inputs=[inp, fmt], outputs=out)
|
| 122 |
|
| 123 |
# 4) Launch
|
| 124 |
if __name__ == "__main__":
|
| 125 |
-
demo.launch()
|
|
|
|
| 16 |
# Convert the file
|
| 17 |
doc = converter.convert(file.name)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# Export based on the selected format
|
| 20 |
if out_format == "Markdown":
|
| 21 |
# Save as Markdown file
|
|
|
|
| 22 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as temp_file:
|
| 23 |
temp_filename = temp_file.name
|
| 24 |
with open(temp_filename, 'w') as f:
|
| 25 |
f.write(doc.document.export_to_markdown())
|
| 26 |
+
return temp_filename # Return file for download
|
| 27 |
|
| 28 |
elif out_format == "HTML":
|
| 29 |
# Save as HTML file
|
|
|
|
| 30 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as temp_file:
|
| 31 |
temp_filename = temp_file.name
|
| 32 |
with open(temp_filename, 'w') as f:
|
| 33 |
f.write(doc.document.export_to_html())
|
| 34 |
+
return temp_filename # Return file for download
|
| 35 |
|
| 36 |
elif out_format == "JSON":
|
| 37 |
# Save as JSON file (lossless serialization)
|
|
|
|
| 38 |
doctags = doc.document.export_to_dict() # Correct method for JSON export
|
| 39 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".json") as temp_file:
|
| 40 |
temp_filename = temp_file.name
|
| 41 |
with open(temp_filename, 'w') as f:
|
| 42 |
json.dump(doctags, f, indent=4)
|
| 43 |
+
return temp_filename # Return file for download
|
| 44 |
|
| 45 |
elif out_format == "Text":
|
| 46 |
# Save as Text file (plain text)
|
|
|
|
| 47 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as temp_file:
|
| 48 |
temp_filename = temp_file.name
|
| 49 |
with open(temp_filename, 'w') as f:
|
| 50 |
f.write(doc.document.export_to_text()) # Correct method for plain text extraction
|
| 51 |
+
return temp_filename # Return file for download
|
| 52 |
|
| 53 |
elif out_format == "Doctags":
|
| 54 |
# Save as Doctags file
|
|
|
|
| 55 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".doctags") as temp_file:
|
| 56 |
temp_filename = temp_file.name
|
| 57 |
doc.document.save_as_doctags(temp_filename)
|
| 58 |
+
return temp_filename # Return file for download
|
| 59 |
|
| 60 |
else:
|
| 61 |
return "Unsupported output format"
|
|
|
|
| 106 |
out = gr.File(label="📄 Download Output File")
|
| 107 |
|
| 108 |
# Wire it up
|
| 109 |
+
btn.click(fn=convert, inputs=[inp, fmt], outputs=out)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# 4) Launch
|
| 112 |
if __name__ == "__main__":
|
| 113 |
+
demo.launch().
|