-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
95 lines (91 loc) · 3.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
from openai import Audio, Completion
import openai
from pytube import YouTube
import moviepy.editor as mp
openai.api_key=os.environ.get('OPENAI_API_KEY')
VIDEO_PATH='videos'
AUDIO_PATH='audios'
def download_video(url):
try:
# create a directory to store the video
if not os.path.exists("videos"):
os.mkdir("videos")
# create youtube object
yt = YouTube(url)
video = yt.streams.filter(only_audio=True).first()
out_file= video.download(output_path=VIDEO_PATH)
# get the video name
out_file = os.path.basename(out_file)
new_out_file=out_file.replace(" ", "_")
# remove whitespace from video name
if os.path.exists(os.path.join(VIDEO_PATH,new_out_file)) is False:
os.rename(os.path.join(VIDEO_PATH,out_file),os.path.join(VIDEO_PATH,new_out_file))
# return the video name
return new_out_file
except Exception as e:
print(e)
def convert_video_to_audio(video_name):
try:
if os.path.exists(AUDIO_PATH) is False:
os.mkdir(AUDIO_PATH)
# create audio file name
audio_file_name =os.path.join(AUDIO_PATH,video_name.split(".")[0] + ".mp3")
# convert video to audio
print(video_name)
print(audio_file_name)
clip = mp.AudioFileClip(os.path.join(VIDEO_PATH,video_name))
clip.write_audiofile(audio_file_name)
clip.close()
# remove video file
if os.path.exists(os.path.join(VIDEO_PATH,video_name)):
os.remove(os.path.join(VIDEO_PATH,video_name))
# return audio file name
return audio_file_name
except Exception as e:
print(e)
return None
def get_transcript(audio_file_name):
try:
print(audio_file_name)
# get transcript
audio_file= open(audio_file_name, "rb")
transcript = Audio.translate("whisper-1", audio_file)
audio_file.close()
if os.path.exists(audio_file_name):
os.remove(audio_file_name)
return True,transcript['text']
except openai.error.AuthenticationError as e:
return False,'Invalid OpenAI API Key'
except openai.error.PermissionError as e:
return False,'Your OpenAI API Key does not have access to this model'
except openai.error.RateLimitError as e:
return False,'OpenAI API Key Rate Limit Reached'
except Exception as e:
print(e)
return False,None
def summarize_video(transcript):
if transcript is None:
return False,None
length= len(transcript.split())
if length > 2500:
transcript=' '.join(transcript.split()[:2500])
try:
response = Completion.create(
model="text-davinci-003",
prompt=f"{transcript} tl;dr:",
temperature=0.91,
max_tokens=500,
top_p=0.8,
frequency_penalty=0.1,
presence_penalty=0.1)
return True,response.choices[0].text.strip()
except openai.error.AuthenticationError as e:
return False,'Invalid OpenAI API Key'
except openai.error.PermissionError as e:
return False,'Your OpenAI API Key does not have access to this model'
except openai.error.RateLimitError as e:
return False,'OpenAI API Key Rate Limit Reached'
except Exception as e:
print(e)
return False, None