Skip to content

Commit abd2e23

Browse files
committedFeb 23, 2024
return the best image
1 parent 5d3b7d4 commit abd2e23

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed
 

‎app.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ def create_image(prompt: str) -> str:
102102

103103
# Download the image and save it with a filename based on the prompt and current time
104104
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
105-
shortened_prompt = prompt[:50]
106-
image_file_path = f"{OUTPUT_FOLDER}/{shortened_prompt}_{current_time}.png"
105+
shortened_prompt = format_filename_or_dir(prompt[:50])
106+
image_file_path = f"{OUTPUT_FOLDER}/{current_time}_{shortened_prompt}.png"
107107

108108
response = requests.get(image_url)
109109
if response.status_code == 200:
@@ -153,7 +153,7 @@ def review_image(image_file_path: str, prompt: str):
153153
graphic_critic = AssistantAgent(
154154
name="graphic_critic",
155155
# system_message="You are an AI image critique, you will use img_review function to review the image generated by the text_to_img_prompt_expert against the original prompt, and provide feedback on how to improve the prompt.",
156-
system_message="""Critic. You are a helpful assistant highly skilled in evaluating the quality of a given image code by providing a score from 1 (bad) - 10 (good). Specifically, you can carefully evaluate the image across the following dimensions
156+
system_message="""Critic. You are a helpful assistant highly skilled in evaluating the quality of a given image code by providing a score from 1 (bad) - 10 (good) at each of the following dimensions:
157157
- Content similarity (content): Does the image present the content in the prompt? If any keyword is missing, the score must less than 5.
158158
- Clarity (clarity): Does the image clearly communicate the prompt? Is it easy to understand for the human viewers?
159159
- Balance and proportion (proportion): Are the elements arranged in a visually pleasing and harmonious way?
@@ -199,7 +199,11 @@ def review_image(image_file_path: str, prompt: str):
199199
# message = "In Houston at 2pm, Sunny sky with few clouds. Current Temperature at 37 degree"
200200
# message = "sunny sky with few clouds, Temperature low at 37 degree and high up to 72 degree"
201201
# message = "overcast sky with dense clouds, some foggy, Temperaturelow at 37 degree and high up to 72 degree"
202-
message = "In Houston at 8pm, Heavy rain with very low visibility. Temperature in the 40s"
202+
# message = "In Houston at 8pm, Heavy rain with very low visibility. Temperature in the 40s"
203+
# message = "A city scenery of Houston at 6:30pm, featuring a sunny sky with few clouds, strong wind, a lot of vehicles on the road but no people walking"
204+
# message = "A realistic image of an attractive and professional TV anchor woman, behind a desk, in national TV news station"
205+
# message = "A realistic profile image of an attractive young girl for dating"
206+
message = "A realistic image of a sexy girl wearing bikini standing with the background of weather forecast"
203207

204208
# text_to_image_generation(message)
205209

@@ -209,5 +213,26 @@ def review_image(image_file_path: str, prompt: str):
209213
Path(OUTPUT_FOLDER).mkdir(parents=True, exist_ok=True)
210214

211215
# # Start the conversation
212-
user_proxy.initiate_chat(
216+
chat_result = user_proxy.initiate_chat(
213217
manager, message=message)
218+
219+
print("CHAT_RESULT: ", chat_result)
220+
221+
# get the last generated image, which is the best image
222+
last_image_file_path = None
223+
for history in chat_result.chat_history:
224+
history_keys = history.keys()
225+
if "content" in history_keys and "name" in history_keys and "role" in history_keys:
226+
if history["name"] == "create_image" and history["role"] == "function":
227+
last_image_file_path = history["content"]
228+
# if "name" in history_keys and "role" in history_keys and "function_call" in history_keys:
229+
# if history["name"] in ["graphic_designer", ""] and history["role"] == "assistant":
230+
# func_call = history["function_call"]
231+
# if "arguments" in func_call.keys():
232+
# func_call_arguments = func_call["arguments"]
233+
# if isinstance(func_call_arguments, str):
234+
# func_call_arguments = json.loads(func_call["arguments"])
235+
# if "image_file_path" in func_call_arguments.keys():
236+
# last_image_file_path = func_call_arguments["image_file_path"]
237+
238+
print("Best Image: ", last_image_file_path)

0 commit comments

Comments
 (0)
Please sign in to comment.