AhmadHazem
commited on
Commit
·
7648fbe
1
Parent(s):
24267d5
Add Repo
Browse files- README.md +7 -5
- app.py +159 -0
- packages.txt +1 -0
- requirements.txt +2 -0
- whisper_notebook.ipynb +307 -0
README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
10 |
---
|
11 |
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Whisper Turbo
|
3 |
+
emoji: 🤯
|
4 |
+
colorFrom: indigo
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.0.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
tags:
|
11 |
+
- whisper-event
|
12 |
---
|
13 |
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import spaces
|
3 |
+
import torch
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import yt_dlp as youtube_dl
|
7 |
+
from transformers import pipeline, MarianMTModel, MarianTokenizer
|
8 |
+
from transformers.pipelines.audio_utils import ffmpeg_read
|
9 |
+
|
10 |
+
import tempfile
|
11 |
+
import os
|
12 |
+
|
13 |
+
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
14 |
+
BATCH_SIZE = 8
|
15 |
+
FILE_LIMIT_MB = 1000
|
16 |
+
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
|
17 |
+
|
18 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
19 |
+
|
20 |
+
pipe = pipeline(
|
21 |
+
task="automatic-speech-recognition",
|
22 |
+
model=MODEL_NAME,
|
23 |
+
chunk_length_s=30,
|
24 |
+
device=device,
|
25 |
+
)
|
26 |
+
|
27 |
+
model_name_translate = "Helsinki-NLP/opus-mt-en-ar"
|
28 |
+
tokenizer_translation = MarianTokenizer.from_pretrained(model_name_translate)
|
29 |
+
model_translate = MarianMTModel.from_pretrained(model_name_translate)
|
30 |
+
|
31 |
+
|
32 |
+
def translate(sentence):
|
33 |
+
batch = tokenizer_translation([sentence], return_tensors="pt")
|
34 |
+
generated_ids = model_translate.generate(batch["input_ids"])
|
35 |
+
text = tokenizer_translation.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
36 |
+
return text
|
37 |
+
|
38 |
+
@spaces.GPU
|
39 |
+
def transcribe(inputs, task):
|
40 |
+
if inputs is None:
|
41 |
+
raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
|
42 |
+
|
43 |
+
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
44 |
+
text = translate(text)
|
45 |
+
return text
|
46 |
+
|
47 |
+
|
48 |
+
def _return_yt_html_embed(yt_url):
|
49 |
+
video_id = yt_url.split("?v=")[-1]
|
50 |
+
HTML_str = (
|
51 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
52 |
+
" </center>"
|
53 |
+
)
|
54 |
+
return HTML_str
|
55 |
+
|
56 |
+
def download_yt_audio(yt_url, filename):
|
57 |
+
info_loader = youtube_dl.YoutubeDL()
|
58 |
+
|
59 |
+
try:
|
60 |
+
info = info_loader.extract_info(yt_url, download=False)
|
61 |
+
except youtube_dl.utils.DownloadError as err:
|
62 |
+
raise gr.Error(str(err))
|
63 |
+
|
64 |
+
file_length = info["duration_string"]
|
65 |
+
file_h_m_s = file_length.split(":")
|
66 |
+
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
67 |
+
|
68 |
+
if len(file_h_m_s) == 1:
|
69 |
+
file_h_m_s.insert(0, 0)
|
70 |
+
if len(file_h_m_s) == 2:
|
71 |
+
file_h_m_s.insert(0, 0)
|
72 |
+
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
73 |
+
|
74 |
+
if file_length_s > YT_LENGTH_LIMIT_S:
|
75 |
+
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
76 |
+
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
77 |
+
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
78 |
+
|
79 |
+
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
80 |
+
|
81 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
82 |
+
try:
|
83 |
+
ydl.download([yt_url])
|
84 |
+
except youtube_dl.utils.ExtractorError as err:
|
85 |
+
raise gr.Error(str(err))
|
86 |
+
|
87 |
+
@spaces.GPU
|
88 |
+
def yt_transcribe(yt_url, task, max_filesize=75.0):
|
89 |
+
html_embed_str = _return_yt_html_embed(yt_url)
|
90 |
+
|
91 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
92 |
+
filepath = os.path.join(tmpdirname, "video.mp4")
|
93 |
+
download_yt_audio(yt_url, filepath)
|
94 |
+
with open(filepath, "rb") as f:
|
95 |
+
inputs = f.read()
|
96 |
+
|
97 |
+
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
98 |
+
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
99 |
+
|
100 |
+
text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
|
101 |
+
text = translate(text)
|
102 |
+
return html_embed_str, text
|
103 |
+
|
104 |
+
|
105 |
+
demo = gr.Blocks(theme=gr.themes.Ocean())
|
106 |
+
|
107 |
+
mf_transcribe = gr.Interface(
|
108 |
+
fn=transcribe,
|
109 |
+
inputs=[
|
110 |
+
gr.Audio(sources="microphone", type="filepath"),
|
111 |
+
gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
|
112 |
+
],
|
113 |
+
outputs="text",
|
114 |
+
title="Whisper Large V3 Turbo: Transcribe Audio",
|
115 |
+
description=(
|
116 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
117 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
118 |
+
" of arbitrary length."
|
119 |
+
),
|
120 |
+
allow_flagging="never",
|
121 |
+
)
|
122 |
+
|
123 |
+
file_transcribe = gr.Interface(
|
124 |
+
fn=transcribe,
|
125 |
+
inputs=[
|
126 |
+
gr.Audio(sources="upload", type="filepath", label="Audio file"),
|
127 |
+
gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
|
128 |
+
],
|
129 |
+
outputs="text",
|
130 |
+
title="Whisper Large V3: Transcribe Audio",
|
131 |
+
description=(
|
132 |
+
"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
|
133 |
+
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
|
134 |
+
" of arbitrary length."
|
135 |
+
),
|
136 |
+
allow_flagging="never",
|
137 |
+
)
|
138 |
+
|
139 |
+
yt_transcribe = gr.Interface(
|
140 |
+
fn=yt_transcribe,
|
141 |
+
inputs=[
|
142 |
+
gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
|
143 |
+
gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
144 |
+
],
|
145 |
+
outputs=["html", "text"],
|
146 |
+
title="Whisper Large V3: Transcribe YouTube",
|
147 |
+
description=(
|
148 |
+
"Transcribe long-form YouTube videos with the click of a button! Demo uses the checkpoint"
|
149 |
+
f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
|
150 |
+
" arbitrary length."
|
151 |
+
),
|
152 |
+
allow_flagging="never",
|
153 |
+
)
|
154 |
+
|
155 |
+
with demo:
|
156 |
+
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
|
157 |
+
|
158 |
+
demo.queue().launch(ssr_mode=False)
|
159 |
+
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ffmpeg
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
yt-dlp
|
whisper_notebook.ipynb
ADDED
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "OXaUqiE-eyXM"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# Whisper v3 is here!\n",
|
10 |
+
"\n",
|
11 |
+
"Whisper v3 is a new model open sourced by OpenAI. The model can do multilingual transcriptions and is quite impressive. For example, you can change from English to Spanish or Chinese in the middle of a sentence and it will work well!\n",
|
12 |
+
"\n",
|
13 |
+
"The model can be run in a free Google Colab instance and is integrated into `transformers` already, so switching can be a very smooth process if you already use the previous versions."
|
14 |
+
]
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"cell_type": "code",
|
18 |
+
"execution_count": 1,
|
19 |
+
"metadata": {
|
20 |
+
"id": "WFQeUT9EcIcK"
|
21 |
+
},
|
22 |
+
"outputs": [
|
23 |
+
{
|
24 |
+
"name": "stdout",
|
25 |
+
"output_type": "stream",
|
26 |
+
"text": [
|
27 |
+
"Collecting git+https://github.com/huggingface/transformers\n",
|
28 |
+
" Cloning https://github.com/huggingface/transformers to c:\\users\\blu-ray\\appdata\\local\\temp\\pip-req-build-jqimnzmp\n",
|
29 |
+
" Resolved https://github.com/huggingface/transformers to commit 816f4424964c1a1631e303b663fc3d68f731e923\n",
|
30 |
+
" Installing build dependencies: started\n",
|
31 |
+
" Installing build dependencies: finished with status 'done'\n",
|
32 |
+
" Getting requirements to build wheel: started\n",
|
33 |
+
" Getting requirements to build wheel: finished with status 'done'\n",
|
34 |
+
" Preparing metadata (pyproject.toml): started\n",
|
35 |
+
" Preparing metadata (pyproject.toml): finished with status 'done'\n",
|
36 |
+
"Requirement already satisfied: gradio in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (4.44.1)\n",
|
37 |
+
"Requirement already satisfied: filelock in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (3.12.3)\n",
|
38 |
+
"Requirement already satisfied: huggingface-hub<1.0,>=0.23.2 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (0.25.2)\n",
|
39 |
+
"Requirement already satisfied: numpy>=1.17 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (1.24.2)\n",
|
40 |
+
"Requirement already satisfied: packaging>=20.0 in c:\\users\\blu-ray\\appdata\\roaming\\python\\python39\\site-packages (from transformers==4.46.0.dev0) (23.0)\n",
|
41 |
+
"Requirement already satisfied: pyyaml>=5.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (6.0.1)\n",
|
42 |
+
"Requirement already satisfied: regex!=2019.12.17 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (2024.9.11)\n",
|
43 |
+
"Requirement already satisfied: requests in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (2.32.3)\n",
|
44 |
+
"Requirement already satisfied: tokenizers<0.21,>=0.20 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (0.20.1)\n",
|
45 |
+
"Requirement already satisfied: safetensors>=0.4.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (0.4.5)\n",
|
46 |
+
"Requirement already satisfied: tqdm>=4.27 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from transformers==4.46.0.dev0) (4.66.5)\n",
|
47 |
+
"Requirement already satisfied: aiofiles<24.0,>=22.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (23.2.1)\n",
|
48 |
+
"Requirement already satisfied: anyio<5.0,>=3.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (4.6.2.post1)\n",
|
49 |
+
"Requirement already satisfied: fastapi<1.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.115.2)\n",
|
50 |
+
"Requirement already satisfied: ffmpy in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.4.0)\n",
|
51 |
+
"Requirement already satisfied: gradio-client==1.3.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (1.3.0)\n",
|
52 |
+
"Requirement already satisfied: httpx>=0.24.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.27.2)\n",
|
53 |
+
"Requirement already satisfied: importlib-resources<7.0,>=1.3 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (5.12.0)\n",
|
54 |
+
"Requirement already satisfied: jinja2<4.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (3.1.2)\n",
|
55 |
+
"Requirement already satisfied: markupsafe~=2.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (2.1.3)\n",
|
56 |
+
"Requirement already satisfied: matplotlib~=3.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (3.7.1)\n",
|
57 |
+
"Requirement already satisfied: orjson~=3.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (3.10.7)\n",
|
58 |
+
"Requirement already satisfied: pandas<3.0,>=1.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (2.0.1)\n",
|
59 |
+
"Requirement already satisfied: pillow<11.0,>=8.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (9.5.0)\n",
|
60 |
+
"Requirement already satisfied: pydantic>=2.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (2.9.2)\n",
|
61 |
+
"Requirement already satisfied: pydub in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.25.1)\n",
|
62 |
+
"Requirement already satisfied: python-multipart>=0.0.9 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.0.12)\n",
|
63 |
+
"Requirement already satisfied: ruff>=0.2.2 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.7.0)\n",
|
64 |
+
"Requirement already satisfied: semantic-version~=2.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (2.10.0)\n",
|
65 |
+
"Requirement already satisfied: tomlkit==0.12.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.12.0)\n",
|
66 |
+
"Requirement already satisfied: typer<1.0,>=0.12 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.12.5)\n",
|
67 |
+
"Requirement already satisfied: typing-extensions~=4.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (4.12.2)\n",
|
68 |
+
"Requirement already satisfied: urllib3~=2.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (2.2.3)\n",
|
69 |
+
"Requirement already satisfied: uvicorn>=0.14.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio) (0.32.0)\n",
|
70 |
+
"Requirement already satisfied: fsspec in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio-client==1.3.0->gradio) (2023.9.0)\n",
|
71 |
+
"Requirement already satisfied: websockets<13.0,>=10.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from gradio-client==1.3.0->gradio) (12.0)\n",
|
72 |
+
"Requirement already satisfied: idna>=2.8 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from anyio<5.0,>=3.0->gradio) (3.4)\n",
|
73 |
+
"Requirement already satisfied: sniffio>=1.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n",
|
74 |
+
"Requirement already satisfied: exceptiongroup>=1.0.2 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from anyio<5.0,>=3.0->gradio) (1.2.2)\n",
|
75 |
+
"Requirement already satisfied: starlette<0.41.0,>=0.37.2 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from fastapi<1.0->gradio) (0.40.0)\n",
|
76 |
+
"Requirement already satisfied: certifi in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from httpx>=0.24.1->gradio) (2022.12.7)\n",
|
77 |
+
"Requirement already satisfied: httpcore==1.* in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from httpx>=0.24.1->gradio) (1.0.6)\n",
|
78 |
+
"Requirement already satisfied: h11<0.15,>=0.13 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n",
|
79 |
+
"Requirement already satisfied: zipp>=3.1.0 in c:\\users\\blu-ray\\appdata\\roaming\\python\\python39\\site-packages (from importlib-resources<7.0,>=1.3->gradio) (3.15.0)\n",
|
80 |
+
"Requirement already satisfied: contourpy>=1.0.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from matplotlib~=3.0->gradio) (1.0.7)\n",
|
81 |
+
"Requirement already satisfied: cycler>=0.10 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from matplotlib~=3.0->gradio) (0.11.0)\n",
|
82 |
+
"Requirement already satisfied: fonttools>=4.22.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from matplotlib~=3.0->gradio) (4.39.3)\n",
|
83 |
+
"Requirement already satisfied: kiwisolver>=1.0.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from matplotlib~=3.0->gradio) (1.4.4)\n",
|
84 |
+
"Requirement already satisfied: pyparsing>=2.3.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from matplotlib~=3.0->gradio) (3.0.9)\n",
|
85 |
+
"Requirement already satisfied: python-dateutil>=2.7 in c:\\users\\blu-ray\\appdata\\roaming\\python\\python39\\site-packages (from matplotlib~=3.0->gradio) (2.8.2)\n",
|
86 |
+
"Requirement already satisfied: pytz>=2020.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from pandas<3.0,>=1.0->gradio) (2023.3)\n",
|
87 |
+
"Requirement already satisfied: tzdata>=2022.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from pandas<3.0,>=1.0->gradio) (2023.3)\n",
|
88 |
+
"Requirement already satisfied: annotated-types>=0.6.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from pydantic>=2.0->gradio) (0.7.0)\n",
|
89 |
+
"Requirement already satisfied: pydantic-core==2.23.4 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from pydantic>=2.0->gradio) (2.23.4)\n",
|
90 |
+
"Requirement already satisfied: colorama in c:\\users\\blu-ray\\appdata\\roaming\\python\\python39\\site-packages (from tqdm>=4.27->transformers==4.46.0.dev0) (0.4.6)\n",
|
91 |
+
"Requirement already satisfied: click>=8.0.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
|
92 |
+
"Requirement already satisfied: shellingham>=1.3.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
|
93 |
+
"Requirement already satisfied: rich>=10.11.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from typer<1.0,>=0.12->gradio) (13.9.2)\n",
|
94 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from requests->transformers==4.46.0.dev0) (3.1.0)\n",
|
95 |
+
"Requirement already satisfied: six>=1.5 in c:\\users\\blu-ray\\appdata\\roaming\\python\\python39\\site-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n",
|
96 |
+
"Requirement already satisfied: markdown-it-py>=2.2.0 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n",
|
97 |
+
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in c:\\users\\blu-ray\\appdata\\roaming\\python\\python39\\site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.14.0)\n",
|
98 |
+
"Requirement already satisfied: mdurl~=0.1 in c:\\users\\blu-ray\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n",
|
99 |
+
"Building wheels for collected packages: transformers\n",
|
100 |
+
" Building wheel for transformers (pyproject.toml): started\n",
|
101 |
+
" Building wheel for transformers (pyproject.toml): finished with status 'done'\n",
|
102 |
+
" Created wheel for transformers: filename=transformers-4.46.0.dev0-py3-none-any.whl size=9991917 sha256=ad63aaf442d2aa5151b0780d1a1d4deab93c5606a8a1f3b83a4f860e16f6820f\n",
|
103 |
+
" Stored in directory: C:\\Users\\BLU-RAY\\AppData\\Local\\Temp\\pip-ephem-wheel-cache-0te5vjox\\wheels\\14\\a0\\7b\\8f6b25ba4110aa215fcb8d6aedd6cd4f9b9b6619190999ac2b\n",
|
104 |
+
"Successfully built transformers\n",
|
105 |
+
"Installing collected packages: transformers\n",
|
106 |
+
" Attempting uninstall: transformers\n",
|
107 |
+
" Found existing installation: transformers 4.45.2\n",
|
108 |
+
" Uninstalling transformers-4.45.2:\n",
|
109 |
+
" Successfully uninstalled transformers-4.45.2\n",
|
110 |
+
"Successfully installed transformers-4.46.0.dev0\n"
|
111 |
+
]
|
112 |
+
},
|
113 |
+
{
|
114 |
+
"name": "stderr",
|
115 |
+
"output_type": "stream",
|
116 |
+
"text": [
|
117 |
+
" Running command git clone --filter=blob:none --quiet https://github.com/huggingface/transformers 'C:\\Users\\BLU-RAY\\AppData\\Local\\Temp\\pip-req-build-jqimnzmp'\n"
|
118 |
+
]
|
119 |
+
}
|
120 |
+
],
|
121 |
+
"source": [
|
122 |
+
"%%capture\n",
|
123 |
+
"!pip install git+https://github.com/huggingface/transformers gradio"
|
124 |
+
]
|
125 |
+
},
|
126 |
+
{
|
127 |
+
"cell_type": "markdown",
|
128 |
+
"metadata": {
|
129 |
+
"id": "sZONes21fHTA"
|
130 |
+
},
|
131 |
+
"source": [
|
132 |
+
"Let's use the high level `pipeline` from the `transformers` library to load the model."
|
133 |
+
]
|
134 |
+
},
|
135 |
+
{
|
136 |
+
"cell_type": "code",
|
137 |
+
"execution_count": null,
|
138 |
+
"metadata": {
|
139 |
+
"colab": {
|
140 |
+
"base_uri": "https://localhost:8080/"
|
141 |
+
},
|
142 |
+
"id": "DvBdwMdPcr-Y",
|
143 |
+
"outputId": "47f32218-fd85-49ea-d880-d31577bcf9b8"
|
144 |
+
},
|
145 |
+
"outputs": [
|
146 |
+
{
|
147 |
+
"name": "stderr",
|
148 |
+
"output_type": "stream",
|
149 |
+
"text": [
|
150 |
+
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n",
|
151 |
+
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
|
152 |
+
]
|
153 |
+
}
|
154 |
+
],
|
155 |
+
"source": [
|
156 |
+
"import torch\n",
|
157 |
+
"from transformers import pipeline,MarianMTModel, MarianTokenizer\n",
|
158 |
+
"\n",
|
159 |
+
"pipe = pipeline(\"automatic-speech-recognition\",\n",
|
160 |
+
" \"openai/whisper-large-v3\",\n",
|
161 |
+
" torch_dtype=torch.float16,\n",
|
162 |
+
" device=\"cuda:0\")\n",
|
163 |
+
"\n",
|
164 |
+
"model_name_translate = \"Helsinki-NLP/opus-mt-en-ar\"\n",
|
165 |
+
"tokenizer_translation = MarianTokenizer.from_pretrained(model_name_translate)\n",
|
166 |
+
"model_translate = MarianMTModel.from_pretrained(model_name_translate)"
|
167 |
+
]
|
168 |
+
},
|
169 |
+
{
|
170 |
+
"cell_type": "code",
|
171 |
+
"execution_count": null,
|
172 |
+
"metadata": {
|
173 |
+
"colab": {
|
174 |
+
"base_uri": "https://localhost:8080/"
|
175 |
+
},
|
176 |
+
"id": "GZFkIyhjc0Nc",
|
177 |
+
"outputId": "f1463431-3e08-4438-815f-b71e5e7a1503"
|
178 |
+
},
|
179 |
+
"outputs": [
|
180 |
+
{
|
181 |
+
"data": {
|
182 |
+
"text/plain": [
|
183 |
+
"{'text': \" going along slushy country roads and speaking to damp audiences in draughty schoolrooms day after day for a fortnight he'll have to put in an appearance at some place of worship on sunday morning and he can come to us immediately afterwards\"}"
|
184 |
+
]
|
185 |
+
},
|
186 |
+
"execution_count": 2,
|
187 |
+
"metadata": {},
|
188 |
+
"output_type": "execute_result"
|
189 |
+
}
|
190 |
+
],
|
191 |
+
"source": [
|
192 |
+
"pipe(\"https://cdn-media.huggingface.co/speech_samples/sample1.flac\")"
|
193 |
+
]
|
194 |
+
},
|
195 |
+
{
|
196 |
+
"cell_type": "markdown",
|
197 |
+
"metadata": {
|
198 |
+
"id": "pt3YtM_PfTQY"
|
199 |
+
},
|
200 |
+
"source": [
|
201 |
+
"Let's now build a quick Gradio demo where we can play with the model directly using our microphone! You can run this code in a Google Colab instance (or locally!) or just head to the <a href=\"https://huggingface.co/spaces/hf-audio/whisper-large-v3\" target=\"_blank\">Space</a> to play directly with it online."
|
202 |
+
]
|
203 |
+
},
|
204 |
+
{
|
205 |
+
"cell_type": "code",
|
206 |
+
"execution_count": 4,
|
207 |
+
"metadata": {
|
208 |
+
"colab": {
|
209 |
+
"base_uri": "https://localhost:8080/",
|
210 |
+
"height": 648
|
211 |
+
},
|
212 |
+
"id": "K0b2UZLVdIze",
|
213 |
+
"outputId": "bcff00e0-4fc8-4883-9ba4-480f5a6665f0"
|
214 |
+
},
|
215 |
+
"outputs": [
|
216 |
+
{
|
217 |
+
"name": "stdout",
|
218 |
+
"output_type": "stream",
|
219 |
+
"text": [
|
220 |
+
"Running on local URL: http://127.0.0.1:7862\n",
|
221 |
+
"\n",
|
222 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
223 |
+
]
|
224 |
+
},
|
225 |
+
{
|
226 |
+
"data": {
|
227 |
+
"text/html": [
|
228 |
+
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
229 |
+
],
|
230 |
+
"text/plain": [
|
231 |
+
"<IPython.core.display.HTML object>"
|
232 |
+
]
|
233 |
+
},
|
234 |
+
"metadata": {},
|
235 |
+
"output_type": "display_data"
|
236 |
+
},
|
237 |
+
{
|
238 |
+
"data": {
|
239 |
+
"text/plain": []
|
240 |
+
},
|
241 |
+
"execution_count": 4,
|
242 |
+
"metadata": {},
|
243 |
+
"output_type": "execute_result"
|
244 |
+
}
|
245 |
+
],
|
246 |
+
"source": [
|
247 |
+
"import gradio as gr\n",
|
248 |
+
"\n",
|
249 |
+
"def translate(sentence):\n",
|
250 |
+
" batch = tokenizer_translation([sentence], return_tensors=\"pt\")\n",
|
251 |
+
" generated_ids = model_translate.generate(batch[\"input_ids\"])\n",
|
252 |
+
" text = tokenizer_translation.batch_decode(generated_ids, skip_special_tokens=True)[0]\n",
|
253 |
+
" return text\n",
|
254 |
+
"\n",
|
255 |
+
"def transcribe(inputs):\n",
|
256 |
+
" if inputs is None:\n",
|
257 |
+
" raise gr.Error(\"No audio file submitted! Please record an audio before submitting your request.\")\n",
|
258 |
+
"\n",
|
259 |
+
" text = pipe(inputs, generate_kwargs={\"task\": \"transcribe\"}, return_timestamps=True)[\"text\"]\n",
|
260 |
+
" text = translate({\"text\": text})\n",
|
261 |
+
" return text\n",
|
262 |
+
"\n",
|
263 |
+
"demo = gr.Interface(\n",
|
264 |
+
" fn=transcribe,\n",
|
265 |
+
" inputs=[\n",
|
266 |
+
" gr.Audio(sources=[\"microphone\", \"upload\"], type=\"filepath\"),\n",
|
267 |
+
" ],\n",
|
268 |
+
" outputs=\"text\",\n",
|
269 |
+
" title=\"Whisper Large V3: Transcribe Audio\",\n",
|
270 |
+
" description=(\n",
|
271 |
+
" \"Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the\"\n",
|
272 |
+
" \" checkpoint [openai/whisper-large-v3](https://huggingface.co/openai/whisper-large-v3) and 🤗 Transformers to transcribe audio files\"\n",
|
273 |
+
" \" of arbitrary length.\"\n",
|
274 |
+
" ),\n",
|
275 |
+
" allow_flagging=\"never\",\n",
|
276 |
+
")\n",
|
277 |
+
"\n",
|
278 |
+
"demo.launch()\n"
|
279 |
+
]
|
280 |
+
}
|
281 |
+
],
|
282 |
+
"metadata": {
|
283 |
+
"accelerator": "GPU",
|
284 |
+
"colab": {
|
285 |
+
"gpuType": "T4",
|
286 |
+
"provenance": []
|
287 |
+
},
|
288 |
+
"kernelspec": {
|
289 |
+
"display_name": "Python 3",
|
290 |
+
"name": "python3"
|
291 |
+
},
|
292 |
+
"language_info": {
|
293 |
+
"codemirror_mode": {
|
294 |
+
"name": "ipython",
|
295 |
+
"version": 3
|
296 |
+
},
|
297 |
+
"file_extension": ".py",
|
298 |
+
"mimetype": "text/x-python",
|
299 |
+
"name": "python",
|
300 |
+
"nbconvert_exporter": "python",
|
301 |
+
"pygments_lexer": "ipython3",
|
302 |
+
"version": "3.9.5"
|
303 |
+
}
|
304 |
+
},
|
305 |
+
"nbformat": 4,
|
306 |
+
"nbformat_minor": 0
|
307 |
+
}
|