|
import uuid |
|
import numpy as np |
|
from flask import Flask, request, jsonify |
|
import sys |
|
import os |
|
import whisper |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
asr_model = whisper.load_model("medium") |
|
app = Flask(__name__) |
|
|
|
@app.route('/') |
|
def hello(): |
|
return "Server is alive" |
|
|
|
def transcribe_audio(audio_path): |
|
try: |
|
output = asr_model.transcribe(audio_path,language="ru") |
|
text = output["text"] |
|
return {"text":text},200 |
|
except Exception as e: |
|
return {f"Ошибка обработки: {e}"}, 500 |
|
|
|
@app.route('/transcribe', methods=['POST']) |
|
def handle_audio(): |
|
if 'audio' not in request.files: |
|
return jsonify({"error": "No audio file"}), 400 |
|
|
|
audio = request.files['audio'] |
|
temp_file = f"/tmp/audio_{uuid.uuid4()}.wav" |
|
try: |
|
audio.save(temp_file) |
|
if os.path.getsize(temp_file) == 0: |
|
return jsonify({"error": "Empty audio file"}), 400 |
|
|
|
response,status = transcribe_audio(temp_file) |
|
return jsonify(response), status, {'Content-Type': 'application/json; charset=utf-8'} |
|
|
|
except Exception as e: |
|
return jsonify({"error": str(e)}), 500 |
|
|
|
finally: |
|
if os.path.exists(temp_file): |
|
os.remove(temp_file) |
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=7860, debug=True) |
|
|