|
import subprocess |
|
subprocess.run(["pip", "install", "gradio=2.7.5.2"]) |
|
subprocess.run(["pip", "install", "transformers"]) |
|
subprocess.run(["pip", "install", "torchaudio", "--upgrade"]) |
|
|
|
import gradio as gr |
|
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor |
|
import torchaudio |
|
import torch |
|
|
|
|
|
processor = Wav2Vec2Processor.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-italian") |
|
model = Wav2Vec2ForCTC.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-italian") |
|
|
|
|
|
def transcribe_audio(audio_data): |
|
print("Received audio data:", audio_data) |
|
|
|
|
|
if audio_data is None or not isinstance(audio_data, tuple) or len(audio_data) != 2: |
|
return "Invalid audio data format." |
|
|
|
sample_rate, waveform = audio_data |
|
|
|
|
|
if waveform is None or not isinstance(waveform, torch.Tensor): |
|
return "Invalid audio data format." |
|
|
|
try: |
|
|
|
audio_data = torchaudio.transforms.Resample(sample_rate, 100000)(waveform) |
|
audio_data = torchaudio.functional.gain(audio_data, gain_db=5.0) |
|
|
|
|
|
input_values = processor(audio_data[0], return_tensors="pt").input_values |
|
|
|
|
|
with torch.no_grad(): |
|
logits = model(input_values).logits |
|
|
|
|
|
predicted_ids = torch.argmax(logits, dim=-1) |
|
transcription = processor.batch_decode(predicted_ids) |
|
|
|
return transcription[0] |
|
|
|
except Exception as e: |
|
return f"An error occurred: {str(e)}" |
|
|
|
|
|
audio_input = gr.Audio(sources=["microphone"]) |
|
gr.Interface(fn=transcribe_audio, inputs=audio_input, outputs="text").launch() |
|
|