Spaces:
Running
Running
Niki Zhang
commited on
2 recommendation gallery
Browse files
app.py
CHANGED
|
@@ -349,11 +349,12 @@ def extract_features_siglip(image):
|
|
| 349 |
@spaces.GPU
|
| 350 |
def infer(crop_image_path,full_image_path,state,language,task_type=None):
|
| 351 |
print("task type",task_type)
|
| 352 |
-
|
|
|
|
| 353 |
|
| 354 |
if task_type=="task 1":
|
| 355 |
-
|
| 356 |
-
|
| 357 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 358 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
| 359 |
input_features = input_features.detach().cpu().numpy()
|
|
@@ -364,17 +365,17 @@ def infer(crop_image_path,full_image_path,state,language,task_type=None):
|
|
| 364 |
sim = -distances[0][i]
|
| 365 |
image_url = df.iloc[v]["Link"]
|
| 366 |
img_retrieved = read_image_from_url(image_url)
|
| 367 |
-
|
| 368 |
if language=="English":
|
| 369 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 370 |
else:
|
| 371 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 372 |
state+=[(None,msg)]
|
| 373 |
|
| 374 |
-
return
|
| 375 |
elif task_type=="task 2":
|
| 376 |
-
|
| 377 |
-
|
| 378 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 379 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
| 380 |
input_features = input_features.detach().cpu().numpy()
|
|
@@ -385,18 +386,18 @@ def infer(crop_image_path,full_image_path,state,language,task_type=None):
|
|
| 385 |
sim = -distances[0][i]
|
| 386 |
image_url = df.iloc[v]["Link"]
|
| 387 |
img_retrieved = read_image_from_url(image_url)
|
| 388 |
-
|
| 389 |
if language=="English":
|
| 390 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 391 |
else:
|
| 392 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 393 |
state+=[(None,msg)]
|
| 394 |
|
| 395 |
-
return
|
| 396 |
|
| 397 |
elif task_type=="task 3":
|
| 398 |
-
|
| 399 |
-
|
| 400 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 401 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
| 402 |
input_features = input_features.detach().cpu().numpy()
|
|
@@ -407,14 +408,14 @@ def infer(crop_image_path,full_image_path,state,language,task_type=None):
|
|
| 407 |
sim = -distances[0][i]
|
| 408 |
image_url = df.iloc[v]["Link"]
|
| 409 |
img_retrieved = read_image_from_url(image_url)
|
| 410 |
-
|
| 411 |
if language=="English":
|
| 412 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 413 |
else:
|
| 414 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 415 |
state+=[(None,msg)]
|
| 416 |
|
| 417 |
-
return
|
| 418 |
|
| 419 |
elif crop_image_path:
|
| 420 |
input_image = Image.open(crop_image_path).convert("RGB")
|
|
@@ -427,7 +428,7 @@ def infer(crop_image_path,full_image_path,state,language,task_type=None):
|
|
| 427 |
sim = -distances[0][i]
|
| 428 |
image_url = df.iloc[v]["Link"]
|
| 429 |
img_retrieved = read_image_from_url(image_url)
|
| 430 |
-
|
| 431 |
|
| 432 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 433 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
|
@@ -439,14 +440,14 @@ def infer(crop_image_path,full_image_path,state,language,task_type=None):
|
|
| 439 |
sim = -distances[0][i]
|
| 440 |
image_url = df.iloc[v]["Link"]
|
| 441 |
img_retrieved = read_image_from_url(image_url)
|
| 442 |
-
|
| 443 |
if language=="English":
|
| 444 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 445 |
else:
|
| 446 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 447 |
state+=[(None,msg)]
|
| 448 |
|
| 449 |
-
return
|
| 450 |
else:
|
| 451 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 452 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
|
@@ -458,14 +459,14 @@ def infer(crop_image_path,full_image_path,state,language,task_type=None):
|
|
| 458 |
sim = -distances[0][i]
|
| 459 |
image_url = df.iloc[v]["Link"]
|
| 460 |
img_retrieved = read_image_from_url(image_url)
|
| 461 |
-
|
| 462 |
if language=="English":
|
| 463 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 464 |
else:
|
| 465 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 466 |
state+=[(None,msg)]
|
| 467 |
|
| 468 |
-
return
|
| 469 |
|
| 470 |
|
| 471 |
|
|
@@ -1646,10 +1647,10 @@ async def texttospeech(text, language,gender='female'):
|
|
| 1646 |
return None
|
| 1647 |
|
| 1648 |
# give the reason of recommendation
|
| 1649 |
-
async def
|
| 1650 |
persona=naritive_mapping[narritive]
|
| 1651 |
rec_path=evt._data['value']['image']['path']
|
| 1652 |
-
index=evt.index
|
| 1653 |
print("rec_path",rec_path)
|
| 1654 |
prompt=recommendation_prompt[persona].format(language=language,length=length)
|
| 1655 |
if new_crop:
|
|
@@ -1670,6 +1671,32 @@ async def associate(image_path,new_crop,openai_api_key,language,autoplay,length,
|
|
| 1670 |
audio_output = await texttospeech(read_info, language)
|
| 1671 |
return state,state,audio_output,log_state,index,gr.update(value=[])
|
| 1672 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1673 |
def change_naritive(session_type,image_input, state, click_state, paragraph, origin_image,narritive,task_instruct,gallery_output,reco_reasons,language="English"):
|
| 1674 |
if session_type=="Session 1":
|
| 1675 |
return None, [], [], [[], [], []], "", None, None, [], [],[]
|
|
@@ -1736,7 +1763,7 @@ def print_like_dislike(x: gr.LikeData,state,log_state):
|
|
| 1736 |
return log_state,state
|
| 1737 |
|
| 1738 |
def get_recommendationscore(index,score,log_state):
|
| 1739 |
-
log_state+=[(f"
|
| 1740 |
log_state+=[("%% recommendation %%",None)]
|
| 1741 |
return log_state
|
| 1742 |
|
|
@@ -2009,9 +2036,24 @@ def create_ui():
|
|
| 2009 |
)
|
| 2010 |
|
| 2011 |
gallery_result = gr.Gallery(
|
| 2012 |
-
label="Recommendations",
|
| 2013 |
height="auto",
|
| 2014 |
-
columns=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2015 |
# columns=4,
|
| 2016 |
# rows=2,
|
| 2017 |
# show_label=False,
|
|
@@ -2208,11 +2250,19 @@ def create_ui():
|
|
| 2208 |
recommend_btn.click(
|
| 2209 |
fn=infer,
|
| 2210 |
inputs=[new_crop_save_path,image_path,state,language,task_type],
|
| 2211 |
-
outputs=[gallery_result,chatbot,state]
|
| 2212 |
)
|
| 2213 |
|
| 2214 |
gallery_result.select(
|
| 2215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2216 |
inputs=[image_path,new_crop_save_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state],
|
| 2217 |
outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
|
| 2218 |
|
|
|
|
| 349 |
@spaces.GPU
|
| 350 |
def infer(crop_image_path,full_image_path,state,language,task_type=None):
|
| 351 |
print("task type",task_type)
|
| 352 |
+
style_gallery_output = []
|
| 353 |
+
item_gallery_output=[]
|
| 354 |
|
| 355 |
if task_type=="task 1":
|
| 356 |
+
item_gallery_output.append("recomendation_pic/1.8.jpg")
|
| 357 |
+
item_gallery_output.append("recomendation_pic/1.9.jpg")
|
| 358 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 359 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
| 360 |
input_features = input_features.detach().cpu().numpy()
|
|
|
|
| 365 |
sim = -distances[0][i]
|
| 366 |
image_url = df.iloc[v]["Link"]
|
| 367 |
img_retrieved = read_image_from_url(image_url)
|
| 368 |
+
style_gallery_output.append(img_retrieved)
|
| 369 |
if language=="English":
|
| 370 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 371 |
else:
|
| 372 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 373 |
state+=[(None,msg)]
|
| 374 |
|
| 375 |
+
return item_gallery_output, style_gallery_output,state,state
|
| 376 |
elif task_type=="task 2":
|
| 377 |
+
item_gallery_output.append("recomendation_pic/2.8.jpg")
|
| 378 |
+
item_gallery_output.append("recomendation_pic/2.9.png")
|
| 379 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 380 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
| 381 |
input_features = input_features.detach().cpu().numpy()
|
|
|
|
| 386 |
sim = -distances[0][i]
|
| 387 |
image_url = df.iloc[v]["Link"]
|
| 388 |
img_retrieved = read_image_from_url(image_url)
|
| 389 |
+
style_gallery_output.append(img_retrieved)
|
| 390 |
if language=="English":
|
| 391 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 392 |
else:
|
| 393 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 394 |
state+=[(None,msg)]
|
| 395 |
|
| 396 |
+
return item_gallery_output, style_gallery_output,state,state
|
| 397 |
|
| 398 |
elif task_type=="task 3":
|
| 399 |
+
item_gallery_output.append("recomendation_pic/3.8.png")
|
| 400 |
+
item_gallery_output.append("recomendation_pic/3.9.png")
|
| 401 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 402 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
| 403 |
input_features = input_features.detach().cpu().numpy()
|
|
|
|
| 408 |
sim = -distances[0][i]
|
| 409 |
image_url = df.iloc[v]["Link"]
|
| 410 |
img_retrieved = read_image_from_url(image_url)
|
| 411 |
+
style_gallery_output.append(img_retrieved)
|
| 412 |
if language=="English":
|
| 413 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 414 |
else:
|
| 415 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 416 |
state+=[(None,msg)]
|
| 417 |
|
| 418 |
+
return item_gallery_output, style_gallery_output,state,state
|
| 419 |
|
| 420 |
elif crop_image_path:
|
| 421 |
input_image = Image.open(crop_image_path).convert("RGB")
|
|
|
|
| 428 |
sim = -distances[0][i]
|
| 429 |
image_url = df.iloc[v]["Link"]
|
| 430 |
img_retrieved = read_image_from_url(image_url)
|
| 431 |
+
item_gallery_output.append(img_retrieved)
|
| 432 |
|
| 433 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 434 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
|
|
|
| 440 |
sim = -distances[0][i]
|
| 441 |
image_url = df.iloc[v]["Link"]
|
| 442 |
img_retrieved = read_image_from_url(image_url)
|
| 443 |
+
style_gallery_output.append(img_retrieved)
|
| 444 |
if language=="English":
|
| 445 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 446 |
else:
|
| 447 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 448 |
state+=[(None,msg)]
|
| 449 |
|
| 450 |
+
return item_gallery_output, style_gallery_output,state,state
|
| 451 |
else:
|
| 452 |
input_image = Image.open(full_image_path).convert("RGB")
|
| 453 |
input_features = extract_features_siglip(input_image.convert("RGB"))
|
|
|
|
| 459 |
sim = -distances[0][i]
|
| 460 |
image_url = df.iloc[v]["Link"]
|
| 461 |
img_retrieved = read_image_from_url(image_url)
|
| 462 |
+
style_gallery_output.append(img_retrieved)
|
| 463 |
if language=="English":
|
| 464 |
msg="🖼️ Please refer to the section below to see the recommended results."
|
| 465 |
else:
|
| 466 |
msg="🖼️ 请到下方查看推荐结果。"
|
| 467 |
state+=[(None,msg)]
|
| 468 |
|
| 469 |
+
return item_gallery_output, style_gallery_output,state,state
|
| 470 |
|
| 471 |
|
| 472 |
|
|
|
|
| 1647 |
return None
|
| 1648 |
|
| 1649 |
# give the reason of recommendation
|
| 1650 |
+
async def item_associate(image_path,new_crop,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,evt: gr.SelectData):
|
| 1651 |
persona=naritive_mapping[narritive]
|
| 1652 |
rec_path=evt._data['value']['image']['path']
|
| 1653 |
+
index="Item Recommendation Picture "+str(evt.index)
|
| 1654 |
print("rec_path",rec_path)
|
| 1655 |
prompt=recommendation_prompt[persona].format(language=language,length=length)
|
| 1656 |
if new_crop:
|
|
|
|
| 1671 |
audio_output = await texttospeech(read_info, language)
|
| 1672 |
return state,state,audio_output,log_state,index,gr.update(value=[])
|
| 1673 |
|
| 1674 |
+
|
| 1675 |
+
async def style_associate(image_path,new_crop,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,evt: gr.SelectData):
|
| 1676 |
+
persona=naritive_mapping[narritive]
|
| 1677 |
+
rec_path=evt._data['value']['image']['path']
|
| 1678 |
+
index="Style Recommendation Picture "+str(evt.index)
|
| 1679 |
+
print("rec_path",rec_path)
|
| 1680 |
+
prompt=recommendation_prompt[persona].format(language=language,length=length)
|
| 1681 |
+
if new_crop:
|
| 1682 |
+
image_paths=[new_crop,rec_path]
|
| 1683 |
+
else:
|
| 1684 |
+
image_paths=[image_path,rec_path]
|
| 1685 |
+
result=get_gpt_response(openai_api_key, image_paths, prompt)
|
| 1686 |
+
print("recommend result",result)
|
| 1687 |
+
state += [(None, f"{result}")]
|
| 1688 |
+
log_state = log_state + [(narritive, None)]
|
| 1689 |
+
log_state = log_state + [(f"image sort ranking {sort_score}", None)]
|
| 1690 |
+
log_state = log_state + [(None, f"{result}")]
|
| 1691 |
+
read_info = re.sub(r'[#[\]!*]','',result)
|
| 1692 |
+
read_info = emoji.replace_emoji(read_info,replace="")
|
| 1693 |
+
print("associate",read_info)
|
| 1694 |
+
audio_output=None
|
| 1695 |
+
if autoplay:
|
| 1696 |
+
audio_output = await texttospeech(read_info, language)
|
| 1697 |
+
return state,state,audio_output,log_state,index,gr.update(value=[])
|
| 1698 |
+
|
| 1699 |
+
|
| 1700 |
def change_naritive(session_type,image_input, state, click_state, paragraph, origin_image,narritive,task_instruct,gallery_output,reco_reasons,language="English"):
|
| 1701 |
if session_type=="Session 1":
|
| 1702 |
return None, [], [], [[], [], []], "", None, None, [], [],[]
|
|
|
|
| 1763 |
return log_state,state
|
| 1764 |
|
| 1765 |
def get_recommendationscore(index,score,log_state):
|
| 1766 |
+
log_state+=[(f"{index} : {score}",None)]
|
| 1767 |
log_state+=[("%% recommendation %%",None)]
|
| 1768 |
return log_state
|
| 1769 |
|
|
|
|
| 2036 |
)
|
| 2037 |
|
| 2038 |
gallery_result = gr.Gallery(
|
| 2039 |
+
label="Recommendations Based on Item",
|
| 2040 |
height="auto",
|
| 2041 |
+
columns=2
|
| 2042 |
+
# columns=4,
|
| 2043 |
+
# rows=2,
|
| 2044 |
+
# show_label=False,
|
| 2045 |
+
# allow_preview=True,
|
| 2046 |
+
# object_fit="contain",
|
| 2047 |
+
# height="auto",
|
| 2048 |
+
# preview=True,
|
| 2049 |
+
# show_share_button=True,
|
| 2050 |
+
# show_download_button=True
|
| 2051 |
+
)
|
| 2052 |
+
|
| 2053 |
+
style_gallery_result = gr.Gallery(
|
| 2054 |
+
label="Recommendations Based on Style",
|
| 2055 |
+
height="auto",
|
| 2056 |
+
columns=2
|
| 2057 |
# columns=4,
|
| 2058 |
# rows=2,
|
| 2059 |
# show_label=False,
|
|
|
|
| 2250 |
recommend_btn.click(
|
| 2251 |
fn=infer,
|
| 2252 |
inputs=[new_crop_save_path,image_path,state,language,task_type],
|
| 2253 |
+
outputs=[gallery_result,style_gallery_result,chatbot,state]
|
| 2254 |
)
|
| 2255 |
|
| 2256 |
gallery_result.select(
|
| 2257 |
+
item_associate,
|
| 2258 |
+
inputs=[image_path,new_crop_save_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state],
|
| 2259 |
+
outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
|
| 2260 |
+
|
| 2261 |
+
|
| 2262 |
+
)
|
| 2263 |
+
|
| 2264 |
+
style_gallery_result.select(
|
| 2265 |
+
style_associate,
|
| 2266 |
inputs=[image_path,new_crop_save_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state],
|
| 2267 |
outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
|
| 2268 |
|