88 lines
2.9 KiB
Python
88 lines
2.9 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):
|
|
task_folder = os.path.join('../transcribe_data/backup', task_id)
|
|
os.makedirs(task_folder, exist_ok=True)
|
|
|
|
try:
|
|
transcription = transcrire_audio(file_path)
|
|
with open(os.path.join(task_folder, 'transcription.txt'), 'w') as file:
|
|
file.write(transcription)
|
|
|
|
resume = resumer_texte(transcription)
|
|
with open(os.path.join(task_folder, 'resume.md'), 'w') as file:
|
|
file.write(resume)
|
|
|
|
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('/tasks', methods=['GET'])
|
|
def list_saved_tasks():
|
|
base_path = '../transcribe_data/backup'
|
|
task_ids = [d for d in os.listdir(base_path) if os.path.isdir(os.path.join(base_path, d))]
|
|
return jsonify(task_ids)
|
|
|
|
@app.route('/resume/<task_id>', methods=['GET'])
|
|
def get_saved_resume(task_id):
|
|
resume_path = os.path.join('../transcribe_data/backup', task_id, 'resume.md')
|
|
if os.path.exists(resume_path):
|
|
with open(resume_path, 'r') as file:
|
|
resume = file.read()
|
|
return jsonify(resume=resume)
|
|
else:
|
|
return jsonify(error="Résumé non trouvé"), 404
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return app.send_static_file('index.html')
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, port=8080) |