Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import uuid
|
| 4 |
+
import yt_dlp as youtube_dl
|
| 5 |
+
from typing import Generator
|
| 6 |
+
from faster_whisper import WhisperModel
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from typing import Generator
|
| 9 |
+
from faster_whisper import WhisperModel
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
class YouTubeTranscriber:
|
| 14 |
+
def __init__(self, model_path: str):
|
| 15 |
+
self.model = WhisperModel(model_path)
|
| 16 |
+
|
| 17 |
+
def download_audio(self, url: str, preferred_quality: str = "192") -> str:
|
| 18 |
+
file_name = f"{uuid.uuid4()}.mp3"
|
| 19 |
+
output_path = os.path.join("/tmp", file_name) # Use /tmp directory for temporary storage
|
| 20 |
+
|
| 21 |
+
ydl_opts = {
|
| 22 |
+
'format': 'bestaudio/best',
|
| 23 |
+
'postprocessors': [{
|
| 24 |
+
'key': 'FFmpegExtractAudio',
|
| 25 |
+
'preferredcodec': 'mp3',
|
| 26 |
+
'preferredquality': preferred_quality,
|
| 27 |
+
}],
|
| 28 |
+
'outtmpl': output_path, # Specify the output path and file name
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
| 33 |
+
info_dict = ydl.extract_info(url, download=False)
|
| 34 |
+
video_title = info_dict.get('title', 'Unknown title')
|
| 35 |
+
print(f"Downloading audio for: {video_title}")
|
| 36 |
+
|
| 37 |
+
ydl.download([url])
|
| 38 |
+
print(f"Audio file saved as: {output_path}")
|
| 39 |
+
|
| 40 |
+
return output_path
|
| 41 |
+
|
| 42 |
+
except youtube_dl.utils.DownloadError as e:
|
| 43 |
+
print(f"Error downloading audio: {e}")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
def transcribe_audio(self, path: str) -> Generator:
|
| 47 |
+
print(f"Reading {path}")
|
| 48 |
+
segments, _ = self.model.transcribe(path)
|
| 49 |
+
return segments
|
| 50 |
+
|
| 51 |
+
def process_segments(self, segments: Generator) -> pd.DataFrame:
|
| 52 |
+
result = []
|
| 53 |
+
for i, segment in enumerate(segments):
|
| 54 |
+
result.append({
|
| 55 |
+
'chunk_id': f"chunk_{i}",
|
| 56 |
+
'chunk_length': segment.end - segment.start,
|
| 57 |
+
'text': segment.text,
|
| 58 |
+
'start_time': segment.start,
|
| 59 |
+
'end_time': segment.end
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
df = pd.DataFrame(result)
|
| 63 |
+
return df
|
| 64 |
+
|
| 65 |
+
# Function to be called by the Gradio interface
|
| 66 |
+
def transcribe_youtube_video(url: str, model_path: str = "distil-large-v2") -> str:
|
| 67 |
+
yt_transcriber = YouTubeTranscriber(model_path)
|
| 68 |
+
audio_path = yt_transcriber.download_audio(url)
|
| 69 |
+
|
| 70 |
+
if audio_path:
|
| 71 |
+
segments = yt_transcriber.transcribe_audio(audio_path)
|
| 72 |
+
df = yt_transcriber.process_segments(segments)
|
| 73 |
+
output_csv = os.path.join("/tmp", f"{uuid.uuid4()}.csv")
|
| 74 |
+
df.to_csv(output_csv, index=False)
|
| 75 |
+
return output_csv
|
| 76 |
+
else:
|
| 77 |
+
return "Failed to download audio."
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
import gradio as gr
|
| 81 |
+
|
| 82 |
+
interface = gr.Interface(
|
| 83 |
+
fn=transcribe_youtube_video,
|
| 84 |
+
inputs=[
|
| 85 |
+
gr.Textbox(lines=1, placeholder="Enter YouTube URL here...", label="YouTube URL"),
|
| 86 |
+
gr.Textbox(lines=1, label="Whisper Model Path")
|
| 87 |
+
],
|
| 88 |
+
outputs=gr.File(label="Transcribed Segments CSV"), # Use gr.File directly
|
| 89 |
+
title="YouTube Audio Transcriber",
|
| 90 |
+
description="Enter a YouTube URL to download the audio and transcribe it using Whisper."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Launch the interface
|
| 94 |
+
interface.launch()
|