archit11 commited on
Commit
e21f6d3
1 Parent(s): 74c1f5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -80
app.py CHANGED
@@ -1,95 +1,85 @@
 
 
 
1
  import os
2
- import spaces
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()}"
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
- @spaces.GPU
67
- def transcribe_youtube_video(url: str, model_path: str = "distil-large-v2") -> str:
68
- yt_transcriber = YouTubeTranscriber(model_path)
69
- audio_path = yt_transcriber.download_audio(url)
70
 
71
- if audio_path:
72
- segments = yt_transcriber.transcribe_audio(audio_path)
73
- df = yt_transcriber.process_segments(segments)
74
- output_csv = os.path.join("/tmp", f"{uuid.uuid4()}.csv")
75
- df.to_csv(output_csv, index=False)
76
- return output_csv
77
- else:
78
- return "Failed to download audio."
79
 
 
 
 
 
 
80
 
81
- import gradio as gr
82
 
83
- interface = gr.Interface(
84
- fn=transcribe_youtube_video,
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
- interface.launch()
 
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()