From bbbea2e00bffcfe33668f30873ca6867d0b123e4 Mon Sep 17 00:00:00 2001 From: Pierre-Edouard Portier Date: Thu, 23 Nov 2023 22:20:20 +0100 Subject: [PATCH] First version of the Flask API --- summary_service.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 summary_service.py diff --git a/summary_service.py b/summary_service.py new file mode 100644 index 0000000..d4595f0 --- /dev/null +++ b/summary_service.py @@ -0,0 +1,63 @@ +from audio_summary import transcrire_audio, resumer_texte +from flask import Flask, request, jsonify +import os +import threading +import uuid +from werkzeug.utils import secure_filename + +app = Flask(__name__) + +tasks = {} # Dictionnaire pour stocker l'état des tâches + +def handle_audio_processing(file_path, task_id): + try: + transcription = transcrire_audio(file_path) + resume = resumer_texte(transcription) + tasks[task_id] = {'status': 'completed', 'result': resume} + except Exception as e: + tasks[task_id] = {'status': 'error', 'error': str(e)} + finally: + os.remove(file_path) + +@app.route('/upload', methods=['POST']) +def upload_file(): + if 'file' not in request.files: + return jsonify(error="Aucun fichier trouvé"), 400 + + file = request.files['file'] + if file.filename == '': + return jsonify(error="Aucun fichier sélectionné"), 400 + + if file: + filename = secure_filename(file.filename) + filepath = os.path.join('/Users/peportier/tmp/audio_summary', filename) + file.save(filepath) + + task_id = str(uuid.uuid4()) + tasks[task_id] = {'status': 'in_progress'} + thread = threading.Thread(target=handle_audio_processing, args=(filepath, task_id)) + thread.start() + + return jsonify(task_id=task_id) + else: + return jsonify(error="Type de fichier non pris en charge"), 400 + +@app.route('/result/', methods=['GET']) +def get_result(task_id): + task = tasks.get(task_id, None) + if task is None: + return jsonify(error="Tâche non trouvée"), 404 + + if task['status'] == 'in_progress': + return jsonify(status="Traitement en cours"), 202 + elif task['status'] == 'completed': + return jsonify(result=task['result']), 200 + else: # Erreur dans le traitement + return jsonify(error=task.get('error', 'Erreur inconnue')), 500 + +@app.route('/') +def index(): + return app.send_static_file('index.html') + +if __name__ == '__main__': + app.run(debug=True, port=8080) \ No newline at end of file