Spaces:
Sleeping
Sleeping
init commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from vertexai.preview import tokenization
|
| 2 |
+
|
| 3 |
+
color_map = {
|
| 4 |
+
'0': '#ccbfee',
|
| 5 |
+
'1': '#beedc6',
|
| 6 |
+
'2': '#f4d7ab',
|
| 7 |
+
'3': '#f4aeb1',
|
| 8 |
+
'4': '#a4dcf3',
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def tokens_len(prompt, model):
|
| 12 |
+
model_name = "gemini-1.5-pro"
|
| 13 |
+
tokenizer = tokenization.get_tokenizer_for_model(model)
|
| 14 |
+
|
| 15 |
+
result = tokenizer.compute_tokens(prompt)
|
| 16 |
+
tokens = result.tokens_info[0].tokens
|
| 17 |
+
|
| 18 |
+
return (
|
| 19 |
+
f'{len(tokens)} tokens, {len(prompt)} chracters',
|
| 20 |
+
[
|
| 21 |
+
(
|
| 22 |
+
token.decode('utf-8').replace("\n", "\\n"),
|
| 23 |
+
str(i % len(color_map))
|
| 24 |
+
) for i, token in enumerate(tokens)
|
| 25 |
+
]
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
web_tokenizer = gr.Interface(
|
| 29 |
+
fn = tokens_len,
|
| 30 |
+
inputs = [
|
| 31 |
+
gr.Textbox(
|
| 32 |
+
label='輸入提示文字',
|
| 33 |
+
lines=6),
|
| 34 |
+
gr.Dropdown(
|
| 35 |
+
label='模型名稱',
|
| 36 |
+
choices = [
|
| 37 |
+
'gemini-1.5-pro',
|
| 38 |
+
'gemini-1.5-flash',
|
| 39 |
+
],
|
| 40 |
+
value='gemini-1.5-pro')
|
| 41 |
+
],
|
| 42 |
+
outputs = [
|
| 43 |
+
gr.Textbox(
|
| 44 |
+
label='統計量'),
|
| 45 |
+
gr.Highlightedtext(
|
| 46 |
+
label='切割結果:',
|
| 47 |
+
color_map = color_map,
|
| 48 |
+
show_inline_category=False)
|
| 49 |
+
]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
web_tokenizer.launch(share=True, debug=True)
|