Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,32 @@
|
|
1 |
import re
|
2 |
import threading
|
|
|
|
|
3 |
|
4 |
import gradio as gr
|
5 |
import spaces
|
6 |
import transformers
|
7 |
-
from transformers import pipeline
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
10 |
available_models = {
|
11 |
-
"meta-llama/Llama-3.2-3B-Instruct": "Llama 3.2(3B)",
|
12 |
"Hermes-3-Llama-3.1-8B": "Hermes 3 Llama 3.1 (8B)",
|
13 |
"nvidia/Llama-3.1-Nemotron-Nano-8B-v1": "Nvidia Nemotron Nano (8B)",
|
14 |
"mistralai/Mistral-Small-3.1-24B-Instruct-2503": "Mistral Small 3.1 (24B)",
|
15 |
-
"bartowski/mistralai_Mistral-Small-3.1-24B-Instruct-2503-GGUF": "Mistral Small GGUF (24B)",
|
16 |
"google/gemma-3-27b-it": "Google Gemma 3 (27B)",
|
17 |
-
"gemma-3-27b-it-abliterated": "Gemma 3 Abliterated (27B)",
|
18 |
"Qwen/Qwen2.5-Coder-32B-Instruct": "Qwen 2.5 Coder (32B)",
|
19 |
"open-r1/OlympicCoder-32B": "Olympic Coder (32B)"
|
20 |
}
|
21 |
|
22 |
-
#
|
23 |
pipe = None
|
|
|
24 |
|
25 |
# ์ต์ข
๋ต๋ณ์ ๊ฐ์งํ๊ธฐ ์ํ ๋ง์ปค
|
26 |
ANSWER_MARKER = "**๋ต๋ณ**"
|
@@ -40,31 +46,69 @@ rethink_prepends = [
|
|
40 |
f"\n{ANSWER_MARKER}\n",
|
41 |
]
|
42 |
|
43 |
-
|
44 |
# ์์ ํ์ ๋ฌธ์ ํด๊ฒฐ์ ์ํ ์ค์
|
45 |
latex_delimiters = [
|
46 |
{"left": "$$", "right": "$$", "display": True},
|
47 |
{"left": "$", "right": "$", "display": False},
|
48 |
]
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
def reformat_math(text):
|
52 |
-
"""Gradio ๊ตฌ๋ฌธ(Katex)์ ์ฌ์ฉํ๋๋ก MathJax ๊ตฌ๋ถ ๊ธฐํธ ์์ .
|
53 |
-
์ด๊ฒ์ Gradio์์ ์ํ ๊ณต์์ ํ์ํ๊ธฐ ์ํ ์์ ํด๊ฒฐ์ฑ
์
๋๋ค. ํ์ฌ๋ก์๋
|
54 |
-
๋ค๋ฅธ latex_delimiters๋ฅผ ์ฌ์ฉํ์ฌ ์์๋๋ก ์๋ํ๊ฒ ํ๋ ๋ฐฉ๋ฒ์ ์ฐพ์ง ๋ชปํ์ต๋๋ค...
|
55 |
-
"""
|
56 |
text = re.sub(r"\\\[\s*(.*?)\s*\\\]", r"$$\1$$", text, flags=re.DOTALL)
|
57 |
text = re.sub(r"\\\(\s*(.*?)\s*\\\)", r"$\1$", text, flags=re.DOTALL)
|
58 |
return text
|
59 |
|
60 |
-
|
61 |
def user_input(message, history: list):
|
62 |
"""์ฌ์ฉ์ ์
๋ ฅ์ ํ์คํ ๋ฆฌ์ ์ถ๊ฐํ๊ณ ์
๋ ฅ ํ
์คํธ ์์ ๋น์ฐ๊ธฐ"""
|
63 |
return "", history + [
|
64 |
gr.ChatMessage(role="user", content=message.replace(ANSWER_MARKER, ""))
|
65 |
]
|
66 |
|
67 |
-
|
68 |
def rebuild_messages(history: list):
|
69 |
"""์ค๊ฐ ์๊ฐ ๊ณผ์ ์์ด ๋ชจ๋ธ์ด ์ฌ์ฉํ ํ์คํ ๋ฆฌ์์ ๋ฉ์์ง ์ฌ๊ตฌ์ฑ"""
|
70 |
messages = []
|
@@ -79,27 +123,68 @@ def rebuild_messages(history: list):
|
|
79 |
messages.append({"role": h.role, "content": h.content})
|
80 |
return messages
|
81 |
|
82 |
-
|
83 |
def load_model(model_names):
|
84 |
-
"""์ ํ๋ ๋ชจ๋ธ ์ด๋ฆ์ ๋ฐ๋ผ ๋ชจ๋ธ ๋ก๋"""
|
85 |
-
global pipe
|
|
|
|
|
|
|
86 |
|
87 |
# ๋ชจ๋ธ์ด ์ ํ๋์ง ์์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ ์ง์
|
88 |
if not model_names:
|
89 |
-
model_name = "
|
90 |
else:
|
91 |
-
# ์ฒซ ๋ฒ์งธ ์ ํ๋ ๋ชจ๋ธ ์ฌ์ฉ
|
92 |
model_name = model_names[0]
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
device_map="auto",
|
98 |
-
torch_dtype="auto",
|
99 |
-
)
|
100 |
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
@spaces.GPU
|
105 |
def bot(
|
@@ -123,9 +208,17 @@ def bot(
|
|
123 |
yield history
|
124 |
return
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
# ๋์ค์ ์ค๋ ๋์์ ํ ํฐ์ ์คํธ๋ฆผ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ ์ํจ
|
127 |
streamer = transformers.TextIteratorStreamer(
|
128 |
-
pipe.tokenizer,
|
129 |
skip_special_tokens=True,
|
130 |
skip_prompt=True,
|
131 |
)
|
@@ -144,41 +237,75 @@ def bot(
|
|
144 |
|
145 |
# ํ์ฌ ์ฑํ
์ ํ์๋ ์ถ๋ก ๊ณผ์
|
146 |
messages = rebuild_messages(history)
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
yield history
|
177 |
-
t.join()
|
178 |
|
179 |
yield history
|
180 |
|
181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
with gr.Blocks(fill_height=True, title="ThinkFlow - Step-by-step Reasoning Service") as demo:
|
183 |
# ์๋จ์ ํ์ดํ๊ณผ ์ค๋ช
์ถ๊ฐ
|
184 |
gr.Markdown("""
|
@@ -193,6 +320,7 @@ with gr.Blocks(fill_height=True, title="ThinkFlow - Step-by-step Reasoning Servi
|
|
193 |
scale=1,
|
194 |
type="messages",
|
195 |
latex_delimiters=latex_delimiters,
|
|
|
196 |
)
|
197 |
msg = gr.Textbox(
|
198 |
submit_btn=True,
|
@@ -203,49 +331,63 @@ with gr.Blocks(fill_height=True, title="ThinkFlow - Step-by-step Reasoning Servi
|
|
203 |
)
|
204 |
|
205 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
206 |
# ๋ชจ๋ธ ์ ํ ์น์
์ถ๊ฐ
|
207 |
gr.Markdown("""## ๋ชจ๋ธ ์ ํ""")
|
208 |
-
model_selector = gr.
|
209 |
choices=list(available_models.values()),
|
210 |
-
value=
|
211 |
-
label="์ฌ์ฉํ LLM ๋ชจ๋ธ ์ ํ
|
212 |
)
|
213 |
|
214 |
# ๋ชจ๋ธ ๋ก๋ ๋ฒํผ
|
215 |
-
load_model_btn = gr.Button("๋ชจ๋ธ ๋ก๋")
|
216 |
model_status = gr.Textbox(label="๋ชจ๋ธ ์ํ", interactive=False)
|
217 |
|
|
|
|
|
|
|
218 |
gr.Markdown("""## ๋งค๊ฐ๋ณ์ ์กฐ์ """)
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
|
|
237 |
|
238 |
# ์ ํ๋ ๋ชจ๋ธ ๋ก๋ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
239 |
-
def get_model_names(
|
240 |
# ํ์ ์ด๋ฆ์์ ์๋ ๋ชจ๋ธ ์ด๋ฆ์ผ๋ก ๋ณํ
|
241 |
inverse_map = {v: k for k, v in available_models.items()}
|
242 |
-
return [inverse_map[
|
243 |
|
244 |
load_model_btn.click(
|
245 |
lambda selected: load_model(get_model_names(selected)),
|
246 |
inputs=[model_selector],
|
247 |
outputs=[model_status]
|
248 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
|
250 |
# ์ฌ์ฉ์๊ฐ ๋ฉ์์ง๋ฅผ ์ ์ถํ๋ฉด ๋ด์ด ์๋ตํฉ๋๋ค
|
251 |
msg.submit(
|
@@ -265,4 +407,12 @@ with gr.Blocks(fill_height=True, title="ThinkFlow - Step-by-step Reasoning Servi
|
|
265 |
)
|
266 |
|
267 |
if __name__ == "__main__":
|
268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import re
|
2 |
import threading
|
3 |
+
import gc
|
4 |
+
import torch
|
5 |
|
6 |
import gradio as gr
|
7 |
import spaces
|
8 |
import transformers
|
9 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
10 |
|
11 |
+
# ๋ชจ๋ธ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ ๋ฐ ์ต์ ํ๋ฅผ ์ํ ์ค์
|
12 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
DTYPE = torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
14 |
+
MAX_GPU_MEMORY = 80 * 1024 * 1024 * 1024 # 80GB A100 ๊ธฐ์ค (์ค์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฉ๋ชจ๋ฆฌ๋ ์ด๋ณด๋ค ์ ์)
|
15 |
+
|
16 |
+
# ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ธ ๋ชฉ๋ก - A100์์ ํจ์จ์ ์ผ๋ก ์คํ ๊ฐ๋ฅํ ๋ชจ๋ธ๋ก ํํฐ๋ง
|
17 |
available_models = {
|
18 |
+
"meta-llama/Llama-3.2-3B-Instruct": "Llama 3.2 (3B)",
|
19 |
"Hermes-3-Llama-3.1-8B": "Hermes 3 Llama 3.1 (8B)",
|
20 |
"nvidia/Llama-3.1-Nemotron-Nano-8B-v1": "Nvidia Nemotron Nano (8B)",
|
21 |
"mistralai/Mistral-Small-3.1-24B-Instruct-2503": "Mistral Small 3.1 (24B)",
|
|
|
22 |
"google/gemma-3-27b-it": "Google Gemma 3 (27B)",
|
|
|
23 |
"Qwen/Qwen2.5-Coder-32B-Instruct": "Qwen 2.5 Coder (32B)",
|
24 |
"open-r1/OlympicCoder-32B": "Olympic Coder (32B)"
|
25 |
}
|
26 |
|
27 |
+
# ๋ชจ๋ธ ๋ก๋์ ์ฌ์ฉ๋๋ ์ ์ญ ๋ณ์
|
28 |
pipe = None
|
29 |
+
current_model_name = None
|
30 |
|
31 |
# ์ต์ข
๋ต๋ณ์ ๊ฐ์งํ๊ธฐ ์ํ ๋ง์ปค
|
32 |
ANSWER_MARKER = "**๋ต๋ณ**"
|
|
|
46 |
f"\n{ANSWER_MARKER}\n",
|
47 |
]
|
48 |
|
|
|
49 |
# ์์ ํ์ ๋ฌธ์ ํด๊ฒฐ์ ์ํ ์ค์
|
50 |
latex_delimiters = [
|
51 |
{"left": "$$", "right": "$$", "display": True},
|
52 |
{"left": "$", "right": "$", "display": False},
|
53 |
]
|
54 |
|
55 |
+
# ๋ชจ๋ธ ํฌ๊ธฐ ๊ธฐ๋ฐ ๊ตฌ์ฑ - ๋ชจ๋ธ ํฌ๊ธฐ์ ๋ฐ๋ฅธ ์ต์ ์ค์ ์ ์
|
56 |
+
MODEL_CONFIG = {
|
57 |
+
"small": { # <10B
|
58 |
+
"max_memory": {0: "20GiB"},
|
59 |
+
"offload": False,
|
60 |
+
"quantization": None
|
61 |
+
},
|
62 |
+
"medium": { # 10B-30B
|
63 |
+
"max_memory": {0: "40GiB"},
|
64 |
+
"offload": False,
|
65 |
+
"quantization": "4bit"
|
66 |
+
},
|
67 |
+
"large": { # >30B
|
68 |
+
"max_memory": {0: "70GiB"},
|
69 |
+
"offload": True,
|
70 |
+
"quantization": "4bit"
|
71 |
+
}
|
72 |
+
}
|
73 |
+
|
74 |
+
def get_model_size_category(model_name):
|
75 |
+
"""๋ชจ๋ธ ํฌ๊ธฐ ์นดํ
๊ณ ๋ฆฌ ๊ฒฐ์ """
|
76 |
+
if "3B" in model_name or "8B" in model_name:
|
77 |
+
return "small"
|
78 |
+
elif "24B" in model_name or "27B" in model_name:
|
79 |
+
return "medium"
|
80 |
+
elif "32B" in model_name or "70B" in model_name:
|
81 |
+
return "large"
|
82 |
+
else:
|
83 |
+
# ๊ธฐ๋ณธ๊ฐ์ผ๋ก medium ๋ฐํ
|
84 |
+
return "medium"
|
85 |
+
|
86 |
+
def clear_gpu_memory():
|
87 |
+
"""GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ"""
|
88 |
+
global pipe
|
89 |
+
|
90 |
+
if pipe is not None:
|
91 |
+
del pipe
|
92 |
+
pipe = None
|
93 |
+
|
94 |
+
# CUDA ์บ์ ์ ๋ฆฌ
|
95 |
+
gc.collect()
|
96 |
+
if torch.cuda.is_available():
|
97 |
+
torch.cuda.empty_cache()
|
98 |
+
torch.cuda.synchronize()
|
99 |
|
100 |
def reformat_math(text):
|
101 |
+
"""Gradio ๊ตฌ๋ฌธ(Katex)์ ์ฌ์ฉํ๋๋ก MathJax ๊ตฌ๋ถ ๊ธฐํธ ์์ ."""
|
|
|
|
|
|
|
102 |
text = re.sub(r"\\\[\s*(.*?)\s*\\\]", r"$$\1$$", text, flags=re.DOTALL)
|
103 |
text = re.sub(r"\\\(\s*(.*?)\s*\\\)", r"$\1$", text, flags=re.DOTALL)
|
104 |
return text
|
105 |
|
|
|
106 |
def user_input(message, history: list):
|
107 |
"""์ฌ์ฉ์ ์
๋ ฅ์ ํ์คํ ๋ฆฌ์ ์ถ๊ฐํ๊ณ ์
๋ ฅ ํ
์คํธ ์์ ๋น์ฐ๊ธฐ"""
|
108 |
return "", history + [
|
109 |
gr.ChatMessage(role="user", content=message.replace(ANSWER_MARKER, ""))
|
110 |
]
|
111 |
|
|
|
112 |
def rebuild_messages(history: list):
|
113 |
"""์ค๊ฐ ์๊ฐ ๊ณผ์ ์์ด ๋ชจ๋ธ์ด ์ฌ์ฉํ ํ์คํ ๋ฆฌ์์ ๋ฉ์์ง ์ฌ๊ตฌ์ฑ"""
|
114 |
messages = []
|
|
|
123 |
messages.append({"role": h.role, "content": h.content})
|
124 |
return messages
|
125 |
|
|
|
126 |
def load_model(model_names):
|
127 |
+
"""์ ํ๋ ๋ชจ๋ธ ์ด๋ฆ์ ๋ฐ๋ผ ๋ชจ๋ธ ๋ก๋ (A100์ ์ต์ ํ๋ ์ค์ ์ฌ์ฉ)"""
|
128 |
+
global pipe, current_model_name
|
129 |
+
|
130 |
+
# ๊ธฐ์กด ๋ชจ๋ธ ์ ๋ฆฌ
|
131 |
+
clear_gpu_memory()
|
132 |
|
133 |
# ๋ชจ๋ธ์ด ์ ํ๋์ง ์์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ ์ง์
|
134 |
if not model_names:
|
135 |
+
model_name = "meta-llama/Llama-3.2-3B-Instruct" # ๋ ์์ ๋ชจ๋ธ์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ฌ์ฉ
|
136 |
else:
|
137 |
+
# ์ฒซ ๋ฒ์งธ ์ ํ๋ ๋ชจ๋ธ ์ฌ์ฉ
|
138 |
model_name = model_names[0]
|
139 |
|
140 |
+
# ๋ชจ๋ธ ํฌ๊ธฐ ์นดํ
๊ณ ๋ฆฌ ํ์ธ
|
141 |
+
size_category = get_model_size_category(model_name)
|
142 |
+
config = MODEL_CONFIG[size_category]
|
|
|
|
|
|
|
143 |
|
144 |
+
# ๋ชจ๋ธ ๋ก๋ (ํฌ๊ธฐ์ ๋ฐ๋ผ ์ต์ ํ๋ ์ค์ ์ ์ฉ)
|
145 |
+
try:
|
146 |
+
# BF16 ์ ๋ฐ๋ ์ฌ์ฉ (A100์ ์ต์ ํ)
|
147 |
+
if config["quantization"]:
|
148 |
+
# ์์ํ ์ ์ฉ
|
149 |
+
from transformers import BitsAndBytesConfig
|
150 |
+
quantization_config = BitsAndBytesConfig(
|
151 |
+
load_in_4bit=config["quantization"] == "4bit",
|
152 |
+
bnb_4bit_compute_dtype=DTYPE
|
153 |
+
)
|
154 |
+
|
155 |
+
model = AutoModelForCausalLM.from_pretrained(
|
156 |
+
model_name,
|
157 |
+
device_map="auto",
|
158 |
+
max_memory=config["max_memory"],
|
159 |
+
torch_dtype=DTYPE,
|
160 |
+
quantization_config=quantization_config if config["quantization"] else None,
|
161 |
+
offload_folder="offload" if config["offload"] else None,
|
162 |
+
trust_remote_code=True
|
163 |
+
)
|
164 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
165 |
+
|
166 |
+
pipe = pipeline(
|
167 |
+
"text-generation",
|
168 |
+
model=model,
|
169 |
+
tokenizer=tokenizer,
|
170 |
+
torch_dtype=DTYPE,
|
171 |
+
device_map="auto"
|
172 |
+
)
|
173 |
+
else:
|
174 |
+
# ์์ํ ์์ด ๋ก๋
|
175 |
+
pipe = pipeline(
|
176 |
+
"text-generation",
|
177 |
+
model=model_name,
|
178 |
+
device_map="auto",
|
179 |
+
torch_dtype=DTYPE,
|
180 |
+
trust_remote_code=True
|
181 |
+
)
|
182 |
+
|
183 |
+
current_model_name = model_name
|
184 |
+
return f"๋ชจ๋ธ '{model_name}'์ด(๊ฐ) ์ฑ๊ณต์ ์ผ๋ก ๋ก๋๋์์ต๋๋ค. (์ต์ ํ: {size_category} ์นดํ
๊ณ ๋ฆฌ)"
|
185 |
+
|
186 |
+
except Exception as e:
|
187 |
+
return f"๋ชจ๋ธ ๋ก๋ ์คํจ: {str(e)}"
|
188 |
|
189 |
@spaces.GPU
|
190 |
def bot(
|
|
|
208 |
yield history
|
209 |
return
|
210 |
|
211 |
+
# ํ ํฐ ๊ธธ์ด ์๋ ์กฐ์ (๋ชจ๋ธ ํฌ๊ธฐ์ ๋ฐ๋ผ)
|
212 |
+
size_category = get_model_size_category(current_model_name)
|
213 |
+
|
214 |
+
# ๋ํ ๋ชจ๋ธ์ ํ ํฐ ์๋ฅผ ์ค์ฌ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ ํฅ์
|
215 |
+
if size_category == "large":
|
216 |
+
max_num_tokens = min(max_num_tokens, 1000)
|
217 |
+
final_num_tokens = min(final_num_tokens, 1500)
|
218 |
+
|
219 |
# ๋์ค์ ์ค๋ ๋์์ ํ ํฐ์ ์คํธ๋ฆผ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ ์ํจ
|
220 |
streamer = transformers.TextIteratorStreamer(
|
221 |
+
pipe.tokenizer,
|
222 |
skip_special_tokens=True,
|
223 |
skip_prompt=True,
|
224 |
)
|
|
|
237 |
|
238 |
# ํ์ฌ ์ฑํ
์ ํ์๋ ์ถ๋ก ๊ณผ์
|
239 |
messages = rebuild_messages(history)
|
240 |
+
|
241 |
+
try:
|
242 |
+
for i, prepend in enumerate(rethink_prepends):
|
243 |
+
if i > 0:
|
244 |
+
messages[-1]["content"] += "\n\n"
|
245 |
+
messages[-1]["content"] += prepend.format(question=question)
|
246 |
|
247 |
+
num_tokens = int(
|
248 |
+
max_num_tokens if ANSWER_MARKER not in prepend else final_num_tokens
|
249 |
+
)
|
250 |
+
|
251 |
+
# ์ค๋ ๋์์ ๋ชจ๋ธ ์คํ
|
252 |
+
t = threading.Thread(
|
253 |
+
target=pipe,
|
254 |
+
args=(messages,),
|
255 |
+
kwargs=dict(
|
256 |
+
max_new_tokens=num_tokens,
|
257 |
+
streamer=streamer,
|
258 |
+
do_sample=do_sample,
|
259 |
+
temperature=temperature,
|
260 |
+
# ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ์ ์ํ ์ถ๊ฐ ํ๋ผ๋ฏธํฐ
|
261 |
+
repetition_penalty=1.2, # ๋ฐ๋ณต ๋ฐฉ์ง
|
262 |
+
use_cache=True, # KV ์บ์ ์ฌ์ฉ
|
263 |
+
),
|
264 |
+
)
|
265 |
+
t.start()
|
266 |
|
267 |
+
# ์ ๋ด์ฉ์ผ๋ก ํ์คํ ๋ฆฌ ์ฌ๊ตฌ์ฑ
|
268 |
+
history[-1].content += prepend.format(question=question)
|
269 |
+
if ANSWER_MARKER in prepend:
|
270 |
+
history[-1].metadata = {"title": "๐ญ ์ฌ๊ณ ๊ณผ์ ", "status": "done"}
|
271 |
+
# ์๊ฐ ์ข
๋ฃ, ์ด์ ๋ต๋ณ์
๋๋ค (์ค๊ฐ ๋จ๊ณ์ ๋ํ ๋ฉํ๋ฐ์ดํฐ ์์)
|
272 |
+
history.append(gr.ChatMessage(role="assistant", content=""))
|
273 |
+
|
274 |
+
# ํ ํฐ ์คํธ๋ฆฌ๋ฐ
|
275 |
+
for token in streamer:
|
276 |
+
history[-1].content += token
|
277 |
+
history[-1].content = reformat_math(history[-1].content)
|
278 |
+
yield history
|
279 |
+
|
280 |
+
t.join()
|
281 |
+
|
282 |
+
# ๋ํ ๋ชจ๋ธ์ธ ๊ฒฝ์ฐ ๊ฐ ๋จ๊ณ ํ ๋ถ๋ถ์ ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
283 |
+
if size_category == "large" and torch.cuda.is_available():
|
284 |
+
torch.cuda.empty_cache()
|
285 |
+
|
286 |
+
except Exception as e:
|
287 |
+
# ์ค๋ฅ ๋ฐ์์ ์ฌ์ฉ์์๊ฒ ์๋ฆผ
|
288 |
+
if len(history) > 0 and history[-1].role == "assistant":
|
289 |
+
history[-1].content += f"\n\nโ ๏ธ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
290 |
yield history
|
|
|
291 |
|
292 |
yield history
|
293 |
|
294 |
|
295 |
+
# ์ฌ์ฉ ๊ฐ๋ฅํ GPU ์ ๋ณด ํ์ ํจ์
|
296 |
+
def get_gpu_info():
|
297 |
+
if not torch.cuda.is_available():
|
298 |
+
return "GPU๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค."
|
299 |
+
|
300 |
+
gpu_info = []
|
301 |
+
for i in range(torch.cuda.device_count()):
|
302 |
+
gpu_name = torch.cuda.get_device_name(i)
|
303 |
+
total_memory = torch.cuda.get_device_properties(i).total_memory / 1024**3
|
304 |
+
gpu_info.append(f"GPU {i}: {gpu_name} ({total_memory:.1f} GB)")
|
305 |
+
|
306 |
+
return "\n".join(gpu_info)
|
307 |
+
|
308 |
+
# Gradio ์ธํฐํ์ด์ค
|
309 |
with gr.Blocks(fill_height=True, title="ThinkFlow - Step-by-step Reasoning Service") as demo:
|
310 |
# ์๋จ์ ํ์ดํ๊ณผ ์ค๋ช
์ถ๊ฐ
|
311 |
gr.Markdown("""
|
|
|
320 |
scale=1,
|
321 |
type="messages",
|
322 |
latex_delimiters=latex_delimiters,
|
323 |
+
height=600,
|
324 |
)
|
325 |
msg = gr.Textbox(
|
326 |
submit_btn=True,
|
|
|
331 |
)
|
332 |
|
333 |
with gr.Column(scale=1):
|
334 |
+
# ํ๋์จ์ด ์ ๋ณด ํ์
|
335 |
+
gpu_info = gr.Markdown(f"**์ฌ์ฉ ๊ฐ๋ฅํ ํ๋์จ์ด:**\n{get_gpu_info()}")
|
336 |
+
|
337 |
# ๋ชจ๋ธ ์ ํ ์น์
์ถ๊ฐ
|
338 |
gr.Markdown("""## ๋ชจ๋ธ ์ ํ""")
|
339 |
+
model_selector = gr.Radio(
|
340 |
choices=list(available_models.values()),
|
341 |
+
value=available_models["meta-llama/Llama-3.2-3B-Instruct"], # ์์ ๋ชจ๋ธ์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก
|
342 |
+
label="์ฌ์ฉํ LLM ๋ชจ๋ธ ์ ํ",
|
343 |
)
|
344 |
|
345 |
# ๋ชจ๋ธ ๋ก๋ ๋ฒํผ
|
346 |
+
load_model_btn = gr.Button("๋ชจ๋ธ ๋ก๋", variant="primary")
|
347 |
model_status = gr.Textbox(label="๋ชจ๋ธ ์ํ", interactive=False)
|
348 |
|
349 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ๋ฒํผ
|
350 |
+
clear_memory_btn = gr.Button("GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ", variant="secondary")
|
351 |
+
|
352 |
gr.Markdown("""## ๋งค๊ฐ๋ณ์ ์กฐ์ """)
|
353 |
+
with gr.Accordion("๊ณ ๊ธ ์ค์ ", open=False):
|
354 |
+
num_tokens = gr.Slider(
|
355 |
+
50,
|
356 |
+
2000,
|
357 |
+
1000, # ๊ธฐ๋ณธ๊ฐ ์ถ์
|
358 |
+
step=50,
|
359 |
+
label="์ถ๋ก ๋จ๊ณ๋น ์ต๋ ํ ํฐ ์",
|
360 |
+
interactive=True,
|
361 |
+
)
|
362 |
+
final_num_tokens = gr.Slider(
|
363 |
+
50,
|
364 |
+
3000,
|
365 |
+
1500, # ๊ธฐ๋ณธ๊ฐ ์ถ์
|
366 |
+
step=50,
|
367 |
+
label="์ต์ข
๋ต๋ณ์ ์ต๋ ํ ํฐ ์",
|
368 |
+
interactive=True,
|
369 |
+
)
|
370 |
+
do_sample = gr.Checkbox(True, label="์ํ๋ง ์ฌ์ฉ")
|
371 |
+
temperature = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="์จ๋")
|
372 |
|
373 |
# ์ ํ๋ ๋ชจ๋ธ ๋ก๋ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
374 |
+
def get_model_names(selected_model):
|
375 |
# ํ์ ์ด๋ฆ์์ ์๋ ๋ชจ๋ธ ์ด๋ฆ์ผ๋ก ๋ณํ
|
376 |
inverse_map = {v: k for k, v in available_models.items()}
|
377 |
+
return [inverse_map[selected_model]] if selected_model else []
|
378 |
|
379 |
load_model_btn.click(
|
380 |
lambda selected: load_model(get_model_names(selected)),
|
381 |
inputs=[model_selector],
|
382 |
outputs=[model_status]
|
383 |
)
|
384 |
+
|
385 |
+
# GPU ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
386 |
+
clear_memory_btn.click(
|
387 |
+
lambda: (clear_gpu_memory(), "GPU ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ ๋ฆฌ๋์์ต๋๋ค."),
|
388 |
+
inputs=[],
|
389 |
+
outputs=[model_status]
|
390 |
+
)
|
391 |
|
392 |
# ์ฌ์ฉ์๊ฐ ๋ฉ์์ง๋ฅผ ์ ์ถํ๋ฉด ๋ด์ด ์๋ตํฉ๋๋ค
|
393 |
msg.submit(
|
|
|
407 |
)
|
408 |
|
409 |
if __name__ == "__main__":
|
410 |
+
# ๋๋ฒ๊น
์ ๋ณด ์ถ๋ ฅ
|
411 |
+
print(f"GPU ์ฌ์ฉ ๊ฐ๋ฅ: {torch.cuda.is_available()}")
|
412 |
+
if torch.cuda.is_available():
|
413 |
+
print(f"์ฌ์ฉ ๊ฐ๋ฅํ GPU ๊ฐ์: {torch.cuda.device_count()}")
|
414 |
+
print(f"ํ์ฌ GPU: {torch.cuda.current_device()}")
|
415 |
+
print(f"GPU ์ด๋ฆ: {torch.cuda.get_device_name(0)}")
|
416 |
+
|
417 |
+
# ํ ์ฌ์ฉ ๋ฐ ์ฑ ์คํ
|
418 |
+
demo.queue(max_size=10).launch()
|