Spaces:
Running
Running
chenge
commited on
Commit
·
95ea941
1
Parent(s):
9d82030
qudai log
Browse files
app.py
CHANGED
@@ -13,7 +13,6 @@ from typing import Generator, Dict, Any, List, Union
|
|
13 |
import logging
|
14 |
import time
|
15 |
from PIL import Image
|
16 |
-
import datetime
|
17 |
|
18 |
# =========== Configuration
|
19 |
# MODEL NAME
|
@@ -31,33 +30,6 @@ save_history = True
|
|
31 |
logging.basicConfig(level=logging.INFO)
|
32 |
logger = logging.getLogger(__name__)
|
33 |
|
34 |
-
# =========== 对话日志功能
|
35 |
-
# 创建对话日志文件夹
|
36 |
-
CONVERSATION_LOG_DIR = "conversation_logs"
|
37 |
-
os.makedirs(CONVERSATION_LOG_DIR, exist_ok=True)
|
38 |
-
|
39 |
-
def save_conversation_log(history_messages, assistant_content, metadata=None):
|
40 |
-
"""保存对话日志到JSON文件"""
|
41 |
-
try:
|
42 |
-
timestamp = datetime.datetime.now()
|
43 |
-
filename = f"gradio_app_{timestamp.strftime('%Y%m%d_%H%M%S_%f')}.json"
|
44 |
-
filepath = os.path.join(CONVERSATION_LOG_DIR, filename)
|
45 |
-
|
46 |
-
log_data = {
|
47 |
-
"timestamp": timestamp.isoformat(),
|
48 |
-
"history_messages": history_messages, # 原封不动保存发送给模型的消息
|
49 |
-
"assistant_content": assistant_content,
|
50 |
-
"metadata": metadata or {}
|
51 |
-
}
|
52 |
-
|
53 |
-
with open(filepath, 'w', encoding='utf-8') as f:
|
54 |
-
json.dump(log_data, f, ensure_ascii=False, indent=2)
|
55 |
-
|
56 |
-
logger.info(f"对话日志已保存: {filepath}")
|
57 |
-
|
58 |
-
except Exception as e:
|
59 |
-
logger.error(f"保存对话日志失败: {str(e)}")
|
60 |
-
|
61 |
# =========== 图像处理工具函数
|
62 |
def encode_image_to_base64(image_path_or_pil: Union[str, Image.Image]) -> str:
|
63 |
"""将图像文件或PIL图像对象转换为base64编码字符串"""
|
@@ -486,27 +458,6 @@ class Gradio_Events:
|
|
486 |
|
487 |
history[-1]["meta"]["end"] = True
|
488 |
print("Answer: ", history[-1]["content"])
|
489 |
-
|
490 |
-
# 保存对话日志
|
491 |
-
# 获取用户消息(倒数第二个消息)
|
492 |
-
user_message = None
|
493 |
-
for i in range(len(history) - 2, -1, -1):
|
494 |
-
if history[i]["role"] == "user":
|
495 |
-
user_message = history[i]
|
496 |
-
break
|
497 |
-
|
498 |
-
if user_message:
|
499 |
-
save_conversation_log(
|
500 |
-
history_messages=history_messages, # 这是发送给模型的原始数据
|
501 |
-
assistant_content=history[-1]["content"],
|
502 |
-
metadata={
|
503 |
-
"model": model,
|
504 |
-
"proxy_base_url": PROXY_BASE_URL,
|
505 |
-
"conversation_id": state_value["conversation_id"],
|
506 |
-
"thinking_content": history[-1]["meta"].get("thinking_content", ""),
|
507 |
-
"has_thinking": bool(history[-1]["meta"].get("thinking_content"))
|
508 |
-
}
|
509 |
-
)
|
510 |
|
511 |
except Exception as e:
|
512 |
history[-1]["loading"] = False
|
@@ -956,9 +907,8 @@ css = """
|
|
956 |
.thinking-content .ant-collapse {
|
957 |
background: linear-gradient(135deg, #f6f9fc 0%, #f0f4f8 100%);
|
958 |
border: 1px solid #e1e8ed;
|
959 |
-
border-radius: 8px
|
960 |
-
|
961 |
-
margin-bottom: 0;
|
962 |
}
|
963 |
.thinking-content .ant-collapse > .ant-collapse-item > .ant-collapse-header {
|
964 |
padding: 8px 12px;
|
@@ -967,93 +917,16 @@ css = """
|
|
967 |
font-weight: 500;
|
968 |
}
|
969 |
.thinking-content .ant-collapse-content > .ant-collapse-content-box {
|
970 |
-
padding:
|
971 |
-
background:
|
972 |
-
border-radius: 0;
|
973 |
-
}
|
974 |
-
|
975 |
-
/* Thinking markdown 内容样式 */
|
976 |
-
.thinking-markdown-content {
|
977 |
font-size: 13px;
|
978 |
color: #667788;
|
979 |
-
line-height: 1.
|
980 |
-
|
981 |
-
|
982 |
-
.thinking-markdown-content h1,
|
983 |
-
.thinking-markdown-content h2,
|
984 |
-
.thinking-markdown-content h3,
|
985 |
-
.thinking-markdown-content h4,
|
986 |
-
.thinking-markdown-content h5,
|
987 |
-
.thinking-markdown-content h6 {
|
988 |
-
color: #5a6c7d;
|
989 |
-
margin: 12px 0 8px 0;
|
990 |
-
font-weight: 600;
|
991 |
-
}
|
992 |
-
|
993 |
-
.thinking-markdown-content h1 { font-size: 16px; }
|
994 |
-
.thinking-markdown-content h2 { font-size: 15px; }
|
995 |
-
.thinking-markdown-content h3 { font-size: 14px; }
|
996 |
-
.thinking-markdown-content h4,
|
997 |
-
.thinking-markdown-content h5,
|
998 |
-
.thinking-markdown-content h6 { font-size: 13px; }
|
999 |
-
|
1000 |
-
.thinking-markdown-content p {
|
1001 |
-
margin: 8px 0;
|
1002 |
-
}
|
1003 |
-
|
1004 |
-
.thinking-markdown-content code {
|
1005 |
-
background: #f8f9fa;
|
1006 |
-
border: 1px solid #e9ecef;
|
1007 |
-
border-radius: 3px;
|
1008 |
-
padding: 2px 4px;
|
1009 |
-
font-size: 12px;
|
1010 |
font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace;
|
1011 |
}
|
1012 |
|
1013 |
-
.thinking-markdown-content pre {
|
1014 |
-
background: #f8f9fa;
|
1015 |
-
border: 1px solid #e9ecef;
|
1016 |
-
border-radius: 4px;
|
1017 |
-
padding: 12px;
|
1018 |
-
overflow-x: auto;
|
1019 |
-
margin: 8px 0;
|
1020 |
-
}
|
1021 |
-
|
1022 |
-
.thinking-markdown-content pre code {
|
1023 |
-
background: transparent;
|
1024 |
-
border: none;
|
1025 |
-
padding: 0;
|
1026 |
-
font-size: 12px;
|
1027 |
-
}
|
1028 |
-
|
1029 |
-
.thinking-markdown-content blockquote {
|
1030 |
-
border-left: 3px solid #6A57FF;
|
1031 |
-
padding-left: 12px;
|
1032 |
-
margin: 8px 0;
|
1033 |
-
color: #666;
|
1034 |
-
font-style: italic;
|
1035 |
-
}
|
1036 |
-
|
1037 |
-
.thinking-markdown-content ul,
|
1038 |
-
.thinking-markdown-content ol {
|
1039 |
-
margin: 8px 0;
|
1040 |
-
padding-left: 20px;
|
1041 |
-
}
|
1042 |
-
|
1043 |
-
.thinking-markdown-content li {
|
1044 |
-
margin: 4px 0;
|
1045 |
-
}
|
1046 |
-
|
1047 |
-
.thinking-markdown-content strong {
|
1048 |
-
color: #333;
|
1049 |
-
font-weight: 600;
|
1050 |
-
}
|
1051 |
-
|
1052 |
-
.thinking-markdown-content em {
|
1053 |
-
color: #666;
|
1054 |
-
font-style: italic;
|
1055 |
-
}
|
1056 |
-
|
1057 |
/* 图片预览和展示样式 */
|
1058 |
.image-preview-container {
|
1059 |
background: #fafafa;
|
@@ -1384,45 +1257,30 @@ with gr.Blocks(css=css, fill_width=True) as demo:
|
|
1384 |
const thinking_done = bubble?.meta?.thinking_done || false
|
1385 |
|
1386 |
return {
|
1387 |
-
thinking_container: {
|
1388 |
-
style: {
|
1389 |
-
display: (thinking_content || is_thinking) ? 'block' : 'none',
|
1390 |
-
marginBottom: thinking_content || is_thinking ? '12px' : '0'
|
1391 |
-
}
|
1392 |
-
},
|
1393 |
thinking_collapse: {
|
1394 |
items: thinking_content || is_thinking ? [{
|
1395 |
key: '1',
|
1396 |
label: is_thinking ? '🤔 正在思考...' : '🤔 思考过程',
|
1397 |
-
children: ''
|
1398 |
}] : [],
|
1399 |
-
|
|
|
|
|
|
|
1400 |
size: 'small',
|
1401 |
ghost: true
|
1402 |
},
|
1403 |
-
thinking_markdown: {
|
1404 |
-
value: thinking_content || (is_thinking ? '思考中...' : ''),
|
1405 |
-
style: {
|
1406 |
-
display: (thinking_content || is_thinking) ? 'block' : 'none'
|
1407 |
-
}
|
1408 |
-
},
|
1409 |
answer: {
|
1410 |
value: content
|
1411 |
},
|
1412 |
canceled: bubble.meta?.canceled ? undefined : { style: { display: 'none' } }
|
1413 |
}
|
1414 |
}"""):
|
1415 |
-
# Thinking区域 -
|
1416 |
-
|
1417 |
-
|
1418 |
-
|
1419 |
-
|
1420 |
-
)
|
1421 |
-
with ms.Div(elem_style=dict(padding="12px 16px 16px 16px", background="#fff", borderRadius="0 0 6px 6px", border="1px solid #e1e8ed", borderTop="none")):
|
1422 |
-
ms.Markdown(
|
1423 |
-
as_item="thinking_markdown",
|
1424 |
-
elem_classes="thinking-markdown-content"
|
1425 |
-
)
|
1426 |
|
1427 |
# 回答内容
|
1428 |
ms.Markdown(
|
@@ -1718,6 +1576,7 @@ if __name__ == "__main__":
|
|
1718 |
demo.queue(default_concurrency_limit=200).launch(
|
1719 |
ssr_mode=False,
|
1720 |
max_threads=200,
|
|
|
1721 |
# server_port=args.port,
|
1722 |
# server_name=args.host # 使用命令行参数控制访问地址
|
1723 |
)
|
|
|
13 |
import logging
|
14 |
import time
|
15 |
from PIL import Image
|
|
|
16 |
|
17 |
# =========== Configuration
|
18 |
# MODEL NAME
|
|
|
30 |
logging.basicConfig(level=logging.INFO)
|
31 |
logger = logging.getLogger(__name__)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
# =========== 图像处理工具函数
|
34 |
def encode_image_to_base64(image_path_or_pil: Union[str, Image.Image]) -> str:
|
35 |
"""将图像文件或PIL图像对象转换为base64编码字符串"""
|
|
|
458 |
|
459 |
history[-1]["meta"]["end"] = True
|
460 |
print("Answer: ", history[-1]["content"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
461 |
|
462 |
except Exception as e:
|
463 |
history[-1]["loading"] = False
|
|
|
907 |
.thinking-content .ant-collapse {
|
908 |
background: linear-gradient(135deg, #f6f9fc 0%, #f0f4f8 100%);
|
909 |
border: 1px solid #e1e8ed;
|
910 |
+
border-radius: 8px;
|
911 |
+
margin-bottom: 12px;
|
|
|
912 |
}
|
913 |
.thinking-content .ant-collapse > .ant-collapse-item > .ant-collapse-header {
|
914 |
padding: 8px 12px;
|
|
|
917 |
font-weight: 500;
|
918 |
}
|
919 |
.thinking-content .ant-collapse-content > .ant-collapse-content-box {
|
920 |
+
padding: 12px;
|
921 |
+
background: #fff;
|
922 |
+
border-radius: 0 0 6px 6px;
|
|
|
|
|
|
|
|
|
923 |
font-size: 13px;
|
924 |
color: #667788;
|
925 |
+
line-height: 1.5;
|
926 |
+
white-space: pre-wrap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
927 |
font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace;
|
928 |
}
|
929 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
930 |
/* 图片预览和展示样式 */
|
931 |
.image-preview-container {
|
932 |
background: #fafafa;
|
|
|
1257 |
const thinking_done = bubble?.meta?.thinking_done || false
|
1258 |
|
1259 |
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
1260 |
thinking_collapse: {
|
1261 |
items: thinking_content || is_thinking ? [{
|
1262 |
key: '1',
|
1263 |
label: is_thinking ? '🤔 正在思考...' : '🤔 思考过程',
|
1264 |
+
children: thinking_content || '思考中...'
|
1265 |
}] : [],
|
1266 |
+
style: {
|
1267 |
+
display: (thinking_content || is_thinking) ? 'block' : 'none',
|
1268 |
+
marginBottom: thinking_content || is_thinking ? '12px' : '0'
|
1269 |
+
},
|
1270 |
size: 'small',
|
1271 |
ghost: true
|
1272 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
1273 |
answer: {
|
1274 |
value: content
|
1275 |
},
|
1276 |
canceled: bubble.meta?.canceled ? undefined : { style: { display: 'none' } }
|
1277 |
}
|
1278 |
}"""):
|
1279 |
+
# Thinking区域 - 可折叠显示
|
1280 |
+
antd.Collapse(
|
1281 |
+
as_item="thinking_collapse",
|
1282 |
+
elem_classes="thinking-content"
|
1283 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
1284 |
|
1285 |
# 回答内容
|
1286 |
ms.Markdown(
|
|
|
1576 |
demo.queue(default_concurrency_limit=200).launch(
|
1577 |
ssr_mode=False,
|
1578 |
max_threads=200,
|
1579 |
+
# server_port = 7971,
|
1580 |
# server_port=args.port,
|
1581 |
# server_name=args.host # 使用命令行参数控制访问地址
|
1582 |
)
|