alexandrecorreia commited on
Commit
d646457
·
1 Parent(s): 8bc16b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -31
app.py CHANGED
@@ -24,11 +24,34 @@ from transformers import CLIPProcessor, CLIPModel
24
  static_dir = Path('./static')
25
  static_dir.mkdir(parents=True, exist_ok=True)
26
 
27
- client = Client("http://ec2-54-220-125-165.eu-west-1.compute.amazonaws.com:8882")
28
  # client = Client()
29
- index_name = "new_look_expanded_dresses"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- # sys.path.insert(1, 'C:/Users/Alexandre/Documents/University/5_Ano/Estagio/repos_1')
 
 
 
 
32
 
33
  # Create custom Color objects for our primary, secondary, and neutral colors
34
  primary_color = gr.themes.colors.slate
@@ -85,9 +108,9 @@ def get_items_from_dataset(start_index=0, end_index=50, dataset=pd.read_json('{}
85
  # return dataset_to_gallery(dedup_by(df, 'colour_code'))
86
 
87
  def start_page(num_results=50):
88
- result = client.index(index_name).search("Dress", score_modifiers = {
89
  "add_to_score": [{"field_name": "best_seller_score","weight": 5}],
90
- }, searchable_attributes=['image'], device="cpu", limit=num_results)
91
  imgs = [r for r in result["hits"]]
92
  return return_results_page(imgs)
93
 
@@ -99,14 +122,14 @@ def return_results_page(results_list: list):
99
 
100
  def return_item(combined) -> list:
101
  colour_code = combined.split("@@")[2]
102
- result = client.index(index_name).search("", filter_string = "colour_code:" + str(colour_code), searchable_attributes=['image'], device="cpu")
103
  imgs = [r for r in result["hits"]]
104
  df = pd.DataFrame(imgs)
105
  return dataset_to_gallery(df), imgs[0]["description_total"], imgs[0]["url"]
106
 
107
  def return_specific_item(combined) -> list:
108
  _id = combined.split("@@")[3]
109
- result = client.index(index_name).search("", filter_string = "_id:" + str(_id), searchable_attributes=['image'], device="cpu")
110
  imgs = [r for r in result["hits"]]
111
  print(imgs)
112
  df = pd.DataFrame(imgs)
@@ -117,24 +140,13 @@ def load_image(image_input):
117
  image_input.save("../../../Documents/images/img_path.jpg")
118
  os.system('docker cp "../../../Documents/images/img_path.jpg" marqo:"/images/images/"')
119
 
120
- ### Search local
121
  def search_images(query, best_seller_score_weight):
122
- result = client.index(index_name).search(query, score_modifiers = {
123
  "add_to_score": [{"field_name": "best_seller_score","weight": best_seller_score_weight/1000}],
124
- }, searchable_attributes=['image'], device="cpu", limit=40)
125
  imgs = [r for r in result["hits"]]
126
  return imgs
127
 
128
- ### Search AWS
129
- # def search_images(query, best_seller_score_weight):
130
- # client = Client("http://ec2-54-220-125-165.eu-west-1.compute.amazonaws.com:8882")
131
- # result = client.index("new_look_expanded_dresses").search(query, score_modifiers = {
132
- # "add_to_score": [{"field_name": "best_seller_score","weight": best_seller_score_weight/1000}],
133
- # }, searchable_attributes=['primary_image'], device="cpu", limit=40)
134
- # imgs = [r for r in result["hits"]]
135
-
136
- # return imgs
137
-
138
  # def get_labels_probs(labels, image):
139
  # inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
140
 
@@ -183,7 +195,6 @@ with gr.Blocks(theme=theme, title="New Look", css=css) as demo:
183
  with gr.Column(scale=3):
184
  best_seller_score_weight = gr.Slider(label = "Best seller relevance", minimum=-1, maximum=1, value=0, step=0.01)
185
  search_button = gr.Button(value="Search")
186
- index_name_state = gr.State(value=index_name)
187
  with gr.Column(scale=2):
188
  image_input = gr.Image(type="pil", label="Search with image")
189
  image_path = gr.State(visible=False)
@@ -198,7 +209,6 @@ with gr.Blocks(theme=theme, title="New Look", css=css) as demo:
198
  height="400",object_fit="contain")
199
  image_description = gr.Text(label="Description")
200
  product_link = gr.State()
201
- # button_go_to_page = gr.Button(value="Go to product page")
202
  page = gr.HTML()
203
 
204
 
@@ -255,15 +265,21 @@ with gr.Blocks(theme=theme, title="New Look", css=css) as demo:
255
  gr.Markdown()
256
  gr.Markdown()
257
 
258
- def on_change_dataset(choice):
259
- index_name_state = ""
260
- if choice == "New Look Dresses":
261
- index_name_state = "new_look_expanded_dresses"
262
- elif choice == "New Look All":
263
- index_name_state = "new_look_expanded_all"
264
- return index_name_state
265
-
266
- select_dataset_button.click(on_change_dataset, list_datasets, index_name_state)
 
 
 
 
 
 
267
 
268
  def load(image_input):
269
  if image_input != None:
 
24
  static_dir = Path('./static')
25
  static_dir.mkdir(parents=True, exist_ok=True)
26
 
27
+ # client = Client("http://ec2-54-220-125-165.eu-west-1.compute.amazonaws.com:8882")
28
  # client = Client()
29
+ # index_name = "new_look_expanded_dresses"
30
+ # device = "cpu"
31
+
32
+ class Client_Settings():
33
+ def __init__(self):
34
+ self.client = Client()
35
+ self.index_name = "new_look_expanded_dresses"
36
+ self.device = "cpu"
37
+
38
+ def conn_to_local(self):
39
+ self.client = Client()
40
+
41
+ def conn_to_server(self, url):
42
+ self.client = Client(url)
43
+
44
+ def set_index_name(self, new_index_name):
45
+ self.index_name = new_index_name
46
+
47
+ def set_device(self, new_device):
48
+ self.device = new_device
49
 
50
+ client_obj = Client_Settings()
51
+ # client_obj.conn_to_local()
52
+ client_obj.conn_to_server("http://ec2-54-220-125-165.eu-west-1.compute.amazonaws.com:8882")
53
+ client_obj.set_index_name("new_look_expanded_dresses")
54
+ client_obj.set_device("cuda")
55
 
56
  # Create custom Color objects for our primary, secondary, and neutral colors
57
  primary_color = gr.themes.colors.slate
 
108
  # return dataset_to_gallery(dedup_by(df, 'colour_code'))
109
 
110
  def start_page(num_results=50):
111
+ result = client_obj.client.index(client_obj.index_name).search("Dress", score_modifiers = {
112
  "add_to_score": [{"field_name": "best_seller_score","weight": 5}],
113
+ }, searchable_attributes=['image'], device=client_obj.device, limit=num_results)
114
  imgs = [r for r in result["hits"]]
115
  return return_results_page(imgs)
116
 
 
122
 
123
  def return_item(combined) -> list:
124
  colour_code = combined.split("@@")[2]
125
+ result = client_obj.client.index(client_obj.index_name).search("", filter_string = "colour_code:" + str(colour_code), searchable_attributes=['image'], device=client_obj.device)
126
  imgs = [r for r in result["hits"]]
127
  df = pd.DataFrame(imgs)
128
  return dataset_to_gallery(df), imgs[0]["description_total"], imgs[0]["url"]
129
 
130
  def return_specific_item(combined) -> list:
131
  _id = combined.split("@@")[3]
132
+ result = client_obj.client.index(client_obj.index_name).search("", filter_string = "_id:" + str(_id), searchable_attributes=['image'], device=client_obj.device)
133
  imgs = [r for r in result["hits"]]
134
  print(imgs)
135
  df = pd.DataFrame(imgs)
 
140
  image_input.save("../../../Documents/images/img_path.jpg")
141
  os.system('docker cp "../../../Documents/images/img_path.jpg" marqo:"/images/images/"')
142
 
 
143
  def search_images(query, best_seller_score_weight):
144
+ result = client_obj.client.index(client_obj.index_name).search(query, score_modifiers = {
145
  "add_to_score": [{"field_name": "best_seller_score","weight": best_seller_score_weight/1000}],
146
+ }, searchable_attributes=['image'], device=client_obj.device, limit=40)
147
  imgs = [r for r in result["hits"]]
148
  return imgs
149
 
 
 
 
 
 
 
 
 
 
 
150
  # def get_labels_probs(labels, image):
151
  # inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
152
 
 
195
  with gr.Column(scale=3):
196
  best_seller_score_weight = gr.Slider(label = "Best seller relevance", minimum=-1, maximum=1, value=0, step=0.01)
197
  search_button = gr.Button(value="Search")
 
198
  with gr.Column(scale=2):
199
  image_input = gr.Image(type="pil", label="Search with image")
200
  image_path = gr.State(visible=False)
 
209
  height="400",object_fit="contain")
210
  image_description = gr.Text(label="Description")
211
  product_link = gr.State()
 
212
  page = gr.HTML()
213
 
214
 
 
265
  gr.Markdown()
266
  gr.Markdown()
267
 
268
+ def on_change_dataset(choice):
269
+ index_name = ""
270
+ if choice == "New Look Dresses":
271
+ index_name = "new_look_expanded_dresses"
272
+ elif choice == "New Look All":
273
+ index_name = "new_look_expanded_all"
274
+
275
+ print("Dataset selected: " + index_name)
276
+ client_obj.set_index_name(index_name)
277
+
278
+ time.sleep(0.5)
279
+
280
+ return choice
281
+
282
+ select_dataset_button.click(on_change_dataset, list_datasets, list_datasets)
283
 
284
  def load(image_input):
285
  if image_input != None: