dwb2023 commited on
Commit
7bbd83c
·
verified ·
1 Parent(s): 71429cb

Update app.py

Browse files

update to use dataset

Files changed (1) hide show
  1. app.py +33 -83
app.py CHANGED
@@ -1,136 +1,87 @@
1
- import spaces
2
- import torch
3
-
4
  import gradio as gr
5
  import yt_dlp as youtube_dl
6
  from transformers import pipeline
7
  from transformers.pipelines.audio_utils import ffmpeg_read
8
-
9
  import tempfile
10
  import os
 
 
 
 
11
 
12
  MODEL_NAME = "openai/whisper-large-v3"
13
  BATCH_SIZE = 8
14
- FILE_LIMIT_MB = 1000
15
- YT_LENGTH_LIMIT_S = 4800 # limit to 1.5 hour YouTube files
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
 
27
- @spaces.GPU(duration=120)
28
- def transcribe(inputs, task):
29
  if inputs is None:
30
  raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
31
-
32
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
33
- return text
34
-
35
-
36
- def _return_yt_html_embed(yt_url):
37
- video_id = yt_url.split("?v=")[-1]
38
- HTML_str = (
39
- f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
40
- " </center>"
41
- )
42
- return HTML_str
43
 
44
  def download_yt_audio(yt_url, filename):
45
  info_loader = youtube_dl.YoutubeDL()
46
-
47
  try:
48
  info = info_loader.extract_info(yt_url, download=False)
49
  except youtube_dl.utils.DownloadError as err:
50
  raise gr.Error(str(err))
51
-
52
  file_length = info["duration_string"]
53
- file_h_m_s = file_length.split(":")
54
- file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
55
-
56
  if len(file_h_m_s) == 1:
57
  file_h_m_s.insert(0, 0)
58
  if len(file_h_m_s) == 2:
59
  file_h_m_s.insert(0, 0)
60
- file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
61
-
62
  if file_length_s > YT_LENGTH_LIMIT_S:
63
  yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
64
  file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
65
  raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
66
-
67
- ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
68
-
69
  with youtube_dl.YoutubeDL(ydl_opts) as ydl:
70
- try:
71
- ydl.download([yt_url])
72
- except youtube_dl.utils.ExtractorError as err:
73
- raise gr.Error(str(err))
74
-
75
- @spaces.GPU(duration=120)
76
- def yt_transcribe(yt_url, task, max_filesize=75.0):
77
- html_embed_str = _return_yt_html_embed(yt_url)
78
 
 
79
  with tempfile.TemporaryDirectory() as tmpdirname:
80
  filepath = os.path.join(tmpdirname, "video.mp4")
81
  download_yt_audio(yt_url, filepath)
82
  with open(filepath, "rb") as f:
83
  inputs = f.read()
84
-
85
  inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
86
  inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
87
-
88
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
 
 
89
 
90
- return html_embed_str, text
91
-
 
 
 
92
 
93
  demo = gr.Blocks()
94
 
