Spaces:
Running
Running
Upload 4 files
Browse files- app.py +23 -2
- templates/dashboard.html +13 -19
app.py
CHANGED
@@ -55,6 +55,9 @@ total_tokens = {
|
|
55 |
"total": 0 # 总token统计
|
56 |
}
|
57 |
|
|
|
|
|
|
|
58 |
# 计算点信息
|
59 |
compute_points = {
|
60 |
"left": 0, # 剩余计算点
|
@@ -802,7 +805,24 @@ def num_tokens_from_string(string, model="gpt-3.5-turbo"):
|
|
802 |
|
803 |
# 更新模型使用统计
|
804 |
def update_model_stats(model, prompt_tokens, completion_tokens):
|
805 |
-
global model_usage_stats, total_tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
806 |
if model not in model_usage_stats:
|
807 |
model_usage_stats[model] = {
|
808 |
"count": 0,
|
@@ -1011,7 +1031,8 @@ def dashboard():
|
|
1011 |
compute_points=compute_points,
|
1012 |
compute_points_log=compute_points_log,
|
1013 |
space_url=SPACE_URL, # 传递空间URL
|
1014 |
-
users_compute_points=users_compute_points # 传递用户计算点信息
|
|
|
1015 |
)
|
1016 |
|
1017 |
|
|
|
55 |
"total": 0 # 总token统计
|
56 |
}
|
57 |
|
58 |
+
# 模型调用记录
|
59 |
+
model_usage_records = [] # 每次调用详细记录
|
60 |
+
|
61 |
# 计算点信息
|
62 |
compute_points = {
|
63 |
"left": 0, # 剩余计算点
|
|
|
805 |
|
806 |
# 更新模型使用统计
|
807 |
def update_model_stats(model, prompt_tokens, completion_tokens):
|
808 |
+
global model_usage_stats, total_tokens, model_usage_records
|
809 |
+
|
810 |
+
# 添加调用记录
|
811 |
+
call_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # 北京时间
|
812 |
+
record = {
|
813 |
+
"model": model,
|
814 |
+
"call_time": call_time,
|
815 |
+
"prompt_tokens": prompt_tokens,
|
816 |
+
"completion_tokens": completion_tokens,
|
817 |
+
"calculation_method": "tiktoken" if any(x in model.lower() for x in ["gpt", "claude"]) or model in ["llama-3", "mistral", "gemma"] else "estimate"
|
818 |
+
}
|
819 |
+
model_usage_records.append(record)
|
820 |
+
|
821 |
+
# 限制记录数量,保留最新的500条
|
822 |
+
if len(model_usage_records) > 500:
|
823 |
+
model_usage_records.pop(0)
|
824 |
+
|
825 |
+
# 更新聚合统计
|
826 |
if model not in model_usage_stats:
|
827 |
model_usage_stats[model] = {
|
828 |
"count": 0,
|
|
|
1031 |
compute_points=compute_points,
|
1032 |
compute_points_log=compute_points_log,
|
1033 |
space_url=SPACE_URL, # 传递空间URL
|
1034 |
+
users_compute_points=users_compute_points, # 传递用户计算点信息
|
1035 |
+
model_usage_records=model_usage_records # 传递模型使用记录
|
1036 |
)
|
1037 |
|
1038 |
|
templates/dashboard.html
CHANGED
@@ -596,15 +596,11 @@
|
|
596 |
</h2>
|
597 |
</div>
|
598 |
<div class="status-item">
|
599 |
-
<span class="status-label"
|
600 |
-
<span class="status-value token-count">{{ total_tokens.total|int }}</span>
|
601 |
-
</div>
|
602 |
-
<div class="status-item">
|
603 |
-
<span class="status-label">输入Token</span>
|
604 |
<span class="status-value token-count">{{ total_tokens.prompt|int }}</span>
|
605 |
</div>
|
606 |
<div class="status-item">
|
607 |
-
<span class="status-label"
|
608 |
<span class="status-value token-count">{{ total_tokens.completion|int }}</span>
|
609 |
</div>
|
610 |
<div class="token-note">
|
@@ -615,7 +611,7 @@
|
|
615 |
<thead>
|
616 |
<tr>
|
617 |
<th>模型</th>
|
618 |
-
<th
|
619 |
<th>输入Token</th>
|
620 |
<th>输出Token</th>
|
621 |
</tr>
|
@@ -624,7 +620,7 @@
|
|
624 |
{% for model, stats in model_stats.items() %}
|
625 |
<tr>
|
626 |
<td>{{ model }}</td>
|
627 |
-
<td class="
|
628 |
<td class="token-count">{{ stats.prompt_tokens|int }}</td>
|
629 |
<td class="token-count">{{ stats.completion_tokens|int }}</td>
|
630 |
</tr>
|
@@ -712,7 +708,7 @@
|
|
712 |
<div class="card-header">
|
713 |
<h2 class="card-title">
|
714 |
<span class="card-icon">📈</span>
|
715 |
-
|
716 |
</h2>
|
717 |
<button id="toggleModelStats" class="btn-toggle">显示全部</button>
|
718 |
</div>
|
@@ -720,24 +716,22 @@
|
|
720 |
<table class="data-table">
|
721 |
<thead>
|
722 |
<tr>
|
|
|
723 |
<th>模型</th>
|
724 |
-
<th>调用次数</th>
|
725 |
<th>输入Token</th>
|
726 |
<th>输出Token</th>
|
727 |
-
<th>总Token</th>
|
728 |
<th>计算方式</th>
|
729 |
</tr>
|
730 |
</thead>
|
731 |
<tbody>
|
732 |
-
{% for
|
733 |
-
<tr class="model-row {% if loop.index >
|
734 |
-
<td>{{
|
735 |
-
<td
|
736 |
-
<td class="token-count">{{
|
737 |
-
<td class="token-count">{{
|
738 |
-
<td class="token-count">{{ stats.total_tokens|int }}</td>
|
739 |
<td>
|
740 |
-
{% if
|
741 |
<span class="token-method tiktoken">精确</span>
|
742 |
{% else %}
|
743 |
<span class="token-method estimate">估算</span>
|
|
|
596 |
</h2>
|
597 |
</div>
|
598 |
<div class="status-item">
|
599 |
+
<span class="status-label">总输入Token</span>
|
|
|
|
|
|
|
|
|
600 |
<span class="status-value token-count">{{ total_tokens.prompt|int }}</span>
|
601 |
</div>
|
602 |
<div class="status-item">
|
603 |
+
<span class="status-label">总输出Token</span>
|
604 |
<span class="status-value token-count">{{ total_tokens.completion|int }}</span>
|
605 |
</div>
|
606 |
<div class="token-note">
|
|
|
611 |
<thead>
|
612 |
<tr>
|
613 |
<th>模型</th>
|
614 |
+
<th>调用次数</th>
|
615 |
<th>输入Token</th>
|
616 |
<th>输出Token</th>
|
617 |
</tr>
|
|
|
620 |
{% for model, stats in model_stats.items() %}
|
621 |
<tr>
|
622 |
<td>{{ model }}</td>
|
623 |
+
<td class="call-count">{{ stats.count }}</td>
|
624 |
<td class="token-count">{{ stats.prompt_tokens|int }}</td>
|
625 |
<td class="token-count">{{ stats.completion_tokens|int }}</td>
|
626 |
</tr>
|
|
|
708 |
<div class="card-header">
|
709 |
<h2 class="card-title">
|
710 |
<span class="card-icon">📈</span>
|
711 |
+
模型调用记录
|
712 |
</h2>
|
713 |
<button id="toggleModelStats" class="btn-toggle">显示全部</button>
|
714 |
</div>
|
|
|
716 |
<table class="data-table">
|
717 |
<thead>
|
718 |
<tr>
|
719 |
+
<th>调用时间</th>
|
720 |
<th>模型</th>
|
|
|
721 |
<th>输入Token</th>
|
722 |
<th>输出Token</th>
|
|
|
723 |
<th>计算方式</th>
|
724 |
</tr>
|
725 |
</thead>
|
726 |
<tbody>
|
727 |
+
{% for record in model_usage_records|reverse %}
|
728 |
+
<tr class="model-row {% if loop.index > 10 %}hidden-model{% endif %}">
|
729 |
+
<td>{{ record.call_time }}</td>
|
730 |
+
<td>{{ record.model }}</td>
|
731 |
+
<td class="token-count">{{ record.prompt_tokens|int }}</td>
|
732 |
+
<td class="token-count">{{ record.completion_tokens|int }}</td>
|
|
|
733 |
<td>
|
734 |
+
{% if record.calculation_method == "tiktoken" %}
|
735 |
<span class="token-method tiktoken">精确</span>
|
736 |
{% else %}
|
737 |
<span class="token-method estimate">估算</span>
|