-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFlyteLatestYouTubeSummary.py
152 lines (123 loc) · 4.96 KB
/
getFlyteLatestYouTubeSummary.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from typing import List
import flytekit
from flytekit import ImageSpec, Secret, dynamic, task, workflow
from flytekitplugins.chatgpt import ChatGPTTask
flytekit_master = "git+https://github.com/flyteorg/flytekit.git@master"
chatgpt_plugin = "git+https://github.com/flyteorg/flytekit.git@master#subdirectory=plugins/flytekit-openai"
image = ImageSpec(
apt_packages=["git"],
packages=[
flytekit_master,
chatgpt_plugin,
"scrapetube==2.5.1",
"youtube_transcript_api==0.6.1",
"slack_sdk==3.23.0",
],
registry="futureoutlier",
)
chatgpt_job = ChatGPTTask(
name="3.5-turbo",
openai_organization="org-NayNG68kGnVXMJ8Ak4PMgQv7",
chatgpt_config={
"model": "gpt-3.5-turbo",
"temperature": 0.7,
},
)
@task(
container_image=image,
secret_requests=[Secret(key="token", group="slack-api")],
)
def post_message_on_slack(message: str):
if message == "":
return
from slack_sdk import WebClient
token = flytekit.current_context().secrets.get("slack-api", "token")
client = WebClient(token=token)
client.chat_postMessage(channel="youtube-summary", text=message)
@task(container_image=image)
def get_latest_video_transcript_chunks(channel_url: str) -> List[str]:
import scrapetube
from youtube_transcript_api import YouTubeTranscriptApi
# fetch_latest_video_id
video_generator = scrapetube.get_channel(channel_url=channel_url)
latest_video = next(video_generator)
video_id = latest_video["videoId"]
# fetch_transcript
transcript = YouTubeTranscriptApi.get_transcript(video_id)
# chunk_transcript
text_transcript = "\n".join([entry["text"] for entry in transcript])
return [
text_transcript[i : i + 10000] for i in range(0, len(text_transcript), 10000)
]
@workflow
def wf(channel_url: str):
chunks = get_latest_video_transcript_chunks(channel_url=channel_url)
dynamic_subwf(channel_url=channel_url, chunks=chunks)
@task(container_image=image)
def check_strs_len_less_than_num(msg1: str, msg2: str, num: int) -> bool:
return len(msg1) + len(msg2) < num
@task(container_image=image)
def concatenate_str(msg1: str, msg2: str) -> str:
return msg1 + msg2 + "\n"
@task(container_image=image)
def str_is_non_empty(msg: str) -> bool:
return len(msg) == 0
@dynamic(container_image=image)
def dynamic_subwf(channel_url: str, chunks: List[str]):
post_message_on_slack(
message=f"This is the latest video summary, checkout in Flyte's Youtube Channel!\n{channel_url}"
)
summary_messages = []
for chunk in chunks:
message = chatgpt_job(
message=concatenate_str(
msg1=(
"Please provide a summary of the following portion of the transcript"
" from the latest Flyte YouTube video. Note: This is only a segment"
" of the entire transcript, which has been split into multiple parts."
" The summary should be concise, not exceeding 4000 characters, and"
" suitable for sharing on Slack."
"Note: Handling via the Slack API is not required. Format the response in bullet points.\n\n"
"Transcript:\n"
),
msg2=chunk,
)
)
summary_messages.append(message)
message = ""
for summary_message in summary_messages:
b = check_strs_len_less_than_num(msg1=message, msg2=summary_message, num=15000)
if b.is_true:
message = concatenate_str(msg1=message, msg2=summary_message)
if b.is_false:
message = chatgpt_job(
message=concatenate_str(
msg1=(
"Please provide a concise summary of the following messages"
" generated by ChatGPT. The summary should be suitable for sharing"
" on Slack and not exceed 4000 characters."
"Note: Handling via the Slack API is not required. Format the response in bullet points.\n\n"
"Transcript:\n"
),
msg2=message,
)
)
post_message_on_slack(message=message)
message = summary_message
b = str_is_non_empty(msg=message)
if b.is_true:
message = chatgpt_job(
message=concatenate_str(
msg1=(
"Please provide a concise summary of the following messages"
" generated by ChatGPT. The summary should be suitable for sharing"
" on Slack and not exceed 4000 characters."
"Note: Handling via the Slack API is not required. Format the response in bullet points.\n\n"
"Transcript:\n"
),
msg2=message,
)
)
post_message_on_slack(message=message)
if __name__ == "__main__":
wf(channel_url="https://www.youtube.com/@flyteorg")