added some try except blocks and remove file after transcript is finished.
This commit is contained in:
13
main.py
13
main.py
@@ -1,6 +1,3 @@
|
||||
# Youtube Video Summarizer
|
||||
# Steps: Download audio of youtube video, transcribe text using openaimodels, from text use chatgpt models to summarize the content.
|
||||
|
||||
import os
|
||||
from openai import OpenAI
|
||||
from pytube import YouTube
|
||||
@@ -17,6 +14,7 @@ def download_audio(video_url, output_filename='audio.mp4'):
|
||||
return None
|
||||
|
||||
def transcription(OPENAI_API_KEY, audio_filename):
|
||||
try:
|
||||
client = OpenAI(api_key=OPENAI_API_KEY)
|
||||
|
||||
audio_file = open(audio_filename, "rb")
|
||||
@@ -32,9 +30,15 @@ def transcription(OPENAI_API_KEY, audio_filename):
|
||||
|
||||
print("Transcription finished")
|
||||
|
||||
os.remove(audio_filename)
|
||||
|
||||
return transcript
|
||||
except Exception as e:
|
||||
print(f"Failed to transcript audio: {e}")
|
||||
return None
|
||||
|
||||
def summarize(OPENAI_API_KEY, transcript):
|
||||
try:
|
||||
client = OpenAI(api_key=OPENAI_API_KEY)
|
||||
stream = client.chat.completions.create(
|
||||
model="gpt-4",
|
||||
@@ -48,6 +52,9 @@ def summarize(OPENAI_API_KEY, transcript):
|
||||
for chunk in stream:
|
||||
if chunk.choices[0].delta.content is not None:
|
||||
print(chunk.choices[0].delta.content, end="", flush=True)
|
||||
except Exception as e:
|
||||
print(f"Failed to summarize transcript: {e}")
|
||||
return None
|
||||
|
||||
def main():
|
||||
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
||||
|
||||
Reference in New Issue
Block a user