Spaces:
Runtime error
Runtime error
upload app files
Browse files- app.py +90 -0
- packages.txt +1 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import yt_dlp as youtube_dl
|
5 |
+
from transformers import pipeline
|
6 |
+
from transformers.pipelines.audio_utils import ffmpeg_read
|
7 |
+
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
8 |
+
|
9 |
+
|
10 |
+
import tempfile
|
11 |
+
import os
|
12 |
+
|
13 |
+
MODEL_NAME = "openai/whisper-medium"
|
14 |
+
BATCH_SIZE = 8
|
15 |
+
FILE_LIMIT_MB = 1000
|
16 |
+
|
17 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
18 |
+
|
19 |
+
pipe = pipeline(
|
20 |
+
task="automatic-speech-recognition",
|
21 |
+
model=MODEL_NAME,
|
22 |
+
chunk_length_s=30,
|
23 |
+
device=device,
|
24 |
+
)
|
25 |
+
|
26 |
+
model = MBartForConditionalGeneration.from_pretrained("sanjitaa/mbart-many-to-many")
|
27 |
+
tokenizer = MBart50TokenizerFast.from_pretrained("sanjitaa/mbart-many-to-many")
|
28 |
+
|
29 |
+
def translate(inputs, task):
|
30 |
+
if inputs is None:
|
31 |
+
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
32 |
+
|
33 |
+
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
34 |
+
|
35 |
+
encoded_text = tokenizer(text, return_tensors="pt")
|
36 |
+
tokenizer.src_lang = "en_XX"
|
37 |
+
|
38 |
+
generated_tokens = model.generate(
|
39 |
+
**encoded_text,
|
40 |
+
forced_bos_token_id=tokenizer.lang_code_to_id["fr_XX"]
|
41 |
+
)
|
42 |
+
result = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)
|
43 |
+
return result
|
44 |
+
|
45 |
+
|
46 |
+
demo = gr.Blocks()
|
47 |
+
|
48 |
+
mf_transcribe = gr.Interface(
|
49 |
+
fn=translate,
|
50 |
+
inputs=[
|
51 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
52 |
+
gr.inputs.Radio(["translate"], label="Task", default="translate"),
|
53 |
+
|
54 |
+
],
|
55 |
+
outputs="text",
|
56 |
+
layout="horizontal",
|
57 |
+
theme="huggingface",
|
58 |
+
title="Whisper Medium: Transcribe Audio",
|
59 |
+
description=(
|
60 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
61 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and π€ Transformers to transcribe audio files"
|
62 |
+
" of arbitrary length."
|
63 |
+
),
|
64 |
+
allow_flagging="never",
|
65 |
+
)
|
66 |
+
|
67 |
+
file_transcribe = gr.Interface(
|
68 |
+
fn=transcribe,
|
69 |
+
inputs=[
|
70 |
+
gr.inputs.Audio(source="upload", type="filepath", optional=True, label="Audio file"),
|
71 |
+
gr.inputs.Radio(["translate"], label="Task", default="transcribe"),
|
72 |
+
],
|
73 |
+
outputs="text",
|
74 |
+
layout="horizontal",
|
75 |
+
theme="huggingface",
|
76 |
+
title="Whisper Large V2: Transcribe Audio",
|
77 |
+
description=(
|
78 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
79 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and π€ Transformers to transcribe audio files"
|
80 |
+
" of arbitrary length."
|
81 |
+
),
|
82 |
+
allow_flagging="never",
|
83 |
+
)
|
84 |
+
|
85 |
+
|
86 |
+
with demo:
|
87 |
+
gr.TabbedInterface([mf_transcribe, file_transcribe], ["Microphone", "Audio file"])
|
88 |
+
|
89 |
+
demo.launch(enable_queue=True)
|
90 |
+
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ffmpeg
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers
|
2 |
+
torch
|
3 |
+
yt-dlp
|