zxsipola123456 commited on
Commit
5c7444a
·
verified ·
1 Parent(s): cf5528d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -2
app.py CHANGED
@@ -1,6 +1,34 @@
1
  from openai import OpenAI
2
  import gradio as gr
3
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def update_textbox(endpoint, transmit_api_key, official_api_key ):
6
 
@@ -159,9 +187,11 @@ with app:
159
  message_btn = gr.Button("发送", variant="primary", elem_classes="custom-button")
160
  clear_btn = gr.Button("清除", variant="primary", elem_classes="custom-button")
161
  with gr.Column():
162
- output_textbox = gr.Textbox(label="显示问题答案", lines=16, max_lines=16, elem_classes="custom-textbox",interactive=True,show_label=True,show_copy_button=True)
 
163
  message_btn.click(generate_response,inputs=[question,model_name,endpoint,transmit_api_key, official_api_key],outputs=output_textbox,show_progress=False)
164
- clear_btn.click(clear_output,outputs=[question,output_textbox],show_progress=False)
 
165
  #添加页面底部
166
  gr.HTML('''
167
  <div class="footer">
 
1
  from openai import OpenAI
2
  import gradio as gr
3
  import time
4
+ import tiktoken
5
+
6
+ def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):
7
+ """Return the number of tokens used by a list of messages."""
8
+ try:
9
+ encoding = tiktoken.encoding_for_model(model)
10
+ except KeyError:
11
+ print("Warning: model not found. Using cl100k_base encoding.")
12
+ encoding = tiktoken.get_encoding("cl100k_base")
13
+
14
+ # Define token counts based on model
15
+ tokens_per_message = 3
16
+ tokens_per_name = 1
17
+ if model == "gpt-3.5-turbo-0301":
18
+ tokens_per_message = 4 # {role/name}\n{content}\n
19
+ tokens_per_name = -1 # if there's a name, the role is omitted
20
+ elif "gpt-3.5-turbo" in model:
21
+ print("Warning: gpt-3.5-turbo may update over time. Using default for gpt-3.5-turbo-0613.")
22
+ elif "gpt-4" in model:
23
+ print("Warning: gpt-4 may update over time. Using default for gpt-4-0613.")
24
+
25
+ num_tokens = sum(tokens_per_message + sum(len(encoding.encode(value)) for key, value in message.items() if key != "name")
26
+ + (len(encoding.encode(message.get("name", ""))) + tokens_per_name if "name" in message else 0)
27
+ for message in messages)
28
+ num_tokens += 3 # every reply is primed with assistant
29
+
30
+ return num_tokens
31
+
32
 
33
  def update_textbox(endpoint, transmit_api_key, official_api_key ):
34
 
 
187
  message_btn = gr.Button("发送", variant="primary", elem_classes="custom-button")
188
  clear_btn = gr.Button("清除", variant="primary", elem_classes="custom-button")
189
  with gr.Column():
190
+ output_textbox = gr.Textbox(label="显示问题答案", lines=15, max_lines=15, elem_classes="custom-textbox",interactive=True,show_label=True,show_copy_button=True)
191
+ output_textbox2 = gr.Textbox(label="显示tokens数", lines=1, max_lines=2, elem_classes="custom-textbox",interactive=False,show_label=True)
192
  message_btn.click(generate_response,inputs=[question,model_name,endpoint,transmit_api_key, official_api_key],outputs=output_textbox,show_progress=False)
193
+ clear_btn.click(clear_output,outputs=[question,output_textbox],show_progress=False)
194
+ output_textbox.change(fn=num_tokens_from_messages,inputs=output_texbox,outputs=output_textbox2)
195
  #添加页面底部
196
  gr.HTML('''
197
  <div class="footer">