Spaces:
Build error
Build error
Commit
Β·
916a703
1
Parent(s):
b5455c9
up
Browse files- README.md +4 -4
- app.py +37 -0
- packages.txt +2 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title: XLS R 2B
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
|
|
|
| 1 |
---
|
| 2 |
+
title: XLS R 2B 22 EN
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import librosa
|
| 3 |
+
from transformers import AutoFeatureExtractor, AutoTokenizer, SpeechEncoderDecoderModel
|
| 4 |
+
|
| 5 |
+
model_name = "facebook/wav2vec2-xls-r-2b-21-to-en",
|
| 6 |
+
|
| 7 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
| 9 |
+
model = SpeechEncoderDecoderModel.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
def process_audio_file(file):
|
| 12 |
+
data, sr = librosa.load(file)
|
| 13 |
+
if sr != 16000:
|
| 14 |
+
data = librosa.resample(data, sr, 16000)
|
| 15 |
+
input_values = feature_extractor(data, return_tensors="pt").input_values
|
| 16 |
+
return input_values
|
| 17 |
+
|
| 18 |
+
def transcribe(file):
|
| 19 |
+
input_values = process_audio_file(file)
|
| 20 |
+
|
| 21 |
+
sequences = model.generate(input_values, num_beams=1, max_length=30)
|
| 22 |
+
|
| 23 |
+
transcription = tokenizer.batch_decode(sequences, skip_special_tokens=True)
|
| 24 |
+
return transcription[0]
|
| 25 |
+
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=transcribe,
|
| 28 |
+
inputs=[
|
| 29 |
+
gr.inputs.Audio(source="microphone", type='filepath'),
|
| 30 |
+
],
|
| 31 |
+
outputs="text",
|
| 32 |
+
layout="horizontal",
|
| 33 |
+
theme="huggingface",
|
| 34 |
+
title="XLS-R 2B 21-to-EN Speech Translation",
|
| 35 |
+
description="A simple interface to translate from 21 spoken languages to written English.",
|
| 36 |
+
)
|
| 37 |
+
iface.launch()
|
packages.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
libsndfile1
|
| 2 |
+
sox
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SoundFile==0.9.0.post1
|
| 2 |
+
librosa
|
| 3 |
+
sentencepiece
|
| 4 |
+
torch
|
| 5 |
+
git+git://github.com/huggingface/transformers
|