-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlaskAPI.py
More file actions
61 lines (49 loc) · 2.1 KB
/
FlaskAPI.py
File metadata and controls
61 lines (49 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
from flask import Flask, request, jsonify,Response
from werkzeug.utils import secure_filename
from ASRTranscriber import ASRTranscriber # Asegúrate de que ASRTranscriber esté en el path o definido en el mismo archivo
from time import time
app = Flask(__name__)
# Configura una carpeta para los archivos subidos
UPLOAD_FOLDER = 'uploads'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
start_time = time()
# Instancia de ASRTranscriber
transcriber = ASRTranscriber(
model_id="openai/whisper-medium",
batch_size=16,
chunk_length_s=10,
max_new_tokens=128
)
print(f"Tiempo de carga del modelo: {time()-start_time}")
@app.route('/transcribe', methods=['POST'])
def transcribe_audio():
# Comprueba si el post request tiene el file part
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
# Si el usuario no selecciona un archivo, el navegador
# puede enviar una parte vacía sin filename
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file and file.filename.endswith('.wav'):
filename = secure_filename(file.filename)
audio_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(audio_path)
# Procesar la transcripción
try:
output_file = f"{os.path.splitext(filename)[0]}_transcription.txt"
output_path = os.path.join(app.config['UPLOAD_FOLDER'], output_file)
transcriber.transcribe_and_save(audio_path, output_path)
# Leer y devolver la transcripción
with open(output_path, 'r', encoding='utf-8') as f:
transcription = f.read()
#return jsonify({"filename": filename, "transcription": transcription})
return Response(transcription, mimetype='text/plain')
except Exception as e:
return jsonify({"error": str(e)}), 500
else:
return jsonify({"error": "Unsupported file type"}), 400
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=5000)