Update app.py
Browse files
app.py
CHANGED
|
@@ -240,6 +240,47 @@ def evaluate_single_data(model_name, data, client, executor, prompt_template, pr
|
|
| 240 |
|
| 241 |
return messages
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
def process_message(messages):
|
| 244 |
# 创建HTML输出
|
| 245 |
html_output = '<div style="color: black;">' # 添加一个包裹所有内容的div,设置文本颜色为黑色
|
|
@@ -302,8 +343,10 @@ def o3_chat(model_name, api_key, base_url, question, image):
|
|
| 302 |
|
| 303 |
# 将消息转换为JSON字符串,用于下载
|
| 304 |
json_str = json.dumps(messages, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
| 305 |
|
| 306 |
-
return html_output
|
| 307 |
|
| 308 |
# Gradio界面
|
| 309 |
def create_demo():
|
|
@@ -327,12 +370,15 @@ def create_demo():
|
|
| 327 |
|
| 328 |
with gr.Row():
|
| 329 |
output = gr.HTML(label="Response")
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
# 处理提交
|
| 332 |
submit_btn.click(
|
| 333 |
fn=o3_chat,
|
| 334 |
inputs=[model_name, api_key, base_url, question, image_input],
|
| 335 |
-
outputs=[output]
|
| 336 |
)
|
| 337 |
|
| 338 |
# 示例部分
|
|
|
|
| 240 |
|
| 241 |
return messages
|
| 242 |
|
| 243 |
+
def process_message_to_sharegpt_format(message):
|
| 244 |
+
|
| 245 |
+
for i, message_item in enumerate(message):
|
| 246 |
+
role = message_item['role']
|
| 247 |
+
|
| 248 |
+
content_list = message_item['content']
|
| 249 |
+
whole_content = ""
|
| 250 |
+
for content_item in content_list:
|
| 251 |
+
content_type = content_item['type']
|
| 252 |
+
if content_type == "text":
|
| 253 |
+
content_value = content_item['text']
|
| 254 |
+
whole_content += content_value
|
| 255 |
+
elif content_type == "image_url":
|
| 256 |
+
content_value = content_item['image_url']['url']
|
| 257 |
+
whole_content += "<image>"
|
| 258 |
+
# image_path = os.path.join(sub_images_save_folder_path, f"{image_idx}.png")
|
| 259 |
+
image = base64_to_image(content_value)
|
| 260 |
+
if image:
|
| 261 |
+
# image.save(image_path)
|
| 262 |
+
# sharegpt_images.append(image_path)
|
| 263 |
+
sharegpt_images.append(image)
|
| 264 |
+
image_idx += 1
|
| 265 |
+
|
| 266 |
+
if i == 0:
|
| 267 |
+
sharegpt_conversation.append({"from": "human", "value": whole_content})
|
| 268 |
+
continue
|
| 269 |
+
|
| 270 |
+
if "<interpreter>" in whole_content:
|
| 271 |
+
gpt_content, observation_content = whole_content.split("<interpreter>", -1)
|
| 272 |
+
sharegpt_conversation.append({"from": "gpt", "value": gpt_content})
|
| 273 |
+
sharegpt_conversation.append({"from": "observation", "value": "<interpreter>"+observation_content})
|
| 274 |
+
elif i != 0:
|
| 275 |
+
sharegpt_conversation.append({"from": "gpt", "value": whole_content})
|
| 276 |
+
|
| 277 |
+
sharegpt_data_item = {
|
| 278 |
+
"conversations": sharegpt_conversation,
|
| 279 |
+
"images": sharegpt_images
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
return sharegpt_data_item
|
| 283 |
+
|
| 284 |
def process_message(messages):
|
| 285 |
# 创建HTML输出
|
| 286 |
html_output = '<div style="color: black;">' # 添加一个包裹所有内容的div,设置文本颜色为黑色
|
|
|
|
| 343 |
|
| 344 |
# 将消息转换为JSON字符串,用于下载
|
| 345 |
json_str = json.dumps(messages, ensure_ascii=False, indent=4)
|
| 346 |
+
|
| 347 |
+
sharegpt_data_item = process_message_to_sharegpt_format(messages)
|
| 348 |
|
| 349 |
+
return html_output, sharegpt_data_item['images']
|
| 350 |
|
| 351 |
# Gradio界面
|
| 352 |
def create_demo():
|
|
|
|
| 370 |
|
| 371 |
with gr.Row():
|
| 372 |
output = gr.HTML(label="Response")
|
| 373 |
+
|
| 374 |
+
with gr.Row():
|
| 375 |
+
extracted_images = gr.Gallery(label="Extracted Images")
|
| 376 |
|
| 377 |
# 处理提交
|
| 378 |
submit_btn.click(
|
| 379 |
fn=o3_chat,
|
| 380 |
inputs=[model_name, api_key, base_url, question, image_input],
|
| 381 |
+
outputs=[output, extracted_images]
|
| 382 |
)
|
| 383 |
|
| 384 |
# 示例部分
|