yakdoli commited on
Commit
de2c765
ยท
verified ยท
1 Parent(s): 6f2fe4d
Files changed (1) hide show
  1. app.py +142 -4
app.py CHANGED
@@ -1,7 +1,145 @@
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
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()