-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.py
More file actions
259 lines (222 loc) · 8.45 KB
/
agent.py
File metadata and controls
259 lines (222 loc) · 8.45 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import os
from langchain_google_genai import ChatGoogleGenerativeAI
from prompts import moments_template , podcast_selector_prompt
from langgraph.graph import END, StateGraph
from typing import List
from schema import MomentsList , Moment , Best_Podcast
from state import AgentState
from dotenv import load_dotenv
from copy import deepcopy
from tools import yt_tool ,send_video , upload_video , report_error , youtube_tool , trim_media , get_youtube_object , convert_and_add_captions , print_green , print_yellow
from constants import *
from chunking import chunk_srt_by_chars , save_srt , extract_srt_segment
import json
import time
import shutil
assert os.getenv("GOOGLE_API_KEY"), "Missing GOOGLE_API_KEY in .env"
assert os.getenv("GOOGLE_PROJECT_ID") , "Missing GOOGLE_PROJECT_ID in .env"
load_dotenv()
model_name = "gemini-2.0-flash"
llm = ChatGoogleGenerativeAI(
model=model_name,
model_kwargs = {
"project_id":os.getenv("GOOGLE_PROJECT_ID"),
# CUSTOMIZE AGENT ARGS TO GET THE BEST OUTPUT FOR YOUR NEEDS
# "temperature": 0.7,
# "top_p": 0.9,
# "top_k": 40,
# "max_output_tokens": 1024
}
)
youtube_filter_codes = {
"week" : "EgIIAw",
"month" : "EgQIBBAB"
}
def search_podcasts(state:AgentState):
"""
Searches for trending podcasts on YouTube.
"""
print_yellow("GETTING PODCASTS .....")
if state['retry'] !=None:
podcast_list = yt_tool.invoke(f"podcast,15,{youtube_filter_codes[state['retry']]}")
else:
podcast_list = yt_tool.invoke(f"podcast,15")
state['retry'] = None
podcast_list = json.loads(podcast_list)
try:
for podcast in podcast_list['videos']:
description , lang = get_youtube_object.invoke(podcast['id'])
del podcast['thumbnails']
del podcast['long_desc']
podcast['description'] = description
# podcast['youtube_object'] = youtube_object
podcast['subtitle_lang'] = lang
except Exception as e:
print(f"SKIPPING {podcast['id']} due to {e}")
return {
"podcast_list":podcast_list['videos']
}
def get_best_podcast_from_llm(state:AgentState):
"""
Filters the best podcast out of the trending ones.
"""
print_green("FETCHING BEST PODCASTS .....")
fp = open('burnt_podcasts.json' , 'r')
excluded_list = json.load(fp)
fp.close()
chain = podcast_selector_prompt | llm.with_structured_output(Best_Podcast)
filtered_podcast_list = [
{k: v for k, v in podcast.items() if k != 'subtitle_lang'}
for podcast in state["podcast_list"]
]
result = chain.invoke({"podcast_list":filtered_podcast_list , "excluded_list":excluded_list})
if result.selected == False:
state['retry'] = 'month' if state['retry']==None else 'week'
podcast_metadata = next((p for p in state["podcast_list"] if p['id'] == result.video_id), None)
return {
"podcast" : {
"podcast_title" : podcast_metadata["title"],
"podcast_description" : podcast_metadata["description"],
"transcript" : [] ,
"video_id" : podcast_metadata["id"],
"publish_date" : podcast_metadata["publish_time"],
"subtitle_lang" : podcast_metadata["subtitle_lang"]
# "youtube_object" : podcast_metadata['youtube_object']
} ,
"podcast_list" :[]
}
def get_clips(state:AgentState):
"""
Gets the best moments of the podcast using transcriptions.
"""
print_yellow("GETTING BEST CLIPS ......")
chain = moments_template | llm.with_structured_output(MomentsList)
chunks = chunk_srt_by_chars(state["podcast"]["transcript"])
moments:List[Moment] = []
print("NUMBER OF CHUNKS :: " ,len(chunks))
for chunk in chunks:
result = chain.invoke({
"podcast_title" : state["podcast"]["podcast_title"],
"podcast_description" : state["podcast"]["podcast_description"],
"transcript" : chunk["srt_text"]
})
for moment in result.Moments:
moments.append(moment)
time.sleep(5)
return {"Moments":moments }
def process_moments(state):
"""
Extracts the subtitles of the moments from the main transcript.
"""
clips = 0
for i, moment in enumerate(state['Moments']):
clips+=1
subs = extract_srt_segment(state['podcast']['transcript'], moment.start_time, moment.end_time)
save_srt(subs, f'./data/{i}_subs.srt')
print_green(f"NO OF CLIPS :: {clips}")
def edit_video(state):
"""
Edits the video trims, change orientation, adds the blur background effect , and burn the subtitles on the video
"""
print_green("EDITING VIDEOS ......")
process_moments(state)
for i, moment in enumerate(state['Moments']):
trim_media_input_data = {
"input_file":"./data/current_podcast.mp4",
"output_file":f"./data/{i}_index.mp4",
"start_time":moment.start_time.replace(",", "."),
"end_time":moment.end_time.replace(",", ".")
}
trim_media(trim_media_input_data)
convert_and_add_captions(f'./data/{i}_index.mp4' , f'./data/{i}_subs.srt' , f'./data/{i}_final.mp4')
def process_video(state):
"""
Processes video extracts description metadata , subtitles
"""
print_yellow("PROCESSING VIDEO .....")
transcript = youtube_tool(state["podcast"]["video_id"] , state["podcast"]["subtitle_lang"])
new_state = deepcopy(state)
new_state["podcast"]["transcript"] = transcript
return new_state
def post_video(state):
"""
Posts the clips on youtube using v3 api
"""
print_green("POSTING VIDEOS .....")
post_metadata = []
for i, moment in enumerate(state['Moments']):
meta_data = {}
meta_data['clip_addr'] = f'./data/{i}_final.mp4'
meta_data['title'] = moment.title
meta_data["description"] = moment.description
meta_data["keywords"] = moment.keywords
post_metadata.append(meta_data)
#state['podcast']['video_id'] = "EDBFFgs6Ifs"
print("Final Metadata is :: " , post_metadata)
with open('burnt_podcasts.json', 'r+') as fp:
try:
burnt_podcasts = json.load(fp)
except json.JSONDecodeError:
burnt_podcasts = []
burnt_podcasts.append(state["podcast"]["video_id"])
# Go back to beginning and truncate before dumping
fp.seek(0)
fp.truncate()
json.dump(burnt_podcasts, fp, indent=2)
print_yellow(f"UPLOADING {len(post_metadata)} videos ......")
send_video.invoke(f"UPLOADING {len(post_metadata)} videos ...")
for video in post_metadata:
upload_video(video['clip_addr'] , metadata=video)
time.sleep(300)
folder_path = './data'
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path) or os.path.islink(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
send_video.invoke("Completed cycle ...")
return {
"podcast" : {},
"podcast_list" : [],
"Moments":[],
"agent_outcome": None,
"intermediate_steps": []
}
def where_to_go(state):
"""
Changes the filter to current week/month if all the podcasts are burnt
"""
if state['retry'] != None:
send_video.invoke("Podcasts exhausted retrying with filter")
return SEARCH_PODCASTS
return PROCESS_VIDEO
def report_error_node(state):
send_video.invoke("ERROR OCCURRED")
graph = StateGraph(AgentState)
graph.add_node(SEARCH_PODCASTS , search_podcasts)
graph.add_node(SELECT_BEST_PODCAST , get_best_podcast_from_llm)
graph.add_node(PROCESS_VIDEO , process_video)
graph.add_node(FETCH_CLIPS , get_clips)
graph.add_node(EDIT_VIDEO , edit_video)
graph.add_node(POST_VIDEO , post_video)
graph.add_node(REPORT_ERROR , report_error_node)
graph.add_conditional_edges(SELECT_BEST_PODCAST , where_to_go)
graph.add_edge(SEARCH_PODCASTS , SELECT_BEST_PODCAST)
graph.add_edge(SELECT_BEST_PODCAST , PROCESS_VIDEO)
graph.add_edge(PROCESS_VIDEO , FETCH_CLIPS)
graph.add_edge(FETCH_CLIPS , EDIT_VIDEO)
graph.add_edge(EDIT_VIDEO , POST_VIDEO)
graph.add_edge(POST_VIDEO , end_key=END)
graph.set_entry_point(SEARCH_PODCASTS)
app = graph.compile()
if __name__ == "__main__":
app.invoke({
"input": "Answer according to instructions.",
"podcast": {},
"Moments": [],
"agent_outcome": None,
"intermediate_steps": [],
"podcast_list": [],
"retry": None
})