You-shen commited on
Commit
87dcf5c
·
verified ·
1 Parent(s): 482f4a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -40,16 +40,17 @@ def img_size(image):
40
  width = int(width)
41
  height = int(height)
42
  resized_img = image.resize((width, height))
 
43
  return resized_img
44
 
45
 
 
46
  def encode_image(image):
47
  buffered = io.BytesIO()
48
  image.save(buffered, format="PNG")
49
  return base64.b64encode(buffered.getvalue()).decode("utf-8")
50
 
51
 
52
-
53
  def image_summarize(img_base64, prompt):
54
  chat = ChatOpenAI(model="gpt-4o", max_tokens=256)
55
  msg = chat.invoke(
@@ -156,7 +157,7 @@ Pre Thought: 简要理解要验证的内容,是否包含多个要验证的细
156
  Thought: 为了验证第一个细节部分, 你应该怎么做.
157
  Action: 工具列表({tool_names})之一的工具名,不能有其他内容
158
  Action Input: Action的输入参数.
159
- Observation: Action的执行结果.
160
  ... (这个 'Thought/Action/Action Input/Observation' 逻辑可以最多重复3次)
161
 
162
  Thought: 我现在知道最后的答案了.
@@ -212,7 +213,7 @@ flag = 8
212
 
213
  def update_image_summaries():
214
  global image_summaries
215
- return image_summaries, gr.update(lines=5)
216
 
217
 
218
  def update_flag():
@@ -280,9 +281,10 @@ def DailyNews():
280
  container_html = '<div class="new-container2">'
281
  for news in result:
282
  card_html = f"""
283
- <div class="title2">
284
- <a href="{news['url']}" target="_blank">{str(news['index'])+"、 "+news['title']}</a>
285
- </div>
 
286
  """
287
  container_html += card_html
288
  container_html += '</div>'
@@ -330,7 +332,7 @@ css = """
330
  }
331
 
332
  .new-container2{
333
- width: 300px; /* 容器宽度 */
334
  height: 200px; /* 容器高度 */
335
  overflow-y: auto; /* 超出容器的内容显示滚动条 */
336
  border: 1px solid #ccc; /* 给容器添加边框 */
@@ -446,21 +448,16 @@ def react(dict, space):
446
  agent_thought = []
447
  pattern = r"(.*?)\nThought: (.*?)\nAction: (.*?)\nAction Input: (.*)"
448
  for index in response['intermediate_steps']:
449
- match = re.search(pattern, index[0].log, re.S)
450
  before_thought = match.group(1).strip() if match else ""
451
  thought = match.group(2).strip() if match else ""
452
  action = match.group(3).strip() if match else ""
453
  action_input = match.group(4).strip() if match else ""
454
- agent_thought.append(
455
- ChatMessage(
456
- role="assistant",
457
- content=before_thought
458
- )
459
- )
460
  agent_thought.append(
461
  ChatMessage(
462
  role="assistant",
463
  content=thought,
 
464
  )
465
  )
466
  agent_thought.append(
@@ -524,13 +521,12 @@ with gr.Blocks(css=css, theme='soft') as demo:
524
  {
525
  "text": "据最新天文研究,火星的轨道将逐渐接近地球,最终成为地球的“第二月亮”。天文学家预测这一变化将在2025年发生,届时火星将在夜空中与月亮一样明亮,影响全球潮汐和生态平衡。"}],
526
  chatbot=gr.Chatbot(label='聊天机器人',
527
- avatar_images=("./image/human.png", "./image/bot.png"),
528
  type="messages"
529
  ),
530
  multimodal=True,
531
  show_progress='full',
532
- type="messages",
533
- cache_examples = False
534
  )
535
  with gr.Row():
536
  language_select = gr.Dropdown(["中文", "英文"], label="请选择要使用的语言", scale=1, value="中文")
@@ -542,14 +538,18 @@ with gr.Blocks(css=css, theme='soft') as demo:
542
  with gr.Column(scale=1):
543
  flag = gr.Image(label="新闻标签", type="numpy", visible=False)
544
  contrast_model = gr.Textbox(label="对比模型", lines=5)
545
- img_info = gr.Textbox(label="提取到的信息", lines=1)
 
546
  # daily_news = gr.Textbox(label="每日热搜", lines=1)
547
- daily_news = gr.HTML(label="每日热搜", show_label=True,container=True)
 
548
  img_bn = gr.Button("显示图片信息")
549
  # flag_bn = gr.Button("展示标签信息")
550
  daily_bn = gr.Button("查看每日热搜")
551
  constract_bn = gr.Button("展示对比模型")
552
- img_bn.click(update_image_summaries, [], [img_info, img_info])
 
 
553
  # flag_bn.click(update_flag, [], flag)
554
  daily_bn.click(DailyNews, [], [daily_news])
555
  language_select.change(SelectLanguage, language_select, [])
 
40
  width = int(width)
41
  height = int(height)
42
  resized_img = image.resize((width, height))
43
+ # resized_img.save(image_path)
44
  return resized_img
45
 
46
 
47
+
48
  def encode_image(image):
49
  buffered = io.BytesIO()
50
  image.save(buffered, format="PNG")
51
  return base64.b64encode(buffered.getvalue()).decode("utf-8")
52
 
53
 
 
54
  def image_summarize(img_base64, prompt):
55
  chat = ChatOpenAI(model="gpt-4o", max_tokens=256)
56
  msg = chat.invoke(
 
157
  Thought: 为了验证第一个细节部分, 你应该怎么做.
158
  Action: 工具列表({tool_names})之一的工具名,不能有其他内容
159
  Action Input: Action的输入参数.
160
+ Observation: Action的执行结果.
161
  ... (这个 'Thought/Action/Action Input/Observation' 逻辑可以最多重复3次)
162
 
163
  Thought: 我现在知道最后的答案了.
 
213
 
214
  def update_image_summaries():
215
  global image_summaries
216
+ return image_summaries
217
 
218
 
219
  def update_flag():
 
281
  container_html = '<div class="new-container2">'
282
  for news in result:
283
  card_html = f"""
284
+ <div class="title2">
285
+ <span>{str(news['index']) + "、 " + news['title']}</span>
286
+ <a href="{news['url']}" target="_blank">跳转</a>
287
+ </div>
288
  """
289
  container_html += card_html
290
  container_html += '</div>'
 
332
  }
333
 
334
  .new-container2{
335
+ width: 400px; /* 容器宽度 */
336
  height: 200px; /* 容器高度 */
337
  overflow-y: auto; /* 超出容器的内容显示滚动条 */
338
  border: 1px solid #ccc; /* 给容器添加边框 */
 
448
  agent_thought = []
449
  pattern = r"(.*?)\nThought: (.*?)\nAction: (.*?)\nAction Input: (.*)"
450
  for index in response['intermediate_steps']:
451
+ match = re.search(pattern, index[0].log, re.S)
452
  before_thought = match.group(1).strip() if match else ""
453
  thought = match.group(2).strip() if match else ""
454
  action = match.group(3).strip() if match else ""
455
  action_input = match.group(4).strip() if match else ""
 
 
 
 
 
 
456
  agent_thought.append(
457
  ChatMessage(
458
  role="assistant",
459
  content=thought,
460
+ metadata={"title": "💥" + before_thought}
461
  )
462
  )
463
  agent_thought.append(
 
521
  {
522
  "text": "据最新天文研究,火星的轨道将逐渐接近地球,最终成为地球的“第二月亮”。天文学家预测这一变化将在2025年发生,届时火星将在夜空中与月亮一样明亮,影响全球潮汐和生态平衡。"}],
523
  chatbot=gr.Chatbot(label='聊天机器人',
524
+ avatar_images=("../image/human.png", "../image/bot.png"),
525
  type="messages"
526
  ),
527
  multimodal=True,
528
  show_progress='full',
529
+ type="messages"
 
530
  )
531
  with gr.Row():
532
  language_select = gr.Dropdown(["中文", "英文"], label="请选择要使用的语言", scale=1, value="中文")
 
538
  with gr.Column(scale=1):
539
  flag = gr.Image(label="新闻标签", type="numpy", visible=False)
540
  contrast_model = gr.Textbox(label="对比模型", lines=5)
541
+ with gr.Accordion("展示提取到的图片信息", open=False):
542
+ img_info = gr.Textbox(label="提取到的信息", lines=5)
543
  # daily_news = gr.Textbox(label="每日热搜", lines=1)
544
+ with gr.Accordion("展示每日热搜", open=False):
545
+ daily_news = gr.HTML(label="每日热搜", show_label=True, container=True)
546
  img_bn = gr.Button("显示图片信息")
547
  # flag_bn = gr.Button("展示标签信息")
548
  daily_bn = gr.Button("查看每日热搜")
549
  constract_bn = gr.Button("展示对比模型")
550
+ # with gr.Accordion("See Details"):
551
+ # gr.Markdown("lorem ipsum")
552
+ img_bn.click(update_image_summaries, [], img_info)
553
  # flag_bn.click(update_flag, [], flag)
554
  daily_bn.click(DailyNews, [], [daily_news])
555
  language_select.change(SelectLanguage, language_select, [])