lite
parent
4d20931ecc
commit
1c43f63b70
31
app.py
31
app.py
|
@ -3,6 +3,8 @@ import os
|
||||||
import uuid
|
import uuid
|
||||||
import subprocess
|
import subprocess
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
UPLOAD_FOLDER = '/data'
|
UPLOAD_FOLDER = '/data'
|
||||||
|
@ -11,6 +13,11 @@ ALLOWED_EXTENSIONS = {'wav', 'mp3', 'ogg', 'flac'}
|
||||||
def allowed_file(filename):
|
def allowed_file(filename):
|
||||||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||||
|
|
||||||
|
def generate_filename():
|
||||||
|
timestamp = int(time.time())
|
||||||
|
random_num = random.randint(1000, 9999)
|
||||||
|
return f"{timestamp}-{random_num}"
|
||||||
|
|
||||||
@app.route('/stt', methods=['POST'])
|
@app.route('/stt', methods=['POST'])
|
||||||
def transcribe():
|
def transcribe():
|
||||||
if 'audio' not in request.files:
|
if 'audio' not in request.files:
|
||||||
|
@ -24,12 +31,12 @@ if not file or not allowed_file(file.filename):
|
||||||
return jsonify({"error": "Invalid file type"}), 400
|
return jsonify({"error": "Invalid file type"}), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Generate unique filename
|
# Generate filename with timestamp + random number
|
||||||
file_id = str(uuid.uuid4())
|
file_base = generate_filename()
|
||||||
orig_ext = secure_filename(file.filename).rsplit('.', 1)[1].lower()
|
orig_ext = secure_filename(file.filename).rsplit('.', 1)[1].lower()
|
||||||
orig_path = os.path.join(UPLOAD_FOLDER, f"{file_id}.{orig_ext}")
|
orig_path = os.path.join(UPLOAD_FOLDER, f"{file_base}.{orig_ext}")
|
||||||
wav_path = os.path.join(UPLOAD_FOLDER, f"{file_id}.wav")
|
wav_path = os.path.join(UPLOAD_FOLDER, f"{file_base}.wav")
|
||||||
output_base = os.path.join(UPLOAD_FOLDER, file_id)
|
output_base = os.path.join(UPLOAD_FOLDER, file_base)
|
||||||
|
|
||||||
# Save original file
|
# Save original file
|
||||||
file.save(orig_path)
|
file.save(orig_path)
|
||||||
|
@ -48,7 +55,7 @@ audio_path = orig_path
|
||||||
# Run whisper.cpp
|
# Run whisper.cpp
|
||||||
subprocess.run([
|
subprocess.run([
|
||||||
'/app/whisper.cpp/build/bin/main',
|
'/app/whisper.cpp/build/bin/main',
|
||||||
'-m', '/app/whisper.cpp/models/ggml-base.bin',
|
'-m', '/app/whisper.cpp/models/ggml-tiny.en-q5_1.bin',
|
||||||
'-f', audio_path,
|
'-f', audio_path,
|
||||||
'-otxt', '-of', output_base
|
'-otxt', '-of', output_base
|
||||||
], check=True)
|
], check=True)
|
||||||
|
@ -58,12 +65,14 @@ output_file = f"{output_base}.txt"
|
||||||
with open(output_file, 'r') as f:
|
with open(output_file, 'r') as f:
|
||||||
transcription = f.read()
|
transcription = f.read()
|
||||||
|
|
||||||
# Cleanup
|
# Return the filenames in the response
|
||||||
for f in [orig_path, wav_path, audio_path, output_file]:
|
response = {
|
||||||
if os.path.exists(f):
|
"text": transcription.strip(),
|
||||||
os.remove(f)
|
"audio_file": f"{file_base}.{orig_ext}",
|
||||||
|
"text_file": f"{file_base}.txt"
|
||||||
|
}
|
||||||
|
|
||||||
return jsonify({"text": transcription.strip()})
|
return jsonify(response)
|
||||||
|
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
return jsonify({"error": f"Whisper processing failed: {e.stderr}"}), 500
|
return jsonify({"error": f"Whisper processing failed: {e.stderr}"}), 500
|
||||||
|
|
Loading…
Reference in New Issue