Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,190 @@
|
|
1 |
-
import gradio as gr
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
|
4 |
+
def count_tokens(model_name, text, hf_token=None):
|
5 |
+
"""ํ ํฐ ์๋ฅผ ๊ณ์ฐํ๋ ํจ์"""
|
6 |
+
try:
|
7 |
+
if not model_name or not text:
|
8 |
+
return "๋ชจ๋ธ๋ช
๊ณผ ํ
์คํธ๋ฅผ ๋ชจ๋ ์
๋ ฅํด์ฃผ์ธ์."
|
9 |
+
|
10 |
+
# transformers ์ํฌํธ๋ฅผ ํจ์ ๋ด๋ถ์์ ์ฒ๋ฆฌ
|
11 |
+
from transformers import AutoTokenizer
|
12 |
+
|
13 |
+
# ํ ํฌ๋์ด์ ๋ก๋ (ํ ํฐ์ด ์์ผ๋ฉด ์ฌ์ฉ)
|
14 |
+
tokenizer_kwargs = {"trust_remote_code": True}
|
15 |
+
if hf_token and hf_token.strip():
|
16 |
+
tokenizer_kwargs["token"] = hf_token.strip()
|
17 |
+
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, **tokenizer_kwargs)
|
19 |
+
|
20 |
+
# ํ ํฐํ
|
21 |
+
tokens = tokenizer.encode(text)
|
22 |
+
token_count = len(tokens)
|
23 |
+
|
24 |
+
# ํ ํฐ ๋์ฝ๋ฉ (์ ํ์ฌํญ - ํ ํฐ๋ค์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด)
|
25 |
+
try:
|
26 |
+
decoded_tokens = [tokenizer.decode([token]) for token in tokens]
|
27 |
+
except:
|
28 |
+
decoded_tokens = ["ํ ํฐ ๋์ฝ๋ฉ ์คํจ"]
|
29 |
+
|
30 |
+
result = f"โ
ํ ํฐ ์: {token_count}\n\n"
|
31 |
+
result += f"ํ ํฐ๋ค: {decoded_tokens[:50]}" # ์ฒ์ 50๊ฐ๋ง ํ์
|
32 |
+
if len(decoded_tokens) > 50:
|
33 |
+
result += f"\n... (์ด {len(decoded_tokens)}๊ฐ ํ ํฐ ์ค 50๊ฐ๋ง ํ์)"
|
34 |
+
|
35 |
+
return result
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
error_msg = f"โ ์ค๋ฅ ๋ฐ์: {str(e)}\n\n"
|
39 |
+
|
40 |
+
if "gated repo" in str(e):
|
41 |
+
error_msg += "๐ ์ด ๋ชจ๋ธ์ ์ ๊ทผ ๊ถํ์ด ํ์ํฉ๋๋ค:\n"
|
42 |
+
error_msg += f"1. https://huggingface.co/{model_name} ์์ ์ ๊ทผ ๊ถํ์ ์์ฒญํ์ธ์\n"
|
43 |
+
error_msg += "2. ํ๊น
ํ์ด์ค ํ ํฐ์ ์
๋ ฅํ์ธ์\n"
|
44 |
+
error_msg += "3. ํ ํฐ ์์ฑ: https://huggingface.co/settings/tokens\n\n"
|
45 |
+
elif "does not exist" in str(e) or "not found" in str(e):
|
46 |
+
error_msg += "๐ ๋ชจ๋ธ์ ์ฐพ์ ์ ์์ต๋๋ค:\n"
|
47 |
+
error_msg += "1. ๋ชจ๋ธ๋ช
์ ํ์ธํด์ฃผ์ธ์\n"
|
48 |
+
error_msg += "2. ๊ณต๊ฐ ๋ชจ๋ธ ์์: 'klue/bert-base', 'beomi/KcELECTRA-base', 'gpt2'\n\n"
|
49 |
+
else:
|
50 |
+
error_msg += "๐ง ๊ฐ๋ฅํ ํด๊ฒฐ ๋ฐฉ๋ฒ:\n"
|
51 |
+
error_msg += "1. ๋ชจ๋ธ๋ช
์ ํ์ธํด์ฃผ์ธ์\n"
|
52 |
+
error_msg += "2. ๋คํธ์ํฌ ์ฐ๊ฒฐ์ ํ์ธํด์ฃผ์ธ์\n"
|
53 |
+
error_msg += "3. ํ์์ ํ๊น
ํ์ด์ค ํ ํฐ์ ์
๋ ฅํด์ฃผ์ธ์\n"
|
54 |
+
|
55 |
+
return error_msg
|
56 |
+
|
57 |
+
def check_model_access(model_name, hf_token=None):
|
58 |
+
"""๋ชจ๋ธ ์ ๊ทผ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ"""
|
59 |
+
try:
|
60 |
+
if not model_name:
|
61 |
+
return "๋ชจ๋ธ๋ช
์ ์
๋ ฅํด์ฃผ์ธ์."
|
62 |
+
|
63 |
+
from transformers import AutoTokenizer
|
64 |
+
|
65 |
+
tokenizer_kwargs = {"trust_remote_code": True}
|
66 |
+
if hf_token and hf_token.strip():
|
67 |
+
tokenizer_kwargs["token"] = hf_token.strip()
|
68 |
+
|
69 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, **tokenizer_kwargs)
|
70 |
+
return f"โ
{model_name} ๋ชจ๋ธ ์ ๊ทผ ๊ฐ๋ฅํฉ๋๋ค!"
|
71 |
+
|
72 |
+
except Exception as e:
|
73 |
+
if "gated repo" in str(e):
|
74 |
+
return f"๐ {model_name}์ ์ ๊ทผ ๊ถํ์ด ํ์ํ ๋ชจ๋ธ์
๋๋ค. ํ ํฐ์ ์
๋ ฅํด์ฃผ์ธ์."
|
75 |
+
elif "does not exist" in str(e):
|
76 |
+
return f"โ {model_name} ๋ชจ๋ธ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
77 |
+
else:
|
78 |
+
return f"โ ์ค๋ฅ: {str(e)}"
|
79 |
+
|
80 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
81 |
+
def create_interface():
|
82 |
+
with gr.Blocks(title="ํ ํฐ ๊ณ์ฐ๊ธฐ", theme=gr.themes.Soft()) as demo:
|
83 |
+
gr.Markdown("# ๐ข ํ๊น
ํ์ด์ค ๋ชจ๋ธ ํ ํฐ ๊ณ์ฐ๊ธฐ")
|
84 |
+
gr.Markdown("ํ๊น
ํ์ด์ค์ ์ฌ๋ผ์จ ๋ชจ๋ธ์ ํ ํฌ๋์ด์ ๋ฅผ ์ฌ์ฉํด ํ
์คํธ์ ํ ํฐ ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.")
|
85 |
+
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Column():
|
88 |
+
model_input = gr.Textbox(
|
89 |
+
label="๋ชจ๋ธ๋ช
",
|
90 |
+
placeholder="์: klue/bert-base, beomi/KcELECTRA-base, gpt2",
|
91 |
+
value="klue/bert-base"
|
92 |
+
)
|
93 |
+
|
94 |
+
token_input = gr.Textbox(
|
95 |
+
label="ํ๊น
ํ์ด์ค ํ ํฐ (์ ํ์ฌํญ)",
|
96 |
+
placeholder="gated ๋ชจ๋ธ ์ฌ์ฉ์ ํ์ (hf_xxx...)",
|
97 |
+
type="password"
|
98 |
+
)
|
99 |
+
|
100 |
+
text_input = gr.Textbox(
|
101 |
+
label="ํ
์คํธ",
|
102 |
+
placeholder="ํ ํฐ ์๋ฅผ ๊ณ์ฐํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์...",
|
103 |
+
lines=5
|
104 |
+
)
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
check_btn = gr.Button("๋ชจ๋ธ ์ ๊ทผ ํ์ธ", variant="secondary")
|
108 |
+
calculate_btn = gr.Button("ํ ํฐ ์ ๊ณ์ฐ", variant="primary")
|
109 |
+
|
110 |
+
with gr.Column():
|
111 |
+
output = gr.Textbox(
|
112 |
+
label="๊ฒฐ๊ณผ",
|
113 |
+
lines=10,
|
114 |
+
show_copy_button=True
|
115 |
+
)
|
116 |
+
|
117 |
+
# ๋ชจ๋ธ ์นดํ
๊ณ ๋ฆฌ๋ณ ์์
|
118 |
+
with gr.Tabs():
|
119 |
+
with gr.TabItem("๊ณต๊ฐ ๋ชจ๋ธ (ํ ํฐ ๋ถํ์)"):
|
120 |
+
gr.Markdown("### ์์ ๋กญ๊ฒ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ธ๋ค:")
|
121 |
+
with gr.Row():
|
122 |
+
public_models = [
|
123 |
+
"klue/bert-base",
|
124 |
+
"beomi/KcELECTRA-base",
|
125 |
+
"gpt2",
|
126 |
+
"microsoft/DialoGPT-medium"
|
127 |
+
]
|
128 |
+
|
129 |
+
for model in public_models:
|
130 |
+
btn = gr.Button(model, size="sm")
|
131 |
+
btn.click(lambda x=model: x, outputs=model_input)
|
132 |
+
|
133 |
+
with gr.TabItem("์ ํ๋ ๋ชจ๋ธ (ํ ํฐ ํ์)"):
|
134 |
+
gr.Markdown("### ์ ๊ทผ ๊ถํ์ด ํ์ํ ๋ชจ๋ธ๋ค:")
|
135 |
+
gr.Markdown("โ ๏ธ ์ด ๋ชจ๋ธ๋ค์ ํ๊น
ํ์ด์ค ํ ํฐ์ด ํ์ํฉ๋๋ค")
|
136 |
+
with gr.Row():
|
137 |
+
gated_models = [
|
138 |
+
"meta-llama/Llama-2-7b-hf",
|
139 |
+
"google/gemma-7b",
|
140 |
+
"mistralai/Mistral-7B-v0.1"
|
141 |
+
]
|
142 |
+
|
143 |
+
for model in gated_models:
|
144 |
+
btn = gr.Button(model, size="sm")
|
145 |
+
btn.click(lambda x=model: x, outputs=model_input)
|
146 |
+
|
147 |
+
# ํ ํฐ ๊ฐ์ด๋
|
148 |
+
with gr.Accordion("๐ ํ๊น
ํ์ด์ค ํ ํฐ ๊ฐ์ด๋", open=False):
|
149 |
+
gr.Markdown("""
|
150 |
+
### ํ ํฐ์ด ํ์ํ ๊ฒฝ์ฐ:
|
151 |
+
1. **Gated ๋ชจ๋ธ**: Meta Llama, Google Gemma ๋ฑ
|
152 |
+
2. **๋น๊ณต๊ฐ ๋ชจ๋ธ**: ๊ฐ์ธ์ด๋ ์กฐ์ง์ private ๋ชจ๋ธ
|
153 |
+
|
154 |
+
### ํ ํฐ ์์ฑ ๋ฐฉ๋ฒ:
|
155 |
+
1. [ํ๊น
ํ์ด์ค ํ ํฐ ํ์ด์ง](https://huggingface.co/settings/tokens) ์ ์
|
156 |
+
2. "New token" ํด๋ฆญ
|
157 |
+
3. "Read" ๊ถํ์ผ๋ก ํ ํฐ ์์ฑ
|
158 |
+
4. ์์ฑ๋ ํ ํฐ์ ์์ "ํ๊น
ํ์ด์ค ํ ํฐ" ํ๋์ ์
๋ ฅ
|
159 |
+
|
160 |
+
### ๋ชจ๋ธ ์ ๊ทผ ๊ถํ ์์ฒญ:
|
161 |
+
1. ์ฌ์ฉํ๋ ค๋ ๋ชจ๋ธ ํ์ด์ง ๋ฐฉ๋ฌธ
|
162 |
+
2. "Request access" ๋ฒํผ ํด๋ฆญ
|
163 |
+
3. ์น์ธ ํ ํ ํฐ๊ณผ ํจ๊ป ์ฌ์ฉ
|
164 |
+
""")
|
165 |
+
|
166 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
167 |
+
check_btn.click(
|
168 |
+
check_model_access,
|
169 |
+
inputs=[model_input, token_input],
|
170 |
+
outputs=output
|
171 |
+
)
|
172 |
+
|
173 |
+
calculate_btn.click(
|
174 |
+
count_tokens,
|
175 |
+
inputs=[model_input, text_input, token_input],
|
176 |
+
outputs=output
|
177 |
+
)
|
178 |
+
|
179 |
+
# ์ํฐํค๋ก๋ ์คํ ๊ฐ๋ฅํ๊ฒ
|
180 |
+
text_input.submit(
|
181 |
+
count_tokens,
|
182 |
+
inputs=[model_input, text_input, token_input],
|
183 |
+
outputs=output
|
184 |
+
)
|
185 |
+
|
186 |
+
return demo
|
187 |
+
|
188 |
+
if __name__ == "__main__":
|
189 |
+
demo = create_interface()
|
190 |
demo.launch()
|