Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
'preferredquality': preferred_quality,
|
| 27 |
-
}],
|
| 28 |
-
'outtmpl': output_path, # Specify the output path and file name
|
| 29 |
-
}
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
return None
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
'end_time': segment.end
|
| 60 |
-
})
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
yt_transcriber = YouTubeTranscriber(model_path)
|
| 69 |
-
audio_path = yt_transcriber.download_audio(url)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
fn=
|
| 85 |
-
inputs=[
|
| 86 |
-
gr.Textbox(lines=1, placeholder="Enter YouTube URL here...", label="YouTube URL"),
|
| 87 |
-
gr.Textbox(lines=1, label="Whisper Model Path")
|
| 88 |
-
],
|
| 89 |
-
outputs=gr.File(label="Transcribed Segments CSV"), # Use gr.File directly
|
| 90 |
-
title="YouTube Audio Transcriber",
|
| 91 |
-
description="Enter a YouTube URL to download the audio and transcribe it using Whisper."
|
| 92 |
-
)
|
| 93 |
|
| 94 |
# Launch the interface
|
| 95 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import yt_dlp
|
| 4 |
import os
|
| 5 |
+
from transcriber import gen_csv()
|
| 6 |
+
df = pd.read_csv("./final.csv")
|
| 7 |
+
transcripts = df.to_dict(orient='records')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Function to download video using yt-dlp and generate transcript HTML
|
| 10 |
+
def download_video(youtube_url):
|
| 11 |
+
# Define download options
|
| 12 |
+
ydl_opts = {
|
| 13 |
+
'format': 'mp4',
|
| 14 |
+
'outtmpl': 'downloaded_video.%(ext)s',
|
| 15 |
+
'quiet': True
|
| 16 |
+
}
|
| 17 |
|
| 18 |
+
# Extract video ID to check if already downloaded
|
| 19 |
+
with yt_dlp.YoutubeDL({'quiet': True}) as ydl:
|
| 20 |
+
info_dict = ydl.extract_info(youtube_url, download=False)
|
| 21 |
+
video_ext = info_dict.get('ext')
|
| 22 |
+
video_path = f'downloaded_video.mp4'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Check if video already downloaded
|
| 25 |
+
if not os.path.exists(video_path):
|
| 26 |
+
# Download the video
|
| 27 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 28 |
+
ydl.download([youtube_url])
|
| 29 |
|
| 30 |
+
# Generate HTML for the transcript
|
| 31 |
+
transcript_html = ""
|
| 32 |
+
for t in transcripts:
|
| 33 |
+
transcript_html += f'<div class="transcript-block"><a href="#" onclick="var video = document.getElementById(\'video-player\').querySelector(\'video\'); video.currentTime={t["start_time"]}; return false;">' \
|
| 34 |
+
f'[{t["start_time"]:.2f} - {t["end_time"]:.2f}]<br>{t["text"]}</a></div>'
|
| 35 |
+
|
| 36 |
+
return video_path, transcript_html
|
|
|
|
| 37 |
|
| 38 |
+
# Function to search the transcript
|
| 39 |
+
def search_transcript(keyword):
|
| 40 |
+
search_results = ""
|
| 41 |
+
for t in transcripts:
|
| 42 |
+
if keyword.lower() in t['text'].lower():
|
| 43 |
+
search_results += f'<div class="transcript-block"><a href="#" onclick="var video = document.getElementById(\'video-player\').querySelector(\'video\'); video.currentTime={t["start_time"]}; return false;">' \
|
| 44 |
+
f'[{t["start_time"]:.2f} - {t["end_time"]:.2f}]<br>{t["text"]}</a></div>'
|
| 45 |
+
return search_results
|
| 46 |
|
| 47 |
+
# CSS for styling
|
| 48 |
+
css = """
|
| 49 |
+
.fixed-video { width: 480px !important; height: 270px !important; }
|
| 50 |
+
.fixed-transcript { width: 480px !important; height: 270px !important; overflow-y: auto; }
|
| 51 |
+
.transcript-block { margin: 10px 0; padding: 10px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; }
|
| 52 |
+
.transcript-block a { text-decoration: none; color: #007bff; }
|
| 53 |
+
.transcript-block a:hover { text-decoration: underline; }
|
| 54 |
+
"""
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Gradio interface
|
| 57 |
+
with gr.Blocks(css=css) as demo:
|
| 58 |
+
gr.Markdown("# YouTube Video Player with Clickable Transcript")
|
| 59 |
|
| 60 |
+
with gr.Row():
|
| 61 |
+
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video link here")
|
| 62 |
+
download_button = gr.Button("Download and Display Transcript")
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
with gr.Row():
|
| 65 |
+
video = gr.Video(label="Video Player", elem_id="video-player", elem_classes="fixed-video")
|
| 66 |
+
transcript_display = gr.HTML(label="Transcript", elem_classes="fixed-transcript")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
search_box = gr.Textbox(label="Search Transcript", placeholder="Enter keyword to search")
|
| 70 |
+
search_button = gr.Button("Search")
|
| 71 |
+
search_results_display = gr.HTML(label="Search Results", elem_classes="fixed-transcript")
|
| 72 |
|
| 73 |
+
# On button click, download the video and display the transcript
|
| 74 |
+
def display_transcript(youtube_url):
|
| 75 |
+
video_path, transcript_html = download_video(youtube_url)
|
| 76 |
+
# Ensure the video path is correctly passed to the Gradio video component
|
| 77 |
+
return video_path, transcript_html
|
| 78 |
|
| 79 |
+
download_button.click(fn=display_transcript, inputs=youtube_url, outputs=[video, transcript_display])
|
| 80 |
|
| 81 |
+
# On search button click, search the transcript and display results
|
| 82 |
+
search_button.click(fn=search_transcript, inputs=search_box, outputs=search_results_display)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Launch the interface
|
| 85 |
+
demo.launch()
|