Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import HubertForCTC, Wav2Vec2Processor
|
| 4 |
+
import librosa
|
| 5 |
+
|
| 6 |
+
# Load the model and processor from Hugging Face Hub
|
| 7 |
+
model_name = "Ansu/mHubert_basque_ASR" # Change this to your model
|
| 8 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name)
|
| 9 |
+
model = HubertForCTC.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Function to transcribe audio
|
| 12 |
+
def transcribe(audio):
|
| 13 |
+
# Load audio file
|
| 14 |
+
audio, _ = librosa.load(audio, sr=16000)
|
| 15 |
+
|
| 16 |
+
# Process input
|
| 17 |
+
inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True)
|
| 18 |
+
|
| 19 |
+
# Get model predictions
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
logits = model(inputs.input_values).logits
|
| 22 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 23 |
+
|
| 24 |
+
# Decode predictions
|
| 25 |
+
transcription = processor.batch_decode(predicted_ids)[0]
|
| 26 |
+
|
| 27 |
+
return transcription
|
| 28 |
+
|
| 29 |
+
# Create Gradio interface
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=transcribe,
|
| 32 |
+
inputs=[
|
| 33 |
+
gr.Audio(source="microphone", type="filepath", label="🎤 Record or Upload Audio") # Microphone & Upload
|
| 34 |
+
],
|
| 35 |
+
outputs="text",
|
| 36 |
+
title="HuBERT ASR Demo",
|
| 37 |
+
description="🎙️ Speak into the microphone or upload an audio file to get a transcription.",
|
| 38 |
+
live=True, # Enables real-time recording
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
iface.launch()
|