rynmurdock commited on
Commit
f9c1121
·
1 Parent(s): 56cff2e

basically delirious lol

Browse files
Files changed (1) hide show
  1. app.py +69 -69
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
- # 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
- time.sleep(.2)
233
-
234
- user_id_list = set(rated_rows['latest_user_to_rate'].to_list())
235
- for uid in user_id_list:
236
- rated_rows = prevs_df[[i[1]['user:rating'].get(uid, None) is not None for i in prevs_df.iterrows()]]
237
- not_rated_rows = prevs_df[[i[1]['user:rating'].get(uid, None) is None for i in prevs_df.iterrows()]]
238
-
239
- # we need to intersect not_rated_rows from this user's embed > 7. Just add a new column on which user_id spawned the
240
- # media.
241
-
242
- unrated_from_user = not_rated_rows[[i[1]['from_user_id'] == uid for i in not_rated_rows.iterrows()]]
243
- rated_from_user = rated_rows[[i[1]['from_user_id'] == uid for i in rated_rows.iterrows()]]
244
-
245
- # we pop previous ratings if there are > 10
246
- if len(rated_from_user) >= 10:
247
- oldest = rated_from_user.iloc[0]['paths']
248
- prevs_df = prevs_df[prevs_df['paths'] != oldest]
249
- print('popping 10th rated')
250
- # we don't compute more after 10 are in the queue for them
251
- if len(unrated_from_user) >= 10:
252
- print('skipping because >=10 in queue unrated for user')
253
- continue
254
-
255
- print(uid)
256
- embs, ys = pluck_embs_ys(uid)
257
-
258
- user_emb = get_user_emb(embs, ys)
259
- img, embs = generate(user_emb)
260
- print(img)
261
- if img:
262
- tmp_df = pd.DataFrame(columns=['paths', 'embeddings', 'ips', 'user:rating', 'latest_user_to_rate'])
263
- tmp_df['paths'] = [img]
264
- tmp_df['embeddings'] = [embs]
265
- tmp_df['user:rating'] = [{' ': ' '}]
266
- tmp_df['from_user_id'] = [uid]
267
- prevs_df = pd.concat((prevs_df, tmp_df))
268
- # we can free up storage by deleting the image
269
- if len(prevs_df) > 50:
270
- oldest_path = prevs_df.iloc[6]['paths']
271
- if os.path.isfile(oldest_path):
272
- os.remove(oldest_path)
273
- else:
274
- # If it fails, inform the user.
275
- print("Error: %s file not found" % oldest_path)
276
- # only keep 50 images & embeddings & ips, then remove oldest besides calibrating
277
- prevs_df = pd.concat((prevs_df.iloc[:6], prevs_df.iloc[7:]))
 
 
 
 
 
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
- is_started = True
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
- def bg_check(_):
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='_', interactive=False, visible=False),
338
  gr.Button(value='Dislike (A)', interactive=True),
339
- gr.Button(value=bg_check, interactive=False),
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='_', interactive=False, visible=False)
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):