anki-csv2ankicards/server.py

43 lines
1.4 KiB
Python

from flask import Flask, request, send_from_directory, jsonify
import os
import tempfile # For creating temporary directories
import shutil # For removing directories
from pipeline import pipeline
app = Flask(__name__)
@app.route('/generate-deck', methods=['POST'])
def generate_deck():
# Assuming images are sent as multipart/form-data
if 'image' not in request.files:
return jsonify({'error': 'No image part'}), 400
images = request.files.getlist('image')
if not images or all([img.filename == '' for img in images]):
return jsonify({'error': 'No selected file'}), 400
# Create a temporary directory to store multiple images
temp_dir = tempfile.mkdtemp()
image_paths = []
for img in images:
image_path = os.path.join(temp_dir, img.filename)
img.save(image_path)
image_paths.append(image_path)
try:
# Run the pipeline using the saved images
# You might need to modify your pipeline to accept and handle multiple images
pipeline(temp_dir) # Assuming pipeline works per directory of images
return send_from_directory('.', 'output.apkg', as_attachment=True)
except Exception as e:
return jsonify({'error': str(e)}), 500
finally:
# Cleanup: Remove the temporary directory and its content
shutil.rmtree(temp_dir)
if __name__ == '__main__':
app.run(debug=True)