63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
|
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/<task_id>', 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)
|