| 
							 | 
						import gradio as gr | 
					
					
						
						| 
							 | 
						import torch | 
					
					
						
						| 
							 | 
						import torchaudio | 
					
					
						
						| 
							 | 
						import torch.nn as nn | 
					
					
						
						| 
							 | 
						import torch.nn.functional as F | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						class M5(nn.Module): | 
					
					
						
						| 
							 | 
						    def __init__(self, n_input=1, n_output=35, stride=16, n_channel=32): | 
					
					
						
						| 
							 | 
						        super().__init__() | 
					
					
						
						| 
							 | 
						        self.conv1 = nn.Conv1d(n_input, n_channel, kernel_size=80, stride=stride) | 
					
					
						
						| 
							 | 
						        self.bn1 = nn.BatchNorm1d(n_channel) | 
					
					
						
						| 
							 | 
						        self.pool1 = nn.MaxPool1d(4) | 
					
					
						
						| 
							 | 
						        self.conv2 = nn.Conv1d(n_channel, n_channel, kernel_size=3) | 
					
					
						
						| 
							 | 
						        self.bn2 = nn.BatchNorm1d(n_channel) | 
					
					
						
						| 
							 | 
						        self.pool2 = nn.MaxPool1d(4) | 
					
					
						
						| 
							 | 
						        self.conv3 = nn.Conv1d(n_channel, 2 * n_channel, kernel_size=3) | 
					
					
						
						| 
							 | 
						        self.bn3 = nn.BatchNorm1d(2 * n_channel) | 
					
					
						
						| 
							 | 
						        self.pool3 = nn.MaxPool1d(4) | 
					
					
						
						| 
							 | 
						        self.conv4 = nn.Conv1d(2 * n_channel, 2 * n_channel, kernel_size=3) | 
					
					
						
						| 
							 | 
						        self.bn4 = nn.BatchNorm1d(2 * n_channel) | 
					
					
						
						| 
							 | 
						        self.pool4 = nn.MaxPool1d(4) | 
					
					
						
						| 
							 | 
						        self.fc1 = nn.Linear(2 * n_channel, n_output) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    def forward(self, x): | 
					
					
						
						| 
							 | 
						        x = self.conv1(x) | 
					
					
						
						| 
							 | 
						        x = F.relu(self.bn1(x)) | 
					
					
						
						| 
							 | 
						        x = self.pool1(x) | 
					
					
						
						| 
							 | 
						        x = self.conv2(x) | 
					
					
						
						| 
							 | 
						        x = F.relu(self.bn2(x)) | 
					
					
						
						| 
							 | 
						        x = self.pool2(x) | 
					
					
						
						| 
							 | 
						        x = self.conv3(x) | 
					
					
						
						| 
							 | 
						        x = F.relu(self.bn3(x)) | 
					
					
						
						| 
							 | 
						        x = self.pool3(x) | 
					
					
						
						| 
							 | 
						        x = self.conv4(x) | 
					
					
						
						| 
							 | 
						        x = F.relu(self.bn4(x)) | 
					
					
						
						| 
							 | 
						        x = self.pool4(x) | 
					
					
						
						| 
							 | 
						        x = F.avg_pool1d(x, x.shape[-1]) | 
					
					
						
						| 
							 | 
						        x = x.permute(0, 2, 1) | 
					
					
						
						| 
							 | 
						        x = self.fc1(x) | 
					
					
						
						| 
							 | 
						        return F.log_softmax(x, dim=2) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						labels = ['backward', 'bed', 'bird', 'cat', 'dog', 'down', 'eight', 'five', 'follow', | 
					
					
						
						| 
							 | 
						          'forward', 'four', 'go', 'happy', 'house', 'learn', 'left', 'marvin', 'nine', | 
					
					
						
						| 
							 | 
						          'no', 'off', 'on', 'one', 'right', 'seven', 'sheila', 'six', 'stop', 'three', | 
					
					
						
						| 
							 | 
						          'tree', 'two', 'up', 'visual', 'wow', 'yes', 'zero'] | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						def label_to_index(word): | 
					
					
						
						| 
							 | 
						    return torch.tensor(labels.index(word)) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						def index_to_label(index): | 
					
					
						
						| 
							 | 
						    return labels[index] | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						def get_likely_index(tensor): | 
					
					
						
						| 
							 | 
						    return tensor.argmax(dim=-1) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | 
					
					
						
						| 
							 | 
						model = M5() | 
					
					
						
						| 
							 | 
						model.load_state_dict(torch.load("modelo_entrenado.pth", map_location=device)) | 
					
					
						
						| 
							 | 
						model.to(device) | 
					
					
						
						| 
							 | 
						model.eval() | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						def predict(audio): | 
					
					
						
						| 
							 | 
						    waveform, sample_rate = torchaudio.load(audio) | 
					
					
						
						| 
							 | 
						    transform = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=8000).to(device) | 
					
					
						
						| 
							 | 
						    waveform = waveform.to(device) | 
					
					
						
						| 
							 | 
						    waveform = transform(waveform) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						    with torch.no_grad(): | 
					
					
						
						| 
							 | 
						        output = model(waveform.unsqueeze(0)) | 
					
					
						
						| 
							 | 
						        tensor = get_likely_index(output) | 
					
					
						
						| 
							 | 
						        prediction = index_to_label(tensor.squeeze()) | 
					
					
						
						| 
							 | 
						    return prediction | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						iface = gr.Interface( | 
					
					
						
						| 
							 | 
						    fn=predict, | 
					
					
						
						| 
							 | 
						    inputs=gr.Audio(type="filepath"), | 
					
					
						
						| 
							 | 
						    outputs="text", | 
					
					
						
						| 
							 | 
						    title="Reconocimiento de comandos de voz", | 
					
					
						
						| 
							 | 
						    description="Graba un comando de voz y el modelo lo predecir谩." | 
					
					
						
						| 
							 | 
						) | 
					
					
						
						| 
							 | 
						
 | 
					
					
						
						| 
							 | 
						 | 
					
					
						
						| 
							 | 
						iface.launch(share=True) |