stzhao commited on
Commit
6150e51
·
verified ·
1 Parent(s): 1c5f934

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -10
app.py CHANGED
@@ -9,6 +9,7 @@ import markdown
9
  import zipfile
10
  import tempfile
11
  from datetime import datetime
 
12
 
13
  def export_to_zip(images, conversations):
14
  """
@@ -81,12 +82,40 @@ def base64_to_image(
81
  print(f"Base64解码失败: {str(e)}")
82
  return None
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  def process_message(file_path):
85
  try:
86
  # 读取JSON文件
87
  with open(file_path, "r", encoding="utf-8") as f:
88
  messages = json.load(f)
89
 
 
 
 
90
  # 创建HTML输出
91
  html_output = '<div style="color: black;">' # 添加一个包裹所有内容的div,设置文本颜色为黑色
92
 
@@ -110,8 +139,6 @@ def process_message(file_path):
110
  # 将Markdown文本转换为HTML
111
  md_text = content_item['text']
112
  html_text = markdown.markdown(md_text, extensions=['fenced_code', 'codehilite'])
113
- # html_text = markdown.markdown(md_text)
114
- # html_text = md_text
115
  html_output += f'<div style="color: black;">{html_text}</div>'
116
 
117
  elif content_type == "image_url":
@@ -125,22 +152,31 @@ def process_message(file_path):
125
  html_output += '</div>'
126
 
127
  html_output += '</div>' # 关闭最外层div
128
- return html_output
129
 
130
  except Exception as e:
131
- return f"<div style='color: red;'>Error processing file: {str(e)}</div>"
132
 
133
  def upload_and_process(file):
134
  if file is None:
135
- return "请上传一个JSON文件"
136
 
137
- return process_message(file.name)
 
138
 
139
  def use_example():
140
  # 使用示例文件
141
  example_path = "test_message_gpt.json"
142
  return process_message(example_path)
143
 
 
 
 
 
 
 
 
 
144
  # 确保示例文件存在
145
  def setup_example_file():
146
  # 这里我们需要创建示例文件,因为我们没有实际的内容
@@ -196,8 +232,8 @@ with gr.Blocks(title="ChatGPT Conversation Visualizer", css="div.prose * {color:
196
 
197
  # 添加导出按钮
198
  with gr.Row():
199
- export_btn = gr.Button("Export to ZIP")
200
- download_file = gr.File(label="Download ZIP")
201
 
202
  # 存储当前结果的状态变量
203
  current_images = gr.State([])
@@ -206,13 +242,19 @@ with gr.Blocks(title="ChatGPT Conversation Visualizer", css="div.prose * {color:
206
  visualize_button.click(
207
  fn=upload_and_process,
208
  inputs=[file_input],
209
- outputs=[output]
210
  )
211
 
212
  example_button.click(
213
  fn=use_example,
214
  inputs=[],
215
- outputs=[output]
 
 
 
 
 
 
216
  )
217
 
218
  # 启动Gradio应用
 
9
  import zipfile
10
  import tempfile
11
  from datetime import datetime
12
+ import re
13
 
14
  def export_to_zip(images, conversations):
15
  """
 
82
  print(f"Base64解码失败: {str(e)}")
83
  return None
84
 
85
+ def extract_images_from_messages(messages):
86
+ """
87
+ 从消息中提取所有图像
88
+
89
+ Args:
90
+ messages: 消息JSON数据
91
+
92
+ Returns:
93
+ 提取的图像列表和更新后的消息
94
+ """
95
+ images = []
96
+
97
+ for message in messages:
98
+ if 'content' in message and isinstance(message['content'], list):
99
+ for content_item in message['content']:
100
+ if content_item.get('type') == 'image_url':
101
+ image_url = content_item.get('image_url', {}).get('url', '')
102
+ if image_url.startswith('data:'):
103
+ # 提取Base64图像
104
+ image = base64_to_image(image_url)
105
+ if image:
106
+ images.append(image)
107
+
108
+ return images, messages
109
+
110
  def process_message(file_path):
111
  try:
112
  # 读取JSON文件
113
  with open(file_path, "r", encoding="utf-8") as f:
114
  messages = json.load(f)
115
 
116
+ # 提取图像
117
+ images, messages = extract_images_from_messages(messages)
118
+
119
  # 创建HTML输出
120
  html_output = '<div style="color: black;">' # 添加一个包裹所有内容的div,设置文本颜色为黑色
121
 
 
139
  # 将Markdown文本转换为HTML
140
  md_text = content_item['text']
141
  html_text = markdown.markdown(md_text, extensions=['fenced_code', 'codehilite'])
 
 
142
  html_output += f'<div style="color: black;">{html_text}</div>'
143
 
144
  elif content_type == "image_url":
 
152
  html_output += '</div>'
153
 
154
  html_output += '</div>' # 关闭最外层div
155
+ return html_output, images, messages
156
 
157
  except Exception as e:
158
+ return f"<div style='color: red;'>Error processing file: {str(e)}</div>", [], None
159
 
160
  def upload_and_process(file):
161
  if file is None:
162
+ return "请上传一个JSON文件", [], None
163
 
164
+ html_output, images, messages = process_message(file.name)
165
+ return html_output, images, messages
166
 
167
  def use_example():
168
  # 使用示例文件
169
  example_path = "test_message_gpt.json"
170
  return process_message(example_path)
171
 
172
+ def handle_export(images, conversations):
173
+ """处理导出请求"""
174
+ if not images or conversations is None:
175
+ return None
176
+
177
+ zip_path = export_to_zip(images, conversations)
178
+ return zip_path
179
+
180
  # 确保示例文件存在
181
  def setup_example_file():
182
  # 这里我们需要创建示例文件,因为我们没有实际的内容
 
232
 
233
  # 添加导出按钮
234
  with gr.Row():
235
+ export_btn = gr.Button("导出为ZIP")
236
+ download_file = gr.File(label="下载ZIP文件")
237
 
238
  # 存储当前结果的状态变量
239
  current_images = gr.State([])
 
242
  visualize_button.click(
243
  fn=upload_and_process,
244
  inputs=[file_input],
245
+ outputs=[output, current_images, current_json]
246
  )
247
 
248
  example_button.click(
249
  fn=use_example,
250
  inputs=[],
251
+ outputs=[output, current_images, current_json]
252
+ )
253
+
254
+ export_btn.click(
255
+ fn=handle_export,
256
+ inputs=[current_images, current_json],
257
+ outputs=[download_file]
258
  )
259
 
260
  # 启动Gradio应用