added error handling for api key missing

This commit is contained in:
2024-02-13 23:53:27 -05:00
parent 50576d2c37
commit 1f0c3b715f

22
main.py
View File

@@ -3,6 +3,14 @@ import os
from openai import OpenAI from openai import OpenAI
from pytube import YouTube 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. # Downloads the audio from a YouTube video.
def download(video_url, output_filename='audio.mp4'): def download(video_url, output_filename='audio.mp4'):
try: try:
@@ -71,14 +79,18 @@ def cleanup(audio_filename):
# Main function to orchestrate the download, transcription, summarization, and cleanup process. # Main function to orchestrate the download, transcription, summarization, and cleanup process.
def main(): def main():
try: try:
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') OPENAI_API_KEY = get_api_key()
video_url = str(input("video url:")) video_url = str(input("Video url: "))
audio_filename = download(video_url) audio_filename = download(video_url)
transcript = transcription(OPENAI_API_KEY, audio_filename) if audio_filename is not None:
summary(OPENAI_API_KEY, transcript) transcript = transcription(OPENAI_API_KEY, audio_filename)
cleanup(audio_filename) if transcript is not None:
summary(OPENAI_API_KEY, transcript)
cleanup(audio_filename)
except Exception as e: except Exception as e:
print(e) print(e)
finally:
input("Press Enter to exit...")
if __name__ == "__main__": if __name__ == "__main__":
main() main()