Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,7 +34,7 @@ print("Summarization model loaded.")
|
|
| 34 |
def download_and_extract_audio(youtube_url):
|
| 35 |
"""
|
| 36 |
Downloads a YouTube video and extracts its audio.
|
| 37 |
-
Returns
|
| 38 |
"""
|
| 39 |
video_id = youtube_url.split("v=")[-1].split("&")[0] # Extract video ID
|
| 40 |
audio_path = f"/tmp/{video_id}.mp3"
|
|
@@ -66,10 +66,15 @@ def download_and_extract_audio(youtube_url):
|
|
| 66 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 67 |
ydl.download([youtube_url])
|
| 68 |
print("Audio download and extraction complete.")
|
| 69 |
-
return audio_path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
except Exception as e:
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
def transcribe_audio(audio_file_path):
|
| 75 |
"""
|
|
@@ -109,9 +114,11 @@ def process_youtube_video(youtube_url):
|
|
| 109 |
Main function to process the YouTube video: download, transcribe, and summarize.
|
| 110 |
"""
|
| 111 |
# 1. Download and Extract Audio
|
| 112 |
-
audio_file_path = download_and_extract_audio(youtube_url)
|
|
|
|
|
|
|
| 113 |
if not audio_file_path or not os.path.exists(audio_file_path):
|
| 114 |
-
return "Failed to download or extract audio.", "N/A"
|
| 115 |
|
| 116 |
# 2. Transcribe Audio
|
| 117 |
transcript = transcribe_audio(audio_file_path)
|
|
@@ -139,7 +146,8 @@ iface = gr.Interface(
|
|
| 139 |
"Enter a YouTube video URL, and this tool will download its audio, "
|
| 140 |
"transcribe it using OpenAI Whisper, and then generate a summary/notes."
|
| 141 |
"<br><b>Note:</b> If you encounter download issues (e.g., 'Sign in to confirm you’re not a bot'), "
|
| 142 |
-
"ensure you have uploaded a `cookies.txt` file (exported from your browser) to the root of this Hugging Face Space."
|
|
|
|
| 143 |
"<br><b>Performance:</b> Analyzing long videos (e.g., 1 hour+) can take a significant amount of time "
|
| 144 |
"and consume considerable resources, especially on CPU-only Hugging Face Spaces. "
|
| 145 |
"For faster processing of long videos, consider upgrading your Hugging Face Space to a GPU instance."
|
|
|
|
| 34 |
def download_and_extract_audio(youtube_url):
|
| 35 |
"""
|
| 36 |
Downloads a YouTube video and extracts its audio.
|
| 37 |
+
Returns a tuple: (path to extracted audio file or None, error message or None).
|
| 38 |
"""
|
| 39 |
video_id = youtube_url.split("v=")[-1].split("&")[0] # Extract video ID
|
| 40 |
audio_path = f"/tmp/{video_id}.mp3"
|
|
|
|
| 66 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 67 |
ydl.download([youtube_url])
|
| 68 |
print("Audio download and extraction complete.")
|
| 69 |
+
return audio_path, None
|
| 70 |
+
except yt_dlp.utils.DownloadError as e:
|
| 71 |
+
error_message = f"Download Error: {e.exc_info[1].msg if e.exc_info else str(e)}"
|
| 72 |
+
print(error_message)
|
| 73 |
+
return None, error_message
|
| 74 |
except Exception as e:
|
| 75 |
+
error_message = f"An unexpected error occurred during download: {str(e)}"
|
| 76 |
+
print(error_message)
|
| 77 |
+
return None, error_message
|
| 78 |
|
| 79 |
def transcribe_audio(audio_file_path):
|
| 80 |
"""
|
|
|
|
| 114 |
Main function to process the YouTube video: download, transcribe, and summarize.
|
| 115 |
"""
|
| 116 |
# 1. Download and Extract Audio
|
| 117 |
+
audio_file_path, download_error = download_and_extract_audio(youtube_url)
|
| 118 |
+
if download_error:
|
| 119 |
+
return f"Failed to download or extract audio: {download_error}", "N/A"
|
| 120 |
if not audio_file_path or not os.path.exists(audio_file_path):
|
| 121 |
+
return "Failed to download or extract audio due to an unknown reason.", "N/A"
|
| 122 |
|
| 123 |
# 2. Transcribe Audio
|
| 124 |
transcript = transcribe_audio(audio_file_path)
|
|
|
|
| 146 |
"Enter a YouTube video URL, and this tool will download its audio, "
|
| 147 |
"transcribe it using OpenAI Whisper, and then generate a summary/notes."
|
| 148 |
"<br><b>Note:</b> If you encounter download issues (e.g., 'Sign in to confirm you’re not a bot'), "
|
| 149 |
+
"ensure you have uploaded a `cookies.txt` file (exported from your browser) to the root of this Hugging Face Space. "
|
| 150 |
+
"You can typically export cookies using browser extensions like 'Get cookies.txt' or similar."
|
| 151 |
"<br><b>Performance:</b> Analyzing long videos (e.g., 1 hour+) can take a significant amount of time "
|
| 152 |
"and consume considerable resources, especially on CPU-only Hugging Face Spaces. "
|
| 153 |
"For faster processing of long videos, consider upgrading your Hugging Face Space to a GPU instance."
|