import os from flask import Flask, request, jsonify, sendfromdirectory import whisper…
- User
import os from flask import Flask, request, jsonify, send_from_directory import whisper from flask_swagger_ui import get_swaggerui_blueprint import tempfile from transformers import pipeline import torch
MODEL_NAME = "openai/whisper-large-v3-turbo" BATCH_SIZE = 8 device = 0 if torch.cuda.is_available() else "cpu"
class TranscriptionClient: def init(self, model_name="medium"): self.pipe = pipeline( task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=30, device=device, )
def transcribe(self, audio_path, task="transcribe"): try: result = self.pipe(audio_path, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True) return result['text'] except Exception as e: raise RuntimeError(f"Ошибка при транскрипции: {e}")app = Flask(name) transcription_client = TranscriptionClient()
Setup directory for storing temporary files
TEMP_DIR = './temp_files' os.makedirs(TEMP_DIR, exist_ok=True)
@app.route('/transcribe', methods=['POST']) def transcribe_audio(): try: if 'file' not in request.files: return jsonify({'error': 'Файл не загружен'}), 400
file = request.files['file'] # Save the uploaded file to the custom temporary directory temp_file_path = os.path.join(TEMP_DIR, file.filename) file.save(temp_file_path) try: # Use the saved file path for transcription transcription = transcription_client.transcribe(temp_file_path) finally: # Remove the file after processing os.remove(temp_file_path) return jsonify({'transcription': transcription}) except Exception as e: return jsonify({'error': str(e)}), 500if name == 'main': app.run(host='0.0.0.0', port=9091, debug=True)
Сделай рефакторинг моего кода, проверь правильность организации кода и принципов ооп. Также проверь названия переменных соответствуют ли они логике
- Assistant
- User
а куда делся сваггер?
- Assistant