added some try except blocks and remove file after transcript is finished.
This commit is contained in:
61
main.py
61
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
|
import os
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
from pytube import YouTube
|
from pytube import YouTube
|
||||||
@@ -17,37 +14,47 @@ def download_audio(video_url, output_filename='audio.mp4'):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def transcription(OPENAI_API_KEY, audio_filename):
|
def transcription(OPENAI_API_KEY, audio_filename):
|
||||||
client = OpenAI(api_key=OPENAI_API_KEY)
|
try:
|
||||||
|
client = OpenAI(api_key=OPENAI_API_KEY)
|
||||||
|
|
||||||
audio_file = open(audio_filename, "rb")
|
audio_file = open(audio_filename, "rb")
|
||||||
|
|
||||||
transcript = client.audio.transcriptions.create(
|
transcript = client.audio.transcriptions.create(
|
||||||
file=audio_file,
|
file=audio_file,
|
||||||
model="whisper-1",
|
model="whisper-1",
|
||||||
language="en",
|
language="en",
|
||||||
prompt="",
|
prompt="",
|
||||||
response_format="json",
|
response_format="json",
|
||||||
temperature=0.0
|
temperature=0.0
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Transcription finished")
|
print("Transcription finished")
|
||||||
|
|
||||||
return transcript
|
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):
|
def summarize(OPENAI_API_KEY, transcript):
|
||||||
client = OpenAI(api_key=OPENAI_API_KEY)
|
try:
|
||||||
stream = client.chat.completions.create(
|
client = OpenAI(api_key=OPENAI_API_KEY)
|
||||||
model="gpt-4",
|
stream = client.chat.completions.create(
|
||||||
messages=[
|
model="gpt-4",
|
||||||
{"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."},
|
messages=[
|
||||||
{"role": "user", "content": str(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 parasit noises in the transcript."},
|
||||||
],
|
{"role": "user", "content": str(transcript)}
|
||||||
stream=True,
|
],
|
||||||
)
|
stream=True,
|
||||||
|
)
|
||||||
|
|
||||||
for chunk in stream:
|
for chunk in stream:
|
||||||
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)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to summarize transcript: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
||||||
|
|||||||
Reference in New Issue
Block a user