File size: 5,392 Bytes
de2c765 6f2fe4d de2c765 6f2fe4d de2c765 6f2fe4d de2c765 |
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# app.py
import gradio as gr
import torch
from unsloth import FastLanguageModel
import langid
# 1. ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ์ ์ญ์ ์ผ๋ก ํ ๋ฒ๋ง ๋ก๋ํฉ๋๋ค.
# Zero-GPU ํ๊ฒฝ์ ๋ง๊ฒ 4๋นํธ๋ก ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค.
max_seq_length = 2048
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="unsloth/DeepSeek-R1-0528-Qwen3-8B",
max_seq_length=max_seq_length,
load_in_4bit=True,
# Zero-GPU(CPU) ํ๊ฒฝ์ด๋ฏ๋ก vLLM ๋นํ์ฑํ
fast_inference=False,
# LoRA ์ด๋ํฐ๋ฅผ ๋ก๋ํ๊ธฐ ์ํด ๋ฏธ๋ฆฌ ์ต๋ ๋ญํฌ๋ฅผ ์ง์ ํฉ๋๋ค.
max_lora_rank=32,
)
# PEFT ๋ชจ๋ธ์ LoRA ๋ชจ๋์ ์ถ๊ฐํฉ๋๋ค.
# ์ด ๋จ๊ณ๋ ์ถํ model.load_lora()๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ํ์ํฉ๋๋ค.
model = FastLanguageModel.get_peft_model(
model,
r=32,
target_modules=[
"q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj",
],
lora_alpha=64,
use_gradient_checkpointing="unsloth",
random_state=3407,
)
# 2. ์์คํ
ํ๋กฌํํธ ์ ์
# ๋
ธํธ๋ถ์์ ์ฌ์ฉ๋ ๊ฒ๊ณผ ๋์ผํ ์์คํ
ํ๋กฌํํธ์
๋๋ค.
system_prompt = (
"You are given a problem.\n"
"Think about the problem and provide your working out.\n"
"You must think in Bahasa Indonesia."
)
# 3. ์ถ๋ก ํจ์ ์ ์
def generate_response(user_prompt, use_lora):
"""
์ฌ์ฉ์ ์
๋ ฅ๊ณผ LoRA ์ฌ์ฉ ์ฌ๋ถ์ ๋ฐ๋ผ ๋ชจ๋ธ ์๋ต์ ์์ฑํฉ๋๋ค.
"""
lora_request = None
if use_lora:
try:
# Hugging Face Space์ ํจ๊ป ์
๋ก๋๋ LoRA ์ด๋ํฐ๋ฅผ ๋ก๋ํฉ๋๋ค.
# ํด๋ ์ด๋ฆ์ ๋
ธํธ๋ถ์์ ์ ์ฅํ 'grpo_lora'์ ์ผ์นํด์ผ ํฉ๋๋ค.
lora_request = model.load_lora("grpo_lora")
except Exception as e:
return f"LoRA ์ด๋ํฐ๋ฅผ ๋ก๋ํ๋ ๋ฐ ์คํจํ์ต๋๋ค: {e}\n'grpo_lora' ํด๋๋ฅผ Space์ ์
๋ก๋ํ๋์ง ํ์ธํ์ธ์.", "์ค๋ฅ"
# ์ฑํ
ํ
ํ๋ฆฟ ํ์์ ๋ง๊ฒ ์
๋ ฅ ๋ฉ์์ง๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
]
# ํ ํฌ๋์ด์ ๋ฅผ ์ฌ์ฉํ์ฌ ์
๋ ฅ ํ
์คํธ๋ฅผ ํฌ๋งทํ
ํฉ๋๋ค.
input_text = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=False,
)
inputs = tokenizer(input_text, return_tensors="pt").to("cpu")
# ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ํ
์คํธ ์์ฑ
# Unsloth๋ CPU์์๋ ๋น ๋ฅธ ์์ฑ์ ์ง์ํฉ๋๋ค.
outputs = model.generate(
**inputs,
max_new_tokens=512,
use_cache=True,
pad_token_id=tokenizer.eos_token_id
)
generated_text = tokenizer.batch_decode(outputs)[0]
# ์์ฑ๋ ํ
์คํธ์์ ํ๋กฌํํธ๋ฅผ ์ ์ธํ๊ณ ์์ ์๋ต๋ง ์ถ์ถํฉ๋๋ค.
response_only = generated_text[len(input_text):]
# ์์ฑ๋ ์๋ต์ ์ธ์ด๋ฅผ ๊ฐ์งํฉ๋๋ค.
lang, score = langid.classify(response_only)
lang_info = f"๊ฐ์ง๋ ์ธ์ด: {lang} (์ ๋ขฐ๋: {score:.2f})"
return response_only, lang_info
# 4. Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# ๐ฎ๐ฉ DeepSeek-R1-Qwen3-8B ๋ชจ๋ธ ์ถ๋ก (GRPO ํ๋)
์ด ๋ชจ๋ธ์ ์ํ ๋ฌธ์ ์ ๋ํด ์ธ๋๋ค์์์ด๋ก ์ถ๋ก ๊ณผ์ ์ ์ค๋ช
ํ๋๋ก ๋ฏธ์ธ ์กฐ์ ๋์์ต๋๋ค.
- **'์ธ๋๋ค์์์ด ์ถ๋ก LoRA ์ ์ฉ'** ์ฒดํฌ๋ฐ์ค๋ฅผ ํ์ฑํํ๋ฉด, ํ์ต๋ LoRA ๊ฐ์ค์น๊ฐ ์ ์ฉ๋์ด ์ธ๋๋ค์์์ด๋ก ๋ ๋ต๋ณ์ ์์ฑํ๋๋ก ์ ๋ํฉ๋๋ค.
- ์ฒดํฌ๋ฐ์ค๋ฅผ ๋นํ์ฑํํ๋ฉด ์๋ณธ ๋ชจ๋ธ์ ์ถ๋ก ๋ฅ๋ ฅ์ ํ์ธํ ์ ์์ต๋๋ค.
"""
)
with gr.Row():
with gr.Column(scale=2):
prompt_input = gr.Textbox(
label="์ง๋ฌธ ์
๋ ฅ",
placeholder="์: Solve (x + 2)^2 = 0"
)
lora_checkbox = gr.Checkbox(
label="์ธ๋๋ค์์์ด ์ถ๋ก LoRA ์ ์ฉ",
value=True
)
submit_button = gr.Button("์์ฑํ๊ธฐ", variant="primary")
with gr.Column(scale=3):
output_text = gr.Textbox(
label="๋ชจ๋ธ ์๋ต",
interactive=False
)
language_info = gr.Textbox(
label="์ธ์ด ๊ฐ์ง ๊ฒฐ๊ณผ",
interactive=False
)
submit_button.click(
fn=generate_response,
inputs=[prompt_input, lora_checkbox],
outputs=[output_text, language_info]
)
gr.Examples(
[
["Solve (x + 2)^2 = 0", True],
["What is the square root of 101?", True],
["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]
],
inputs=[prompt_input, lora_checkbox],
outputs=[output_text, language_info],
fn=generate_response,
cache_examples=False,
)
# Gradio ์ฑ ์คํ
demo.launch() |