Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ from typing import Optional, Union
|
|
| 4 |
from PIL import Image
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def base64_to_image(
|
| 9 |
base64_str: str,
|
|
@@ -20,10 +22,6 @@ def base64_to_image(
|
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
PIL.Image.Image 对象,解码失败时返回None
|
| 23 |
-
|
| 24 |
-
Examples:
|
| 25 |
-
>>> img = base64_to_image("data:image/png;base64,iVBORw0KGg...")
|
| 26 |
-
>>> img = base64_to_image("iVBORw0KGg...", remove_prefix=False)
|
| 27 |
"""
|
| 28 |
try:
|
| 29 |
# 1. 处理Base64前缀
|
|
@@ -46,20 +44,127 @@ def base64_to_image(
|
|
| 46 |
print(f"Base64解码失败: {str(e)}")
|
| 47 |
return None
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import markdown
|
| 9 |
|
| 10 |
def base64_to_image(
|
| 11 |
base64_str: str,
|
|
|
|
| 22 |
|
| 23 |
Returns:
|
| 24 |
PIL.Image.Image 对象,解码失败时返回None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
try:
|
| 27 |
# 1. 处理Base64前缀
|
|
|
|
| 44 |
print(f"Base64解码失败: {str(e)}")
|
| 45 |
return None
|
| 46 |
|
| 47 |
+
def process_message(file_path):
|
| 48 |
+
try:
|
| 49 |
+
# 读取JSON文件
|
| 50 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 51 |
+
messages = json.load(f)
|
| 52 |
+
|
| 53 |
+
# 创建HTML输出
|
| 54 |
+
html_output = ""
|
| 55 |
+
|
| 56 |
+
for message_item in messages:
|
| 57 |
+
role = message_item['role']
|
| 58 |
+
content = message_item['content']
|
| 59 |
+
|
| 60 |
+
# 根据角色设置样式
|
| 61 |
+
if role == "user" or role == "human":
|
| 62 |
+
html_output += f'<div style="background-color: #f0f0f0; padding: 10px; margin: 10px 0; border-radius: 10px;"><strong>User:</strong><br>'
|
| 63 |
+
elif role == "assistant":
|
| 64 |
+
html_output += f'<div style="background-color: #e6f7ff; padding: 10px; margin: 10px 0; border-radius: 10px;"><strong>Assistant:</strong><br>'
|
| 65 |
+
else:
|
| 66 |
+
html_output += f'<div style="background-color: #f9f9f9; padding: 10px; margin: 10px 0; border-radius: 10px;"><strong>{role.capitalize()}:</strong><br>'
|
| 67 |
+
|
| 68 |
+
# 处理内容
|
| 69 |
+
for content_item in content:
|
| 70 |
+
content_type = content_item['type']
|
| 71 |
+
|
| 72 |
+
if content_type == "text":
|
| 73 |
+
# 将Markdown文本转换为HTML
|
| 74 |
+
md_text = content_item['text']
|
| 75 |
+
html_text = markdown.markdown(md_text, extensions=['fenced_code', 'codehilite'])
|
| 76 |
+
html_output += html_text
|
| 77 |
+
|
| 78 |
+
elif content_type == "image_url":
|
| 79 |
+
content_value = content_item['image_url']['url']
|
| 80 |
+
# 如果是base64图片
|
| 81 |
+
if content_value.startswith("data:"):
|
| 82 |
+
html_output += f'<img src="{content_value}" style="max-width: 100%; margin: 10px 0;">'
|
| 83 |
+
else:
|
| 84 |
+
html_output += f'<img src="{content_value}" style="max-width: 100%; margin: 10px 0;">'
|
| 85 |
+
|
| 86 |
+
html_output += '</div>'
|
| 87 |
+
|
| 88 |
+
return html_output
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return f"<div style='color: red;'>Error processing file: {str(e)}</div>"
|
| 92 |
|
| 93 |
+
def upload_and_process(file):
|
| 94 |
+
if file is None:
|
| 95 |
+
return "请上传一个JSON文件"
|
| 96 |
+
|
| 97 |
+
return process_message(file.name)
|
| 98 |
|
| 99 |
+
def use_example():
|
| 100 |
+
# 使用示例文件
|
| 101 |
+
example_path = "test_message_gpt.json"
|
| 102 |
+
return process_message(example_path)
|
| 103 |
|
| 104 |
+
# 确保示例文件存在
|
| 105 |
+
def setup_example_file():
|
| 106 |
+
# 这里我们需要创建示例文件,因为我们没有实际的内容
|
| 107 |
+
# 在实际应用中,你应该将原始的test_message_gpt.json文件放在Space的根目录下
|
| 108 |
+
example_path = "test_message_gpt.json"
|
| 109 |
+
|
| 110 |
+
# 如果文件不存在,创建一个简单的示例
|
| 111 |
+
if not os.path.exists(example_path):
|
| 112 |
+
example_messages = [
|
| 113 |
+
{
|
| 114 |
+
"role": "user",
|
| 115 |
+
"content": [
|
| 116 |
+
{
|
| 117 |
+
"type": "text",
|
| 118 |
+
"text": "你好,请介绍一下自己"
|
| 119 |
+
}
|
| 120 |
+
]
|
| 121 |
+
},
|
| 122 |
+
{
|
| 123 |
+
"role": "assistant",
|
| 124 |
+
"content": [
|
| 125 |
+
{
|
| 126 |
+
"type": "text",
|
| 127 |
+
"text": "你好!我是一个AI助手。我可以帮助回答问题、提供信息、进行对话等。我被设计用来协助用户完成各种任务,从简单的问答到更复杂的讨论。\n\n我可以处理文本信息,也能理解和描述图像内容。虽然我有一些限制,但我会尽力提供有用、准确和有帮助的回应。\n\n有什么我可以帮助你的吗?"
|
| 128 |
+
}
|
| 129 |
+
]
|
| 130 |
+
}
|
| 131 |
+
]
|
| 132 |
+
|
| 133 |
+
with open(example_path, "w", encoding="utf-8") as f:
|
| 134 |
+
json.dump(example_messages, f, ensure_ascii=False, indent=2)
|
| 135 |
+
|
| 136 |
+
# 设置示例文件
|
| 137 |
+
setup_example_file()
|
| 138 |
+
|
| 139 |
+
# 创建Gradio界面
|
| 140 |
+
with gr.Blocks(title="ChatGPT Conversation Visualizer") as demo:
|
| 141 |
+
gr.Markdown("# ChatGPT 对话可视化工具")
|
| 142 |
+
gr.Markdown("上传一个包含ChatGPT对话记录的JSON文件,或使用示例文件查看可视化结果")
|
| 143 |
+
|
| 144 |
+
with gr.Row():
|
| 145 |
+
file_input = gr.File(label="上传JSON文件", file_types=[".json"])
|
| 146 |
+
|
| 147 |
+
with gr.Row():
|
| 148 |
+
col1, col2 = gr.Column(), gr.Column()
|
| 149 |
+
with col1:
|
| 150 |
+
visualize_button = gr.Button("可视化上传的对话")
|
| 151 |
+
with col2:
|
| 152 |
+
example_button = gr.Button("使用示例文件")
|
| 153 |
+
|
| 154 |
+
with gr.Row():
|
| 155 |
+
output = gr.HTML(label="对话内容")
|
| 156 |
+
|
| 157 |
+
visualize_button.click(
|
| 158 |
+
fn=upload_and_process,
|
| 159 |
+
inputs=[file_input],
|
| 160 |
+
outputs=[output]
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
example_button.click(
|
| 164 |
+
fn=use_example,
|
| 165 |
+
inputs=[],
|
| 166 |
+
outputs=[output]
|
| 167 |
+
)
|
| 168 |
|
| 169 |
+
# 启动Gradio应用
|
| 170 |
+
demo.launch()
|