From 1f0c3b715f687c5aa1608d0c572426bf02450eb7 Mon Sep 17 00:00:00 2001 From: Leonard Excoffier Date: Tue, 13 Feb 2024 23:53:27 -0500 Subject: [PATCH] added error handling for api key missing --- main.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 9e13f81..7832c3d 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,14 @@ import os from openai import OpenAI from pytube import YouTube +def get_api_key(): + try: + OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') + return OPENAI_API_KEY + except Exception as e: + print(f"Failed to retrieve OpenAI API Key: {e}") + return None + # Downloads the audio from a YouTube video. def download(video_url, output_filename='audio.mp4'): try: @@ -71,14 +79,18 @@ def cleanup(audio_filename): # Main function to orchestrate the download, transcription, summarization, and cleanup process. def main(): try: - OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') - video_url = str(input("video url:")) + OPENAI_API_KEY = get_api_key() + video_url = str(input("Video url: ")) audio_filename = download(video_url) - transcript = transcription(OPENAI_API_KEY, audio_filename) - summary(OPENAI_API_KEY, transcript) - cleanup(audio_filename) + if audio_filename is not None: + transcript = transcription(OPENAI_API_KEY, audio_filename) + if transcript is not None: + summary(OPENAI_API_KEY, transcript) + cleanup(audio_filename) except Exception as e: print(e) + finally: + input("Press Enter to exit...") if __name__ == "__main__": main()