Spaces:
Runtime error
Runtime error
iabachelis
commited on
Commit
•
c40ff49
1
Parent(s):
553dfb4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import yt_dlp as youtube_dl
|
4 |
+
import os
|
5 |
+
os.environ['PATH'] = 'C:\\Program Files (x86)\\ffmpeg-7.0-essentials_build\\bin;' + os.environ['PATH']
|
6 |
+
|
7 |
+
# Define the Whisper pipeline
|
8 |
+
whisper = pipeline(model="openai/whisper-large-v3")
|
9 |
+
|
10 |
+
def download_youtube_audio(youtube_url):
|
11 |
+
# Options for yt-dlp
|
12 |
+
ydl_opts = {
|
13 |
+
'format': 'bestaudio/best',
|
14 |
+
'postprocessors': [{
|
15 |
+
'key': 'FFmpegExtractAudio',
|
16 |
+
'preferredcodec': 'mp3',
|
17 |
+
'preferredquality': '192',
|
18 |
+
}],
|
19 |
+
'ffmpeg_location': 'C:\\Program Files (x86)\\ffmpeg-7.0-essentials_build\\bin', # Add this line
|
20 |
+
'outtmpl': 'downloaded_audio.%(ext)s',
|
21 |
+
'noplaylist': True,
|
22 |
+
}
|
23 |
+
# Use yt-dlp to download the audio
|
24 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
25 |
+
info_dict = ydl.extract_info(youtube_url, download=True)
|
26 |
+
audio_file = ydl.prepare_filename(info_dict)
|
27 |
+
# Replace the extension with 'mp3' since we know we're extracting to mp3
|
28 |
+
audio_file = os.path.splitext(audio_file)[0] + '.mp3'
|
29 |
+
|
30 |
+
return audio_file
|
31 |
+
|
32 |
+
def transcribe(audio_file_path, task):
|
33 |
+
if task == "transcribe":
|
34 |
+
# Load the audio file and run it through the Whisper model
|
35 |
+
transcription = whisper(audio_file_path)
|
36 |
+
# Cleanup downloaded audio after transcription
|
37 |
+
os.remove(audio_file_path)
|
38 |
+
return transcription["text"]
|
39 |
+
else:
|
40 |
+
# Assuming you would have a translation function or model
|
41 |
+
pass # Replace with your translation logic
|
42 |
+
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
with gr.Tab("YouTube"):
|
45 |
+
with gr.Group():
|
46 |
+
youtube_url = gr.Textbox(placeholder="Paste the URL to a YouTube video here")
|
47 |
+
task = gr.Radio(["transcribe", "translate"], label="Task", value="transcribe")
|
48 |
+
submit_button = gr.Button("Submit")
|
49 |
+
output = gr.Textbox(label="output 1")
|
50 |
+
|
51 |
+
submit_button.click(
|
52 |
+
fn=lambda url, task: transcribe(download_youtube_audio(url), task),
|
53 |
+
inputs=[youtube_url, task],
|
54 |
+
outputs=output
|
55 |
+
)
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
demo.launch(share=True)
|