|
|
|
|
|
import gradio as gr |
|
import torch |
|
from unsloth import FastLanguageModel |
|
import langid |
|
|
|
|
|
|
|
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, |
|
|
|
fast_inference=False, |
|
|
|
max_lora_rank=32, |
|
) |
|
|
|
|
|
|
|
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, |
|
) |
|
|
|
|
|
|
|
system_prompt = ( |
|
"You are given a problem.\n" |
|
"Think about the problem and provide your working out.\n" |
|
"You must think in Bahasa Indonesia." |
|
) |
|
|
|
|
|
def generate_response(user_prompt, use_lora): |
|
""" |
|
์ฌ์ฉ์ ์
๋ ฅ๊ณผ LoRA ์ฌ์ฉ ์ฌ๋ถ์ ๋ฐ๋ผ ๋ชจ๋ธ ์๋ต์ ์์ฑํฉ๋๋ค. |
|
""" |
|
lora_request = None |
|
if use_lora: |
|
try: |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
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, |
|
) |
|
|
|
|
|
demo.launch() |