--
Browse files
app.py
CHANGED
@@ -1,7 +1,145 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
demo.launch()
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from unsloth import FastLanguageModel
|
6 |
+
import langid
|
7 |
+
|
8 |
+
# 1. ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ์ ์ญ์ ์ผ๋ก ํ ๋ฒ๋ง ๋ก๋ํฉ๋๋ค.
|
9 |
+
# Zero-GPU ํ๊ฒฝ์ ๋ง๊ฒ 4๋นํธ๋ก ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค.
|
10 |
+
max_seq_length = 2048
|
11 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
12 |
+
model_name="unsloth/DeepSeek-R1-0528-Qwen3-8B",
|
13 |
+
max_seq_length=max_seq_length,
|
14 |
+
load_in_4bit=True,
|
15 |
+
# Zero-GPU(CPU) ํ๊ฒฝ์ด๋ฏ๋ก vLLM ๋นํ์ฑํ
|
16 |
+
fast_inference=False,
|
17 |
+
# LoRA ์ด๋ํฐ๋ฅผ ๋ก๋ํ๊ธฐ ์ํด ๋ฏธ๋ฆฌ ์ต๋ ๋ญํฌ๋ฅผ ์ง์ ํฉ๋๋ค.
|
18 |
+
max_lora_rank=32,
|
19 |
+
)
|
20 |
+
|
21 |
+
# PEFT ๋ชจ๋ธ์ LoRA ๋ชจ๋์ ์ถ๊ฐํฉ๋๋ค.
|
22 |
+
# ์ด ๋จ๊ณ๋ ์ถํ model.load_lora()๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ํ์ํฉ๋๋ค.
|
23 |
+
model = FastLanguageModel.get_peft_model(
|
24 |
+
model,
|
25 |
+
r=32,
|
26 |
+
target_modules=[
|
27 |
+
"q_proj", "k_proj", "v_proj", "o_proj",
|
28 |
+
"gate_proj", "up_proj", "down_proj",
|
29 |
+
],
|
30 |
+
lora_alpha=64,
|
31 |
+
use_gradient_checkpointing="unsloth",
|
32 |
+
random_state=3407,
|
33 |
+
)
|
34 |
+
|
35 |
+
# 2. ์์คํ
ํ๋กฌํํธ ์ ์
|
36 |
+
# ๋
ธํธ๋ถ์์ ์ฌ์ฉ๋ ๊ฒ๊ณผ ๋์ผํ ์์คํ
ํ๋กฌํํธ์
๋๋ค.
|
37 |
+
system_prompt = (
|
38 |
+
"You are given a problem.\n"
|
39 |
+
"Think about the problem and provide your working out.\n"
|
40 |
+
"You must think in Bahasa Indonesia."
|
41 |
+
)
|
42 |
+
|
43 |
+
# 3. ์ถ๋ก ํจ์ ์ ์
|
44 |
+
def generate_response(user_prompt, use_lora):
|
45 |
+
"""
|
46 |
+
์ฌ์ฉ์ ์
๋ ฅ๊ณผ LoRA ์ฌ์ฉ ์ฌ๋ถ์ ๋ฐ๋ผ ๋ชจ๋ธ ์๋ต์ ์์ฑํฉ๋๋ค.
|
47 |
+
"""
|
48 |
+
lora_request = None
|
49 |
+
if use_lora:
|
50 |
+
try:
|
51 |
+
# Hugging Face Space์ ํจ๊ป ์
๋ก๋๋ LoRA ์ด๋ํฐ๋ฅผ ๋ก๋ํฉ๋๋ค.
|
52 |
+
# ํด๋ ์ด๋ฆ์ ๋
ธํธ๋ถ์์ ์ ์ฅํ 'grpo_lora'์ ์ผ์นํด์ผ ํฉ๋๋ค.
|
53 |
+
lora_request = model.load_lora("grpo_lora")
|
54 |
+
except Exception as e:
|
55 |
+
return f"LoRA ์ด๋ํฐ๋ฅผ ๋ก๋ํ๋ ๋ฐ ์คํจํ์ต๋๋ค: {e}\n'grpo_lora' ํด๋๋ฅผ Space์ ์
๋ก๋ํ๋์ง ํ์ธํ์ธ์.", "์ค๋ฅ"
|
56 |
+
|
57 |
+
# ์ฑํ
ํ
ํ๋ฆฟ ํ์์ ๋ง๊ฒ ์
๋ ฅ ๋ฉ์์ง๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.
|
58 |
+
messages = [
|
59 |
+
{"role": "system", "content": system_prompt},
|
60 |
+
{"role": "user", "content": user_prompt},
|
61 |
+
]
|
62 |
+
|
63 |
+
# ํ ํฌ๋์ด์ ๋ฅผ ์ฌ์ฉํ์ฌ ์
๋ ฅ ํ
์คํธ๋ฅผ ํฌ๋งทํ
ํฉ๋๋ค.
|
64 |
+
input_text = tokenizer.apply_chat_template(
|
65 |
+
messages,
|
66 |
+
add_generation_prompt=True,
|
67 |
+
tokenize=False,
|
68 |
+
)
|
69 |
+
|
70 |
+
inputs = tokenizer(input_text, return_tensors="pt").to("cpu")
|
71 |
+
|
72 |
+
# ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ํ
์คํธ ์์ฑ
|
73 |
+
# Unsloth๋ CPU์์๋ ๋น ๋ฅธ ์์ฑ์ ์ง์ํฉ๋๋ค.
|
74 |
+
outputs = model.generate(
|
75 |
+
**inputs,
|
76 |
+
max_new_tokens=512,
|
77 |
+
use_cache=True,
|
78 |
+
pad_token_id=tokenizer.eos_token_id
|
79 |
+
)
|
80 |
+
|
81 |
+
generated_text = tokenizer.batch_decode(outputs)[0]
|
82 |
+
|
83 |
+
# ์์ฑ๋ ํ
์คํธ์์ ํ๋กฌํํธ๋ฅผ ์ ์ธํ๊ณ ์์ ์๋ต๋ง ์ถ์ถํฉ๋๋ค.
|
84 |
+
response_only = generated_text[len(input_text):]
|
85 |
+
|
86 |
+
# ์์ฑ๋ ์๋ต์ ์ธ์ด๋ฅผ ๊ฐ์งํฉ๋๋ค.
|
87 |
+
lang, score = langid.classify(response_only)
|
88 |
+
lang_info = f"๊ฐ์ง๋ ์ธ์ด: {lang} (์ ๋ขฐ๋: {score:.2f})"
|
89 |
+
|
90 |
+
return response_only, lang_info
|
91 |
+
|
92 |
|
93 |
+
# 4. Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
94 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
95 |
+
gr.Markdown(
|
96 |
+
"""
|
97 |
+
# ๐ฎ๐ฉ DeepSeek-R1-Qwen3-8B ๋ชจ๋ธ ์ถ๋ก (GRPO ํ๋)
|
98 |
+
์ด ๋ชจ๋ธ์ ์ํ ๋ฌธ์ ์ ๋ํด ์ธ๋๋ค์์์ด๋ก ์ถ๋ก ๊ณผ์ ์ ์ค๋ช
ํ๋๋ก ๋ฏธ์ธ ์กฐ์ ๋์์ต๋๋ค.
|
99 |
+
- **'์ธ๋๋ค์์์ด ์ถ๋ก LoRA ์ ์ฉ'** ์ฒดํฌ๋ฐ์ค๋ฅผ ํ์ฑํํ๋ฉด, ํ์ต๋ LoRA ๊ฐ์ค์น๊ฐ ์ ์ฉ๋์ด ์ธ๋๋ค์์์ด๋ก ๋ ๋ต๋ณ์ ์์ฑํ๋๋ก ์ ๋ํฉ๋๋ค.
|
100 |
+
- ์ฒดํฌ๋ฐ์ค๋ฅผ ๋นํ์ฑํํ๋ฉด ์๋ณธ ๋ชจ๋ธ์ ์ถ๋ก ๋ฅ๋ ฅ์ ํ์ธํ ์ ์์ต๋๋ค.
|
101 |
+
"""
|
102 |
+
)
|
103 |
+
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Column(scale=2):
|
106 |
+
prompt_input = gr.Textbox(
|
107 |
+
label="์ง๋ฌธ ์
๋ ฅ",
|
108 |
+
placeholder="์: Solve (x + 2)^2 = 0"
|
109 |
+
)
|
110 |
+
lora_checkbox = gr.Checkbox(
|
111 |
+
label="์ธ๋๋ค์์์ด ์ถ๋ก LoRA ์ ์ฉ",
|
112 |
+
value=True
|
113 |
+
)
|
114 |
+
submit_button = gr.Button("์์ฑํ๊ธฐ", variant="primary")
|
115 |
+
|
116 |
+
with gr.Column(scale=3):
|
117 |
+
output_text = gr.Textbox(
|
118 |
+
label="๋ชจ๋ธ ์๋ต",
|
119 |
+
interactive=False
|
120 |
+
)
|
121 |
+
language_info = gr.Textbox(
|
122 |
+
label="์ธ์ด ๊ฐ์ง ๊ฒฐ๊ณผ",
|
123 |
+
interactive=False
|
124 |
+
)
|
125 |
+
|
126 |
+
submit_button.click(
|
127 |
+
fn=generate_response,
|
128 |
+
inputs=[prompt_input, lora_checkbox],
|
129 |
+
outputs=[output_text, language_info]
|
130 |
+
)
|
131 |
+
|
132 |
+
gr.Examples(
|
133 |
+
[
|
134 |
+
["Solve (x + 2)^2 = 0", True],
|
135 |
+
["What is the square root of 101?", True],
|
136 |
+
["In triangle $ABC$, $\\sin \\angle A = \\frac{4}{5}$ and $\\angle A < 90^\\circ$. Let $D$ be a point outside triangle $ABC$ such that $\\angle BAD = \\angle DAC$ and $\\angle BDC = 90^\\circ$. Suppose that $AD = 1$ and that $\\frac{BD}{CD} = \\frac{3}{2}$. If $AB + AC$ can be expressed in the form $\\frac{a\\sqrt{b}}{c}$ where $a, b, c$ are pairwise relatively prime integers, find $a + b + c$.", True]
|
137 |
+
],
|
138 |
+
inputs=[prompt_input, lora_checkbox],
|
139 |
+
outputs=[output_text, language_info],
|
140 |
+
fn=generate_response,
|
141 |
+
cache_examples=False,
|
142 |
+
)
|
143 |
|
144 |
+
# Gradio ์ฑ ์คํ
|
145 |
+
demo.launch()
|