Niki Zhang commited on
Commit
7dc7f05
·
verified ·
1 Parent(s): 9a7446e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -59
app.py CHANGED
@@ -1009,7 +1009,7 @@ async def upload_callback(image_input,state, log_state, task_type, visual_chatgp
1009
  image_input.save(new_image_path)
1010
  print("img_path",new_image_path)
1011
  visual_chatgpt.current_image = new_image_path
1012
- paragraph = get_gpt_response(openai_api_key, new_image_path,f"What's going on in this picture? in {language}")
1013
  # visual_chatgpt.agent.memory.buffer = visual_chatgpt.agent.memory.buffer + visual_chatgpt.global_prompt
1014
  if task_type=="task 3":
1015
  name="Along the River During the Qingming Festival"
@@ -1017,6 +1017,7 @@ async def upload_callback(image_input,state, log_state, task_type, visual_chatgp
1017
  year="12th century (Song Dynasty)"
1018
  material="Chinese painting"
1019
  gender="male"
 
1020
 
1021
  elif task_type=="task 1":
1022
  name ="The Ambassadors"
@@ -1024,6 +1025,7 @@ async def upload_callback(image_input,state, log_state, task_type, visual_chatgp
1024
  year = "1533 (Northern Renaissance)"
1025
  material="Realism"
1026
  gender = "male"
 
1027
 
1028
  elif task_type=="task 2":
1029
  name = "The Football Players"
@@ -1031,18 +1033,15 @@ async def upload_callback(image_input,state, log_state, task_type, visual_chatgp
1031
  year= "1912 (Cubism)"
1032
  material="Cubism"
1033
  gender= "male"
 
1034
  else:
 
1035
  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\"}")
1036
  print(parsed_data)
1037
  parsed_data = json.loads(parsed_data.replace("'", "\""))
1038
  name, artist, year, material,gender= parsed_data["name"],parsed_data["artist"],parsed_data["year"], parsed_data["style"],parsed_data['gender']
1039
  gender=gender.lower()
1040
- print("gender",gender)
1041
- print("material",material)
1042
-
1043
-
1044
-
1045
-
1046
  if language=="English":
1047
  if naritive_mapping[narritive]==0 :
1048
  msg=f"🤖 Hi, I am EyeSee. Let's explore this painting '{name}' together. You can click on the area you're interested in and choose from four types of information: Description, Analysis, Interpretation, and Judgment. Based on your selection, I will provide you with the relevant information."
@@ -1708,56 +1707,60 @@ async def texttospeech(text, language,gender='female'):
1708
  return None
1709
 
1710
  # give the reason of recommendation
1711
- async def item_associate(new_crop,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state, evt: gr.SelectData):
1712
- persona=naritive_mapping[narritive]
1713
- rec_path=evt._data['value']['image']['path']
1714
- index="Item Recommendation Picture "+ str(evt.index)
1715
- print("rec_path",rec_path)
1716
-
1717
- prompt=recommendation_prompt[0][persona].format(language=language,length=length)
1718
-
1719
- image_paths=[new_crop,rec_path]
1720
-
1721
- result=get_gpt_response(openai_api_key, image_paths, prompt)
1722
- print("recommend result",result)
1723
- state += [(None, f"{result}")]
1724
- log_state += [("User wants to know object recomendation reason", None)]
1725
- log_state = log_state + [(narritive, None)]
1726
- log_state = log_state + [(f"image sort ranking {sort_score}", None)]
1727
- log_state = log_state + [(None, f"{result}")]
1728
- read_info = re.sub(r'[#[\]!*]','',result)
1729
- read_info = emoji.replace_emoji(read_info,replace="")
1730
- print("associate",read_info)
1731
- audio_output=None
1732
- if autoplay:
1733
- audio_output = await texttospeech(read_info, language)
1734
- return state,state,audio_output,log_state,index,gr.update(value=[])
1735
-
1736
-
1737
- async def style_associate(image_path,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,artist,evt: gr.SelectData):
1738
- persona=naritive_mapping[narritive]
1739
- rec_path=evt._data['value']['image']['path']
1740
- index="Style Recommendation Picture "+str(evt.index)
1741
- print("rec_path",rec_path)
1742
- if persona==1:
1743
- prompt=recommendation_prompt[1][persona].format(language=language,length=length,artist=artist[8:])
1744
- else:
1745
- prompt=recommendation_prompt[1][persona].format(language=language,length=length)
1746
- image_paths=[image_path,rec_path]
1747
- result=get_gpt_response(openai_api_key, image_paths, prompt )
1748
- print("recommend result",result)
1749
- state += [(None, f"{result}")]
1750
- log_state += [("User wants to know style recomendation reason", None)]
1751
- log_state = log_state + [(narritive, None)]
1752
- log_state = log_state + [(f"image sort ranking {sort_score}", None)]
1753
- log_state = log_state + [(None, f"{result}")]
1754
- read_info = re.sub(r'[#[\]!*]','',result)
1755
- read_info = emoji.replace_emoji(read_info,replace="")
1756
- print("associate",read_info)
1757
- audio_output=None
1758
- if autoplay:
1759
- audio_output = await texttospeech(read_info, language)
1760
- return state,state,audio_output,log_state,index,gr.update(value=[])
 
 
 
 
1761
 
1762
 
1763
  def change_naritive(session_type,image_input, state, click_state, paragraph, origin_image,narritive,task_instruct,gallery_output,style_gallery_result,reco_reasons,language="English"):
@@ -2131,6 +2134,12 @@ def create_ui():
2131
  # show_download_button=True
2132
  )
2133
 
 
 
 
 
 
 
2134
 
2135
  with gr.Column(scale=4,visible=False) as reco_reasons:
2136
  recommend_bot = gr.Chatbot(label="Recommend Reasons", elem_classes="chatbot",height=600)
@@ -2321,7 +2330,7 @@ def create_ui():
2321
 
2322
  gallery_result.select(
2323
  item_associate,
2324
- inputs=[new_crop_save_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state],
2325
  outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
2326
 
2327
 
@@ -2329,7 +2338,7 @@ def create_ui():
2329
 
2330
  style_gallery_result.select(
2331
  style_associate,
2332
- inputs=[image_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state,artist_label],
2333
  outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
2334
 
2335
 
 
1009
  image_input.save(new_image_path)
1010
  print("img_path",new_image_path)
1011
  visual_chatgpt.current_image = new_image_path
1012
+
1013
  # visual_chatgpt.agent.memory.buffer = visual_chatgpt.agent.memory.buffer + visual_chatgpt.global_prompt
1014
  if task_type=="task 3":
1015
  name="Along the River During the Qingming Festival"
 
1017
  year="12th century (Song Dynasty)"
1018
  material="Chinese painting"
1019
  gender="male"
1020
+ paragraph="This image depicts a traditional Chinese medicine clinic scene from an ancient painting. The interior of the clinic shows various activities related to traditional Chinese medical practices. Inside the clinic: 1. To the right, a practitioner is consulting with a patient, examining the pulse, which is a common diagnostic method in traditional Chinese medicine. 2. To the far right, another practitioner is seated at a desk, likely preparing or recording prescriptions. Behind him is a traditional Chinese medicine cabinet with numerous drawers for storing herbs and medicinal ingredients.3. On the left side of the clinic, there are patients seated, possibly waiting for their turn for consultation or treatment.4. Wall hangings, presumably diagrams or medical posters, are displayed within the clinic.Outside and around the clinic: 1. People walking by or moving around, possibly other patients or townspeople, indicating the scene takes place in a lively community setting.2. Someone carrying goods in a traditional yoke balanced across their shoulders, indicative of daily life and commerce. This scene provides a detailed glimpse into the practice of traditional Chinese medicine and everyday life in historical China."
1021
 
1022
  elif task_type=="task 1":
1023
  name ="The Ambassadors"
 
1025
  year = "1533 (Northern Renaissance)"
1026
  material="Realism"
1027
  gender = "male"
1028
+ paragraph="This image is a famous painting known as \"The Ambassadors\" by Hans Holbein the Younger, created in 1533. In the painting, there are two men standing on either side of a table covered with a rich carpet. The man on the left is dressed in elaborate, luxurious clothing, indicating his high social status, and holds a dagger. The man on the right is dressed in more somber, clerical attire, possibly denoting a scholarly or ecclesiastical position.The table between them is filled with various objects, including scientific instruments like a globe, a sundial, a quadrant, and books, reflecting the intellectual interests and pursuits of the time, specifically in navigation and astronomy. A significant detail in the painting is the distorted object at the bottom center, which, when viewed from an angle, reveals itself to be a skull — a classic example of anamorphosis. This hidden skull serves as a memento mori, reminding viewers of the inevitability of death, a common theme in Renaissance art."
1029
 
1030
  elif task_type=="task 2":
1031
  name = "The Football Players"
 
1033
  year= "1912 (Cubism)"
1034
  material="Cubism"
1035
  gender= "male"
1036
+ paragraph="This image is a painting titled \"The Football Players\" created by the artist Albert Gleizes in 1912. It is an example of Cubist art, a style characterized by fragmented and abstract representations. The painting depicts a group of football players, mid-action, seemingly in the midst of a game. The figures are rendered in geometric shapes and planes, creating a sense of movement and dynamism. The use of intersecting lines and overlapping forms adds to the dynamic and somewhat chaotic feel of the composition. The use of color and form communicates the energy and intensity of the sport."
1037
  else:
1038
+ paragraph = get_gpt_response(openai_api_key, new_image_path,f"What's going on in this picture? in {language}")
1039
  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\"}")
1040
  print(parsed_data)
1041
  parsed_data = json.loads(parsed_data.replace("'", "\""))
1042
  name, artist, year, material,gender= parsed_data["name"],parsed_data["artist"],parsed_data["year"], parsed_data["style"],parsed_data['gender']
1043
  gender=gender.lower()
1044
+
 
 
 
 
 
1045
  if language=="English":
1046
  if naritive_mapping[narritive]==0 :
1047
  msg=f"🤖 Hi, I am EyeSee. Let's explore this painting '{name}' together. You can click on the area you're interested in and choose from four types of information: Description, Analysis, Interpretation, and Judgment. Based on your selection, I will provide you with the relevant information."
 
1707
  return None
1708
 
1709
  # give the reason of recommendation
1710
+ async def item_associate(new_crop,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,recommend_type,evt: gr.SelectData):
1711
+ if recommend_type=="Reasons":
1712
+ persona=naritive_mapping[narritive]
1713
+ rec_path=evt._data['value']['image']['path']
1714
+ index="Item Recommendation Picture "+ str(evt.index)
1715
+ print("rec_path",rec_path)
1716
+
1717
+ prompt=recommendation_prompt[0][persona].format(language=language,length=length)
1718
+
1719
+ image_paths=[new_crop,rec_path]
1720
+
1721
+ result=get_gpt_response(openai_api_key, image_paths, prompt)
1722
+ print("recommend result",result)
1723
+ state += [(None, f"{result}")]
1724
+ log_state += [("User wants to know object recomendation reason", None)]
1725
+ log_state = log_state + [(narritive, None)]
1726
+ log_state = log_state + [(f"image sort ranking {sort_score}", None)]
1727
+ log_state = log_state + [(None, f"{result}")]
1728
+ read_info = re.sub(r'[#[\]!*]','',result)
1729
+ read_info = emoji.replace_emoji(read_info,replace="")
1730
+ print("associate",read_info)
1731
+ audio_output=None
1732
+ if autoplay:
1733
+ audio_output = await texttospeech(read_info, language)
1734
+ return state,state,audio_output,log_state,index,gr.update(value=[])
1735
+ return state,state,None,log_state,None,gr.update(value=[])
1736
+
1737
+
1738
+ async def style_associate(image_path,openai_api_key,language,autoplay,length,log_state,sort_score,narritive,state,artist,recommend_type,evt: gr.SelectData):
1739
+ if recommend_type=="Reasons":
1740
+ persona=naritive_mapping[narritive]
1741
+ rec_path=evt._data['value']['image']['path']
1742
+ index="Style Recommendation Picture "+str(evt.index)
1743
+ print("rec_path",rec_path)
1744
+ if persona==1:
1745
+ prompt=recommendation_prompt[1][persona].format(language=language,length=length,artist=artist[8:])
1746
+ else:
1747
+ prompt=recommendation_prompt[1][persona].format(language=language,length=length)
1748
+ image_paths=[image_path,rec_path]
1749
+ result=get_gpt_response(openai_api_key, image_paths, prompt )
1750
+ print("recommend result",result)
1751
+ state += [(None, f"{result}")]
1752
+ log_state += [("User wants to know style recomendation reason", None)]
1753
+ log_state = log_state + [(narritive, None)]
1754
+ log_state = log_state + [(f"image sort ranking {sort_score}", None)]
1755
+ log_state = log_state + [(None, f"{result}")]
1756
+ read_info = re.sub(r'[#[\]!*]','',result)
1757
+ read_info = emoji.replace_emoji(read_info,replace="")
1758
+ print("associate",read_info)
1759
+ audio_output=None
1760
+ if autoplay:
1761
+ audio_output = await texttospeech(read_info, language)
1762
+ return state,state,audio_output,log_state,index,gr.update(value=[])
1763
+ return state,state,None,log_state,None,gr.update(value=[])
1764
 
1765
 
1766
  def change_naritive(session_type,image_input, state, click_state, paragraph, origin_image,narritive,task_instruct,gallery_output,style_gallery_result,reco_reasons,language="English"):
 
2134
  # show_download_button=True
2135
  )
2136
 
2137
+ recommend_type = gr.Radio(
2138
+ choices=["Preview","Reasons"],
2139
+ label="Information Type",
2140
+ value="Preview",
2141
+ interactive=True)
2142
+
2143
 
2144
  with gr.Column(scale=4,visible=False) as reco_reasons:
2145
  recommend_bot = gr.Chatbot(label="Recommend Reasons", elem_classes="chatbot",height=600)
 
2330
 
2331
  gallery_result.select(
2332
  item_associate,
2333
+ inputs=[new_crop_save_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state,recommend_type],
2334
  outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
2335
 
2336
 
 
2338
 
2339
  style_gallery_result.select(
2340
  style_associate,
2341
+ inputs=[image_path,openai_api_key,language,auto_play,length,log_state,sort_rec,naritive,recomended_state,artist_label,recommend_type],
2342
  outputs=[recommend_bot,recomended_state,output_audio,log_state,pic_index,recommend_score],
2343
 
2344