brezende osanseviero commited on
Commit
a4471a2
0 Parent(s):

Duplicate from osanseviero/whisper-medium

Browse files

Co-authored-by: Omar Sanseviero <[email protected]>

Files changed (5) hide show
  1. .gitattributes +34 -0
  2. README.md +15 -0
  3. app.py +101 -0
  4. packages.txt +1 -0
  5. requirements.txt +3 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Whisper Demo
3
+ emoji: 🤫
4
+ colorFrom: indigo
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.9.1
8
+ app_file: app.py
9
+ pinned: false
10
+ tags:
11
+ - whisper-event
12
+ duplicated_from: osanseviero/whisper-medium
13
+ ---
14
+
15
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ import gradio as gr
4
+ import pytube as pt
5
+ from transformers import pipeline
6
+ from huggingface_hub import model_info
7
+
8
+ MODEL_NAME = "openai/whisper-medium"
9
+
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+
12
+ pipe = pipeline(
13
+ task="automatic-speech-recognition",
14
+ model=MODEL_NAME,
15
+ chunk_length_s=30,
16
+ device=device,
17
+ )
18
+
19
+ langs = model_info(MODEL_NAME).cardData["language"]
20
+
21
+ article = f"<details><summary>This model supports {len(langs)} languages! (Click to expand)</summary>> {langs}</details>"
22
+
23
+ def transcribe(microphone, file_upload):
24
+ warn_output = ""
25
+ if (microphone is not None) and (file_upload is not None):
26
+ warn_output = (
27
+ "WARNING: You've uploaded an audio file and used the microphone. "
28
+ "The recorded file from the microphone will be used and the uploaded audio will be discarded.\n"
29
+ )
30
+
31
+ elif (microphone is None) and (file_upload is None):
32
+ return "ERROR: You have to either use the microphone or upload an audio file"
33
+
34
+ file = microphone if microphone is not None else file_upload
35
+
36
+ text = pipe(file)["text"]
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 yt_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
+ stream.download(filename="audio.mp3")
55
+
56
+ text = pipe("audio.mp3")["text"]
57
+
58
+ return html_embed_str, text
59
+
60
+
61
+ demo = gr.Blocks()
62
+
63
+ mf_transcribe = gr.Interface(
64
+ fn=transcribe,
65
+ inputs=[
66
+ gr.inputs.Audio(source="microphone", type="filepath", optional=True),
67
+ gr.inputs.Audio(source="upload", type="filepath", optional=True),
68
+ ],
69
+ outputs="text",
70
+ layout="horizontal",
71
+ theme="huggingface",
72
+ title="Whisper Demo: Transcribe Audio",
73
+ description=(
74
+ "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the the fine-tuned"
75
+ f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
76
+ " of arbitrary length."
77
+ ),
78
+ article=article,
79
+ allow_flagging="never",
80
+ )
81
+
82
+ yt_transcribe = gr.Interface(
83
+ fn=yt_transcribe,
84
+ inputs=[gr.inputs.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL")],
85
+ outputs=["html", "text"],
86
+ layout="horizontal",
87
+ theme="huggingface",
88
+ title="Whisper Demo: Transcribe YouTube",
89
+ description=(
90
+ "Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
91
+ f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
92
+ " arbitrary length."
93
+ ),
94
+ article=article,
95
+ allow_flagging="never",
96
+ )
97
+
98
+ with demo:
99
+ gr.TabbedInterface([mf_transcribe, yt_transcribe], ["Transcribe Audio", "Transcribe YouTube"])
100
+
101
+ demo.launch(enable_queue=True)
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ torch
3
+ pytube