Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,99 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
import yt_dlp
|
4 |
import os
|
5 |
-
from
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Function to download video using yt-dlp and generate transcript HTML
|
10 |
def download_video(youtube_url):
|
@@ -28,6 +117,7 @@ def download_video(youtube_url):
|
|
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;">' \
|
@@ -37,6 +127,7 @@ def download_video(youtube_url):
|
|
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():
|
@@ -73,7 +164,6 @@ with gr.Blocks(css=css) as demo:
|
|
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])
|
@@ -82,4 +172,4 @@ with gr.Blocks(css=css) as demo:
|
|
82 |
search_button.click(fn=search_transcript, inputs=search_box, outputs=search_results_display)
|
83 |
|
84 |
# Launch the interface
|
85 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
import yt_dlp
|
4 |
import os
|
5 |
+
from semantic_chunkers import StatisticalChunker
|
6 |
+
from semantic_router.encoders import HuggingFaceEncoder
|
7 |
+
from faster_whisper import WhisperModel
|
8 |
+
import spaces
|
9 |
+
|
10 |
+
# Function to download YouTube audio
|
11 |
+
def download_youtube_audio(url, output_path, preferred_quality="192"):
|
12 |
+
ydl_opts = {
|
13 |
+
'format': 'bestaudio/best', # Select best audio quality
|
14 |
+
'postprocessors': [{
|
15 |
+
'key': 'FFmpegExtractAudio',
|
16 |
+
'preferredcodec': 'mp3',
|
17 |
+
'preferredquality': preferred_quality,
|
18 |
+
}],
|
19 |
+
'outtmpl': output_path, # Specify the output path and file name
|
20 |
+
}
|
21 |
+
|
22 |
+
try:
|
23 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
24 |
+
info_dict = ydl.extract_info(url, download=False)
|
25 |
+
video_title = info_dict.get('title', None)
|
26 |
+
print(f"Downloading audio for: {video_title}")
|
27 |
+
|
28 |
+
ydl.download([url])
|
29 |
+
print(f"Audio file saved as: {output_path}")
|
30 |
+
|
31 |
+
return output_path
|
32 |
+
|
33 |
+
except yt_dlp.utils.DownloadError as e:
|
34 |
+
print(f"Error downloading audio: {e}")
|
35 |
+
return None # Indicate failure
|
36 |
+
|
37 |
+
# Function to transcribe audio using WhisperModel
|
38 |
+
def transcribe(path, model_name):
|
39 |
+
model = WhisperModel(model_name)
|
40 |
+
print(f"Reading {path}")
|
41 |
+
segments, info = model.transcribe(path)
|
42 |
+
return segments
|
43 |
+
|
44 |
+
# Function to process segments and convert them into a DataFrame
|
45 |
+
@spaces.GPU
|
46 |
+
def process_segments(segments):
|
47 |
+
result = {}
|
48 |
+
print("Processing...")
|
49 |
+
for i, segment in enumerate(segments):
|
50 |
+
chunk_id = f"chunk_{i}"
|
51 |
+
result[chunk_id] = {
|
52 |
+
'chunk_id': segment.id,
|
53 |
+
'chunk_length': segment.end - segment.start,
|
54 |
+
'text': segment.text,
|
55 |
+
'start_time': segment.start,
|
56 |
+
'end_time': segment.end
|
57 |
+
}
|
58 |
+
df = pd.DataFrame.from_dict(result, orient='index')
|
59 |
+
df.to_csv('final.csv') # Save DataFrame to final.csv
|
60 |
+
return df
|
61 |
+
|
62 |
+
# Gradio interface functions
|
63 |
+
@spaces.GPU
|
64 |
+
def generate_transcript(youtube_url, model_name="distil-large-v3"):
|
65 |
+
path = "downloaded_audio.mp3"
|
66 |
+
download_youtube_audio(youtube_url, path)
|
67 |
+
segments = transcribe(path, model_name)
|
68 |
+
df = process_segments(segments)
|
69 |
+
|
70 |
+
lis = list(df['text'])
|
71 |
+
encoder = HuggingFaceEncoder(name="sentence-transformers/all-MiniLM-L6-v2")
|
72 |
+
chunker = StatisticalChunker(encoder=encoder, dynamic_threshold=True, min_split_tokens=30, max_split_tokens=40, window_size=2, enable_statistics=False)
|
73 |
+
chunks = chunker._chunk(lis)
|
74 |
+
|
75 |
+
row_index = 0
|
76 |
+
for i in range(len(chunks)):
|
77 |
+
for j in range(len(chunks[i].splits)):
|
78 |
+
df.at[row_index, 'chunk_id2'] = f'chunk_{i}'
|
79 |
+
row_index += 1
|
80 |
+
|
81 |
+
grouped = df.groupby('chunk_id2').agg({
|
82 |
+
'start_time': 'min',
|
83 |
+
'end_time': 'max',
|
84 |
+
'text': lambda x: ' '.join(x),
|
85 |
+
'chunk_id': list
|
86 |
+
}).reset_index()
|
87 |
+
|
88 |
+
grouped = grouped.rename(columns={'chunk_id': 'chunk_ids'})
|
89 |
+
grouped['chunk_length'] = grouped['end_time'] - grouped['start_time']
|
90 |
+
grouped['chunk_id'] = grouped['chunk_id2']
|
91 |
+
grouped = grouped.drop(columns=['chunk_id2', 'chunk_ids'])
|
92 |
+
grouped.to_csv('final.csv')
|
93 |
+
df = pd.read_csv("final.csv")
|
94 |
+
transcripts = df.to_dict(orient='records')
|
95 |
+
|
96 |
+
return transcripts
|
97 |
|
98 |
# Function to download video using yt-dlp and generate transcript HTML
|
99 |
def download_video(youtube_url):
|
|
|
117 |
ydl.download([youtube_url])
|
118 |
|
119 |
# Generate HTML for the transcript
|
120 |
+
transcripts = generate_transcript(youtube_url)
|
121 |
transcript_html = ""
|
122 |
for t in transcripts:
|
123 |
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;">' \
|
|
|
127 |
|
128 |
# Function to search the transcript
|
129 |
def search_transcript(keyword):
|
130 |
+
transcripts = pd.read_csv("final.csv").to_dict(orient='records')
|
131 |
search_results = ""
|
132 |
for t in transcripts:
|
133 |
if keyword.lower() in t['text'].lower():
|
|
|
164 |
# On button click, download the video and display the transcript
|
165 |
def display_transcript(youtube_url):
|
166 |
video_path, transcript_html = download_video(youtube_url)
|
|
|
167 |
return video_path, transcript_html
|
168 |
|
169 |
download_button.click(fn=display_transcript, inputs=youtube_url, outputs=[video, transcript_display])
|
|
|
172 |
search_button.click(fn=search_transcript, inputs=search_box, outputs=search_results_display)
|
173 |
|
174 |
# Launch the interface
|
175 |
+
demo.launch()
|