Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,10 @@ import os
|
|
| 3 |
import yt_dlp
|
| 4 |
from io import StringIO
|
| 5 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class OutputLogger:
|
| 8 |
def __init__(self):
|
|
@@ -32,12 +36,79 @@ def download_video(url):
|
|
| 32 |
|
| 33 |
return gr.File(filename), "Video downloaded successfully! Click the download button to save the file.", logger.output
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
iface = gr.Interface(
|
| 36 |
-
fn=
|
| 37 |
-
inputs=
|
| 38 |
-
outputs=
|
| 39 |
-
title="YouTube Video
|
| 40 |
-
description="
|
| 41 |
)
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
| 3 |
import yt_dlp
|
| 4 |
from io import StringIO
|
| 5 |
import sys
|
| 6 |
+
import whisper
|
| 7 |
+
import re
|
| 8 |
+
from moviepy.editor import VideoFileClip
|
| 9 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 10 |
|
| 11 |
class OutputLogger:
|
| 12 |
def __init__(self):
|
|
|
|
| 36 |
|
| 37 |
return gr.File(filename), "Video downloaded successfully! Click the download button to save the file.", logger.output
|
| 38 |
|
| 39 |
+
# Load Whisper model
|
| 40 |
+
model = whisper.load_model("base")
|
| 41 |
+
|
| 42 |
+
# Define the function to extract video ID from URL
|
| 43 |
+
def extract_video_id(url):
|
| 44 |
+
video_id = None
|
| 45 |
+
if "youtube.com" in url:
|
| 46 |
+
video_id = re.findall(r"v=([^&]+)", url)[0]
|
| 47 |
+
elif "youtu.be" in url:
|
| 48 |
+
video_id = url.split("/")[-1]
|
| 49 |
+
return video_id
|
| 50 |
+
|
| 51 |
+
# Function to extract audio from video and save as WAV file
|
| 52 |
+
def extract_audio_from_video(video_path):
|
| 53 |
+
clip = VideoFileClip(video_path)
|
| 54 |
+
audio_path = "temp_audio.wav" # Temporary audio file name
|
| 55 |
+
clip.audio.write_audiofile(audio_path, codec='pcm_s16le') # Whisper needs 16-bit PCM audio
|
| 56 |
+
clip.close()
|
| 57 |
+
return audio_path
|
| 58 |
+
|
| 59 |
+
# Define the transcription function
|
| 60 |
+
def transcribe_video(video_source, example_choice, youtube_url, video_file):
|
| 61 |
+
transcription = ""
|
| 62 |
+
if video_source == "Example YouTube Presentations":
|
| 63 |
+
if example_choice in example_videos:
|
| 64 |
+
video_url = example_videos[example_choice]
|
| 65 |
+
video_id = extract_video_id(video_url)
|
| 66 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 67 |
+
transcription = " ".join([d['text'] for d in transcript])
|
| 68 |
+
else:
|
| 69 |
+
transcription = "Invalid example presentation choice."
|
| 70 |
+
elif video_source == "Custom YouTube URL":
|
| 71 |
+
video_id = extract_video_id(youtube_url)
|
| 72 |
+
if video_id:
|
| 73 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 74 |
+
transcription = " ".join([d['text'] for d in transcript])
|
| 75 |
+
else:
|
| 76 |
+
transcription = "Invalid YouTube URL."
|
| 77 |
+
elif video_source == "Upload Video File":
|
| 78 |
+
try:
|
| 79 |
+
audio_path = extract_audio_from_video(video_file) # Use video_file directly as the path
|
| 80 |
+
result = model.transcribe(audio_path) # Transcribe the extracted audio
|
| 81 |
+
transcription = result["text"]
|
| 82 |
+
except Exception as e:
|
| 83 |
+
transcription = f"Error processing video file: {e}"
|
| 84 |
+
else:
|
| 85 |
+
transcription = "Invalid video source choice."
|
| 86 |
+
|
| 87 |
+
return transcription
|
| 88 |
+
|
| 89 |
+
# Dictionary of example YouTube presentations
|
| 90 |
+
example_videos = {
|
| 91 |
+
"I Have a Dream - Martin Luther King Jr.": "https://www.youtube.com/watch?v=vP4iY1TtS3s",
|
| 92 |
+
"We choose to go to the Moon - JFK": "https://www.youtube.com/watch?v=WZyRbnpGyzQ",
|
| 93 |
+
"Steve Jobs' 2005 Stanford Commencement Address": "https://www.youtube.com/watch?v=UF8uR6Z6KLc",
|
| 94 |
+
"Elon Musk's Starship Update Presentation": "https://www.youtube.com/watch?v=sOpMrVnjYeY",
|
| 95 |
+
"The Future of Humanity - Michio Kaku": "https://www.youtube.com/watch?v=zTkf2sCjYrU"
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
# Create the Gradio interface
|
| 99 |
+
video_source = gr.Radio(["Example YouTube Presentations", "Custom YouTube URL", "Upload Video File"], label="Select Video Source")
|
| 100 |
+
example_choice = gr.Dropdown(list(example_videos.keys()), label="Example YouTube Presentations")
|
| 101 |
+
youtube_url = gr.Textbox(label="Enter YouTube Video URL")
|
| 102 |
+
video_file = gr.Video(label="Upload Video File") # Corrected, without 'type' parameter
|
| 103 |
+
output_text = gr.Textbox(label="Transcription Output")
|
| 104 |
+
|
| 105 |
iface = gr.Interface(
|
| 106 |
+
fn=transcribe_video,
|
| 107 |
+
inputs=[video_source, example_choice, youtube_url, video_file],
|
| 108 |
+
outputs=output_text,
|
| 109 |
+
title="YouTube Video Transcription",
|
| 110 |
+
description="Transcribe YouTube videos using Whisper or YouTube Transcript API."
|
| 111 |
)
|
| 112 |
|
| 113 |
+
# Launch the interface
|
| 114 |
+
iface.launch(debug=True, share=True)
|