Spaces:
Running
Running
| 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() |