Spaces:
Runtime error
Runtime error
Yuekai Zhang
commited on
Commit
•
e8bec2d
1
Parent(s):
74585b5
update app
Browse files- Dockerfile +1 -0
- Dockerfile.origin +26 -0
- README.md +9 -1
- app.py +102 -0
- requirements-gradio.txt +11 -0
Dockerfile
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
FROM soar97/torch-paraformer-gradio:22.12
|
Dockerfile.origin
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM soar97/torch-paraformer:22.12
|
2 |
+
|
3 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
4 |
+
|
5 |
+
RUN apt-get update && apt-get install -y ffmpeg
|
6 |
+
|
7 |
+
COPY ./requirements-gradio.txt ./
|
8 |
+
|
9 |
+
RUN pip install --no-cache-dir --upgrade -r ./requirements-gradio.txt
|
10 |
+
|
11 |
+
# Set up a new user named "user" with user ID 1000
|
12 |
+
RUN useradd -m -u 1000 user
|
13 |
+
# Switch to the "user" user
|
14 |
+
USER user
|
15 |
+
# Set home to the user's home directory
|
16 |
+
ENV HOME=/home/user \
|
17 |
+
PATH=/home/user/.local/bin:$PATH
|
18 |
+
|
19 |
+
# Set the working directory to the user's home directory
|
20 |
+
WORKDIR $HOME/app
|
21 |
+
|
22 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
23 |
+
COPY --chown=user app.py $HOME/app/
|
24 |
+
COPY --chown=user --from=soar97/torch-paraformer:22.12 /workspace/ $HOME/app/
|
25 |
+
|
26 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
@@ -7,4 +7,12 @@ sdk: docker
|
|
7 |
pinned: false
|
8 |
---
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
pinned: false
|
8 |
---
|
9 |
|
10 |
+
Using paraformer large to transcribe long audios.
|
11 |
+
|
12 |
+
### Using Docker
|
13 |
+
```
|
14 |
+
docker build -f Dockerfile.origin -t soar97/torch-paraformer-gradio:22.12 .
|
15 |
+
# docker pull soar97/torch-paraformer-gradio:22.12
|
16 |
+
|
17 |
+
docker run -it --name "paraformerX" --net host soar97/torch-paraformer-gradio:22.12
|
18 |
+
```
|
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from funasr_onnx import Fsmn_vad, Paraformer, CT_Transformer
|
2 |
+
from transcribe import get_models, transcribe
|
3 |
+
import soundfile
|
4 |
+
import gradio as gr
|
5 |
+
import pytube as pt
|
6 |
+
import datetime
|
7 |
+
import os
|
8 |
+
|
9 |
+
asr_model, vad_model, punc_model = get_models("./models")
|
10 |
+
|
11 |
+
def convert_to_wav(in_filename: str) -> str:
|
12 |
+
"""Convert the input audio file to a wave file"""
|
13 |
+
out_filename = in_filename + ".wav"
|
14 |
+
if '.mp3' in in_filename:
|
15 |
+
os.system(f"ffmpeg -y -i '{in_filename}' -acodec pcm_s16le -ac 1 -ar 16000 '{out_filename}'")
|
16 |
+
else:
|
17 |
+
_ = os.system(f"ffmpeg -hide_banner -y -i '{in_filename}' -ar 16000 '{out_filename}'")
|
18 |
+
speech, _ = soundfile.read(out_filename)
|
19 |
+
return speech
|
20 |
+
|
21 |
+
def file_transcribe(microphone, file_upload):
|
22 |
+
warn_output = ""
|
23 |
+
if (microphone is not None) and (file_upload is not None):
|
24 |
+
warn_output = (
|
25 |
+
"WARNING: You've uploaded an audio file and used the microphone. "
|
26 |
+
"The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
|
27 |
+
)
|
28 |
+
|
29 |
+
elif (microphone is None) and (file_upload is None):
|
30 |
+
return "ERROR: You have to either use the microphone or upload an audio file"
|
31 |
+
|
32 |
+
file = microphone if microphone is not None else file_upload
|
33 |
+
|
34 |
+
speech = convert_to_wav(file)
|
35 |
+
|
36 |
+
text = "\n".join([item for item in transcribe(speech, asr_model, vad_model, punc_model)])
|
37 |
+
|
38 |
+
return warn_output + text
|
39 |
+
|
40 |
+
|
41 |
+
def _return_yt_html_embed(yt_url):
|
42 |
+
video_id = yt_url.split("?v=")[-1]
|
43 |
+
HTML_str = (
|
44 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
45 |
+
" </center>"
|
46 |
+
)
|
47 |
+
return HTML_str
|
48 |
+
|
49 |
+
|
50 |
+
def youtube_transcribe(yt_url):
|
51 |
+
yt = pt.YouTube(yt_url)
|
52 |
+
html_embed_str = _return_yt_html_embed(yt_url)
|
53 |
+
stream = yt.streams.filter(only_audio=True)[0]
|
54 |
+
filename = f"audio.mp3"
|
55 |
+
stream.download(filename=filename)
|
56 |
+
|
57 |
+
speech=convert_to_wav(filename)
|
58 |
+
text = "\n".join([item for item in transcribe(speech, asr_model, vad_model, punc_model)])
|
59 |
+
os.system(f"rm -rf audio.mp3 audio.mp3.wav")
|
60 |
+
return html_embed_str, text
|
61 |
+
|
62 |
+
|
63 |
+
def run():
|
64 |
+
gr.close_all()
|
65 |
+
demo = gr.Blocks()
|
66 |
+
|
67 |
+
mf_transcribe = gr.Interface(
|
68 |
+
fn=file_transcribe,
|
69 |
+
inputs=[
|
70 |
+
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
|
71 |
+
gr.inputs.Audio(source="upload", type="filepath", optional=True),
|
72 |
+
],
|
73 |
+
outputs="text",
|
74 |
+
layout="horizontal",
|
75 |
+
theme="huggingface",
|
76 |
+
title="ParaformerX: Copilot for Audio",
|
77 |
+
description=(
|
78 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the the pretrained paraformer model to transcribe audio files of arbitrary length."
|
79 |
+
),
|
80 |
+
allow_flagging="never",
|
81 |
+
)
|
82 |
+
|
83 |
+
yt_transcribe = gr.Interface(
|
84 |
+
fn=youtube_transcribe,
|
85 |
+
inputs=[gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
|
86 |
+
outputs=["html", "text"],
|
87 |
+
layout="horizontal",
|
88 |
+
theme="huggingface",
|
89 |
+
title="Demo: Transcribe YouTube",
|
90 |
+
description=(
|
91 |
+
"Transcribe long-form YouTube videos with the click of a button! Demo uses the the pretrained paraformer model to transcribe audio files of arbitrary length."
|
92 |
+
),
|
93 |
+
allow_flagging="never",
|
94 |
+
)
|
95 |
+
|
96 |
+
with demo:
|
97 |
+
gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
|
98 |
+
|
99 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, enable_queue=True)
|
100 |
+
|
101 |
+
if __name__ == "__main__":
|
102 |
+
run()
|
requirements-gradio.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
WeTextProcessing
|
2 |
+
onnxruntime
|
3 |
+
soundfile
|
4 |
+
librosa
|
5 |
+
scipy
|
6 |
+
numpy
|
7 |
+
typeguard==2.13.3
|
8 |
+
kaldi-native-fbank
|
9 |
+
PyYAML>=5.1.2
|
10 |
+
gradio
|
11 |
+
pytube
|