-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
112 lines (80 loc) · 2.38 KB
/
app.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from youtube_transcript_api import YouTubeTranscriptApi
from openai import OpenAI
import argparse
def parse_args():
"""
A function to parse command-line arguments for the API key, model, and URL.
Returns the parsed arguments.
Returns:
argparse.Namespace: The parsed arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
dest = "api_key",
type = str,
help = "OpenAI api key or Deepseek coder api key"
)
parser.add_argument(
dest = "model",
type = str,
help = "OpenAI model to use for summarization"
)
parser.add_argument(
dest = "url",
type = str,
help = "YouTube video URL"
)
return parser.parse_args()
args = parse_args()
api_key = args.api_key
url = args.url
model = args.model
def youtube_url(url):
"""
Function to retrieve the transcript of a YouTube video based on the provided URL.
Parameters:
url (str): The URL of the YouTube video.
Returns:
str: The transcript of the video.
"""
transcript = ""
list=url.split("=")
video_id = list[1]
list = YouTubeTranscriptApi.get_transcript(video_id, languages=['tr', 'en',"de"])
for dict in list:
transcript += dict["text"] + "\n"
return transcript
def summarization(prompt):
"""
This function takes a prompt as input and uses the OpenAI API to generate a chat completion based on the prompt. It returns the summary of the chat completion.
"""
system_msg = "you are a youtube transcript summarizer."
if model == "deepseek-chat":
base_url = "https://api.deepseek.com/v1"
else:
base_url = None
client = OpenAI(api_key = api_key, base_url=base_url)
completion = client.chat.completions.create(
model = model,
messages=[
{"role": "system", "content": system_msg},
{"role": "user", "content": prompt}
]
)
summary = completion.choices[0].message.content
return summary
def main(url):
"""
Function to
summarize the transcript of a YouTube video using the provided URL.
Parameters:
url (str): The URL of the YouTube video.
Returns:
str: The summary of the video transcript.
"""
transcript = youtube_url(url)
query=f"summarize this {transcript} transcript"
summary = summarization(query)
return summary
if __name__ == "__main__":
print(main(url))