Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,44 +3,10 @@ import gradio as gr
|
|
3 |
import time
|
4 |
import tiktoken
|
5 |
|
6 |
-
def
|
7 |
-
"""
|
8 |
-
|
9 |
-
|
10 |
-
except KeyError:
|
11 |
-
print("Warning: model not found. Using cl100k_base encoding.")
|
12 |
-
encoding = tiktoken.get_encoding("cl100k_base")
|
13 |
-
if model in {
|
14 |
-
"gpt-3.5-turbo-0613",
|
15 |
-
"gpt-3.5-turbo-16k-0613",
|
16 |
-
"gpt-4-0314",
|
17 |
-
"gpt-4-32k-0314",
|
18 |
-
"gpt-4-0613",
|
19 |
-
"gpt-4-32k-0613",
|
20 |
-
}:
|
21 |
-
tokens_per_message = 3
|
22 |
-
tokens_per_name = 1
|
23 |
-
elif model == "gpt-3.5-turbo-0301":
|
24 |
-
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
|
25 |
-
tokens_per_name = -1 # if there's a name, the role is omitted
|
26 |
-
elif "gpt-3.5-turbo" in model:
|
27 |
-
print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")
|
28 |
-
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613")
|
29 |
-
elif "gpt-4" in model:
|
30 |
-
print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
|
31 |
-
return num_tokens_from_messages(messages, model="gpt-4-0613")
|
32 |
-
else:
|
33 |
-
raise NotImplementedError(
|
34 |
-
f"""num_tokens_from_messages() is not implemented for model {model}."""
|
35 |
-
)
|
36 |
-
num_tokens = 0
|
37 |
-
for message in messages:
|
38 |
-
num_tokens += tokens_per_message
|
39 |
-
for key, value in message.items():
|
40 |
-
num_tokens += len(encoding.encode(value))
|
41 |
-
if key == "name":
|
42 |
-
num_tokens += tokens_per_name
|
43 |
-
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
|
44 |
return num_tokens
|
45 |
|
46 |
|
@@ -205,7 +171,7 @@ with app:
|
|
205 |
output_textbox2 = gr.Textbox(label="显示tokens数", lines=1, max_lines=1, elem_classes="custom-textbox",interactive=False,show_label=True)
|
206 |
message_btn.click(generate_response,inputs=[question,model_name,endpoint,transmit_api_key, official_api_key],outputs=output_textbox,show_progress=False)
|
207 |
clear_btn.click(clear_output,outputs=[question,output_textbox],show_progress=False)
|
208 |
-
output_textbox.change(fn=
|
209 |
#添加页面底部
|
210 |
gr.HTML('''
|
211 |
<div class="footer">
|
|
|
3 |
import time
|
4 |
import tiktoken
|
5 |
|
6 |
+
def num_tokens_from_message(string: str, encoding_name: str = "cl100k_base") -> int:
|
7 |
+
"""返回文本字符串中的Token数量"""
|
8 |
+
encoding = tiktoken.get_encoding(encoding_name)
|
9 |
+
num_tokens = len(encoding.encode(string))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
return num_tokens
|
11 |
|
12 |
|
|
|
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">
|