Spaces:
Paused
Paused
Upload 5 files
Browse files- app.py +8 -50
- templates/dashboard.html +0 -77
app.py
CHANGED
|
@@ -351,12 +351,7 @@ def get_model_map(session, cookies, session_token):
|
|
| 351 |
|
| 352 |
def init_session():
|
| 353 |
get_password()
|
| 354 |
-
global USER_NUM, MODELS, USER_DATA
|
| 355 |
-
|
| 356 |
-
# 从环境变量读取是否删除上一个对话的设置
|
| 357 |
-
delete_chat_env = os.environ.get("DELETE_CHAT", "true").lower()
|
| 358 |
-
DELETE_CHAT = delete_chat_env in ["true", "1", "yes", "y"]
|
| 359 |
-
print(f"删除上一个对话设置: {DELETE_CHAT}")
|
| 360 |
|
| 361 |
config_list = resolve_config()
|
| 362 |
user_num = len(config_list)
|
|
@@ -678,7 +673,7 @@ def get_or_create_conversation(session, cookies, session_token, conversation_id,
|
|
| 678 |
|
| 679 |
if new_conversation_id:
|
| 680 |
# 获取当前用户的上一个conversation_id
|
| 681 |
-
global USER_DATA, CURRENT_USER, LAST_CONVERSATION_IDS
|
| 682 |
last_conversation_id = LAST_CONVERSATION_IDS[user_index]
|
| 683 |
|
| 684 |
# 更新全局存储的会话ID
|
|
@@ -706,7 +701,6 @@ def generate_trace_id():
|
|
| 706 |
|
| 707 |
def send_message(message, model, think=False):
|
| 708 |
"""Flua traktado kaj plusendo de mesaĝoj"""
|
| 709 |
-
global DELETE_CHAT, LAST_CONVERSATION_IDS
|
| 710 |
print("\n===== 开始处理消息 =====")
|
| 711 |
print(f"模型: {model}")
|
| 712 |
print(f"思考模式: {think}")
|
|
@@ -838,14 +832,10 @@ def send_message(message, model, think=False):
|
|
| 838 |
completion_result, _ = num_tokens_from_string(completion_buffer.getvalue(), model)
|
| 839 |
|
| 840 |
# 保存对话历史并获取计算点数
|
| 841 |
-
_,
|
| 842 |
|
| 843 |
# 更新统计信息
|
| 844 |
-
update_model_stats(model, prompt_tokens, completion_result, calculation_method,
|
| 845 |
-
|
| 846 |
-
# 如果需要删除上一个对话且上一个对话ID不为空且与当前不同
|
| 847 |
-
if DELETE_CHAT and last_conversation_id and last_conversation_id != conversation_id:
|
| 848 |
-
delete_conversation(session, cookies, session_token, last_conversation_id)
|
| 849 |
|
| 850 |
return Response(generate(), mimetype="text/event-stream")
|
| 851 |
except requests.exceptions.RequestException as e:
|
|
@@ -859,7 +849,6 @@ def send_message(message, model, think=False):
|
|
| 859 |
|
| 860 |
def send_message_non_stream(message, model, think=False):
|
| 861 |
"""Ne-flua traktado de mesaĝoj"""
|
| 862 |
-
global DELETE_CHAT, LAST_CONVERSATION_IDS
|
| 863 |
print("\n===== 开始处理消息(非流式) =====")
|
| 864 |
print(f"模型: {model}")
|
| 865 |
print(f"思考模式: {think}")
|
|
@@ -981,14 +970,10 @@ def send_message_non_stream(message, model, think=False):
|
|
| 981 |
completion_result, _ = num_tokens_from_string(think_content + response_content, model)
|
| 982 |
|
| 983 |
# 保存对话历史并获取计算点数
|
| 984 |
-
_,
|
| 985 |
|
| 986 |
# 更新统计信息
|
| 987 |
-
update_model_stats(model, prompt_tokens, completion_result, calculation_method,
|
| 988 |
-
|
| 989 |
-
# 如果需要删除上一个对话且上一个对话ID不为空且与当前不同
|
| 990 |
-
if DELETE_CHAT and last_conversation_id and last_conversation_id != conversation_id:
|
| 991 |
-
delete_conversation(session, cookies, session_token, last_conversation_id)
|
| 992 |
|
| 993 |
return jsonify({
|
| 994 |
"id": f"chatcmpl-{str(uuid.uuid4())}",
|
|
@@ -1023,14 +1008,10 @@ def send_message_non_stream(message, model, think=False):
|
|
| 1023 |
completion_result, _ = num_tokens_from_string(response_content, model)
|
| 1024 |
|
| 1025 |
# 保存对话历史并获取计算点数
|
| 1026 |
-
_,
|
| 1027 |
|
| 1028 |
# 更新统计信息
|
| 1029 |
-
update_model_stats(model, prompt_tokens, completion_result, calculation_method,
|
| 1030 |
-
|
| 1031 |
-
# 如果需要删除上一个对话且上一个对话ID不为空且与当前不同
|
| 1032 |
-
if DELETE_CHAT and last_conversation_id and last_conversation_id != conversation_id:
|
| 1033 |
-
delete_conversation(session, cookies, session_token, last_conversation_id)
|
| 1034 |
|
| 1035 |
return jsonify({
|
| 1036 |
"id": f"chatcmpl-{str(uuid.uuid4())}",
|
|
@@ -1433,32 +1414,9 @@ def dashboard():
|
|
| 1433 |
space_url=SPACE_URL, # 传递空间URL
|
| 1434 |
users_compute_points=users_compute_points, # 传递用户计算点信息
|
| 1435 |
model_usage_records=model_usage_records, # 传递模型使用记录
|
| 1436 |
-
delete_chat=DELETE_CHAT # 传递删除对话设置
|
| 1437 |
)
|
| 1438 |
|
| 1439 |
|
| 1440 |
-
# 添加更新删除对话设置的路由
|
| 1441 |
-
@app.route("/update_delete_chat_setting", methods=["POST"])
|
| 1442 |
-
@require_auth
|
| 1443 |
-
def update_delete_chat_setting():
|
| 1444 |
-
try:
|
| 1445 |
-
data = request.get_json()
|
| 1446 |
-
if data and "delete_chat" in data:
|
| 1447 |
-
global DELETE_CHAT
|
| 1448 |
-
DELETE_CHAT = bool(data["delete_chat"])
|
| 1449 |
-
|
| 1450 |
-
# 将设置保存到环境变量中,以便重启后保留设置
|
| 1451 |
-
os.environ["DELETE_CHAT"] = "true" if DELETE_CHAT else "false"
|
| 1452 |
-
|
| 1453 |
-
print(f"更新删除对话设置为: {DELETE_CHAT}")
|
| 1454 |
-
return jsonify({"success": True})
|
| 1455 |
-
else:
|
| 1456 |
-
return jsonify({"success": False, "error": "缺少delete_chat参数"})
|
| 1457 |
-
except Exception as e:
|
| 1458 |
-
print(f"更新删除对话设置失败: {e}")
|
| 1459 |
-
return jsonify({"success": False, "error": str(e)})
|
| 1460 |
-
|
| 1461 |
-
|
| 1462 |
# 添加更新计算点数记录设置的路由
|
| 1463 |
@app.route("/update_compute_point_toggle", methods=["POST"])
|
| 1464 |
@require_auth
|
|
|
|
| 351 |
|
| 352 |
def init_session():
|
| 353 |
get_password()
|
| 354 |
+
global USER_NUM, MODELS, USER_DATA
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
config_list = resolve_config()
|
| 357 |
user_num = len(config_list)
|
|
|
|
| 673 |
|
| 674 |
if new_conversation_id:
|
| 675 |
# 获取当前用户的上一个conversation_id
|
| 676 |
+
global USER_DATA, CURRENT_USER, LAST_CONVERSATION_IDS
|
| 677 |
last_conversation_id = LAST_CONVERSATION_IDS[user_index]
|
| 678 |
|
| 679 |
# 更新全局存储的会话ID
|
|
|
|
| 701 |
|
| 702 |
def send_message(message, model, think=False):
|
| 703 |
"""Flua traktado kaj plusendo de mesaĝoj"""
|
|
|
|
| 704 |
print("\n===== 开始处理消息 =====")
|
| 705 |
print(f"模型: {model}")
|
| 706 |
print(f"思考模式: {think}")
|
|
|
|
| 832 |
completion_result, _ = num_tokens_from_string(completion_buffer.getvalue(), model)
|
| 833 |
|
| 834 |
# 保存对话历史并获取计算点数
|
| 835 |
+
_, compute_points_used = save_conversation_history(session, cookies, session_token, conversation_id)
|
| 836 |
|
| 837 |
# 更新统计信息
|
| 838 |
+
update_model_stats(model, prompt_tokens, completion_result, calculation_method, compute_points_used)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
|
| 840 |
return Response(generate(), mimetype="text/event-stream")
|
| 841 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 849 |
|
| 850 |
def send_message_non_stream(message, model, think=False):
|
| 851 |
"""Ne-flua traktado de mesaĝoj"""
|
|
|
|
| 852 |
print("\n===== 开始处理消息(非流式) =====")
|
| 853 |
print(f"模型: {model}")
|
| 854 |
print(f"思考模式: {think}")
|
|
|
|
| 970 |
completion_result, _ = num_tokens_from_string(think_content + response_content, model)
|
| 971 |
|
| 972 |
# 保存对话历史并获取计算点数
|
| 973 |
+
_, compute_points_used = save_conversation_history(session, cookies, session_token, conversation_id)
|
| 974 |
|
| 975 |
# 更新统计信息
|
| 976 |
+
update_model_stats(model, prompt_tokens, completion_result, calculation_method, compute_points_used)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 977 |
|
| 978 |
return jsonify({
|
| 979 |
"id": f"chatcmpl-{str(uuid.uuid4())}",
|
|
|
|
| 1008 |
completion_result, _ = num_tokens_from_string(response_content, model)
|
| 1009 |
|
| 1010 |
# 保存对话历史并获取计算点数
|
| 1011 |
+
_, compute_points_used = save_conversation_history(session, cookies, session_token, conversation_id)
|
| 1012 |
|
| 1013 |
# 更新统计信息
|
| 1014 |
+
update_model_stats(model, prompt_tokens, completion_result, calculation_method, compute_points_used)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1015 |
|
| 1016 |
return jsonify({
|
| 1017 |
"id": f"chatcmpl-{str(uuid.uuid4())}",
|
|
|
|
| 1414 |
space_url=SPACE_URL, # 传递空间URL
|
| 1415 |
users_compute_points=users_compute_points, # 传递用户计算点信息
|
| 1416 |
model_usage_records=model_usage_records, # 传递模型使用记录
|
|
|
|
| 1417 |
)
|
| 1418 |
|
| 1419 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1420 |
# 添加更新计算点数记录设置的路由
|
| 1421 |
@app.route("/update_compute_point_toggle", methods=["POST"])
|
| 1422 |
@require_auth
|
templates/dashboard.html
CHANGED
|
@@ -672,43 +672,6 @@
|
|
| 672 |
</div>
|
| 673 |
</div>
|
| 674 |
|
| 675 |
-
<div class="card">
|
| 676 |
-
<div class="card-header">
|
| 677 |
-
<h2 class="card-title">
|
| 678 |
-
<span class="card-icon">🗑️</span>
|
| 679 |
-
对话管理设置
|
| 680 |
-
</h2>
|
| 681 |
-
</div>
|
| 682 |
-
<div class="status-item">
|
| 683 |
-
<span class="status-label">是否自动删除上一个对话</span>
|
| 684 |
-
<div class="toggle-switch-container">
|
| 685 |
-
<label class="toggle-switch">
|
| 686 |
-
<input type="checkbox" id="delete-chat-toggle" {% if delete_chat %}checked{% endif %}>
|
| 687 |
-
<span class="toggle-slider"></span>
|
| 688 |
-
</label>
|
| 689 |
-
<span class="toggle-status" id="delete-chat-status">{{ "开启" if delete_chat else "关闭" }}</span>
|
| 690 |
-
</div>
|
| 691 |
-
</div>
|
| 692 |
-
<div class="status-item">
|
| 693 |
-
<span class="status-label">设置说明</span>
|
| 694 |
-
<span class="status-value info-text">开启后,系统将在每次对话完成后自动删除上一次对话,只保留最新对话</span>
|
| 695 |
-
</div>
|
| 696 |
-
<div class="status-item">
|
| 697 |
-
<span class="status-label">是否记录计算点数</span>
|
| 698 |
-
<div class="toggle-switch-container">
|
| 699 |
-
<label class="toggle-switch">
|
| 700 |
-
<input type="checkbox" id="compute-point-toggle" checked>
|
| 701 |
-
<span class="toggle-slider"></span>
|
| 702 |
-
</label>
|
| 703 |
-
<span class="toggle-status" id="compute-point-status">开启</span>
|
| 704 |
-
</div>
|
| 705 |
-
</div>
|
| 706 |
-
<div class="status-item">
|
| 707 |
-
<span class="status-label">设置说明</span>
|
| 708 |
-
<span class="status-value info-text">开启后,系统将记录每次对话使用的计算点数,用于统计和分析</span>
|
| 709 |
-
</div>
|
| 710 |
-
</div>
|
| 711 |
-
|
| 712 |
<div class="grid">
|
| 713 |
<div class="card">
|
| 714 |
<div class="card-header">
|
|
@@ -1002,46 +965,6 @@
|
|
| 1002 |
});
|
| 1003 |
}
|
| 1004 |
|
| 1005 |
-
// 处理删除对话开关
|
| 1006 |
-
const deleteToggle = document.getElementById('delete-chat-toggle');
|
| 1007 |
-
const deleteStatus = document.getElementById('delete-chat-status');
|
| 1008 |
-
|
| 1009 |
-
if (deleteToggle && deleteStatus) {
|
| 1010 |
-
deleteToggle.addEventListener('change', function() {
|
| 1011 |
-
const isChecked = this.checked;
|
| 1012 |
-
deleteStatus.textContent = isChecked ? '开启' : '关闭';
|
| 1013 |
-
|
| 1014 |
-
// 发送更新请求到后端
|
| 1015 |
-
fetch('/update_delete_chat_setting', {
|
| 1016 |
-
method: 'POST',
|
| 1017 |
-
headers: {
|
| 1018 |
-
'Content-Type': 'application/json',
|
| 1019 |
-
},
|
| 1020 |
-
body: JSON.stringify({ delete_chat: isChecked })
|
| 1021 |
-
})
|
| 1022 |
-
.then(response => response.json())
|
| 1023 |
-
.then(data => {
|
| 1024 |
-
if (data.success) {
|
| 1025 |
-
// 显示成功提示
|
| 1026 |
-
showNotification(isChecked ? '已开启自动删除对话功能' : '已关闭自动删除对话功能', 'success');
|
| 1027 |
-
} else {
|
| 1028 |
-
// 显示错误提示
|
| 1029 |
-
showNotification('设置更新失败: ' + data.error, 'error');
|
| 1030 |
-
// 回滚UI状态
|
| 1031 |
-
deleteToggle.checked = !isChecked;
|
| 1032 |
-
deleteStatus.textContent = !isChecked ? '开启' : '关闭';
|
| 1033 |
-
}
|
| 1034 |
-
})
|
| 1035 |
-
.catch(error => {
|
| 1036 |
-
console.error('更新设置出错:', error);
|
| 1037 |
-
showNotification('更新设置失败,请重试', 'error');
|
| 1038 |
-
// 回滚UI状态
|
| 1039 |
-
deleteToggle.checked = !isChecked;
|
| 1040 |
-
deleteStatus.textContent = !isChecked ? '开启' : '关闭';
|
| 1041 |
-
});
|
| 1042 |
-
});
|
| 1043 |
-
}
|
| 1044 |
-
|
| 1045 |
// 处理计算点数记录开关
|
| 1046 |
const computePointToggle = document.getElementById('compute-point-toggle');
|
| 1047 |
const computePointStatus = document.getElementById('compute-point-status');
|
|
|
|
| 672 |
</div>
|
| 673 |
</div>
|
| 674 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 675 |
<div class="grid">
|
| 676 |
<div class="card">
|
| 677 |
<div class="card-header">
|
|
|
|
| 965 |
});
|
| 966 |
}
|
| 967 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 968 |
// 处理计算点数记录开关
|
| 969 |
const computePointToggle = document.getElementById('compute-point-toggle');
|
| 970 |
const computePointStatus = document.getElementById('compute-point-status');
|