-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaggregate.py
More file actions
227 lines (187 loc) · 7.35 KB
/
Copy pathaggregate.py
File metadata and controls
227 lines (187 loc) · 7.35 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
import ast
import asyncio
import json
import os
import random
import google.generativeai as genai
from dotenv import load_dotenv
from image_agent import get_images
from templates import templates
from wikipedia_tool import wikipedia_tool
from youtube import get_data
# Load environment variables from a .env file
load_dotenv()
# Parse API keys stored in an environment variable and convert them into a Python list
GEMINI_API_KEYS = os.environ.get("GEMINI_API_KEYS")
KEY_LIST = ast.literal_eval(GEMINI_API_KEYS)
# Shuffle the API keys list to ensure usage of different keys over time
random.shuffle(KEY_LIST)
# Initialize a global index to track the current API key being used
current_api_key_index = 0
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
# List to store historical messages for reference or logging
messages = []
def cycle_api_key():
"""Retrieve the next API key from the list, cycling back to the start if necessary."""
global current_api_key_index
if current_api_key_index >= len(KEY_LIST) - 1:
current_api_key_index = 0
else:
current_api_key_index += 1
return KEY_LIST[current_api_key_index]
def generate_new_model():
"""Generate and configure a new AI model instance with a cycled API key."""
global current_api_key_index
global messages
api_key = cycle_api_key() # Cycle to the next API key
# Configure the generative AI model with the new API key
genai.configure(api_key=api_key)
# Initialize the model with specific configurations
model = genai.GenerativeModel(
"gemini-1.5-pro-latest",
generation_config=genai.GenerationConfig(
temperature=0, # Set deterministic behavior for the model
max_output_tokens=8000,
),
)
return model
def sources_to_lecture(model, original_prompt, sources, audio, video):
"""Converts a list of sources into a single lecture template.
:param sources: A list of sources, each containing a 'content' key with the source content.
:type sources: list
:param model:
:param original_prompt:
:param audio:
:param video:
:returns: A single lecture template combining all the source content.
:rtype: str
"""
prompt = ("Available templates: \n\n" + str(templates) + "\n\n" +
"Original Topic: " + original_prompt + "\n\n" + """
You are an advanced assistant that is in charge of aggregating multiple sources of information into a lecture based on a specific prompt.
Output 8 different slides on the topic given above using the sources provided as well as the given templates.
Make sure the slides flow logically and are easy to understand. Use the correct templates for the content you are presenting.
Keep text content short and concise, at most 8 words per text.
Make sure you always start off with a title slide.
Try to make each slide have different information.
Return a json parsable string of lectures that are generated from the sources provided.
Make sure the response is in the following format, only output the keys and values shown below:
{
"title": title of the lecture,
"description" : description of the lecture,
"slides": [
{
"title": title of the slide,,
"template_id": 1,
"texts" : [
"a stack is a data structure",
"stacks are used in DFS"
],
"speaker_notes" : A script for the speaker to read
}
]
}
""")
lecture = model.generate_content(
video + [
audio,
sources + prompt,
],
request_options={"timeout": 1000},
)
return lecture.text
def sources_to_lecture_simple(model, original_prompt, sources):
"""Converts a list of sources into a single lecture template.
:param sources: A list of sources, each containing a 'content' key with the source content.
:type sources: list
:param model:
:param original_prompt:
:returns: A single lecture template combining all the source content.
:rtype: str
"""
prompt = ("Available templates: \n\n" + str(templates) + "\n\n" +
"Original Topic: " + original_prompt + "\n\n" + """
You are an advanced assistant that is in charge of aggregating multiple sources of information into a lecture based on a specific prompt.
Output 8 different slides on the topic given above using the sources provided as well as the given templates.
Make sure the slides flow logically and are easy to understand. Use the correct templates for the content you are presenting.
Keep text content short and concise, at most 8 words per text.
Make sure you always start off with a title slide.
Try to make each slide have different information.
Return a json parsable string of lectures that are generated from the sources provided.
Make sure the response is in the following format, only output the keys and values shown below:
{
"title": title of the lecture,
"description" : description of the lecture,
"slides": [
{
"title": title of the slide,,
"template_id": 1,
"texts" : [
"a stack is a data structure",
"stacks are used in DFS"
],
"speaker_notes" : A script for the speaker to read
}
]
}
""")
lecture = model.generate_content(
sources + prompt,
request_options={"timeout": 1000},
)
return lecture.text
async def get_lecture(result):
tasks = []
new_slides = []
for slide in result["slides"]:
template = [
t for t in templates if t["template_id"] == slide["template_id"]
][0]
num_images = template["num_images"]
if num_images == 0:
print(num_images)
new_slides.append({**slide, "images": []})
else:
topic = slide["title"]
print(topic)
print(num_images)
# Schedule the get_images task for concurrent execution
task = get_images(topic, num_images)
tasks.append(task)
# Run all tasks concurrently and collect results
images_results = await asyncio.gather(*tasks)
# Iterate over the slides that require images
image_index = 0
for slide in result["slides"]:
template = [
t for t in templates if t["template_id"] == slide["template_id"]
][0]
num_images = template["num_images"]
if num_images != 0:
new_slides.append({**slide, "images": images_results[image_index]})
image_index += 1
lecture = {**result, "slides": new_slides}
return lecture
async def generate(topic):
sources = wikipedia_tool.run(topic)
model = generate_new_model()
audio, video = get_data(topic)
result = sources_to_lecture(model, topic, sources, audio, video)
if "```json" in result:
# Get the JSON content from the result
result = result.split("```json")[1]
result = result.split("```")[0]
result = json.loads(result)
lecture = asyncio.run(get_lecture(result))
return lecture
async def generate_simple(topic):
sources = wikipedia_tool.run(topic)
model = generate_new_model()
result = sources_to_lecture_simple(model, topic, sources)
if "```json" in result:
# Get the JSON content from the result
result = result.split("```json")[1]
result = result.split("```")[0]
result = json.loads(result)
lecture = await get_lecture(result)
return lecture