Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,23 +13,28 @@ from selenium import webdriver
|
|
| 13 |
from selenium.webdriver.chrome.options import Options
|
| 14 |
from selenium.webdriver.chrome.service import Service
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def get_video_url(page_url):
|
| 17 |
"""
|
| 18 |
Uses Selenium in headless mode to load the page and extract the video URL from a <video> element.
|
| 19 |
Adjust the element-finding logic if the video is embedded differently.
|
| 20 |
"""
|
| 21 |
-
# Set Chrome options for headless operation.
|
| 22 |
chrome_options = Options()
|
| 23 |
chrome_options.add_argument("--headless")
|
| 24 |
chrome_options.add_argument("--no-sandbox")
|
| 25 |
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 26 |
-
#
|
| 27 |
chrome_options.binary_location = "/usr/bin/chromium-browser"
|
| 28 |
-
|
| 29 |
-
# Point to the apt-installed ChromiumDriver
|
| 30 |
service = Service("/usr/bin/chromedriver")
|
| 31 |
|
| 32 |
-
# Initialize Chrome driver
|
| 33 |
driver = webdriver.Chrome(service=service, options=chrome_options)
|
| 34 |
driver.get(page_url)
|
| 35 |
|
|
@@ -48,7 +53,7 @@ def get_video_url(page_url):
|
|
| 48 |
|
| 49 |
def download_video(video_url, output_path):
|
| 50 |
"""
|
| 51 |
-
Downloads the video from the extracted URL to a local file.
|
| 52 |
"""
|
| 53 |
response = requests.get(video_url, stream=True)
|
| 54 |
if response.status_code != 200:
|
|
@@ -62,15 +67,15 @@ def download_video(video_url, output_path):
|
|
| 62 |
|
| 63 |
def extract_audio(video_file, audio_file):
|
| 64 |
"""
|
| 65 |
-
Uses FFmpeg
|
| 66 |
"""
|
| 67 |
command = [
|
| 68 |
"ffmpeg",
|
| 69 |
"-i", video_file, # Input video file
|
| 70 |
-
"-vn", # Disable video
|
| 71 |
"-acodec", "pcm_s16le", # Audio codec for WAV
|
| 72 |
-
"-ar", "44100", #
|
| 73 |
-
"-ac", "2", #
|
| 74 |
audio_file
|
| 75 |
]
|
| 76 |
try:
|
|
@@ -85,8 +90,7 @@ def transcribe_audio(audio_file):
|
|
| 85 |
"""
|
| 86 |
model = whisper.load_model("base")
|
| 87 |
result = model.transcribe(audio_file)
|
| 88 |
-
|
| 89 |
-
return transcription
|
| 90 |
|
| 91 |
def summarize_text(transcription, openai_api_key, model_name="text-davinci-003"):
|
| 92 |
"""
|
|
@@ -104,74 +108,23 @@ def summarize_text(transcription, openai_api_key, model_name="text-davinci-003")
|
|
| 104 |
max_tokens=150,
|
| 105 |
temperature=0.5
|
| 106 |
)
|
| 107 |
-
|
| 108 |
-
return summary
|
| 109 |
|
| 110 |
def process_page(page_url, openai_api_key):
|
| 111 |
"""
|
| 112 |
-
|
| 113 |
-
1.
|
| 114 |
-
2.
|
| 115 |
-
3.
|
| 116 |
-
4.
|
| 117 |
-
5.
|
|
|
|
| 118 |
|
| 119 |
-
Returns
|
| 120 |
"""
|
| 121 |
try:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
video_file = video_temp.name
|
| 128 |
-
video_temp.close()
|
| 129 |
-
|
| 130 |
-
# Download the video.
|
| 131 |
-
download_video(video_url, video_file)
|
| 132 |
-
|
| 133 |
-
# 3. Create a temporary file for the audio.
|
| 134 |
-
audio_temp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
| 135 |
-
audio_file = audio_temp.name
|
| 136 |
-
audio_temp.close()
|
| 137 |
-
|
| 138 |
-
# Extract audio from the video.
|
| 139 |
-
extract_audio(video_file, audio_file)
|
| 140 |
-
|
| 141 |
-
# 4. Transcribe the audio.
|
| 142 |
-
transcription = transcribe_audio(audio_file)
|
| 143 |
-
|
| 144 |
-
# 5. Summarize the transcription.
|
| 145 |
-
summary = summarize_text(transcription, openai_api_key)
|
| 146 |
-
|
| 147 |
-
# Clean up temporary files.
|
| 148 |
-
os.remove(video_file)
|
| 149 |
-
os.remove(audio_file)
|
| 150 |
-
|
| 151 |
-
return video_url, transcription, summary
|
| 152 |
-
except Exception as e:
|
| 153 |
-
return "Error: " + str(e), "", ""
|
| 154 |
-
|
| 155 |
-
# Create a Gradio interface for the HF Spaces app.
|
| 156 |
-
interface = gr.Interface(
|
| 157 |
-
fn=process_page,
|
| 158 |
-
inputs=[
|
| 159 |
-
gr.Textbox(label="Course Page URL", placeholder="Enter the deeplearning.ai course page URL here..."),
|
| 160 |
-
gr.Textbox(label="OpenAI API Key", type="password", placeholder="Enter your OpenAI API key")
|
| 161 |
-
],
|
| 162 |
-
outputs=[
|
| 163 |
-
gr.Textbox(label="Extracted Video URL"),
|
| 164 |
-
gr.Textbox(label="Transcription"),
|
| 165 |
-
gr.Textbox(label="Summary")
|
| 166 |
-
],
|
| 167 |
-
title="Video Analyzer",
|
| 168 |
-
description=(
|
| 169 |
-
"Enter a page URL with embedded video or video URL and your OpenAI API key. "
|
| 170 |
-
"The app will scrape the page to find the embedded video, download it, "
|
| 171 |
-
"extract the audio, transcribe the speech using Whisper, and summarize the content using GPT.\n\n"
|
| 172 |
-
"Note: Ensure FFmpeg, Chromium, and ChromiumDriver are installed via apt.txt."
|
| 173 |
-
)
|
| 174 |
-
)
|
| 175 |
-
|
| 176 |
-
if __name__ == "__main__":
|
| 177 |
-
interface.launch()
|
|
|
|
| 13 |
from selenium.webdriver.chrome.options import Options
|
| 14 |
from selenium.webdriver.chrome.service import Service
|
| 15 |
|
| 16 |
+
def is_direct_video_url(url: str):
|
| 17 |
+
"""
|
| 18 |
+
Naive check if the user input is a direct link to a video file.
|
| 19 |
+
You can expand this list as needed (e.g. .mp4, .webm, .mov, .avi).
|
| 20 |
+
"""
|
| 21 |
+
video_extensions = (".mp4", ".webm", ".mov", ".avi", ".mkv")
|
| 22 |
+
return url.lower().endswith(video_extensions)
|
| 23 |
+
|
| 24 |
def get_video_url(page_url):
|
| 25 |
"""
|
| 26 |
Uses Selenium in headless mode to load the page and extract the video URL from a <video> element.
|
| 27 |
Adjust the element-finding logic if the video is embedded differently.
|
| 28 |
"""
|
|
|
|
| 29 |
chrome_options = Options()
|
| 30 |
chrome_options.add_argument("--headless")
|
| 31 |
chrome_options.add_argument("--no-sandbox")
|
| 32 |
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 33 |
+
# Location of Chromium browser
|
| 34 |
chrome_options.binary_location = "/usr/bin/chromium-browser"
|
| 35 |
+
# Location of the matching Chromedriver
|
|
|
|
| 36 |
service = Service("/usr/bin/chromedriver")
|
| 37 |
|
|
|
|
| 38 |
driver = webdriver.Chrome(service=service, options=chrome_options)
|
| 39 |
driver.get(page_url)
|
| 40 |
|
|
|
|
| 53 |
|
| 54 |
def download_video(video_url, output_path):
|
| 55 |
"""
|
| 56 |
+
Downloads the video from the extracted URL (or direct link) to a local file.
|
| 57 |
"""
|
| 58 |
response = requests.get(video_url, stream=True)
|
| 59 |
if response.status_code != 200:
|
|
|
|
| 67 |
|
| 68 |
def extract_audio(video_file, audio_file):
|
| 69 |
"""
|
| 70 |
+
Uses FFmpeg to extract the audio track from the video.
|
| 71 |
"""
|
| 72 |
command = [
|
| 73 |
"ffmpeg",
|
| 74 |
"-i", video_file, # Input video file
|
| 75 |
+
"-vn", # Disable video output
|
| 76 |
"-acodec", "pcm_s16le", # Audio codec for WAV
|
| 77 |
+
"-ar", "44100", # Sample rate
|
| 78 |
+
"-ac", "2", # Stereo channels
|
| 79 |
audio_file
|
| 80 |
]
|
| 81 |
try:
|
|
|
|
| 90 |
"""
|
| 91 |
model = whisper.load_model("base")
|
| 92 |
result = model.transcribe(audio_file)
|
| 93 |
+
return result["text"]
|
|
|
|
| 94 |
|
| 95 |
def summarize_text(transcription, openai_api_key, model_name="text-davinci-003"):
|
| 96 |
"""
|
|
|
|
| 108 |
max_tokens=150,
|
| 109 |
temperature=0.5
|
| 110 |
)
|
| 111 |
+
return response.choices[0].text.strip()
|
|
|
|
| 112 |
|
| 113 |
def process_page(page_url, openai_api_key):
|
| 114 |
"""
|
| 115 |
+
Main function that:
|
| 116 |
+
1. Checks if user input is direct video URL or a page.
|
| 117 |
+
2. Scrapes for video URL if needed.
|
| 118 |
+
3. Downloads the video.
|
| 119 |
+
4. Extracts the audio using FFmpeg.
|
| 120 |
+
5. Transcribes audio with Whisper.
|
| 121 |
+
6. Summarizes via OpenAI.
|
| 122 |
|
| 123 |
+
Returns (video_url, transcription, summary).
|
| 124 |
"""
|
| 125 |
try:
|
| 126 |
+
if is_direct_video_url(page_url):
|
| 127 |
+
# The user provided a direct video link; no scraping needed
|
| 128 |
+
video_url = page_url
|
| 129 |
+
else:
|
| 130 |
+
# The user provided a page URL,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|