formating
This commit is contained in:
25
main.py
25
main.py
@@ -6,7 +6,9 @@ from pytube import YouTube
|
|||||||
|
|
||||||
def get_api_key():
|
def get_api_key():
|
||||||
try:
|
try:
|
||||||
|
print("Retreiving OpenAI API Key...")
|
||||||
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
||||||
|
print(f"Retreived OpenAI API Key.")
|
||||||
return OPENAI_API_KEY
|
return OPENAI_API_KEY
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to retrieve OpenAI API Key: {e}")
|
print(f"Failed to retrieve OpenAI API Key: {e}")
|
||||||
@@ -19,7 +21,7 @@ def download(video_url, output_filename='audio.mp4'):
|
|||||||
yt = YouTube(video_url)
|
yt = YouTube(video_url)
|
||||||
audio_stream = yt.streams.get_audio_only()
|
audio_stream = yt.streams.get_audio_only()
|
||||||
audio_filename = audio_stream.download(filename=output_filename)
|
audio_filename = audio_stream.download(filename=output_filename)
|
||||||
print(f"Downloaded '{yt.title}' audio.")
|
print(f"Downloaded audio.")
|
||||||
return audio_filename
|
return audio_filename
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to download audio: {e}")
|
print(f"Failed to download audio: {e}")
|
||||||
@@ -38,7 +40,7 @@ def transcription(OPENAI_API_KEY, audio_filename):
|
|||||||
model="whisper-1",
|
model="whisper-1",
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Transcription finished.")
|
print("Transcripted audio.")
|
||||||
return transcript
|
return transcript
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to transcript audio: {e}")
|
print(f"Failed to transcript audio: {e}")
|
||||||
@@ -52,7 +54,7 @@ def summary(OPENAI_API_KEY, transcript):
|
|||||||
stream = client.chat.completions.create(
|
stream = client.chat.completions.create(
|
||||||
model="gpt-4",
|
model="gpt-4",
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "system", "content": "The prompt you will receive will be the transcript of a youtube video, your objective is to summarize the content of that transcript to the best of your ability, keep in mind there may be music or other parasit noises in the transcript."},
|
{"role": "system", "content": "The prompt you will receive will be the transcript of a youtube video, your objective is to summarize the content of that transcript to the best of your ability, keep in mind there may be music or other parasite noises in the transcript."},
|
||||||
{"role": "user", "content": str(transcript)}
|
{"role": "user", "content": str(transcript)}
|
||||||
],
|
],
|
||||||
stream=True,
|
stream=True,
|
||||||
@@ -62,7 +64,7 @@ def summary(OPENAI_API_KEY, transcript):
|
|||||||
if chunk.choices[0].delta.content is not None:
|
if chunk.choices[0].delta.content is not None:
|
||||||
print(chunk.choices[0].delta.content, end="", flush=True)
|
print(chunk.choices[0].delta.content, end="", flush=True)
|
||||||
|
|
||||||
print("\nSummarizing finished.")
|
print("\nSummarized transcript.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to summarize transcript: {e}")
|
print(f"Failed to summarize transcript: {e}")
|
||||||
return None
|
return None
|
||||||
@@ -77,17 +79,18 @@ def cleanup(audio_filename):
|
|||||||
print(f"Failed to delete audio: {e}")
|
print(f"Failed to delete audio: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Main function to orchestrate the download, transcription, summarization, and cleanup process.
|
# Main function to orchestrate the API Key retreival, download, transcription, summarization, and cleanup process.
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
OPENAI_API_KEY = get_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)
|
if OPENAI_API_KEY is not None:
|
||||||
if audio_filename is not None:
|
audio_filename = download(video_url)
|
||||||
transcript = transcription(OPENAI_API_KEY, audio_filename)
|
if audio_filename is not None:
|
||||||
if transcript is not None:
|
transcript = transcription(OPENAI_API_KEY, audio_filename)
|
||||||
summary(OPENAI_API_KEY, transcript)
|
if transcript is not None:
|
||||||
cleanup(audio_filename)
|
summary(OPENAI_API_KEY, transcript)
|
||||||
|
cleanup(audio_filename)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
Reference in New Issue
Block a user