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 # Load model and processor processor = Wav2Vec2Processor.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-italian") model = Wav2Vec2ForCTC.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-italian") # Function to perform ASR on audio data def transcribe_audio(audio_data): print("Received audio data:", audio_data) # Debug print # Check if audio_data is None or not a tuple of length 2 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 # Check if waveform is None or not a NumPy array if waveform is None or not isinstance(waveform, torch.Tensor): return "Invalid audio data format." try: # Convert audio data to mono and normalize audio_data = torchaudio.transforms.Resample(sample_rate, 100000)(waveform) audio_data = torchaudio.functional.gain(audio_data, gain_db=5.0) # Apply custom preprocessing to the audio data if needed input_values = processor(audio_data[0], return_tensors="pt").input_values # Perform ASR with torch.no_grad(): logits = model(input_values).logits # Decode the output 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)}" # Create Gradio interface audio_input = gr.Audio(sources=["microphone"]) gr.Interface(fn=transcribe_audio, inputs=audio_input, outputs="text").launch()