Created a server to serve apkg files in respose to image posts

This commit is contained in:
B.J. Dweck 2023-09-08 16:24:34 +03:00
parent af4b615aa3
commit c3ca249877
2 changed files with 44 additions and 1 deletions

View File

@ -1,3 +1,4 @@
genanki==0.8.0
Pillow
openai
openai
flask

42
server.py Normal file
View File

@ -0,0 +1,42 @@
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)