Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = Flask(__name__)
|
|
|
|
| 4 |
|
| 5 |
@app.route('/')
|
| 6 |
def home():
|
|
@@ -8,7 +11,32 @@ def home():
|
|
| 8 |
|
| 9 |
@app.route('/process', methods=['POST'])
|
| 10 |
def process_document():
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
if __name__ == "__main__":
|
| 14 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
from docling.document_converter import DocumentConverter
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
+
converter = DocumentConverter()
|
| 7 |
|
| 8 |
@app.route('/')
|
| 9 |
def home():
|
|
|
|
| 11 |
|
| 12 |
@app.route('/process', methods=['POST'])
|
| 13 |
def process_document():
|
| 14 |
+
if 'file' not in request.files:
|
| 15 |
+
return jsonify({"status": "error", "message": "No file part"}), 400
|
| 16 |
+
|
| 17 |
+
file = request.files['file']
|
| 18 |
+
if file.filename == '':
|
| 19 |
+
return jsonify({"status": "error", "message": "No selected file"}), 400
|
| 20 |
+
|
| 21 |
+
# Save file temporarily
|
| 22 |
+
temp_path = f"/tmp/{file.filename}"
|
| 23 |
+
file.save(temp_path)
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Convert the document
|
| 27 |
+
result = converter.convert(temp_path)
|
| 28 |
+
|
| 29 |
+
# Return the exported text (or other format)
|
| 30 |
+
return jsonify({
|
| 31 |
+
"status": "success",
|
| 32 |
+
"content": result.document.export_to_markdown()
|
| 33 |
+
})
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return jsonify({"status": "error", "message": str(e)}), 500
|
| 36 |
+
finally:
|
| 37 |
+
# Cleanup
|
| 38 |
+
if os.path.exists(temp_path):
|
| 39 |
+
os.remove(temp_path)
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
app.run(host="0.0.0.0", port=7860)
|