zxsipola123456 commited on
Commit
3ea205f
·
verified ·
1 Parent(s): 8d1a2f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -31
app.py CHANGED
@@ -6,18 +6,19 @@ import tiktoken
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]
@@ -29,28 +30,9 @@ def calculate_cost_and_tokens(string: str, model_name: str, encoding_name: str =
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
 
@@ -209,11 +191,11 @@ with app:
209
  message_btn = gr.Button("发送", variant="primary", elem_classes="custom-button")
210
  clear_btn = gr.Button("清除", variant="primary", elem_classes="custom-button")
211
  with gr.Column():
212
- output_textbox = gr.Textbox(label="显示问题答案", lines=13, max_lines=13, elem_classes="custom-textbox",interactive=True,show_label=True,show_copy_button=True)
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">
 
6
  def calculate_cost_and_tokens(string: str, model_name: str, encoding_name: str = "cl100k_base", models_info=None):
7
  """
8
  计算字符串的token数量和消耗费用。
9
+ :参数 string: 需要计算的字符串
10
+ :参数 model_name: 模型名称,用于获取token价格
11
+ :参数 encoding_name: 编码名称,默认为 "cl100k_base"
12
+ :参数 models_info: 包含模型价格和token数量的字典
13
  :return: 格式化的字符串,包含token数量和消耗费用
14
  """
15
  if models_info is None:
16
  models_info = {
17
+ 'gpt-3.5-turbo': (6.000, 1000000), # 价格和token数量
18
+ 'gpt-4o-mini': (0.6, 1000000),
19
+ 'gpt-4o': (15.000, 1000000),
20
+ 'gpt-4o-2024-08-06': (10.000, 1000000),
21
+ }
22
 
23
  # 获取token价格
24
  price_per_token = models_info[model_name][0] / models_info[model_name][1]
 
30
  # 计算总费用
31
  total_cost = num_tokens * price_per_token
32
 
33
+ return f"使用模型:{model_name},消耗tokens数: {num_tokens}, 消耗费用: ${total_cost:.6f}"
34
 
35
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  def update_textbox(endpoint, transmit_api_key, official_api_key ):
38
 
 
191
  message_btn = gr.Button("发送", variant="primary", elem_classes="custom-button")
192
  clear_btn = gr.Button("清除", variant="primary", elem_classes="custom-button")
193
  with gr.Column():
194
+ output_textbox = gr.Textbox(label="显示问题答案", lines=12, max_lines=12, elem_classes="custom-textbox",interactive=True,show_label=True,show_copy_button=True)
195
  output_textbox2 = gr.Textbox(label="显示tokens数", lines=1, max_lines=1, elem_classes="custom-textbox",interactive=False,show_label=True)
196
  message_btn.click(generate_response,inputs=[question,model_name,endpoint,transmit_api_key, official_api_key],outputs=output_textbox,show_progress=False)
197
  clear_btn.click(clear_output,outputs=[question,output_textbox],show_progress=False)
198
+ output_textbox.change(fn=calculate_cost_and_tokens,inputs=[output_textbox,model_name],outputs=output_textbox2,show_progress=False)
199
  #添加页面底部
200
  gr.HTML('''
201
  <div class="footer">