Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,55 @@ import gradio as gr
|
|
3 |
import time
|
4 |
import tiktoken
|
5 |
|
6 |
-
def
|
7 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
encoding = tiktoken.get_encoding(encoding_name)
|
9 |
num_tokens = len(encoding.encode(string))
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def update_textbox(endpoint, transmit_api_key, official_api_key ):
|
14 |
|
15 |
if endpoint == "https://lmzh.top/v1":
|
@@ -171,7 +213,7 @@ with app:
|
|
171 |
output_textbox2 = gr.Textbox(label="显示tokens数", lines=1, max_lines=1, elem_classes="custom-textbox",interactive=False,show_label=True)
|
172 |
message_btn.click(generate_response,inputs=[question,model_name,endpoint,transmit_api_key, official_api_key],outputs=output_textbox,show_progress=False)
|
173 |
clear_btn.click(clear_output,outputs=[question,output_textbox],show_progress=False)
|
174 |
-
output_textbox.change(fn=num_tokens_from_message,inputs=output_textbox,outputs=output_textbox2)
|
175 |
#添加页面底部
|
176 |
gr.HTML('''
|
177 |
<div class="footer">
|
|
|
3 |
import time
|
4 |
import tiktoken
|
5 |
|
6 |
+
def calculate_cost_and_tokens(string: str, model_name: str, encoding_name: str = "cl100k_base", models_info=None):
|
7 |
+
"""
|
8 |
+
计算字符串的token数量和消耗费用。
|
9 |
+
:param string: 需要计算的字符串
|
10 |
+
:param model_name: 模型名称,用于获取token价格
|
11 |
+
:param encoding_name: 编码名称,默认为 "cl100k_base"
|
12 |
+
:param models_info: 包含模型价格和token数量的字典
|
13 |
+
:return: 格式化的字符串,包含token数量和消耗费用
|
14 |
+
"""
|
15 |
+
if models_info is None:
|
16 |
+
models_info = {
|
17 |
+
'Model A': (15.000, 1000000), # 价格和token数量
|
18 |
+
'Model B': (30.000, 2000000),
|
19 |
+
'Model C': (45.000, 3000000)
|
20 |
+
}
|
21 |
+
|
22 |
+
# 获取token价格
|
23 |
+
price_per_token = models_info[model_name][0] / models_info[model_name][1]
|
24 |
+
|
25 |
+
# 获取token数量
|
26 |
encoding = tiktoken.get_encoding(encoding_name)
|
27 |
num_tokens = len(encoding.encode(string))
|
28 |
+
|
29 |
+
# 计算总费用
|
30 |
+
total_cost = num_tokens * price_per_token
|
31 |
+
|
32 |
+
return f"消耗tokens数: {num_tokens}, 消耗费用: ${total_cost:.6f}"
|
33 |
+
|
34 |
+
# 示例用法
|
35 |
+
result = calculate_cost_and_tokens("Example text for processing.", "Model A")
|
36 |
+
print(result)
|
37 |
|
38 |
|
39 |
+
# 示例:不同模型的价格和token数量
|
40 |
+
models_info = {
|
41 |
+
'gpt-3.5-turbo': (6.000, 1000000), # 价格和token数量
|
42 |
+
'gpt-4o-mini': (0.6, 1000000),
|
43 |
+
'gpt-4o': (15.000, 1000000),
|
44 |
+
'gpt-4o-2024-08-06': (10.000, 1000000),
|
45 |
+
}
|
46 |
+
|
47 |
+
# 计算每个模型的token成本
|
48 |
+
token_costs = calculate_token_cost(models_info)
|
49 |
+
|
50 |
+
# 打印结果
|
51 |
+
for model, cost in token_costs.items():
|
52 |
+
print(f"{model} token cost: ${cost:.6f}")
|
53 |
+
|
54 |
+
|
55 |
def update_textbox(endpoint, transmit_api_key, official_api_key ):
|
56 |
|
57 |
if endpoint == "https://lmzh.top/v1":
|
|
|
213 |
output_textbox2 = gr.Textbox(label="显示tokens数", lines=1, max_lines=1, elem_classes="custom-textbox",interactive=False,show_label=True)
|
214 |
message_btn.click(generate_response,inputs=[question,model_name,endpoint,transmit_api_key, official_api_key],outputs=output_textbox,show_progress=False)
|
215 |
clear_btn.click(clear_output,outputs=[question,output_textbox],show_progress=False)
|
216 |
+
output_textbox.change(fn=num_tokens_from_message,inputs=[output_textbox,model_name],outputs=output_textbox2)
|
217 |
#添加页面底部
|
218 |
gr.HTML('''
|
219 |
<div class="footer">
|