Spaces:
Sleeping
Sleeping
File size: 3,134 Bytes
1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 cccf1bf 1f8e2b4 cccf1bf 1f8e2b4 cccf1bf 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 cccf1bf 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 cccf1bf 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 1f8e2b4 1a69151 8e9646f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import gradio as gr
from transformers import AutoTokenizer
def count_tokens(model_name, text, hf_token=None):
"""ํ ํฐ ์ ๊ณ์ฐ"""
try:
if not model_name or not text:
return "๋ชจ๋ธ๋ช
๊ณผ ํ
์คํธ๋ฅผ ๋ชจ๋ ์
๋ ฅํด์ฃผ์ธ์."
# ํ ํฌ๋์ด์ ๋ก๋
tokenizer = AutoTokenizer.from_pretrained(
model_name,
token=hf_token.strip() if hf_token and hf_token.strip() else None
)
# ํ ํฐ ์ธ์ฝ๋ฉ
tokens = tokenizer.encode(text)
token_count = len(tokens)
# ๊ฒฐ๊ณผ ๋ฐํ
result = f"โ
ํ ํฐ ์: {token_count}\n"
result += f"๋ชจ๋ธ: {model_name}\n"
result += f"ํ
์คํธ ๊ธธ์ด: {len(text)} ๊ธ์"
return result
except Exception as e:
return f"โ ์ค๋ฅ: {str(e)}"
def check_model(model_name, hf_token=None):
"""๋ชจ๋ธ ์ ๊ทผ ํ์ธ"""
try:
if not model_name:
return "๋ชจ๋ธ๋ช
์ ์
๋ ฅํด์ฃผ์ธ์."
tokenizer = AutoTokenizer.from_pretrained(
model_name,
token=hf_token.strip() if hf_token and hf_token.strip() else None
)
return f"โ
{model_name} ๋ชจ๋ธ ์ ๊ทผ ๊ฐ๋ฅ!"
except Exception as e:
return f"โ ์ค๋ฅ: {str(e)}"
# Gradio ์ธํฐํ์ด์ค
def create_interface():
with gr.Blocks(title="ํ ํฐ ๊ณ์ฐ๊ธฐ") as demo:
gr.Markdown("# ๐ข ํ ํฐ ๊ณ์ฐ๊ธฐ")
with gr.Row():
with gr.Column():
model_input = gr.Textbox(
label="๋ชจ๋ธ๋ช
",
placeholder="์: gpt2, klue/bert-base",
value="gpt2"
)
token_input = gr.Textbox(
label="HF ํ ํฐ (์ ํ์ฌํญ)",
type="password"
)
text_input = gr.Textbox(
label="ํ
์คํธ",
lines=5,
value="์๋
ํ์ธ์! ํ
์คํธ ํ
์คํธ์
๋๋ค."
)
with gr.Row():
check_btn = gr.Button("๋ชจ๋ธ ํ์ธ")
calc_btn = gr.Button("ํ ํฐ ๊ณ์ฐ", variant="primary")
with gr.Column():
output = gr.Textbox(label="๊ฒฐ๊ณผ", lines=10)
# ์ถ์ฒ ๋ชจ๋ธ
gr.Markdown("### ์ถ์ฒ ๋ชจ๋ธ")
with gr.Row():
models = ["gpt2", "klue/bert-base", "microsoft/DialoGPT-medium"]
for model in models:
btn = gr.Button(model, size="sm")
btn.click(lambda x=model: x, outputs=model_input)
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
check_btn.click(check_model, [model_input, token_input], output)
calc_btn.click(count_tokens, [model_input, text_input, token_input], output)
text_input.submit(count_tokens, [model_input, text_input, token_input], output)
return demo
if __name__ == "__main__":
demo = create_interface()
demo.launch() |