Spaces:
Sleeping
Sleeping
show chunk in colors.
Browse files
app.py
CHANGED
|
@@ -1,15 +1,33 @@
|
|
| 1 |
import tiktoken
|
| 2 |
import gradio as gr
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
def tokens_len(prompt, model):
|
| 5 |
encoder = tiktoken.encoding_for_model(model)
|
| 6 |
tokens = encoder.encode(prompt)
|
| 7 |
-
return len(tokens)
|
|
|
|
|
|
|
| 8 |
|
| 9 |
web_tokenizer = gr.Interface(
|
| 10 |
fn = tokens_len,
|
| 11 |
inputs = [
|
| 12 |
-
gr.Textbox(
|
|
|
|
|
|
|
| 13 |
gr.Dropdown(
|
| 14 |
label='模型名稱',
|
| 15 |
choices = [
|
|
@@ -21,7 +39,11 @@ web_tokenizer = gr.Interface(
|
|
| 21 |
value='gpt-4o-mini')
|
| 22 |
],
|
| 23 |
outputs = [
|
| 24 |
-
gr.Number(label='Token 數量:')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
]
|
| 26 |
)
|
| 27 |
|
|
|
|
| 1 |
import tiktoken
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
color_map = {
|
| 5 |
+
'0': '#ccbfee',
|
| 6 |
+
'1': '#beedc6',
|
| 7 |
+
'2': '#f4d7ab',
|
| 8 |
+
'3': '#f4aeb1',
|
| 9 |
+
'4': '#a4dcf3',
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
def gen_num():
|
| 13 |
+
n = 0
|
| 14 |
+
while True:
|
| 15 |
+
yield str(n)
|
| 16 |
+
n = (n + 1) % 5
|
| 17 |
+
|
| 18 |
def tokens_len(prompt, model):
|
| 19 |
encoder = tiktoken.encoding_for_model(model)
|
| 20 |
tokens = encoder.encode(prompt)
|
| 21 |
+
return (len(tokens),
|
| 22 |
+
[(encoder.decode([token]), category) for
|
| 23 |
+
token, category in zip(tokens, gen_num())])
|
| 24 |
|
| 25 |
web_tokenizer = gr.Interface(
|
| 26 |
fn = tokens_len,
|
| 27 |
inputs = [
|
| 28 |
+
gr.Textbox(
|
| 29 |
+
label='輸入提示文字',
|
| 30 |
+
lines=6),
|
| 31 |
gr.Dropdown(
|
| 32 |
label='模型名稱',
|
| 33 |
choices = [
|
|
|
|
| 39 |
value='gpt-4o-mini')
|
| 40 |
],
|
| 41 |
outputs = [
|
| 42 |
+
gr.Number(label='Token 數量:'),
|
| 43 |
+
gr.Highlightedtext(
|
| 44 |
+
label='切割結果:',
|
| 45 |
+
color_map = color_map,
|
| 46 |
+
show_inline_category=False)
|
| 47 |
]
|
| 48 |
)
|
| 49 |
|