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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import time
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
class MergedModelTester:
|
8 |
+
def __init__(self):
|
9 |
+
self.model = None
|
10 |
+
self.tokenizer = None
|
11 |
+
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
12 |
+
|
13 |
+
def load_model(self, model_id="openfree/gpt2-bert", progress=gr.Progress()):
|
14 |
+
"""๋ณํฉ ๋ชจ๋ธ ๋ก๋"""
|
15 |
+
try:
|
16 |
+
progress(0.2, desc="ํ ํฌ๋์ด์ ๋ก๋ ์ค...")
|
17 |
+
self.tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
18 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
19 |
+
|
20 |
+
progress(0.5, desc="๋ชจ๋ธ ๋ก๋ ์ค...")
|
21 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
22 |
+
model_id,
|
23 |
+
torch_dtype=torch.float16 if self.device.type == 'cuda' else torch.float32,
|
24 |
+
device_map="auto" if self.device.type == 'cuda' else None
|
25 |
+
)
|
26 |
+
|
27 |
+
if self.device.type == 'cpu':
|
28 |
+
self.model = self.model.to(self.device)
|
29 |
+
|
30 |
+
self.model.eval()
|
31 |
+
|
32 |
+
progress(1.0, desc="์๋ฃ!")
|
33 |
+
|
34 |
+
# ๋ชจ๋ธ ์ ๋ณด
|
35 |
+
num_params = sum(p.numel() for p in self.model.parameters())
|
36 |
+
return f"""โ
๋ชจ๋ธ ๋ก๋ ์ฑ๊ณต!
|
37 |
+
- ๋ชจ๋ธ: {model_id}
|
38 |
+
- ํ๋ผ๋ฏธํฐ: {num_params:,}
|
39 |
+
- ๋๋ฐ์ด์ค: {self.device}"""
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
return f"โ ๋ชจ๋ธ ๋ก๋ ์คํจ: {str(e)}"
|
43 |
+
|
44 |
+
def generate_text(self, prompt, max_length=100, temperature=0.8,
|
45 |
+
top_p=0.9, repetition_penalty=1.2, progress=gr.Progress()):
|
46 |
+
"""ํ
์คํธ ์์ฑ"""
|
47 |
+
if self.model is None:
|
48 |
+
return "๋จผ์ ๋ชจ๋ธ์ ๋ก๋ํ์ธ์!", None, None
|
49 |
+
|
50 |
+
try:
|
51 |
+
progress(0.3, desc="ํ
์คํธ ์์ฑ ์ค...")
|
52 |
+
|
53 |
+
# ์
๋ ฅ ํ ํฐํ
|
54 |
+
inputs = self.tokenizer(prompt, return_tensors="pt", padding=True)
|
55 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
56 |
+
|
57 |
+
# ์์ฑ ์์ ์๊ฐ
|
58 |
+
start_time = time.time()
|
59 |
+
|
60 |
+
# ํ
์คํธ ์์ฑ
|
61 |
+
with torch.no_grad():
|
62 |
+
outputs = self.model.generate(
|
63 |
+
**inputs,
|
64 |
+
max_new_tokens=max_length,
|
65 |
+
temperature=temperature,
|
66 |
+
top_p=top_p,
|
67 |
+
repetition_penalty=repetition_penalty,
|
68 |
+
do_sample=True,
|
69 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
70 |
+
eos_token_id=self.tokenizer.eos_token_id
|
71 |
+
)
|
72 |
+
|
73 |
+
# ์์ฑ ์๊ฐ ๊ณ์ฐ
|
74 |
+
generation_time = time.time() - start_time
|
75 |
+
|
76 |
+
# ๋์ฝ๋ฉ
|
77 |
+
generated_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
78 |
+
|
79 |
+
# ํต๊ณ ์ ๋ณด
|
80 |
+
input_tokens = len(inputs['input_ids'][0])
|
81 |
+
output_tokens = len(outputs[0])
|
82 |
+
new_tokens = output_tokens - input_tokens
|
83 |
+
|
84 |
+
stats = f"""๐ ์์ฑ ํต๊ณ:
|
85 |
+
- ์
๋ ฅ ํ ํฐ: {input_tokens}
|
86 |
+
- ์์ฑ ํ ํฐ: {new_tokens}
|
87 |
+
- ์ ์ฒด ํ ํฐ: {output_tokens}
|
88 |
+
- ์์ฑ ์๊ฐ: {generation_time:.2f}์ด
|
89 |
+
- ์๋: {new_tokens/generation_time:.1f} tokens/sec"""
|
90 |
+
|
91 |
+
progress(1.0, desc="์๋ฃ!")
|
92 |
+
|
93 |
+
return generated_text, stats, None
|
94 |
+
|
95 |
+
except Exception as e:
|
96 |
+
return f"โ ์์ฑ ์คํจ: {str(e)}", None, str(e)
|
97 |
+
|
98 |
+
def compare_with_parents(self, prompt, max_length=50, progress=gr.Progress()):
|
99 |
+
"""๋ถ๋ชจ ๋ชจ๋ธ๋ค๊ณผ ๋น๊ต"""
|
100 |
+
results = {}
|
101 |
+
|
102 |
+
# GPT-2 (๋ถ๋ชจ 1)
|
103 |
+
try:
|
104 |
+
progress(0.1, desc="GPT-2 ๋ก๋ ์ค...")
|
105 |
+
gpt2_tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
106 |
+
gpt2_tokenizer.pad_token = gpt2_tokenizer.eos_token
|
107 |
+
gpt2_model = AutoModelForCausalLM.from_pretrained("gpt2").to(self.device)
|
108 |
+
|
109 |
+
progress(0.3, desc="GPT-2 ์์ฑ ์ค...")
|
110 |
+
inputs = gpt2_tokenizer(prompt, return_tensors="pt").to(self.device)
|
111 |
+
with torch.no_grad():
|
112 |
+
outputs = gpt2_model.generate(**inputs, max_new_tokens=max_length, do_sample=True)
|
113 |
+
results['gpt2'] = gpt2_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
114 |
+
del gpt2_model
|
115 |
+
|
116 |
+
except Exception as e:
|
117 |
+
results['gpt2'] = f"๋ก๋ ์คํจ: {str(e)}"
|
118 |
+
|
119 |
+
# BERT๋ ์์ฑ ๋ชจ๋ธ์ด ์๋๋ฏ๋ก ์ ์ธ
|
120 |
+
results['bert'] = "BERT๋ ์์ฑ ๋ชจ๋ธ์ด ์๋๋๋ค (์ธ์ฝ๋ ์ ์ฉ)"
|
121 |
+
|
122 |
+
# ๋ณํฉ ๋ชจ๋ธ
|
123 |
+
try:
|
124 |
+
progress(0.6, desc="๋ณํฉ ๋ชจ๋ธ ์์ฑ ์ค...")
|
125 |
+
if self.model is None:
|
126 |
+
self.load_model()
|
127 |
+
|
128 |
+
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
|
129 |
+
with torch.no_grad():
|
130 |
+
outputs = self.model.generate(**inputs, max_new_tokens=max_length, do_sample=True)
|
131 |
+
results['merged'] = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
132 |
+
|
133 |
+
except Exception as e:
|
134 |
+
results['merged'] = f"์์ฑ ์คํจ: {str(e)}"
|
135 |
+
|
136 |
+
progress(1.0, desc="์๋ฃ!")
|
137 |
+
|
138 |
+
# ๊ฒฐ๊ณผ ํฌ๋งทํ
|
139 |
+
comparison = f"""๐ ๋ชจ๋ธ ๋น๊ต ๊ฒฐ๊ณผ:
|
140 |
+
|
141 |
+
**GPT-2 (๋ถ๋ชจ 1):**
|
142 |
+
{results['gpt2']}
|
143 |
+
|
144 |
+
**BERT (๋ถ๋ชจ 2):**
|
145 |
+
{results['bert']}
|
146 |
+
|
147 |
+
**๋ณํฉ ๋ชจ๋ธ (openfree/gpt2-bert):**
|
148 |
+
{results['merged']}"""
|
149 |
+
|
150 |
+
return comparison
|
151 |
+
|
152 |
+
# ์ ์ญ ์ธ์คํด์ค
|
153 |
+
tester = MergedModelTester()
|
154 |
+
|
155 |
+
# Gradio ์ธํฐํ์ด์ค
|
156 |
+
with gr.Blocks(title="GPT2-BERT ๋ณํฉ ๋ชจ๋ธ ํ
์คํฐ") as demo:
|
157 |
+
gr.Markdown("""
|
158 |
+
# ๐งฌ GPT2-BERT ๋ณํฉ ๋ชจ๋ธ ํ
์คํฐ
|
159 |
+
|
160 |
+
์งํ์ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋ณํฉ๋ [openfree/gpt2-bert](https://huggingface.co/openfree/gpt2-bert) ๋ชจ๋ธ์ ํ
์คํธํฉ๋๋ค.
|
161 |
+
|
162 |
+
## ๐ ๋ชจ๋ธ ์ ๋ณด
|
163 |
+
- **๋ถ๋ชจ 1**: openai-community/gpt2
|
164 |
+
- **๋ถ๋ชจ 2**: google-bert/bert-base-uncased
|
165 |
+
- **๋ณํฉ ๋ฐฉ๋ฒ**: SLERP (์งํ์ ์ต์ ํ)
|
166 |
+
- **์ต์ข
์ฑ๋ฅ**: 82-84% accuracy
|
167 |
+
""")
|
168 |
+
|
169 |
+
with gr.Tab("๐ ๋น ๋ฅธ ํ
์คํธ"):
|
170 |
+
with gr.Row():
|
171 |
+
with gr.Column():
|
172 |
+
load_btn = gr.Button("๐ฅ ๋ชจ๋ธ ๋ก๋", variant="primary")
|
173 |
+
load_status = gr.Textbox(label="๋ก๋ ์ํ", lines=4)
|
174 |
+
|
175 |
+
prompt_input = gr.Textbox(
|
176 |
+
label="ํ๋กฌํํธ",
|
177 |
+
placeholder="ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์...",
|
178 |
+
value="The future of AI is",
|
179 |
+
lines=3
|
180 |
+
)
|
181 |
+
|
182 |
+
with gr.Row():
|
183 |
+
max_length = gr.Slider(20, 200, 100, label="์ต๋ ๊ธธ์ด")
|
184 |
+
temperature = gr.Slider(0.1, 2.0, 0.8, label="Temperature")
|
185 |
+
|
186 |
+
with gr.Row():
|
187 |
+
top_p = gr.Slider(0.1, 1.0, 0.9, label="Top-p")
|
188 |
+
rep_penalty = gr.Slider(1.0, 2.0, 1.2, label="๋ฐ๋ณต ํจ๋ํฐ")
|
189 |
+
|
190 |
+
generate_btn = gr.Button("โจ ํ
์คํธ ์์ฑ", variant="primary")
|
191 |
+
|
192 |
+
with gr.Column():
|
193 |
+
output_text = gr.Textbox(label="์์ฑ๋ ํ
์คํธ", lines=10)
|
194 |
+
stats_text = gr.Textbox(label="์์ฑ ํต๊ณ", lines=6)
|
195 |
+
|
196 |
+
with gr.Tab("๐ฌ ๋ชจ๋ธ ๋น๊ต"):
|
197 |
+
compare_prompt = gr.Textbox(
|
198 |
+
label="๋น๊ตํ ํ๋กฌํํธ",
|
199 |
+
value="Once upon a time",
|
200 |
+
lines=2
|
201 |
+
)
|
202 |
+
compare_length = gr.Slider(20, 100, 50, label="์์ฑ ๊ธธ์ด")
|
203 |
+
compare_btn = gr.Button("๐ ๋ถ๋ชจ ๋ชจ๋ธ๊ณผ ๋น๊ต", variant="primary")
|
204 |
+
comparison_output = gr.Textbox(label="๋น๊ต ๊ฒฐ๊ณผ", lines=20)
|
205 |
+
|
206 |
+
with gr.Tab("๐งช ๊ณ ๊ธ ํ
์คํธ"):
|
207 |
+
gr.Markdown("### ๋ค์ํ ํ์คํฌ ํ
์คํธ")
|
208 |
+
|
209 |
+
task_type = gr.Radio(
|
210 |
+
["์ด์ผ๊ธฐ ์์ฑ", "์ง๋ฌธ ๋ต๋ณ", "์ฝ๋ ์์ฑ", "์ ์์ฑ"],
|
211 |
+
label="ํ์คํฌ ์ ํ",
|
212 |
+
value="์ด์ผ๊ธฐ ์์ฑ"
|
213 |
+
)
|
214 |
+
|
215 |
+
task_prompts = {
|
216 |
+
"์ด์ผ๊ธฐ ์์ฑ": "In a distant galaxy, a young explorer discovered",
|
217 |
+
"์ง๋ฌธ ๋ต๋ณ": "Q: What is machine learning?\nA:",
|
218 |
+
"์ฝ๋ ์์ฑ": "# Python function to calculate fibonacci\ndef fibonacci(n):",
|
219 |
+
"์ ์์ฑ": "Roses are red,\nViolets are blue,"
|
220 |
+
}
|
221 |
+
|
222 |
+
def update_prompt(task):
|
223 |
+
return task_prompts.get(task, "")
|
224 |
+
|
225 |
+
task_prompt = gr.Textbox(label="ํ์คํฌ ํ๋กฌํํธ", lines=3)
|
226 |
+
task_output = gr.Textbox(label="๊ฒฐ๊ณผ", lines=10)
|
227 |
+
task_btn = gr.Button("๐ฏ ํ์คํฌ ์คํ", variant="primary")
|
228 |
+
|
229 |
+
task_type.change(update_prompt, task_type, task_prompt)
|
230 |
+
|
231 |
+
with gr.Tab("๐ ์ฑ๋ฅ ๋ถ์"):
|
232 |
+
gr.Markdown("""
|
233 |
+
### ์งํ ์คํ ๊ฒฐ๊ณผ
|
234 |
+
|
235 |
+
| ๋ฉํธ๋ฆญ | ๊ฐ |
|
236 |
+
|--------|-----|
|
237 |
+
| ์ด๊ธฐ ์ฑ๋ฅ | 10.56% |
|
238 |
+
| ์ต์ข
์ฑ๋ฅ | 82-84% |
|
239 |
+
| ๊ฐ์ ์จ | +700% |
|
240 |
+
| ์ด ๊ฐ์ ํ์ | 2,136ํ |
|
241 |
+
| ํ์ต ์๊ฐ | 7.7๋ถ |
|
242 |
+
|
243 |
+
### ์ธ๋๋ณ ์ฑ๋ฅ
|
244 |
+
- **์ด๊ธฐ (0-2000)**: ํฐ ๊ฐ์ (+20-30%/์ธ๋)
|
245 |
+
- **์ค๊ธฐ (2000-5000)**: ์ค๊ฐ ๊ฐ์ (+10-15%/์ธ๋)
|
246 |
+
- **ํ๊ธฐ (5000-10000)**: ๋ฏธ์ธ ์กฐ์ (+2-5%/์ธ๋)
|
247 |
+
""")
|
248 |
+
|
249 |
+
test_suite_btn = gr.Button("๐ ์ ์ฒด ํ
์คํธ ์ค์ํธ ์คํ", variant="primary")
|
250 |
+
test_results = gr.Textbox(label="ํ
์คํธ ๊ฒฐ๊ณผ", lines=15)
|
251 |
+
|
252 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
253 |
+
load_btn.click(
|
254 |
+
lambda: tester.load_model("openfree/gpt2-bert"),
|
255 |
+
outputs=load_status
|
256 |
+
)
|
257 |
+
|
258 |
+
generate_btn.click(
|
259 |
+
tester.generate_text,
|
260 |
+
inputs=[prompt_input, max_length, temperature, top_p, rep_penalty],
|
261 |
+
outputs=[output_text, stats_text, gr.Textbox(visible=False)]
|
262 |
+
)
|
263 |
+
|
264 |
+
compare_btn.click(
|
265 |
+
tester.compare_with_parents,
|
266 |
+
inputs=[compare_prompt, compare_length],
|
267 |
+
outputs=comparison_output
|
268 |
+
)
|
269 |
+
|
270 |
+
task_btn.click(
|
271 |
+
lambda p: tester.generate_text(p, 100, 0.8, 0.9, 1.2),
|
272 |
+
inputs=task_prompt,
|
273 |
+
outputs=[task_output, gr.Textbox(visible=False), gr.Textbox(visible=False)]
|
274 |
+
)
|
275 |
+
|
276 |
+
def run_test_suite(progress=gr.Progress()):
|
277 |
+
"""์ ์ฒด ํ
์คํธ ์ค์ํธ ์คํ"""
|
278 |
+
results = []
|
279 |
+
|
280 |
+
test_prompts = [
|
281 |
+
"The meaning of life is",
|
282 |
+
"import numpy as np\n",
|
283 |
+
"Scientists have discovered",
|
284 |
+
"def hello_world():",
|
285 |
+
"Breaking news:"
|
286 |
+
]
|
287 |
+
|
288 |
+
for i, prompt in enumerate(test_prompts):
|
289 |
+
progress((i+1)/len(test_prompts), desc=f"ํ
์คํธ {i+1}/{len(test_prompts)}")
|
290 |
+
try:
|
291 |
+
output, stats, _ = tester.generate_text(prompt, 30)
|
292 |
+
results.append(f"โ
ํ๋กฌํํธ: {prompt[:30]}...\n ์์ฑ ์ฑ๊ณต")
|
293 |
+
except:
|
294 |
+
results.append(f"โ ํ๋กฌํํธ: {prompt[:30]}...\n ์์ฑ ์คํจ")
|
295 |
+
|
296 |
+
return "\n".join(results)
|
297 |
+
|
298 |
+
test_suite_btn.click(
|
299 |
+
run_test_suite,
|
300 |
+
outputs=test_results
|
301 |
+
)
|
302 |
+
|
303 |
+
# ์คํ
|
304 |
+
if __name__ == "__main__":
|
305 |
+
demo.launch(share=False)
|