Spaces:
Runtime error
Runtime error
Commit
·
018d89a
1
Parent(s):
ab86a13
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,7 @@ import matplotlib.pyplot as plt
|
|
| 14 |
from pathlib import Path
|
| 15 |
from datetime import datetime
|
| 16 |
import time
|
|
|
|
| 17 |
|
| 18 |
from transformers import CLIPProcessor, CLIPModel
|
| 19 |
|
|
@@ -23,6 +24,8 @@ processor = CLIPProcessor.from_pretrained("patrickjohncyh/fashion-clip")
|
|
| 23 |
static_dir = Path('./static')
|
| 24 |
static_dir.mkdir(parents=True, exist_ok=True)
|
| 25 |
|
|
|
|
|
|
|
| 26 |
# sys.path.insert(1, 'C:/Users/Alexandre/Documents/University/5_Ano/Estagio/repos_1')
|
| 27 |
|
| 28 |
# Create custom Color objects for our primary, secondary, and neutral colors
|
|
@@ -48,36 +51,84 @@ theme = gr.themes.Base(
|
|
| 48 |
font_mono=font_mono
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def load_image(image_input):
|
| 58 |
-
image_input.save("img_path.jpg")
|
| 59 |
-
os.system('docker cp "img_path.jpg" marqo:"/
|
| 60 |
|
| 61 |
### Search local
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# def search_images(query, best_seller_score_weight):
|
| 63 |
-
# client = Client()
|
| 64 |
-
# result = client.index("
|
| 65 |
# "add_to_score": [{"field_name": "best_seller_score","weight": best_seller_score_weight/1000}],
|
| 66 |
-
# }, searchable_attributes=['primary_image'], device="cpu", limit=
|
| 67 |
# imgs = [r for r in result["hits"]]
|
| 68 |
|
| 69 |
# return imgs
|
| 70 |
|
| 71 |
-
### Search AWS
|
| 72 |
-
def search_images(query, best_seller_score_weight):
|
| 73 |
-
client = Client("http://ec2-54-220-125-165.eu-west-1.compute.amazonaws.com:8882")
|
| 74 |
-
result = client.index("test").search(query, score_modifiers = {
|
| 75 |
-
"add_to_score": [{"field_name": "best_seller_score","weight": best_seller_score_weight/1000}],
|
| 76 |
-
}, searchable_attributes=['primary_image'], device="cpu", limit=5)
|
| 77 |
-
imgs = [r for r in result["hits"]]
|
| 78 |
-
|
| 79 |
-
return imgs
|
| 80 |
-
|
| 81 |
def get_labels_probs(labels, image):
|
| 82 |
inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
|
| 83 |
|
|
@@ -95,12 +146,6 @@ def get_bar_plot(labels, probs):
|
|
| 95 |
|
| 96 |
return fig
|
| 97 |
|
| 98 |
-
def get_image_url_in_state(url):
|
| 99 |
-
# print("##### URL")
|
| 100 |
-
# print(url)
|
| 101 |
-
full_url = "https://" + url
|
| 102 |
-
return full_url, full_url
|
| 103 |
-
|
| 104 |
css = """
|
| 105 |
.gradio-container {background-color: beige}
|
| 106 |
button.gallery-item {background-color: grey}
|
|
@@ -108,11 +153,6 @@ button.gallery-item {background-color: grey}
|
|
| 108 |
h1 {background-color: grey; width: 180px}
|
| 109 |
"""
|
| 110 |
|
| 111 |
-
# css = """
|
| 112 |
-
# .gradio-container {background-color: beige}
|
| 113 |
-
# .gallery-item {
|
| 114 |
-
# """
|
| 115 |
-
|
| 116 |
with gr.Blocks(theme=theme, title="New Look", css=css) as demo:
|
| 117 |
gr.Markdown(
|
| 118 |
"""
|
|
@@ -124,91 +164,84 @@ with gr.Blocks(theme=theme, title="New Look", css=css) as demo:
|
|
| 124 |
</div>
|
| 125 |
""")
|
| 126 |
|
| 127 |
-
# gr.Markdown(
|
| 128 |
-
# """
|
| 129 |
-
# # Hello World!
|
| 130 |
-
# Start typing below to see the output.
|
| 131 |
-
# """, primary_color=gr.themes.colors.stone, secondary_color=gr.themes.colors.stone, neutral_color=gr.themes.colors.stone)
|
| 132 |
-
|
| 133 |
with gr.Tab(label="Search for images"):
|
| 134 |
-
# with gr.TabItem(label="Search for images"):
|
| 135 |
|
| 136 |
-
with gr.Row().style(equal_height=False):
|
| 137 |
-
text_input = gr.Text(label="Search with text:")
|
| 138 |
-
text_relevance = gr.Slider(label="Text search relevance", minimum = -5, maximum = 5, value = 1, step = 1)
|
| 139 |
-
image_input = gr.Image(type="pil", label="Search with an image")
|
| 140 |
-
image_path = gr.State(visible=False)
|
| 141 |
-
image_relevance = gr.Slider(label="Image search relevance", minimum = -5, maximum = 5, value = 1, step = 1)
|
| 142 |
with gr.Row():
|
| 143 |
-
gr.
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
gr.Markdown()
|
| 158 |
-
# with gr.Row().style(equal_height=False):
|
| 159 |
-
# gr.Markdown()
|
| 160 |
-
# image_input = gr.Image(type="pil", label="Search with an image")
|
| 161 |
-
# image_relevance = gr.Slider(label="Image search relevance", minimum = -5, maximum = 5, value = 1, step = 1)
|
| 162 |
-
# gr.Markdown(scale=10)
|
| 163 |
-
# with gr.Row():
|
| 164 |
-
# gr.Markdown()
|
| 165 |
-
# gr.Examples(
|
| 166 |
-
# ["../../../Documents/images/2272.jpg",
|
| 167 |
-
# "../../../Documents/images/2697.jpg"],
|
| 168 |
-
# image_input)
|
| 169 |
-
# gr.Markdown()
|
| 170 |
-
# gr.Markdown()
|
| 171 |
-
|
| 172 |
-
with gr.Row():
|
| 173 |
-
gr.Markdown()
|
| 174 |
-
best_seller_score_weight = gr.Slider(label = "Best seller relevance", minimum=-1, maximum=1, value=0, step=0.01)
|
| 175 |
-
gr.Markdown()
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
# image_input = gr.Image(type="pil", label="Search with an image")
|
| 180 |
-
# image_relevance = gr.Slider(label="Image search relevance", minimum = -5, maximum = 5, value = 1, step = 1)
|
| 181 |
-
with gr.Row():
|
| 182 |
-
gr.Markdown()
|
| 183 |
-
search_button = gr.Button(value="Search")
|
| 184 |
-
gr.Markdown()
|
| 185 |
|
| 186 |
-
with gr.Row():
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
|
| 194 |
-
with gr.Tab(label="Search for images"):
|
| 195 |
-
labels_input = gr.Text(label="List of labels")
|
| 196 |
-
gr.Examples(
|
| 197 |
-
["shirt, dress, shoe",
|
| 198 |
-
"short_sleeve, long_sleeve, three_quarter_sleeve, sleeveless, bell_sleeve"],
|
| 199 |
-
labels_input)
|
| 200 |
with gr.Row():
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
gr.
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
with gr.Tab(label="Choose dataset"):
|
| 214 |
gr.Markdown("# Choose Dataset")
|
|
@@ -222,87 +255,93 @@ with gr.Blocks(theme=theme, title="New Look", css=css) as demo:
|
|
| 222 |
gr.Markdown()
|
| 223 |
|
| 224 |
def load(image_input):
|
| 225 |
-
file_name = f"{datetime.utcnow().strftime('%s')}.jpg"
|
|
|
|
| 226 |
# file_path = static_dir / file_name
|
| 227 |
file_path = "static/" + file_name
|
| 228 |
print(file_path)
|
| 229 |
image_input.save(file_path)
|
| 230 |
return "https://minderalabs-newlook.hf.space/file=" + file_path
|
| 231 |
|
| 232 |
-
def search(text_input, image_input, image_path, text_relevance, image_relevance, best_seller_score_weight):
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
empty_response = [None] * 5
|
| 235 |
empty_response.append("")
|
| 236 |
-
return
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
else:
|
| 245 |
query = dict()
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
list_image_results = []
|
| 252 |
response = search_images(query, best_seller_score_weight)
|
| 253 |
-
for i in range(len(response)):
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
list_image_results.append(Image.open(r"img_res_path_" + str(i) + r".jpg"))
|
| 258 |
|
| 259 |
-
return
|
| 260 |
|
| 261 |
-
def get_labels(labels_input, image_labels_input):
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
|
| 267 |
# search_button.click(
|
| 268 |
-
# search, [text_input, image_input, image_path, text_relevance, image_relevance, best_seller_score_weight],
|
| 269 |
# )
|
| 270 |
search_button.click(
|
| 271 |
load, image_input, image_path
|
| 272 |
).then(
|
| 273 |
-
search, [text_input, image_input, image_path, text_relevance, image_relevance, best_seller_score_weight], [
|
| 274 |
-
)
|
| 275 |
-
|
| 276 |
-
compute_button.click(
|
| 277 |
-
get_labels, [labels_input, image_labels_input], [bar_plot, response_labels]
|
| 278 |
)
|
| 279 |
|
| 280 |
-
#
|
| 281 |
-
#
|
| 282 |
-
# ).then(
|
| 283 |
-
# respond, response, [image_res_1, image_res_2, image_res_3, image_res_4, image_res_5, response]
|
| 284 |
-
# )
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
# response = isComplete_state.change(
|
| 288 |
-
# lambda: gr.update(interactive=False), None, [user_input], queue=False
|
| 289 |
-
# ).then(
|
| 290 |
-
# respond_itinerary, [chatbot, isComplete_state, dataCollected_state], [chatbot, map, result_df]
|
| 291 |
-
# ).then(
|
| 292 |
-
# lambda: gr.update(visible=True), None, [map], queue=False
|
| 293 |
-
# ).then(
|
| 294 |
-
# lambda: gr.update(visible=True), None, [result_df], queue=False
|
| 295 |
-
# ).then(
|
| 296 |
-
# lambda: gr.update(visible=False), None, [text_map_before_itinerary], queue=False
|
| 297 |
# )
|
| 298 |
|
| 299 |
-
# response.then(
|
| 300 |
-
# lambda: gr.update(interactive=True), None, [user_input], queue=False
|
| 301 |
-
# )
|
| 302 |
-
|
| 303 |
-
# if map != None:
|
| 304 |
-
# map.update(visible=True)
|
| 305 |
-
# result_df.update(visible=True)
|
| 306 |
-
|
| 307 |
demo.queue()
|
| 308 |
demo.launch()
|
|
|
|
| 14 |
from pathlib import Path
|
| 15 |
from datetime import datetime
|
| 16 |
import time
|
| 17 |
+
import webbrowser
|
| 18 |
|
| 19 |
from transformers import CLIPProcessor, CLIPModel
|
| 20 |
|
|
|
|
| 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 |
+
|
| 29 |
# sys.path.insert(1, 'C:/Users/Alexandre/Documents/University/5_Ano/Estagio/repos_1')
|
| 30 |
|
| 31 |
# Create custom Color objects for our primary, secondary, and neutral colors
|
|
|
|
| 51 |
font_mono=font_mono
|
| 52 |
)
|
| 53 |
|
| 54 |
+
def filter_by_column(dataset, search_term, column_name) -> pd.DataFrame:
|
| 55 |
+
return dataset[dataset[column_name].str.contains(search_term)]
|
| 56 |
+
|
| 57 |
+
def dedup_by(dataset, column_name) -> pd.DataFrame:
|
| 58 |
+
return dataset.drop_duplicates(subset=[column_name])
|
| 59 |
+
|
| 60 |
+
def drop_secondary_images(dataset) -> pd.DataFrame:
|
| 61 |
+
dataset.image = dataset.primary_image
|
| 62 |
+
return dataset.drop_duplicates(subset=['primary_image'])
|
| 63 |
+
|
| 64 |
+
def dataset_to_gallery(dataset: pd.DataFrame) -> list:
|
| 65 |
+
# convert to list of tuples
|
| 66 |
+
new_df = dataset[['_id', 'image', 'name', 'colour_code']].copy()
|
| 67 |
+
new_df['name_code_combined'] = new_df['name'] + '@@' + new_df['colour_code'].astype(str) + '@@' + new_df['image'].astype(str) + '@@' + new_df['_id'].astype(str)
|
| 68 |
+
final_df = new_df[['image', 'name_code_combined']]
|
| 69 |
+
items = final_df.to_records(index=False).tolist()
|
| 70 |
+
return items
|
| 71 |
+
|
| 72 |
+
def get_items_from_dataset(start_index=0, end_index=50, dataset=pd.read_json('{}')) -> pd.DataFrame:
|
| 73 |
+
df = dataset.sort_values(by=['best_seller_score'], ascending=False)
|
| 74 |
+
return df[start_index:end_index]
|
| 75 |
+
|
| 76 |
+
# def return_page(page, dataset: pd.DataFrame):
|
| 77 |
+
# start_index = page * result_per_page
|
| 78 |
+
# end_index = (page + 1) * result_per_page
|
| 79 |
+
# df = get_items_from_dataset(start_index, end_index, dataset)
|
| 80 |
+
# return dataset_to_gallery(dedup_by(df, 'colour_code'))
|
| 81 |
+
|
| 82 |
+
def start_page(num_results=50):
|
| 83 |
+
result = client.index("new_look_expanded_dresses").search("Dress", score_modifiers = {
|
| 84 |
+
"add_to_score": [{"field_name": "best_seller_score","weight": 5}],
|
| 85 |
+
}, searchable_attributes=['image'], device="cpu", limit=num_results)
|
| 86 |
+
imgs = [r for r in result["hits"]]
|
| 87 |
+
return return_results_page(imgs)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def return_results_page(results_list: list):
|
| 91 |
+
df = pd.DataFrame(results_list)
|
| 92 |
+
return dataset_to_gallery(drop_secondary_images(df))
|
| 93 |
+
|
| 94 |
+
def return_item(combined) -> list:
|
| 95 |
+
colour_code = combined.split("@@")[1]
|
| 96 |
+
result = client.index("new_look_expanded_dresses").search("", filter_string = "colour_code:" + str(colour_code), searchable_attributes=['image'], device="cpu")
|
| 97 |
+
imgs = [r for r in result["hits"]]
|
| 98 |
+
df = pd.DataFrame(imgs)
|
| 99 |
+
return dataset_to_gallery(df), imgs[0]["description_total"], imgs[0]["url"]
|
| 100 |
|
| 101 |
+
def return_primary_item(combined) -> list:
|
| 102 |
+
_id = combined.split("@@")[3]
|
| 103 |
+
result = client.index("new_look_expanded_dresses").search("", filter_string = "_id:" + str(_id), searchable_attributes=['image'], device="cpu")
|
| 104 |
+
imgs = [r for r in result["hits"]]
|
| 105 |
+
print(imgs)
|
| 106 |
+
df = pd.DataFrame(imgs)
|
| 107 |
+
return dataset_to_gallery(df)[0][0]
|
| 108 |
+
|
| 109 |
+
### Load local
|
| 110 |
def load_image(image_input):
|
| 111 |
+
image_input.save("../../../Documents/images/img_path.jpg")
|
| 112 |
+
os.system('docker cp "../../../Documents/images/img_path.jpg" marqo:"/images/images/"')
|
| 113 |
|
| 114 |
### Search local
|
| 115 |
+
def search_images(query, best_seller_score_weight):
|
| 116 |
+
result = client.index("new_look_expanded_dresses").search(query, score_modifiers = {
|
| 117 |
+
"add_to_score": [{"field_name": "best_seller_score","weight": best_seller_score_weight/1000}],
|
| 118 |
+
}, searchable_attributes=['image'], device="cpu", limit=40)
|
| 119 |
+
imgs = [r for r in result["hits"]]
|
| 120 |
+
return imgs
|
| 121 |
+
|
| 122 |
+
### Search AWS
|
| 123 |
# def search_images(query, best_seller_score_weight):
|
| 124 |
+
# client = Client("http://ec2-54-220-125-165.eu-west-1.compute.amazonaws.com:8882")
|
| 125 |
+
# result = client.index("new_look_expanded_dresses").search(query, score_modifiers = {
|
| 126 |
# "add_to_score": [{"field_name": "best_seller_score","weight": best_seller_score_weight/1000}],
|
| 127 |
+
# }, searchable_attributes=['primary_image'], device="cpu", limit=40)
|
| 128 |
# imgs = [r for r in result["hits"]]
|
| 129 |
|
| 130 |
# return imgs
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
def get_labels_probs(labels, image):
|
| 133 |
inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
|
| 134 |
|
|
|
|
| 146 |
|
| 147 |
return fig
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
css = """
|
| 150 |
.gradio-container {background-color: beige}
|
| 151 |
button.gallery-item {background-color: grey}
|
|
|
|
| 153 |
h1 {background-color: grey; width: 180px}
|
| 154 |
"""
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
with gr.Blocks(theme=theme, title="New Look", css=css) as demo:
|
| 157 |
gr.Markdown(
|
| 158 |
"""
|
|
|
|
| 164 |
</div>
|
| 165 |
""")
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
with gr.Tab(label="Search for images"):
|
|
|
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
with gr.Row():
|
| 170 |
+
with gr.Column(scale=3):
|
| 171 |
+
text_input = gr.Text(label="Search with text:")
|
| 172 |
+
text_relevance = gr.Slider(label="Text search relevance", minimum = -5, maximum = 5, value = 1, step = 1)
|
| 173 |
+
text_input_1 = gr.Text(label="Search with text:", visible=False)
|
| 174 |
+
text_relevance_1 = gr.Slider(label="Text search relevance", minimum = -5, maximum = 5, value = 1, step = 1, visible=False)
|
| 175 |
+
more_text_search = gr.Button(value="Search with more text")
|
| 176 |
+
text_expanded = gr.State(value=False)
|
| 177 |
+
with gr.Column(scale=3):
|
| 178 |
+
best_seller_score_weight = gr.Slider(label = "Best seller relevance", minimum=-1, maximum=1, value=0, step=0.01)
|
| 179 |
+
search_button = gr.Button(value="Search")
|
| 180 |
+
with gr.Column(scale=2):
|
| 181 |
+
image_input = gr.Image(type="pil", label="Search with an image")
|
| 182 |
+
image_path = gr.State(visible=False)
|
| 183 |
+
image_relevance = gr.Slider(label="Image search relevance", minimum = -5, maximum = 5, value = 1, step = 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
# with gr.Row():
|
| 186 |
+
# with gr.Column(scale=3):
|
| 187 |
+
# ...
|
| 188 |
+
# with gr.Column(scale=3):
|
| 189 |
+
# search_button = gr.Button(value="Search")
|
| 190 |
+
# with gr.Column(scale=2):
|
| 191 |
+
# ...
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
with gr.Row():
|
| 194 |
+
with gr.Column(scale=3):
|
| 195 |
+
images_gallery = gr.Gallery(value=start_page(), columns=4,
|
| 196 |
+
allow_preview=False, show_label=False, object_fit="contain")
|
| 197 |
+
with gr.Column():
|
| 198 |
+
detail_gallery = gr.Gallery(value=[], columns=2, allow_preview=False, show_label=False, rows=1,
|
| 199 |
+
height="400",object_fit="contain")
|
| 200 |
+
image_description = gr.Text(label="Description")
|
| 201 |
+
product_link = gr.State()
|
| 202 |
+
button_go_to_page = gr.Button(value="Go to product page")
|
| 203 |
+
|
| 204 |
+
def on_new_text_box(more_text_search): # SelectData is a subclass of EventData
|
| 205 |
+
if more_text_search == "Search with more text":
|
| 206 |
+
return gr.update(visible=True, interactive=True), gr.update(visible=True, interactive=True), gr.update(value="Hide extra text box")
|
| 207 |
+
else:
|
| 208 |
+
return gr.update(value="", visible=False, interactive=False), gr.update(visible=False, interactive=False), gr.update(value="Search with more text")
|
| 209 |
+
|
| 210 |
+
def on_focus(evt: gr.SelectData): # SelectData is a subclass of EventData
|
| 211 |
+
return return_item(evt.value)
|
| 212 |
+
|
| 213 |
+
def on_new_image_to_search(images, evt: gr.SelectData): # SelectData is a subclass of EventData
|
| 214 |
+
return return_primary_item(evt.value)
|
| 215 |
+
|
| 216 |
+
def on_go_to_product_page(product_link):
|
| 217 |
+
try:
|
| 218 |
+
webbrowser.open(product_link)
|
| 219 |
+
except:
|
| 220 |
+
print("Not able to open product page")
|
| 221 |
+
|
| 222 |
+
more_text_search.click(on_new_text_box, more_text_search, [text_input_1, text_relevance_1, more_text_search])
|
| 223 |
+
images_gallery.select(on_focus, None, [detail_gallery, image_description, product_link])
|
| 224 |
+
detail_gallery.select(on_new_image_to_search, detail_gallery, image_input)
|
| 225 |
+
button_go_to_page.click(on_go_to_product_page, product_link, None)
|
| 226 |
+
|
| 227 |
+
# with gr.Tab(label="Search for images"):
|
| 228 |
+
# labels_input = gr.Text(label="List of labels")
|
| 229 |
+
# gr.Examples(
|
| 230 |
+
# ["shirt, dress, shoe",
|
| 231 |
+
# "short_sleeve, long_sleeve, three_quarter_sleeve, sleeveless, bell_sleeve"],
|
| 232 |
+
# labels_input)
|
| 233 |
+
# with gr.Row():
|
| 234 |
+
# image_labels_input = gr.Image(type="pil", label="Image to compute")
|
| 235 |
+
# bar_plot = gr.Plot()
|
| 236 |
+
# with gr.Row():
|
| 237 |
+
# gr.Examples(
|
| 238 |
+
# ["https://media2.newlookassets.com/i/newlook/869030934/womens/clothing/dresses/khaki-utility-mini-shirt-dress.jpg?strip=true&qlt=50&w=1400",
|
| 239 |
+
# "https://media3.newlookassets.com/i/newlook/872692409/womens/clothing/dresses/black-floral-lace-trim-mini-dress.jpg?strip=true&qlt=50&w=1400"],
|
| 240 |
+
# image_labels_input)
|
| 241 |
+
# gr.Markdown()
|
| 242 |
+
# compute_button = gr.Button(value="Compute")
|
| 243 |
+
|
| 244 |
+
# response_labels = gr.Text()
|
| 245 |
|
| 246 |
with gr.Tab(label="Choose dataset"):
|
| 247 |
gr.Markdown("# Choose Dataset")
|
|
|
|
| 255 |
gr.Markdown()
|
| 256 |
|
| 257 |
def load(image_input):
|
| 258 |
+
# file_name = f"{datetime.utcnow().strftime('%s')}.jpg"
|
| 259 |
+
file_name = f"image_to_search.jpg"
|
| 260 |
# file_path = static_dir / file_name
|
| 261 |
file_path = "static/" + file_name
|
| 262 |
print(file_path)
|
| 263 |
image_input.save(file_path)
|
| 264 |
return "https://minderalabs-newlook.hf.space/file=" + file_path
|
| 265 |
|
| 266 |
+
def search(text_input, text_input_1, image_input, image_path, text_relevance, text_relevance_1, image_relevance, best_seller_score_weight):
|
| 267 |
+
all_queries = [text_input, text_input_1, image_input]
|
| 268 |
+
print(all_queries)
|
| 269 |
+
all_queries_relevance = [text_relevance, text_relevance_1, image_relevance]
|
| 270 |
+
print(all_queries_relevance)
|
| 271 |
+
query_is_none = [True if (query == None or query == "") else False for query in all_queries]
|
| 272 |
+
print(query_is_none)
|
| 273 |
+
if sum([1 if query == False else 0 for query in query_is_none]) == 0:
|
| 274 |
empty_response = [None] * 5
|
| 275 |
empty_response.append("")
|
| 276 |
+
return []
|
| 277 |
+
elif sum([1 if query == False else 0 for query in query_is_none]) == 1:
|
| 278 |
+
for i in range(3):
|
| 279 |
+
if query_is_none[i] == False:
|
| 280 |
+
### Code to run locally
|
| 281 |
+
# if i == 2:
|
| 282 |
+
# load_image(image_input)
|
| 283 |
+
# query = "/images/images/img_path.jpg"
|
| 284 |
+
# break
|
| 285 |
+
###
|
| 286 |
+
query = all_queries[i]
|
| 287 |
+
break
|
| 288 |
else:
|
| 289 |
query = dict()
|
| 290 |
+
for i in range(3):
|
| 291 |
+
if query_is_none[i] == False:
|
| 292 |
+
### Code to run locally
|
| 293 |
+
# if i == 2:
|
| 294 |
+
# load_image(image_input)
|
| 295 |
+
# query["/images/images/img_path.jpg"] = image_relevance
|
| 296 |
+
# continue
|
| 297 |
+
###
|
| 298 |
+
query[all_queries[i]] = all_queries_relevance[i]
|
| 299 |
+
|
| 300 |
+
# if text_input == "" and image_input == None:
|
| 301 |
+
# empty_response = [None] * 5
|
| 302 |
+
# empty_response.append("")
|
| 303 |
+
# return empty_response
|
| 304 |
+
|
| 305 |
+
# if text_input == "":
|
| 306 |
+
# load_image(image_input)
|
| 307 |
+
# query = "/images/images/img_path.jpg"
|
| 308 |
+
# # query = image_path
|
| 309 |
+
# elif image_input == None:
|
| 310 |
+
# query = text_input
|
| 311 |
+
# else:
|
| 312 |
+
# query = dict()
|
| 313 |
+
# load_image(image_input)
|
| 314 |
+
# query["/images/images/img_path.jpg"] = image_relevance
|
| 315 |
+
# # query[image_path] = image_relevance
|
| 316 |
+
# query[text_input] = text_relevance
|
| 317 |
|
| 318 |
list_image_results = []
|
| 319 |
response = search_images(query, best_seller_score_weight)
|
| 320 |
+
# for i in range(len(response)):
|
| 321 |
+
# urllib.request.urlretrieve(response[i]["primary_image"], "img_res_path_" + str(i) + ".jpg")
|
| 322 |
+
# list_image_results.append(Image.open(r"img_res_path_" + str(i) + r".jpg"))
|
| 323 |
+
|
|
|
|
| 324 |
|
| 325 |
+
return return_results_page(response)
|
| 326 |
|
| 327 |
+
# def get_labels(labels_input, image_labels_input):
|
| 328 |
+
# labels_probs = get_labels_probs(labels_input.split(","), image_labels_input)
|
| 329 |
+
# bar_plot = get_bar_plot(labels_input.split(","), labels_probs)
|
| 330 |
+
# return bar_plot, labels_probs
|
| 331 |
|
| 332 |
|
| 333 |
# search_button.click(
|
| 334 |
+
# search, [text_input, text_input_1, image_input, image_path, text_relevance, text_relevance_1, image_relevance, best_seller_score_weight], images_gallery
|
| 335 |
# )
|
| 336 |
search_button.click(
|
| 337 |
load, image_input, image_path
|
| 338 |
).then(
|
| 339 |
+
search, [text_input, text_input_1, image_input, image_path, text_relevance, text_relevance_1, image_relevance, best_seller_score_weight], [images_gallery]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
)
|
| 341 |
|
| 342 |
+
# compute_button.click(
|
| 343 |
+
# get_labels, [labels_input, image_labels_input], [bar_plot, response_labels]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
# )
|
| 345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
demo.queue()
|
| 347 |
demo.launch()
|