rynmurdock commited on
Commit
00de940
1 Parent(s): 4f7211d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -19
app.py CHANGED
@@ -34,15 +34,12 @@ calibrate_prompts = [
34
  '',
35
  ]
36
 
37
- embs = []
38
- ys = []
39
-
40
  start_time = time.time()
41
 
42
-
43
  glob_idx = 0
44
 
45
- def next_image():
46
  global glob_idx
47
  glob_idx = glob_idx + 1
48
  with torch.no_grad():
@@ -59,7 +56,7 @@ def next_image():
59
  image = Image.open(BytesIO(response.content))
60
 
61
  embs.append(torch.tensor([float(i) for i in urlopen(output['file2']).read().decode('utf-8').split(', ')]).unsqueeze(0))
62
- return image
63
  else:
64
  print('######### Roaming #########')
65
 
@@ -93,8 +90,6 @@ def next_image():
93
 
94
  im_emb_st = str(im_emb[0].cpu().detach().tolist())[1:-1]
95
 
96
- # output = StringIO()
97
- # output.write(im_emb_st)
98
 
99
  output = replicate.run(
100
  "rynmurdock/zahir:42c58addd49ab57f1e309f0b9a0f271f483bbef0470758757c623648fe989e42",
@@ -108,7 +103,7 @@ def next_image():
108
  embs.append(im_emb)
109
 
110
  torch.save(lin_class.coef_, f'./{start_time}.pt')
111
- return image
112
 
113
 
114
 
@@ -128,19 +123,21 @@ def start(_):
128
  ]
129
 
130
 
131
- def choose(choice):
132
  if choice == 'Like':
133
  choice = 1
134
  elif choice == 'Neither':
135
  _ = embs.pop(-1)
136
- return next_image()
137
  else:
138
  choice = 0
139
  ys.append(choice)
140
- return next_image()
141
 
142
  css = "div#output-image {height: 768px !important; width: 768px !important; margin:auto;}"
143
  with gr.Blocks(css=css) as demo:
 
 
144
  with gr.Row(elem_id='output-image'):
145
  img = gr.Image(interactive=False, elem_id='output-image',)
146
  with gr.Row(equal_height=True):
@@ -149,23 +146,23 @@ with gr.Blocks(css=css) as demo:
149
  b1 = gr.Button(value='Like', interactive=False,)
150
  b1.click(
151
  choose,
152
- [b1],
153
- [img]
154
  )
155
  b2.click(
156
  choose,
157
- [b2],
158
- [img]
159
  )
160
  b3.click(
161
  choose,
162
- [b3],
163
- [img]
164
  )
165
  with gr.Row():
166
  b4 = gr.Button(value='Start')
167
  b4.click(start,
168
- [b4],
169
  [b1, b2, b3, b4, img,])
170
  with gr.Row():
171
  html = gr.HTML('''<div style='text-align:center; font-size:32'>You will callibrate for several prompts and then roam.</ div>''')
 
34
  '',
35
  ]
36
 
 
 
 
37
  start_time = time.time()
38
 
39
+ # TODO add to state instead of shared across all
40
  glob_idx = 0
41
 
42
+ def next_image(embs, ys):
43
  global glob_idx
44
  glob_idx = glob_idx + 1
45
  with torch.no_grad():
 
56
  image = Image.open(BytesIO(response.content))
57
 
58
  embs.append(torch.tensor([float(i) for i in urlopen(output['file2']).read().decode('utf-8').split(', ')]).unsqueeze(0))
59
+ return image, embs, ys
60
  else:
61
  print('######### Roaming #########')
62
 
 
90
 
91
  im_emb_st = str(im_emb[0].cpu().detach().tolist())[1:-1]
92
 
 
 
93
 
94
  output = replicate.run(
95
  "rynmurdock/zahir:42c58addd49ab57f1e309f0b9a0f271f483bbef0470758757c623648fe989e42",
 
103
  embs.append(im_emb)
104
 
105
  torch.save(lin_class.coef_, f'./{start_time}.pt')
106
+ return image, embs, ys
107
 
108
 
109
 
 
123
  ]
124
 
125
 
126
+ def choose(choice, embs, ys):
127
  if choice == 'Like':
128
  choice = 1
129
  elif choice == 'Neither':
130
  _ = embs.pop(-1)
131
+ return next_image(embs, ys)
132
  else:
133
  choice = 0
134
  ys.append(choice)
135
+ return next_image(embs, ys)
136
 
137
  css = "div#output-image {height: 768px !important; width: 768px !important; margin:auto;}"
138
  with gr.Blocks(css=css) as demo:
139
+ embs = gr.State([])
140
+ ys = gr.State([])
141
  with gr.Row(elem_id='output-image'):
142
  img = gr.Image(interactive=False, elem_id='output-image',)
143
  with gr.Row(equal_height=True):
 
146
  b1 = gr.Button(value='Like', interactive=False,)
147
  b1.click(
148
  choose,
149
+ [b1, embs, ys],
150
+ [img, embs, ys]
151
  )
152
  b2.click(
153
  choose,
154
+ [b2, embs, ys],
155
+ [img, embs, ys]
156
  )
157
  b3.click(
158
  choose,
159
+ [b3, embs, ys],
160
+ [img, embs, ys]
161
  )
162
  with gr.Row():
163
  b4 = gr.Button(value='Start')
164
  b4.click(start,
165
+ [b4, embs, ys],
166
  [b1, b2, b3, b4, img,])
167
  with gr.Row():
168
  html = gr.HTML('''<div style='text-align:center; font-size:32'>You will callibrate for several prompts and then roam.</ div>''')