File size: 1,948 Bytes
2be798f 1922543 2be798f 1922543 2be798f 1922543 2be798f 1922543 2be798f 1922543 2be798f 1922543 2be798f 1922543 2be798f 1922543 2be798f 1922543 2be798f |
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 |
import gradio as gr
import torch
import numpy as np
import soundfile as sf
import librosa
import time
from transformers import pipeline
pipe = pipeline(
"automatic-speech-recognition",
model="antony66/whisper-large-v3-russian",
torch_dtype=torch.float16,
device=0 if torch.cuda.is_available() else -1
)
def transcribe(audio_data):
log_messages = []
start_time = time.time()
log_messages.append("Загрузка файла...")
if audio_data is None:
return "Ошибка: не получены аудиоданные", "\n".join(log_messages)
wav_file = "temp_audio.wav"
if isinstance(audio_data, tuple):
audio_array, sample_rate = audio_data
sf.write(wav_file, audio_array, sample_rate)
elif isinstance(audio_data, str):
audio_array, sample_rate = librosa.load(audio_data, sr=16000)
sf.write(wav_file, audio_array, sample_rate)
else:
return "Ошибка: неизвестный формат аудиоданных", "\n".join(log_messages)
log_messages.append(f"Загрузка файла завершена за {time.time() - start_time:.2f} сек")
start_time = time.time()
log_messages.append("Работа модели... в процессе")
result = pipe(wav_file)
log_messages.append(f"Работа модели завершена за {time.time() - start_time:.2f} сек")
return result["text"], "\n".join(log_messages)
with gr.Blocks() as app:
gr.Markdown("## Распознавание речи с Whisper")
audio_data = gr.Audio(type="filepath")
text_output = gr.Textbox(label="Распознанный текст")
log_output = gr.Textbox(label="Лог выполнения", interactive=False)
btn = gr.Button("Распознать")
btn.click(transcribe, inputs=audio_data, outputs=[text_output, log_output])
app.launch(debug=True)
|