from flask import Flask, request, jsonify, sendfromdirectory import whisper from…
- User
from flask import Flask, request, jsonify, send_from_directory import whisper from flask_swagger_ui import get_swaggerui_blueprint import os
class TranscriptionClient: def init(self, model_name="medium"): self.model = whisper.load_model(model_name)
def transcribe(self, audio_path): try: audio = whisper.load_audio(audio_path) audio = whisper.pad_or_trim(audio) mel = whisper.log_mel_spectrogram(audio).to(self.model.device) _, probs = self.model.detect_language(mel) options = whisper.DecodingOptions() result = whisper.decode(self.model, mel, options) return result.text except Exception as e: raise RuntimeError(f"Ошибка при транскрипции: {e}")app = Flask(name) transcription_client = TranscriptionClient()
SWAGGER_URL = '/swagger' API_URL = '/swagger.json' swaggerui_blueprint = get_swaggerui_blueprint( SWAGGER_URL, API_URL, config={ 'app_name': "Transcription API" } ) app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
@app.route("/swagger.json") def swagger_json(): return send_from_directory('.', 'swagger.json')
@app.route('/transcribe', methods=['POST']) def transcribe_audio(): try: if 'file' not in request.files: return jsonify({'error': 'Файл не загружен'}), 400
file = request.files['file'] file_path = f"/tmp/{file.filename}" file.save(file_path) transcription = transcription_client.transcribe(file_path) os.remove(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)
Переделай мое api с использованием следующего кода: import spaces import torch
import gradio as gr import yt_dlp as youtube_dl from transformers import pipeline from transformers.pipelines.audio_utils import ffmpeg_read
import tempfile import os
MODEL_NAME = "openai/whisper-large-v3-turbo" BATCH_SIZE = 8 FILE_LIMIT_MB = 1000 YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
device = 0 if torch.cuda.is_available() else "cpu"
pipe = pipeline( task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=30, device=device, )
@spaces.GPU def transcribe(inputs, task): if inputs is None: raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"] return textdef _return_yt_html_embed(yt_url): video_id = yt_url.split("?v=")[-1] HTML_str = ( f' ' " " ) return HTML_str
def download_yt_audio(yt_url, filename): info_loader = youtube_dl.YoutubeDL()
- Assistant
- User
мне нужно только апи как у меня было, просто один метод transcribe и сваггер
- Assistant
- User
это не ошибка? inputs = gr.inputs.Audio(file_path, type='filepath')
- Assistant