95
- mf_transcribe = gr.Interface(
96
- fn=transcribe,
97
- inputs=[
98
- gr.Audio(sources="microphone", type="filepath"),
99
- gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
100
- ],
101
- outputs="text",
102
- title="Whisper Large V3: Transcribe Audio",
103
- description=(
104
- "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
105
- f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
106
- " of arbitrary length."
107
- ),
108
- allow_flagging="never",
109
- )
110
-
111
- file_transcribe = gr.Interface(
112
- fn=transcribe,
113
- inputs=[
114
- gr.Audio(sources="upload", type="filepath", label="Audio file"),
115
- gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
116
- ],
117
- outputs="text",
118
- title="Whisper Large V3: Transcribe Audio",
119
- description=(
120
- "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the"
121
- f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
122
- " of arbitrary length."
123
- ),
124
- allow_flagging="never",
125
- )
126
-
127
- yt_transcribe = gr.Interface(
128
  fn=yt_transcribe,
129
  inputs=[
130
  gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
131
  gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
132
  ],
133
- outputs=["html", "text"],
134
  title="Whisper Large V3: Transcribe YouTube",
135
  description=(
136
  "Transcribe long-form YouTube videos with the click of a button! Demo uses the checkpoint"
@@ -141,7 +92,6 @@ yt_transcribe = gr.Interface(
141
  )
142
 
143
  with demo:
144
- gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
145
 
146
  demo.queue().launch()
147
-
 
 
 
 
1
  import gradio as gr
2
  import yt_dlp as youtube_dl
3
  from transformers import pipeline
4
  from transformers.pipelines.audio_utils import ffmpeg_read
5
+ from huggingface_hub import CommitScheduler
6
  import tempfile
7
  import os
8
+ import json
9
+ from datetime import datetime
10
+ from pathlib import Path
11
+ from uuid import uuid4
12
 
13
  MODEL_NAME = "openai/whisper-large-v3"
14
  BATCH_SIZE = 8
15
+ YT_LENGTH_LIMIT_S = 4800 # 1 hour limit
 
16
 
17
  device = 0 if torch.cuda.is_available() else "cpu"
18
+ pipe = pipeline(task="automatic-speech-recognition", model=MODEL_NAME, chunk_length_s=30, device=device)
19
 
20
+ JSON_DATASET_DIR = Path("json_dataset")
21
+ JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
22
+ JSON_DATASET_PATH = JSON_DATASET_DIR / f"transcriptions-{uuid4()}.json"
 
 
 
23
 
24
+ scheduler = CommitScheduler(
25
+ repo_id="your-dataset-repo",
26
+ repo_type="dataset",
27
+ folder_path=JSON_DATASET_DIR,
28
+ path_in_repo="data",
29
+ )
30
 
31
+ def transcribe_audio(inputs, task):
 
32
  if inputs is None:
33
  raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
 
34
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
35
+ return text
 
 
 
 
 
 
 
 
 
36
 
37
  def download_yt_audio(yt_url, filename):
38
  info_loader = youtube_dl.YoutubeDL()
 
39
  try:
40
  info = info_loader.extract_info(yt_url, download=False)
41
  except youtube_dl.utils.DownloadError as err:
42
  raise gr.Error(str(err))
 
43
  file_length = info["duration_string"]
44
+ file_h_m_s = list(map(int, file_length.split(":")))
 
 
45
  if len(file_h_m_s) == 1:
46
  file_h_m_s.insert(0, 0)
47
  if len(file_h_m_s) == 2:
48
  file_h_m_s.insert(0, 0)
49
+ file_length_s = sum(x * 60 ** i for i, x in enumerate(reversed(file_h_m_s)))
 
50
  if file_length_s > YT_LENGTH_LIMIT_S:
51
  yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
52
  file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
53
  raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
54
+ ydl_opts = {"outtmpl": filename, "format": "bestaudio/best"}
 
 
55
  with youtube_dl.YoutubeDL(ydl_opts) as ydl:
56
+ ydl.download([yt_url])
 
 
 
 
 
 
 
57
 
58
+ def yt_transcribe(yt_url, task):
59
  with tempfile.TemporaryDirectory() as tmpdirname:
60
  filepath = os.path.join(tmpdirname, "video.mp4")
61
  download_yt_audio(yt_url, filepath)
62
  with open(filepath, "rb") as f:
63
  inputs = f.read()
 
64
  inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
65
  inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
 
66
  text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
67
+ save_transcription(yt_url, text)
68
+ return text
69
 
70
+ def save_transcription(yt_url, transcription):
71
+ with scheduler.lock:
72
+ with JSON_DATASET_PATH.open("a") as f:
73
+ json.dump({"url": yt_url, "transcription": transcription, "datetime": datetime.now().isoformat()}, f)
74
+ f.write("\n")
75
 
76
  demo = gr.Blocks()
77
 
78
+ yt_transcribe_interface = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  fn=yt_transcribe,
80
  inputs=[
81
  gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
82
  gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
83
  ],
84
+ outputs="text",
85
  title="Whisper Large V3: Transcribe YouTube",
86
  description=(
87
  "Transcribe long-form YouTube videos with the click of a button! Demo uses the checkpoint"
 
92
  )
93
 
94
  with demo:
95
+ gr.TabbedInterface([yt_transcribe_interface], ["YouTube"])
96
 
97
  demo.queue().launch()