pere commited on
Commit
f4d4476
·
1 Parent(s): add94c7

update test

Browse files
Files changed (1) hide show
  1. app.py +16 -21
app.py CHANGED
@@ -65,24 +65,19 @@ def _return_yt_html_embed(yt_url):
65
  return HTML_str
66
 
67
 
68
- def yt_transcribe(yt_url, return_timestamps=False):
69
- ydl_opts = {
70
- 'format': 'bestaudio/best',
71
- 'outtmpl': 'audio.mp3',
72
- 'noplaylist': True
73
- }
74
 
75
- try:
76
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
77
- ydl.download([yt_url])
78
- except Exception as e:
79
- return f"Error downloading audio: {str(e)}"
80
 
81
- if not os.path.exists("audio.mp3"):
82
- return "Downloaded audio file not found."
83
 
84
- text = transcribe("audio.mp3", return_timestamps=return_timestamps)
85
- return _return_yt_html_embed(yt_url), text
86
 
87
 
88
  demo = gr.Blocks()
@@ -90,8 +85,8 @@ demo = gr.Blocks()
90
  mf_transcribe = gr.Interface(
91
  fn=transcribe,
92
  inputs=[
93
- gr.components.Audio(sources=['upload', 'microphone'], type="filepath"),
94
- gr.components.Checkbox(label="Return timestamps"),
95
  ],
96
  outputs="text",
97
  title="NB-Whisper Demo",
@@ -106,20 +101,20 @@ mf_transcribe = gr.Interface(
106
  yt_transcribe = gr.Interface(
107
  fn=yt_transcribe,
108
  inputs=[
109
- gr.components.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
110
- gr.components.Checkbox(label="Return timestamps"),
111
  ],
112
- examples=[["https://www.youtube.com/watch?v=mukeSSa5GKo"]],
113
  outputs=["html", "text"],
114
  title="Whisper Demo: Transcribe YouTube",
115
  description=(
116
  "Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
117
- f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files of"
118
  " arbitrary length."
119
  ),
120
  allow_flagging="never",
121
  )
122
 
 
123
  with demo:
124
  gr.TabbedInterface([
125
  mf_transcribe,
 
65
  return HTML_str
66
 
67
 
68
+ @spaces.GPU
69
+ def yt_transcribe(yt_url, task):
70
+ html_embed_str = _return_yt_html_embed(yt_url)
 
 
 
71
 
72
+ with tempfile.TemporaryDirectory() as tmpdirname:
73
+ filepath = os.path.join(tmpdirname, "audio.mp3")
74
+ download_yt_audio(yt_url, filepath)
 
 
75
 
76
+ inputs = ffmpeg_read(filepath, pipe.feature_extractor.sampling_rate)
77
+ inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
78
 
79
+ text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
80
+ return html_embed_str, text
81
 
82
 
83
  demo = gr.Blocks()
 
85
  mf_transcribe = gr.Interface(
86
  fn=transcribe,
87
  inputs=[
88
+ gr.Audio(sources="microphone", type="filepath"),
89
+ gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
90
  ],
91
  outputs="text",
92
  title="NB-Whisper Demo",
 
101
  yt_transcribe = gr.Interface(
102
  fn=yt_transcribe,
103
  inputs=[
104
+ gr.Textbox(lines=1, placeholder="Paste the URL to a YouTube video here", label="YouTube URL"),
105
+ gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
106
  ],
 
107
  outputs=["html", "text"],
108
  title="Whisper Demo: Transcribe YouTube",
109
  description=(
110
  "Transcribe long-form YouTube videos with the click of a button! Demo uses the the fine-tuned checkpoint:"
111
+ f" [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe video files of"
112
  " arbitrary length."
113
  ),
114
  allow_flagging="never",
115
  )
116
 
117
+
118
  with demo:
119
  gr.TabbedInterface([
120
  mf_transcribe,