forked from Tex6298/Weather_Vane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_start.py
352 lines (287 loc) · 12.9 KB
/
quick_start.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from __future__ import print_function
import os
import streamlit as st
import openai
import requests as r
from PIL import Image
from io import BytesIO
import httpx
import json
import numpy as np
from sentence_transformers import SentenceTransformer
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import ffmpeg
SCOPES = ['https://www.googleapis.com/auth/presentations']
creds =None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
openai.api_key = st.secrets["openai_api"]
minilm = SentenceTransformer('all-MiniLM-L6-v2')
mubert_tags_string = 'tribal,action,kids,neo-classic,run 130,pumped,jazz / funk,ethnic,dubtechno,reggae,acid jazz,liquidfunk,funk,witch house,tech house,underground,artists,mystical,disco,sensorium,r&b,agender,psychedelic trance / psytrance,peaceful,run 140,piano,run 160,setting,meditation,christmas,ambient,horror,cinematic,electro house,idm,bass,minimal,underscore,drums,glitchy,beautiful,technology,tribal house,country pop,jazz & funk,documentary,space,classical,valentines,chillstep,experimental,trap,new jack swing,drama,post-rock,tense,corporate,neutral,happy,analog,funky,spiritual,sberzvuk special,chill hop,dramatic,catchy,holidays,fitness 90,optimistic,orchestra,acid techno,energizing,romantic,minimal house,breaks,hyper pop,warm up,dreamy,dark,urban,microfunk,dub,nu disco,vogue,keys,hardcore,aggressive,indie,electro funk,beauty,relaxing,trance,pop,hiphop,soft,acoustic,chillrave / ethno-house,deep techno,angry,dance,fun,dubstep,tropical,latin pop,heroic,world music,inspirational,uplifting,atmosphere,art,epic,advertising,chillout,scary,spooky,slow ballad,saxophone,summer,erotic,jazzy,energy 100,kara mar,xmas,atmospheric,indie pop,hip-hop,yoga,reggaeton,lounge,travel,running,folk,chillrave & ethno-house,detective,darkambient,chill,fantasy,minimal techno,special,night,tropical house,downtempo,lullaby,meditative,upbeat,glitch hop,fitness,neurofunk,sexual,indie rock,future pop,jazz,cyberpunk,melancholic,happy hardcore,family / kids,synths,electric guitar,comedy,psychedelic trance & psytrance,edm,psychedelic rock,calm,zen,bells,podcast,melodic house,ethnic percussion,nature,heavy,bassline,indie dance,techno,drumnbass,synth pop,vaporwave,sad,8-bit,chillgressive,deep,orchestral,futuristic,hardtechno,nostalgic,big room,sci-fi,tutorial,joyful,pads,minimal 170,drill,ethnic 108,amusing,sleepy ambient,psychill,italo disco,lofi,house,acoustic guitar,bassline house,rock,k-pop,synthwave,deep house,electronica,gabber,nightlife,sport & fitness,road trip,celebration,electro,disco house,electronic'
mubert_tags = np.array(mubert_tags_string.split(','))
mubert_tags_embeddings = minilm.encode(mubert_tags)
email = "martyn.ben.ami@gmail.com" #@param {type:"string"}
r = httpx.post('https://api-b2b.mubert.com/v2/GetServiceAccess',
json={
"method":"GetServiceAccess",
"params": {
"email": email,
"license":"ttmmubertlicense#f0acYBenRcfeFpNT4wpYGaTQIyDI4mJGv5MfIhBFz97NXDwDNFHmMRsBSzmGsJwbTpP1A6i07AXcIeAHo5",
"token":"4951f6428e83172a4f39de05d5b3ab10d58560b8",
"mode": "loop"
}
})
rdata = json.loads(r.text)
assert rdata['status'] == 1, "probably incorrect e-mail"
pat = rdata['data']['pat']
print(f'Got token: {pat}')
def query_image_creation_api(query: str, image_size:str = "256x256" ) -> Image:
response = openai.Image.create(
prompt=query,
n=1,
size=image_size
)
image_url = response['data'][0]['url']
#response = r.get(image_url, timeout=5)
#img = Image.open(BytesIO(response.content))
return image_url
def query_text_completion_api(query: str, model_key = "text-davinci-003", num_tokens = 500, temp=0.7):
template = f""" You are to convert a given topic_query to a list of prompts that are both descriptive and representative of the topic query
for the purpose of querying music and image generation services
This is the Query: {query}
"""
resp = openai.Completion.create(
model=model_key,
prompt=template,
max_tokens=num_tokens,
temperature=temp
)
return resp
def query_bullet_point_api(query: str, model_key = "text-davinci-003", num_tokens = 500, num_bullet_point=2, temp=0.7) -> list:
template = f""" You are to convert a given topic_query to a list of {num_bullet_point} bullet points that are both descriptive and represnetative of the topic query
for the purpose of a slide show, they can be fun facts, interesting observations, or questions
This is the Query: {query}
"""
response = openai.Completion.create(
prompt=query,
max_tokens=num_tokens,
temperature=temp,
frequency_penalty=0,
presence_penalty=0
)
bullet_points = response['choices'][0]['text'].replace("\n", "").split(".")
return bullet_points[:num_bullet_point]
def get_track_by_tags(tags, pat, duration, maxit=20, autoplay=False, loop=False):
if loop:
mode = "loop"
else:
mode = "track"
r = httpx.post('https://api-b2b.mubert.com/v2/RecordTrackTTM',
json={
"method":"RecordTrackTTM",
"params": {
"pat": pat,
"duration": duration,
"tags": tags,
"mode": mode
}
})
rdata = json.loads(r.text)
assert rdata['status'] == 1, rdata['error']['text']
trackurl = rdata['data']['tasks'][0]['download_link']
'''
print('Generating track ', end='')
for i in range(maxit):
r = httpx.get(trackurl)
if r.status_code == 200:
#Audio(trackurl, autoplay=autoplay)
break
time.sleep(1)
print('.', end='')
'''
return trackurl
def find_similar(em, embeddings, method='cosine'):
scores = []
for ref in embeddings:
if method == 'cosine':
scores.append(1 - np.dot(ref, em)/(np.linalg.norm(ref)*np.linalg.norm(em)))
if method == 'norm':
scores.append(np.linalg.norm(ref - em))
return np.array(scores), np.argsort(scores)
def get_tags_for_prompts(prompts, top_n=3, debug=False):
prompts_embeddings = minilm.encode(prompts)
ret = []
for i, pe in enumerate(prompts_embeddings):
scores, idxs = find_similar(pe, mubert_tags_embeddings)
top_tags = mubert_tags[idxs[:top_n]]
top_prob = 1 - scores[idxs[:top_n]]
if debug:
print(f"Prompt: {prompts[i]}\nTags: {', '.join(top_tags)}\nScores: {top_prob}\n\n\n")
ret.append((prompts[i], list(top_tags)))
return ret
def generate_track_by_prompt(prompt, duration, loop=False):
_, tags = get_tags_for_prompts([prompt,])[0]
try:
return get_track_by_tags(tags, pat, duration, autoplay=True, loop=loop)
except Exception as e:
print(str(e))
def create_presentation(title):
"""
Creates the Presentation the user has access to.
Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity
for guides on implementing OAuth2 for the application.
"""
# pylint: disable=maybe-no-member
try:
service = build('slides', 'v1', credentials=creds)
body = {
'title': title
}
presentation = service.presentations() \
.create(body=body).execute()
print(f"Created presentation with ID:"
f"{(presentation.get('presentationId'))}")
return presentation
except HttpError as error:
print(f"An error occurred: {error}")
print("presentation not created")
return error
def create_slide(presentation_id, page_id, n):
"""
Creates the Presentation the user has access to.
Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity
for guides on implementing OAuth2 for the application.\n
"""
#creds, _ = google.auth.default()
# pylint: disable=maybe-no-member
try:
service = build('slides', 'v1', credentials=creds)
# Add a slide at index 1 using the predefined
# 'TITLE_AND_TWO_COLUMNS' layout and the ID page_id.
requests = [
{
'createSlide': {
'objectId': page_id,
'insertionIndex': n,
'slideLayoutReference': {
'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
}
}
}
]
# If you wish to populate the slide with elements,
# add element create requests here, using the page_id.
# Execute the request.
body = {
'requests': requests
}
response = service.presentations() \
.batchUpdate(presentationId=presentation_id, body=body).execute()
create_slide_response = response.get('replies')[0].get('createSlide')
print(f"Created slide with ID:"
f"{(create_slide_response.get('objectId'))}")
except HttpError as error:
print(f"An error occurred: {error}")
print("Slides not created")
return error
return response
def create_image(query, presentation_id, page_id):
"""
Creates images the user has access to.
Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity
for guides on implementing OAuth2 for the application.
"""
#creds, _ = google.auth.default()
# pylint: disable=maybe-no-member
image_url = query_image_creation_api(query, image_size="1024x1024")
try:
service = build('slides', 'v1', credentials=creds)
# pylint: disable = invalid-name
# pylint: disable=invalid-name
requests = []
requests.append({
"updatePageProperties": {
"objectId": page_id,
"pageProperties": {
"pageBackgroundFill": {
"stretchedPictureFill": {
"contentUrl": image_url
}
}
},
"fields": "pageBackgroundFill"
}
})
# Execute the request.
body = {
'requests': requests
}
response = service.presentations() \
.batchUpdate(presentationId=presentation_id, body=body).execute()
#create_image_response = response.get('replies')[0].get('createImage')
#print(f"Created image with ID: "
#f"{(create_image_response.get('objectId'))}")
return response
except HttpError as error:
print(f"An error occurred: {error}")
print("Images not created")
return error
def create_video(query, presentation_id, page_id):
"""
Creates video the user has access to.
Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity
for guides on implementing OAuth2 for the application.
"""
#creds, _ = google.auth.default()
# pylint: disable=maybe-no-member
AUDIO_FILE_URL = generate_track_by_prompt(query, 30)
#add auido as video files
#download auido file
#upload audio file to google drive
service = build('slides', 'v1', credentials=creds)
requests = []
requests.append({
'createVideo': {'source': AUDIO_FILE_URL,
'id': presentation_id,
'elementProperties':
{'pageObjectId': page_id,}
}
})
# Execute the request.
body = {
'requests': requests
}
response = service.presentations() \
.batchUpdate(presentationId=presentation_id, body=body).execute()
create_video_response = response.get('replies')[0].get('createVideo')
print(f"Created video with ID: "
f"{(create_video_response.get('objectId'))}")
return response
# presentation = create_presentation("test6ab1")
# pres_id = presentation.get('presentationId')
# create_slide(pres_id, n)
# create_image(summary, pres_id, n)
#create_video("Dogs playing poker with a wolf", pres_id, "page1") Does not work
#trackurl = generate_track_by_prompt("Dogs playing poker with a wolf", duration=30)