chenge commited on
Commit
1472f6b
·
1 Parent(s): 9dc404a

think 带折叠

Browse files
Files changed (1) hide show
  1. app.py +59 -14
app.py CHANGED
@@ -982,6 +982,41 @@ css = """
982
  white-space: pre-wrap;
983
  font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace;
984
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
985
 
986
  /* 图片预览和展示样式 */
987
  .image-preview-container {
@@ -1312,19 +1347,21 @@ with gr.Blocks(css=css, fill_width=True) as demo:
1312
  const is_thinking = bubble?.meta?.is_thinking || false
1313
  const thinking_done = bubble?.meta?.thinking_done || false
1314
 
 
 
 
 
1315
  return {
1316
- thinking_collapse: {
1317
- items: thinking_content || is_thinking ? [{
1318
- key: '1',
1319
- label: is_thinking ? '🤔 正在思考...' : '🤔 思考过程',
1320
- children: thinking_content || '思考中...'
1321
- }] : [],
1322
  style: {
1323
  display: (thinking_content || is_thinking) ? 'block' : 'none',
1324
  marginBottom: thinking_content || is_thinking ? '12px' : '0'
1325
- },
1326
- size: 'small',
1327
- ghost: true
 
 
1328
  },
1329
  answer: {
1330
  value: content
@@ -1332,11 +1369,19 @@ with gr.Blocks(css=css, fill_width=True) as demo:
1332
  canceled: bubble.meta?.canceled ? undefined : { style: { display: 'none' } }
1333
  }
1334
  }"""):
1335
- # Thinking区域 - 可折叠显示
1336
- antd.Collapse(
1337
- as_item="thinking_collapse",
1338
- elem_classes="thinking-content"
1339
- )
 
 
 
 
 
 
 
 
1340
 
1341
  # 回答内容
1342
  ms.Markdown(
 
982
  white-space: pre-wrap;
983
  font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace;
984
  }
985
+ .thinking-content .ant-collapse-content-box .markdown-body {
986
+ font-size: 13px;
987
+ line-height: 1.5;
988
+ color: #667788;
989
+ }
990
+ .thinking-content .ant-collapse-content-box pre {
991
+ background: #f6f8fa;
992
+ padding: 8px;
993
+ border-radius: 4px;
994
+ overflow: auto;
995
+ }
996
+ .thinking-content .ant-collapse-content-box h1,
997
+ .thinking-content .ant-collapse-content-box h2,
998
+ .thinking-content .ant-collapse-content-box h3,
999
+ .thinking-content .ant-collapse-content-box h4,
1000
+ .thinking-content .ant-collapse-content-box h5,
1001
+ .thinking-content .ant-collapse-content-box h6 {
1002
+ margin-top: 16px;
1003
+ margin-bottom: 8px;
1004
+ font-weight: 600;
1005
+ }
1006
+ .thinking-content .ant-collapse-content-box ul,
1007
+ .thinking-content .ant-collapse-content-box ol {
1008
+ margin: 8px 0;
1009
+ padding-left: 20px;
1010
+ }
1011
+ .thinking-content .ant-collapse-content-box li {
1012
+ margin: 4px 0;
1013
+ }
1014
+ .thinking-content .ant-collapse-content-box code {
1015
+ background: #f1f3f4;
1016
+ padding: 2px 4px;
1017
+ border-radius: 3px;
1018
+ font-size: 85%;
1019
+ }
1020
 
1021
  /* 图片预览和展示样式 */
1022
  .image-preview-container {
 
1347
  const is_thinking = bubble?.meta?.is_thinking || false
1348
  const thinking_done = bubble?.meta?.thinking_done || false
1349
 
1350
+ // 判断是否应该折叠thinking区域
1351
+ // 当thinking完成且有正式回答内容时,自动折叠
1352
+ const shouldCollapse = thinking_done && content && content.trim().length > 0
1353
+
1354
  return {
1355
+ thinking_collapse_props: {
1356
+ active_key: shouldCollapse ? [] : ['1'], // 动态控制折叠状态
 
 
 
 
1357
  style: {
1358
  display: (thinking_content || is_thinking) ? 'block' : 'none',
1359
  marginBottom: thinking_content || is_thinking ? '12px' : '0'
1360
+ }
1361
+ },
1362
+ thinking_label: is_thinking ? '🤔 正在思考...' : '🤔 思考过程',
1363
+ thinking_markdown: {
1364
+ value: thinking_content || '思考中...'
1365
  },
1366
  answer: {
1367
  value: content
 
1369
  canceled: bubble.meta?.canceled ? undefined : { style: { display: 'none' } }
1370
  }
1371
  }"""):
1372
+ # Thinking区域 - 可折叠 + Markdown 渲染
1373
+ with antd.Collapse(
1374
+ size='small',
1375
+ ghost=True,
1376
+ elem_classes="thinking-content",
1377
+ as_item="thinking_collapse_props" # 动态控制所有属性
1378
+ ):
1379
+ with antd.Collapse.Item(
1380
+ as_item="thinking_label", # 动态 label 作为 header
1381
+ key='1',
1382
+ force_render=True # 确保即使折叠也预渲染内容(可选,提高性能)
1383
+ ):
1384
+ ms.Markdown(as_item="thinking_markdown") # 动态 value,支持 Markdown 渲染
1385
 
1386
  # 回答内容
1387
  ms.Markdown(