Spaces:
Runtime error
Runtime error
Commit
·
f9c1121
1
Parent(s):
56cff2e
basically delirious lol
Browse files
app.py
CHANGED
@@ -224,57 +224,62 @@ def pluck_img(user_id, user_emb):
|
|
224 |
def background_next_image():
|
225 |
global prevs_df
|
226 |
global is_started
|
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 |
def pluck_embs_ys(user_id):
|
@@ -292,14 +297,14 @@ def pluck_embs_ys(user_id):
|
|
292 |
return embs, ys
|
293 |
|
294 |
def next_image(calibrate_prompts, user_id):
|
295 |
-
global is_started
|
296 |
print(prevs_df)
|
|
|
297 |
with torch.no_grad():
|
298 |
if len(calibrate_prompts) > 0:
|
299 |
print('######### Calibrating with sample media #########')
|
300 |
cal_video = calibrate_prompts.pop(0)
|
301 |
image = prevs_df[prevs_df['paths'] == cal_video]['paths'].to_list()[0]
|
302 |
-
|
303 |
return image, calibrate_prompts
|
304 |
else:
|
305 |
print('######### Roaming #########')
|
@@ -312,31 +317,21 @@ def next_image(calibrate_prompts, user_id):
|
|
312 |
|
313 |
|
314 |
|
315 |
-
scheduler = BackgroundScheduler()
|
316 |
-
#thread = threading.Thread(target=background_next_image)
|
317 |
|
318 |
-
is_started = False
|
319 |
|
320 |
-
|
321 |
-
global is_started
|
322 |
-
global scheduler
|
323 |
-
if not is_started:
|
324 |
-
scheduler.add_job(func=background_next_image, trigger="interval", seconds=.1)
|
325 |
-
scheduler.start()
|
326 |
-
is_started = True
|
327 |
|
328 |
def start(_, calibrate_prompts, user_id, request: gr.Request):
|
329 |
global is_started
|
330 |
-
global scheduler
|
331 |
user_id = int(str(time.time())[-7:].replace('.', ''))
|
332 |
image, calibrate_prompts = next_image(calibrate_prompts, user_id)
|
333 |
-
|
334 |
-
|
335 |
return [
|
336 |
gr.Button(value='Like (L)', interactive=True),
|
337 |
-
gr.Button(value='
|
338 |
gr.Button(value='Dislike (A)', interactive=True),
|
339 |
-
gr.Button(value=
|
340 |
image,
|
341 |
calibrate_prompts,
|
342 |
user_id
|
@@ -454,7 +449,7 @@ Explore the latent space without text prompts based on your preferences. Learn m
|
|
454 |
img.play(l, js='''document.querySelector('[data-testid="Lightning-player"]').loop = true''')
|
455 |
with gr.Row(equal_height=True):
|
456 |
b3 = gr.Button(value='Dislike (A)', interactive=False, elem_id="dislike")
|
457 |
-
b2 = gr.Button(value='
|
458 |
b1 = gr.Button(value='Like (L)', interactive=False, elem_id="like")
|
459 |
b1.click(
|
460 |
choose,
|
@@ -489,7 +484,12 @@ Explore the latent space without text prompts based on your preferences. Learn m
|
|
489 |
log = logging.getLogger('log_here')
|
490 |
log.setLevel(logging.ERROR)
|
491 |
|
|
|
|
|
|
|
492 |
|
|
|
|
|
493 |
|
494 |
@spaces.GPU()
|
495 |
def encode_space(x):
|
|
|
224 |
def background_next_image():
|
225 |
global prevs_df
|
226 |
global is_started
|
227 |
+
while True:
|
228 |
+
# only let it get N (maybe 3) ahead of the user
|
229 |
+
#not_rated_rows = prevs_df[[i[1]['user:rating'] == {' ': ' '} for i in prevs_df.iterrows()]]
|
230 |
+
rated_rows = prevs_df[[i[1]['user:rating'] != {' ': ' '} for i in prevs_df.iterrows()]]
|
231 |
+
while len(rated_rows) < 4:
|
232 |
+
# not_rated_rows = prevs_df[[i[1]['user:rating'] == {' ': ' '} for i in prevs_df.iterrows()]]
|
233 |
+
rated_rows = prevs_df[[i[1]['user:rating'] != {' ': ' '} for i in prevs_df.iterrows()]]
|
234 |
+
time.sleep(.01)
|
235 |
+
print('all users have 4 or less rows rated')
|
236 |
+
|
237 |
+
user_id_list = set(rated_rows['latest_user_to_rate'].to_list())
|
238 |
+
for uid in user_id_list:
|
239 |
+
rated_rows = prevs_df[[i[1]['user:rating'].get(uid, None) is not None for i in prevs_df.iterrows()]]
|
240 |
+
not_rated_rows = prevs_df[[i[1]['user:rating'].get(uid, None) is None for i in prevs_df.iterrows()]]
|
241 |
+
|
242 |
+
# we need to intersect not_rated_rows from this user's embed > 7. Just add a new column on which user_id spawned the
|
243 |
+
# media.
|
244 |
+
|
245 |
+
unrated_from_user = not_rated_rows[[i[1]['from_user_id'] == uid for i in not_rated_rows.iterrows()]]
|
246 |
+
rated_from_user = rated_rows[[i[1]['from_user_id'] == uid for i in rated_rows.iterrows()]]
|
247 |
+
|
248 |
+
# we pop previous ratings if there are > 10
|
249 |
+
if len(rated_from_user) >= 10:
|
250 |
+
oldest = rated_from_user.iloc[0]['paths']
|
251 |
+
prevs_df = prevs_df[prevs_df['paths'] != oldest]
|
252 |
+
# we don't compute more after 10 are in the queue for them
|
253 |
+
if len(unrated_from_user) >= 10:
|
254 |
+
continue
|
255 |
+
|
256 |
+
if len(rated_rows) < 4:
|
257 |
+
print(f'latest user {uid} has < 4 rows') # or > 7 unrated rows')
|
258 |
+
continue
|
259 |
+
|
260 |
+
print(uid)
|
261 |
+
embs, ys = pluck_embs_ys(uid)
|
262 |
+
|
263 |
+
user_emb = get_user_emb(embs, ys)
|
264 |
+
img, embs = generate(user_emb)
|
265 |
+
print(img)
|
266 |
+
if img:
|
267 |
+
tmp_df = pd.DataFrame(columns=['paths', 'embeddings', 'ips', 'user:rating', 'latest_user_to_rate'])
|
268 |
+
tmp_df['paths'] = [img]
|
269 |
+
tmp_df['embeddings'] = [embs]
|
270 |
+
tmp_df['user:rating'] = [{' ': ' '}]
|
271 |
+
tmp_df['from_user_id'] = [uid]
|
272 |
+
prevs_df = pd.concat((prevs_df, tmp_df))
|
273 |
+
# we can free up storage by deleting the image
|
274 |
+
if len(prevs_df) > 50:
|
275 |
+
oldest_path = prevs_df.iloc[6]['paths']
|
276 |
+
if os.path.isfile(oldest_path):
|
277 |
+
os.remove(oldest_path)
|
278 |
+
else:
|
279 |
+
# If it fails, inform the user.
|
280 |
+
print("Error: %s file not found" % oldest_path)
|
281 |
+
# only keep 50 images & embeddings & ips, then remove oldest besides calibrating
|
282 |
+
prevs_df = pd.concat((prevs_df.iloc[:6], prevs_df.iloc[7:]))
|
283 |
|
284 |
|
285 |
def pluck_embs_ys(user_id):
|
|
|
297 |
return embs, ys
|
298 |
|
299 |
def next_image(calibrate_prompts, user_id):
|
|
|
300 |
print(prevs_df)
|
301 |
+
|
302 |
with torch.no_grad():
|
303 |
if len(calibrate_prompts) > 0:
|
304 |
print('######### Calibrating with sample media #########')
|
305 |
cal_video = calibrate_prompts.pop(0)
|
306 |
image = prevs_df[prevs_df['paths'] == cal_video]['paths'].to_list()[0]
|
307 |
+
|
308 |
return image, calibrate_prompts
|
309 |
else:
|
310 |
print('######### Roaming #########')
|
|
|
317 |
|
318 |
|
319 |
|
|
|
|
|
320 |
|
|
|
321 |
|
322 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
323 |
|
324 |
def start(_, calibrate_prompts, user_id, request: gr.Request):
|
325 |
global is_started
|
|
|
326 |
user_id = int(str(time.time())[-7:].replace('.', ''))
|
327 |
image, calibrate_prompts = next_image(calibrate_prompts, user_id)
|
328 |
+
if not is_started:
|
329 |
+
background_next_image()
|
330 |
return [
|
331 |
gr.Button(value='Like (L)', interactive=True),
|
332 |
+
gr.Button(value='Neither (Space)', interactive=True, visible=False),
|
333 |
gr.Button(value='Dislike (A)', interactive=True),
|
334 |
+
gr.Button(value='Start', interactive=False),
|
335 |
image,
|
336 |
calibrate_prompts,
|
337 |
user_id
|
|
|
449 |
img.play(l, js='''document.querySelector('[data-testid="Lightning-player"]').loop = true''')
|
450 |
with gr.Row(equal_height=True):
|
451 |
b3 = gr.Button(value='Dislike (A)', interactive=False, elem_id="dislike")
|
452 |
+
b2 = gr.Button(value='Neither (Space)', interactive=False, elem_id="neither", visible=False)
|
453 |
b1 = gr.Button(value='Like (L)', interactive=False, elem_id="like")
|
454 |
b1.click(
|
455 |
choose,
|
|
|
484 |
log = logging.getLogger('log_here')
|
485 |
log.setLevel(logging.ERROR)
|
486 |
|
487 |
+
#scheduler = BackgroundScheduler()
|
488 |
+
#scheduler.add_job(func=background_next_image, trigger="interval", seconds=.1)
|
489 |
+
#scheduler.start()
|
490 |
|
491 |
+
#thread = threading.Thread(target=background_next_image,)
|
492 |
+
#thread.start()
|
493 |
|
494 |
@spaces.GPU()
|
495 |
def encode_space(x):
|