Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,12 @@ youtube_api_key = os.getenv("YOUTUBE_API_KEY")
|
|
| 18 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
openai.api_key = openai_api_key
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def extract_video_id(url):
|
| 22 |
"""Extracts the video ID from a YouTube URL."""
|
| 23 |
try:
|
|
@@ -51,6 +57,7 @@ def get_video_duration(video_id, api_key):
|
|
| 51 |
return None
|
| 52 |
|
| 53 |
def download_and_transcribe_with_whisper(youtube_url):
|
|
|
|
| 54 |
try:
|
| 55 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 56 |
temp_audio_file = os.path.join(temp_dir, "audio.mp3")
|
|
@@ -58,8 +65,11 @@ def download_and_transcribe_with_whisper(youtube_url):
|
|
| 58 |
ydl_opts = {
|
| 59 |
'format': 'bestaudio/best',
|
| 60 |
'outtmpl': temp_audio_file,
|
| 61 |
-
'
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
# Download audio using yt-dlp
|
|
@@ -132,6 +142,7 @@ def summarize_text_huggingface(text):
|
|
| 132 |
return " ".join(summaries)
|
| 133 |
|
| 134 |
def generate_optimized_content(summarized_transcript):
|
|
|
|
| 135 |
prompt = f"""
|
| 136 |
Analyze the following summarized YouTube video transcript and:
|
| 137 |
1. Extract the top 10 keywords.
|
|
@@ -163,11 +174,11 @@ def generate_optimized_content(summarized_transcript):
|
|
| 163 |
except Exception as e:
|
| 164 |
return {"error": str(e)}
|
| 165 |
|
| 166 |
-
|
| 167 |
def process_video(youtube_url):
|
|
|
|
| 168 |
transcript = get_transcript(youtube_url)
|
| 169 |
if not transcript:
|
| 170 |
-
return "Could not fetch the transcript. Please try another video."
|
| 171 |
|
| 172 |
summary = summarize_text_huggingface(transcript)
|
| 173 |
optimized_content = generate_optimized_content(summary)
|
|
|
|
| 18 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
openai.api_key = openai_api_key
|
| 20 |
|
| 21 |
+
if not youtube_api_key:
|
| 22 |
+
raise ValueError("YOUTUBE_API_KEY is not set. Please set it as an environment variable.")
|
| 23 |
+
|
| 24 |
+
if not openai_api_key:
|
| 25 |
+
raise ValueError("OPENAI_API_KEY is not set. Please set it as an environment variable.")
|
| 26 |
+
|
| 27 |
def extract_video_id(url):
|
| 28 |
"""Extracts the video ID from a YouTube URL."""
|
| 29 |
try:
|
|
|
|
| 57 |
return None
|
| 58 |
|
| 59 |
def download_and_transcribe_with_whisper(youtube_url):
|
| 60 |
+
"""Downloads audio from YouTube and transcribes it using Whisper."""
|
| 61 |
try:
|
| 62 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 63 |
temp_audio_file = os.path.join(temp_dir, "audio.mp3")
|
|
|
|
| 65 |
ydl_opts = {
|
| 66 |
'format': 'bestaudio/best',
|
| 67 |
'outtmpl': temp_audio_file,
|
| 68 |
+
'postprocessors': [{
|
| 69 |
+
'key': 'FFmpegExtractAudio',
|
| 70 |
+
'preferredcodec': 'mp3',
|
| 71 |
+
'preferredquality': '192',
|
| 72 |
+
}],
|
| 73 |
}
|
| 74 |
|
| 75 |
# Download audio using yt-dlp
|
|
|
|
| 142 |
return " ".join(summaries)
|
| 143 |
|
| 144 |
def generate_optimized_content(summarized_transcript):
|
| 145 |
+
"""Generates optimized video metadata using OpenAI's GPT model."""
|
| 146 |
prompt = f"""
|
| 147 |
Analyze the following summarized YouTube video transcript and:
|
| 148 |
1. Extract the top 10 keywords.
|
|
|
|
| 174 |
except Exception as e:
|
| 175 |
return {"error": str(e)}
|
| 176 |
|
|
|
|
| 177 |
def process_video(youtube_url):
|
| 178 |
+
"""Processes a YouTube URL to generate optimized metadata."""
|
| 179 |
transcript = get_transcript(youtube_url)
|
| 180 |
if not transcript:
|
| 181 |
+
return {"error": "Could not fetch the transcript. Please try another video."}
|
| 182 |
|
| 183 |
summary = summarize_text_huggingface(transcript)
|
| 184 |
optimized_content = generate_optimized_content(summary)
|