Niki Zhang commited on
Commit
f2528ac
·
verified ·
1 Parent(s): 0e8d82d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -943,7 +943,7 @@ async def chat_input_callback(*args):
943
  return state, state, None, audio,log_state,history
944
 
945
 
946
- async def upload_callback(image_input,state, log_state, visual_chatgpt=None, openai_api_key=None,language="English",narritive=None,history=None,autoplay=False,session="Session 1"):
947
  print("narritive", narritive)
948
  if isinstance(image_input, dict): # if upload from sketcher_input, input contains image and mask
949
  image_input = image_input['background']
@@ -993,11 +993,18 @@ async def upload_callback(image_input,state, log_state, visual_chatgpt=None, ope
993
  visual_chatgpt.current_image = new_image_path
994
  paragraph = get_gpt_response(openai_api_key, new_image_path,f"What's going on in this picture? in {language}")
995
  # visual_chatgpt.agent.memory.buffer = visual_chatgpt.agent.memory.buffer + visual_chatgpt.global_prompt
996
- parsed_data = get_gpt_response(openai_api_key, new_image_path,"Please provide the name, artist, year of creation (including the art historical period), and painting style used for this painting. Return the information in dictionary format without any newline characters. Format as follows: { \"name\": \"Name of the painting\", \"artist\": \"Name of the artist\", \"year\": \"Year of creation (Art historical period)\", \"style\": \"Painting style used in the painting\",\"gender\": \"The gender of the author\"}")
997
- print(parsed_data)
998
- parsed_data = json.loads(parsed_data.replace("'", "\""))
999
- name, artist, year, material,gender= parsed_data["name"],parsed_data["artist"],parsed_data["year"], parsed_data["style"],parsed_data['gender']
1000
- gender=gender.lower()
 
 
 
 
 
 
 
1001
  print("gender",gender)
1002
 
1003
 
@@ -1668,7 +1675,7 @@ async def texttospeech(text, language,gender='female'):
1668
  return None
1669
 
1670
  # give the reason of recommendation
1671
- async def item_associate(new_crop,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,evt: gr.SelectData):
1672
  persona=naritive_mapping[narritive]
1673
  rec_path=evt._data['value']['image']['path']
1674
  index="Item Recommendation Picture "+ str(evt.index)
@@ -1677,9 +1684,10 @@ async def item_associate(new_crop,openai_api_key,language,autoplay,length,log_st
1677
 
1678
  image_paths=[new_crop,rec_path]
1679
 
1680
- result=get_gpt_response(openai_api_key, image_paths, prompt)
1681
  print("recommend result",result)
1682
  state += [(None, f"{result}")]
 
1683
  log_state = log_state + [(narritive, None)]
1684
  log_state = log_state + [(f"image sort ranking {sort_score}", None)]
1685
  log_state = log_state + [(None, f"{result}")]
@@ -1692,16 +1700,17 @@ async def item_associate(new_crop,openai_api_key,language,autoplay,length,log_st
1692
  return state,state,audio_output,log_state,index,gr.update(value=[])
1693
 
1694
 
1695
- async def style_associate(image_path,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,evt: gr.SelectData):
1696
  persona=naritive_mapping[narritive]
1697
  rec_path=evt._data['value']['image']['path']
1698
  index="Style Recommendation Picture "+str(evt.index)
1699
  print("rec_path",rec_path)
1700
  prompt=recommendation_prompt[1][persona].format(language=language,length=length)
1701
  image_paths=[image_path,rec_path]
1702
- result=get_gpt_response(openai_api_key, image_paths, prompt)
1703
  print("recommend result",result)
1704
  state += [(None, f"{result}")]
 
1705
  log_state = log_state + [(narritive, None)]
1706
  log_state = log_state + [(f"image sort ranking {sort_score}", None)]
1707
  log_state = log_state + [(None, f"{result}")]
@@ -1866,7 +1875,7 @@ def create_ui():
1866
  )
1867
  with gr.Row():
1868
  with gr.Column(scale=1,min_width=50,visible=False) as instruct:
1869
- task_instuction=gr.Image(type="pil", interactive=False, elem_classes="task_instruct",height=650,label=None)
1870
  with gr.Column(scale=6):
1871
  with gr.Column(visible=False) as modules_not_need_gpt:
1872
 
@@ -2275,7 +2284,7 @@ def create_ui():
2275
 
2276
  gallery_result.select(
2277
  item_associate,
2278
- inputs=[new_crop_save_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state],
2279
  outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
2280
 
2281
 
@@ -2283,7 +2292,7 @@ def create_ui():
2283
 
2284
  style_gallery_result.select(
2285
  style_associate,
2286
- inputs=[image_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state],
2287
  outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
2288
 
2289
 
@@ -2571,7 +2580,7 @@ def create_ui():
2571
  # name_label_base2, artist_label_base2, year_label_base2, material_label_base2,name_label_traj, artist_label_traj, year_label_traj, material_label_traj, \
2572
  # paragraph,artist,gender,image_path])
2573
 
2574
- image_input.upload(upload_callback, [image_input, state, log_state,visual_chatgpt,openai_api_key,language,naritive,history_log,auto_play,session_type],
2575
  [chatbot, state, origin_image, click_state, image_input, image_input_base, sketcher_input,image_input_base_2,
2576
  image_embedding, original_size, input_size,name_label,artist_label,year_label,material_label,name_label_base, artist_label_base, year_label_base, material_label_base, \
2577
  name_label_base2, artist_label_base2, year_label_base2, material_label_base2,name_label_traj, artist_label_traj, year_label_traj, material_label_traj, \
@@ -2596,7 +2605,7 @@ def create_ui():
2596
  # submit_button_text.click(chat_input_callback, [visual_chatgpt, chat_input, click_state, state, aux_state,language,auto_play],
2597
  # [chatbot, state, aux_state,output_audio])
2598
  # submit_button_text.click(lambda: "", None, chat_input)
2599
- example_image.change(upload_callback, [example_image, state, log_state, visual_chatgpt, openai_api_key,language,naritive,history_log,auto_play,session_type],
2600
  [chatbot, state, origin_image, click_state, image_input, image_input_base, sketcher_input,image_input_base_2,
2601
  image_embedding, original_size, input_size,name_label,artist_label,year_label,material_label,name_label_base, artist_label_base, year_label_base, material_label_base, \
2602
  name_label_base2, artist_label_base2, year_label_base2, material_label_base2,name_label_traj, artist_label_traj, year_label_traj, material_label_traj, \
 
943
  return state, state, None, audio,log_state,history
944
 
945
 
946
+ async def upload_callback(image_input,state, log_state, task_type, visual_chatgpt=None, openai_api_key=None,language="English",narritive=None,history=None,autoplay=False,session="Session 1"):
947
  print("narritive", narritive)
948
  if isinstance(image_input, dict): # if upload from sketcher_input, input contains image and mask
949
  image_input = image_input['background']
 
993
  visual_chatgpt.current_image = new_image_path
994
  paragraph = get_gpt_response(openai_api_key, new_image_path,f"What's going on in this picture? in {language}")
995
  # visual_chatgpt.agent.memory.buffer = visual_chatgpt.agent.memory.buffer + visual_chatgpt.global_prompt
996
+ if task_type=="task 3":
997
+ name="Along the River During the Qingming Festival"
998
+ artist="Zhang Zeduan"
999
+ year="12th century (Song Dynasty)"
1000
+ material="Chinese painting"
1001
+ gender="male"
1002
+ else:
1003
+ parsed_data = get_gpt_response(openai_api_key, new_image_path,"Please provide the name, artist, year of creation (including the art historical period), and painting style used for this painting. Return the information in dictionary format without any newline characters. Format as follows: { \"name\": \"Name of the painting\", \"artist\": \"Name of the artist\", \"year\": \"Year of creation (Art historical period)\", \"style\": \"Painting style used in the painting\",\"gender\": \"The gender of the author\"}")
1004
+ print(parsed_data)
1005
+ parsed_data = json.loads(parsed_data.replace("'", "\""))
1006
+ name, artist, year, material,gender= parsed_data["name"],parsed_data["artist"],parsed_data["year"], parsed_data["style"],parsed_data['gender']
1007
+ gender=gender.lower()
1008
  print("gender",gender)
1009
 
1010
 
 
1675
  return None
1676
 
1677
  # give the reason of recommendation
1678
+ async def item_associate(new_crop,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,history_log, evt: gr.SelectData):
1679
  persona=naritive_mapping[narritive]
1680
  rec_path=evt._data['value']['image']['path']
1681
  index="Item Recommendation Picture "+ str(evt.index)
 
1684
 
1685
  image_paths=[new_crop,rec_path]
1686
 
1687
+ result=get_gpt_response(openai_api_key, image_paths, prompt,history_log)
1688
  print("recommend result",result)
1689
  state += [(None, f"{result}")]
1690
+ log_state += [("User wants to know object recomendation reason", None)]
1691
  log_state = log_state + [(narritive, None)]
1692
  log_state = log_state + [(f"image sort ranking {sort_score}", None)]
1693
  log_state = log_state + [(None, f"{result}")]
 
1700
  return state,state,audio_output,log_state,index,gr.update(value=[])
1701
 
1702
 
1703
+ async def style_associate(image_path,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,history_log,evt: gr.SelectData):
1704
  persona=naritive_mapping[narritive]
1705
  rec_path=evt._data['value']['image']['path']
1706
  index="Style Recommendation Picture "+str(evt.index)
1707
  print("rec_path",rec_path)
1708
  prompt=recommendation_prompt[1][persona].format(language=language,length=length)
1709
  image_paths=[image_path,rec_path]
1710
+ result=get_gpt_response(openai_api_key, image_paths, prompt,history_log)
1711
  print("recommend result",result)
1712
  state += [(None, f"{result}")]
1713
+ log_state += [("User wants to know style recomendation reason", None)]
1714
  log_state = log_state + [(narritive, None)]
1715
  log_state = log_state + [(f"image sort ranking {sort_score}", None)]
1716
  log_state = log_state + [(None, f"{result}")]
 
1875
  )
1876
  with gr.Row():
1877
  with gr.Column(scale=1,min_width=50,visible=False) as instruct:
1878
+ task_instuction=gr.Image(type="pil", interactive=False, elem_classes="task_instruct",height=650,label="Instruction")
1879
  with gr.Column(scale=6):
1880
  with gr.Column(visible=False) as modules_not_need_gpt:
1881
 
 
2284
 
2285
  gallery_result.select(
2286
  item_associate,
2287
+ inputs=[new_crop_save_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state,history_log],
2288
  outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
2289
 
2290
 
 
2292
 
2293
  style_gallery_result.select(
2294
  style_associate,
2295
+ inputs=[image_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state,history_log],
2296
  outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
2297
 
2298
 
 
2580
  # name_label_base2, artist_label_base2, year_label_base2, material_label_base2,name_label_traj, artist_label_traj, year_label_traj, material_label_traj, \
2581
  # paragraph,artist,gender,image_path])
2582
 
2583
+ image_input.upload(upload_callback, [image_input, state, log_state,task_type, visual_chatgpt,openai_api_key,language,naritive,history_log,auto_play,session_type],
2584
  [chatbot, state, origin_image, click_state, image_input, image_input_base, sketcher_input,image_input_base_2,
2585
  image_embedding, original_size, input_size,name_label,artist_label,year_label,material_label,name_label_base, artist_label_base, year_label_base, material_label_base, \
2586
  name_label_base2, artist_label_base2, year_label_base2, material_label_base2,name_label_traj, artist_label_traj, year_label_traj, material_label_traj, \
 
2605
  # submit_button_text.click(chat_input_callback, [visual_chatgpt, chat_input, click_state, state, aux_state,language,auto_play],
2606
  # [chatbot, state, aux_state,output_audio])
2607
  # submit_button_text.click(lambda: "", None, chat_input)
2608
+ example_image.change(upload_callback, [example_image, state, log_state, task_type, visual_chatgpt, openai_api_key,language,naritive,history_log,auto_play,session_type],
2609
  [chatbot, state, origin_image, click_state, image_input, image_input_base, sketcher_input,image_input_base_2,
2610
  image_embedding, original_size, input_size,name_label,artist_label,year_label,material_label,name_label_base, artist_label_base, year_label_base, material_label_base, \
2611
  name_label_base2, artist_label_base2, year_label_base2, material_label_base2,name_label_traj, artist_label_traj, year_label_traj, material_label_traj, \