Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 5 |
+
from scipy.signal import resample
|
| 6 |
+
|
| 7 |
+
# Load model and processor
|
| 8 |
+
model_id = "facebook/wav2vec2-large-960h-lv60-self"
|
| 9 |
+
processor = Wav2Vec2Processor.from_pretrained(model_id)
|
| 10 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_id)
|
| 11 |
+
|
| 12 |
+
# Transcription function
|
| 13 |
+
def transcribe(audio, sample_rate):
|
| 14 |
+
if audio is None:
|
| 15 |
+
return "⚠️ No audio received."
|
| 16 |
+
|
| 17 |
+
# Resample if needed
|
| 18 |
+
if sample_rate != 16000:
|
| 19 |
+
number_of_samples = round(len(audio) * 16000 / sample_rate)
|
| 20 |
+
audio = resample(audio, number_of_samples)
|
| 21 |
+
|
| 22 |
+
# Process and predict
|
| 23 |
+
input_values = processor(audio, sampling_rate=16000, return_tensors="pt").input_values
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
logits = model(input_values).logits
|
| 26 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 27 |
+
transcription = processor.batch_decode(predicted_ids)[0]
|
| 28 |
+
|
| 29 |
+
return transcription.lower()
|
| 30 |
+
|
| 31 |
+
# Launch UI
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
fn=transcribe,
|
| 34 |
+
inputs=gr.Audio(sources=["microphone"], type="numpy", label="🎤 Speak now"),
|
| 35 |
+
outputs=gr.Textbox(label="📝 Transcription"),
|
| 36 |
+
title="Wav2Vec2 Speech Transcription",
|
| 37 |
+
description="Speak into the microphone and get a transcription using Wav2Vec2 (Hugging Face)."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
demo.launch()
|