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
|
7 |
+
from transformers import pipeline
|
8 |
+
|
9 |
+
# ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ก๋ฉ
|
10 |
+
model_name = "Qwen/Qwen2-1.5B-Instruct"
|
11 |
+
if gr.NO_RELOAD:
|
12 |
+
pipe = pipeline(
|
13 |
+
"text-generation",
|
14 |
+
model=model_name,
|
15 |
+
device_map="auto",
|
16 |
+
torch_dtype="auto",
|
17 |
+
)
|
18 |
+
|
19 |
+
# ์ต์ข
๋ต๋ณ์ ๊ฐ์งํ๊ธฐ ์ํ ๋ง์ปค
|
20 |
+
ANSWER_MARKER = "**๋ต๋ณ**"
|
21 |
+
|
22 |
+
# ๋จ๊ณ๋ณ ์ถ๋ก ์ ์์ํ๋ ๋ฌธ์ฅ๋ค
|
23 |
+
rethink_prepends = [
|
24 |
+
"์, ์ด์ ๋ค์์ ํ์
ํด์ผ ํฉ๋๋ค ",
|
25 |
+
"์ ์๊ฐ์๋ ",
|
26 |
+
"์ ์๋ง์, ์ ์๊ฐ์๋ ",
|
27 |
+
"๋ค์ ์ฌํญ์ด ๋ง๋์ง ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค ",
|
28 |
+
"๋ํ ๊ธฐ์ตํด์ผ ํ ๊ฒ์ ",
|
29 |
+
"๋ ๋ค๋ฅธ ์ฃผ๋ชฉํ ์ ์ ",
|
30 |
+
"๊ทธ๋ฆฌ๊ณ ์ ๋ ๋ค์๊ณผ ๊ฐ์ ์ฌ์ค๋ ๊ธฐ์ตํฉ๋๋ค ",
|
31 |
+
"์ด์ ์ถฉ๋ถํ ์ดํดํ๋ค๊ณ ์๊ฐํฉ๋๋ค ",
|
32 |
+
"์ง๊ธ๊น์ง์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก, ์๋ ์ง๋ฌธ์ ์ฌ์ฉ๋ ์ธ์ด๋ก ๋ต๋ณํ๊ฒ ์ต๋๋ค:"
|
33 |
+
"\n{question}\n"
|
34 |
+
f"\n{ANSWER_MARKER}\n",
|
35 |
+
]
|
36 |
+
|
37 |
+
|
38 |
+
# ์์ ํ์ ๋ฌธ์ ํด๊ฒฐ์ ์ํ ์ค์
|
39 |
+
latex_delimiters = [
|
40 |
+
{"left": "$$", "right": "$$", "display": True},
|
41 |
+
{"left": "$", "right": "$", "display": False},
|
42 |
+
]
|
43 |
+
|
44 |
+
|
45 |
+
def reformat_math(text):
|
46 |
+
"""Gradio ๊ตฌ๋ฌธ(Katex)์ ์ฌ์ฉํ๋๋ก MathJax ๊ตฌ๋ถ ๊ธฐํธ ์์ .
|
47 |
+
์ด๊ฒ์ Gradio์์ ์ํ ๊ณต์์ ํ์ํ๊ธฐ ์ํ ์์ ํด๊ฒฐ์ฑ
์
๋๋ค. ํ์ฌ๋ก์๋
|
48 |
+
๋ค๋ฅธ latex_delimiters๋ฅผ ์ฌ์ฉํ์ฌ ์์๋๋ก ์๋ํ๊ฒ ํ๋ ๋ฐฉ๋ฒ์ ์ฐพ์ง ๋ชปํ์ต๋๋ค...
|
49 |
+
"""
|
50 |
+
text = re.sub(r"\\\[\s*(.*?)\s*\\\]", r"$$\1$$", text, flags=re.DOTALL)
|
51 |
+
text = re.sub(r"\\\(\s*(.*?)\s*\\\)", r"$\1$", text, flags=re.DOTALL)
|
52 |
+
return text
|
53 |
+
|
54 |
+
|
55 |
+
def user_input(message, history: list):
|
56 |
+
"""์ฌ์ฉ์ ์
๋ ฅ์ ํ์คํ ๋ฆฌ์ ์ถ๊ฐํ๊ณ ์
๋ ฅ ํ
์คํธ ์์ ๋น์ฐ๊ธฐ"""
|
57 |
+
return "", history + [
|
58 |
+
gr.ChatMessage(role="user", content=message.replace(ANSWER_MARKER, ""))
|
59 |
+
]
|
60 |
+
|
61 |
+
|
62 |
+
def rebuild_messages(history: list):
|
63 |
+
"""์ค๊ฐ ์๊ฐ ๊ณผ์ ์์ด ๋ชจ๋ธ์ด ์ฌ์ฉํ ํ์คํ ๋ฆฌ์์ ๋ฉ์์ง ์ฌ๊ตฌ์ฑ"""
|
64 |
+
messages = []
|
65 |
+
for h in history:
|
66 |
+
if isinstance(h, dict) and not h.get("metadata", {}).get("title", False):
|
67 |
+
messages.append(h)
|
68 |
+
elif (
|
69 |
+
isinstance(h, gr.ChatMessage)
|
70 |
+
and h.metadata.get("title")
|
71 |
+
and isinstance(h.content, str)
|
72 |
+
):
|
73 |
+
messages.append({"role": h.role, "content": h.content})
|
74 |
+
return messages
|
75 |
+
|
76 |
+
|
77 |
+
@spaces.GPU
|
78 |
+
def bot(
|
79 |
+
history: list,
|
80 |
+
max_num_tokens: int,
|
81 |
+
final_num_tokens: int,
|
82 |
+
do_sample: bool,
|
83 |
+
temperature: float,
|
84 |
+
):
|
85 |
+
"""๋ชจ๋ธ์ด ์ง๋ฌธ์ ๋ต๋ณํ๋๋ก ํ๊ธฐ"""
|
86 |
+
|
87 |
+
# ๋์ค์ ์ค๋ ๋์์ ํ ํฐ์ ์คํธ๋ฆผ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ ์ํจ
|
88 |
+
streamer = transformers.TextIteratorStreamer(
|
89 |
+
pipe.tokenizer, # pyright: ignore
|
90 |
+
skip_special_tokens=True,
|
91 |
+
skip_prompt=True,
|
92 |
+
)
|
93 |
+
|
94 |
+
# ํ์ํ ๊ฒฝ์ฐ ์ถ๋ก ์ ์ง๋ฌธ์ ๋ค์ ์ฝ์
ํ๊ธฐ ์ํจ
|
95 |
+
question = history[-1]["content"]
|
96 |
+
|
97 |
+
# ๋ณด์กฐ์ ๋ฉ์์ง ์ค๋น
|
98 |
+
history.append(
|
99 |
+
gr.ChatMessage(
|
100 |
+
role="assistant",
|
101 |
+
content=str(""),
|
102 |
+
metadata={"title": "๐ง ์๊ฐ ์ค...", "status": "pending"},
|
103 |
+
)
|
104 |
+
)
|
105 |
+
|
106 |
+
# ํ์ฌ ์ฑํ
์ ํ์๋ ์ถ๋ก ๊ณผ์
|
107 |
+
messages = rebuild_messages(history)
|
108 |
+
for i, prepend in enumerate(rethink_prepends):
|
109 |
+
if i > 0:
|
110 |
+
messages[-1]["content"] += "\n\n"
|
111 |
+
messages[-1]["content"] += prepend.format(question=question)
|
112 |
+
|
113 |
+
num_tokens = int(
|
114 |
+
max_num_tokens if ANSWER_MARKER not in prepend else final_num_tokens
|
115 |
+
)
|
116 |
+
t = threading.Thread(
|
117 |
+
target=pipe,
|
118 |
+
args=(messages,),
|
119 |
+
kwargs=dict(
|
120 |
+
max_new_tokens=num_tokens,
|
121 |
+
streamer=streamer,
|
122 |
+
do_sample=do_sample,
|
123 |
+
temperature=temperature,
|
124 |
+
),
|
125 |
+
)
|
126 |
+
t.start()
|
127 |
+
|
128 |
+
# ์ ๋ด์ฉ์ผ๋ก ํ์คํ ๋ฆฌ ์ฌ๊ตฌ์ฑ
|
129 |
+
history[-1].content += prepend.format(question=question)
|
130 |
+
if ANSWER_MARKER in prepend:
|
131 |
+
history[-1].metadata = {"title": "๐ญ ์ฌ๊ณ ๊ณผ์ ", "status": "done"}
|
132 |
+
# ์๊ฐ ์ข
๋ฃ, ์ด์ ๋ต๋ณ์
๋๋ค (์ค๊ฐ ๋จ๊ณ์ ๋ํ ๋ฉํ๋ฐ์ดํฐ ์์)
|
133 |
+
history.append(gr.ChatMessage(role="assistant", content=""))
|
134 |
+
for token in streamer:
|
135 |
+
history[-1].content += token
|
136 |
+
history[-1].content = reformat_math(history[-1].content)
|
137 |
+
yield history
|
138 |
+
t.join()
|
139 |
+
|
140 |
+
yield history
|
141 |
+
|
142 |
+
|
143 |
+
with gr.Blocks(fill_height=True, title="๋ชจ๋ LLM ๋ชจ๋ธ์ ์ถ๋ก ๋ฅ๋ ฅ ๋ถ์ฌํ๊ธฐ") as demo:
|
144 |
+
with gr.Row(scale=1):
|
145 |
+
with gr.Column(scale=5):
|
146 |
+
gr.Markdown(f"""
|
147 |
+
# ๋ชจ๋ LLM์ ์ถ๋ก ๋ฅ๋ ฅ ๊ฐ์ ํ๊ธฐ
|
148 |
+
|
149 |
+
์ด๊ฒ์ ๋ชจ๋ LLM(๋๊ท๋ชจ ์ธ์ด ๋ชจ๋ธ)์ด ์๋ต ์ ์ ์ถ๋ก ํ ์ ์๋๋ก ํ๋ ๊ฐ๋จํ ๊ฐ๋
์ฆ๋ช
์
๋๋ค.
|
150 |
+
์ด ์ธํฐํ์ด์ค๋ *{model_name}* ๋ชจ๋ธ์ ์ฌ์ฉํ๋๋ฐ, **์ด๋ ์ถ๋ก ๋ชจ๋ธ์ด ์๋๋๋ค**. ์ฌ์ฉ๋ ๋ฐฉ๋ฒ์
|
151 |
+
๋จ์ง ์ ๋์ฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ด ๋ต๋ณ์ ํฅ์์ํค๋ ๋ฐ ๋์์ด ๋๋ "์ถ๋ก " ๋จ๊ณ๋ฅผ ๊ฐ์ ํ๋ ๊ฒ์
๋๋ค.
|
152 |
+
๊ด๋ จ ๊ธฐ์ฌ๋ ๋ค์์์ ํ์ธํ์ธ์: [๋ชจ๋ ๋ชจ๋ธ์ ์ถ๋ก ๋ฅ๋ ฅ ๋ถ์ฌํ๊ธฐ](https://huggingface.co/blog/Metal3d/making-any-model-reasoning)
|
153 |
+
""")
|
154 |
+
chatbot = gr.Chatbot(
|
155 |
+
scale=1,
|
156 |
+
type="messages",
|
157 |
+
latex_delimiters=latex_delimiters,
|
158 |
+
)
|
159 |
+
msg = gr.Textbox(
|
160 |
+
submit_btn=True,
|
161 |
+
label="",
|
162 |
+
show_label=False,
|
163 |
+
placeholder="์ฌ๊ธฐ์ ์ง๋ฌธ์ ์
๋ ฅํ์ธ์.",
|
164 |
+
autofocus=True,
|
165 |
+
)
|
166 |
+
with gr.Column(scale=1):
|
167 |
+
gr.Markdown("""## ๋งค๊ฐ๋ณ์ ์กฐ์ """)
|
168 |
+
num_tokens = gr.Slider(
|
169 |
+
50,
|
170 |
+
1024,
|
171 |
+
100,
|
172 |
+
step=1,
|
173 |
+
label="์ถ๋ก ๋จ๊ณ๋น ์ต๋ ํ ํฐ ์",
|
174 |
+
interactive=True,
|
175 |
+
)
|
176 |
+
final_num_tokens = gr.Slider(
|
177 |
+
50,
|
178 |
+
1024,
|
179 |
+
512,
|
180 |
+
step=1,
|
181 |
+
label="์ต์ข
๋ต๋ณ์ ์ต๋ ํ ํฐ ์",
|
182 |
+
interactive=True,
|
183 |
+
)
|
184 |
+
do_sample = gr.Checkbox(True, label="์ํ๋ง ์ฌ์ฉ")
|
185 |
+
temperature = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="์จ๋")
|
186 |
+
gr.Markdown("""
|
187 |
+
์ถ๋ก ๋จ๊ณ์์ ๋ ์ ์ ์์ ํ ํฐ์ ์ฌ์ฉํ๋ฉด ๋ชจ๋ธ์ด
|
188 |
+
๋ ๋นจ๋ฆฌ ๋ต๋ณํ ์ ์์ง๋ง, ์ถฉ๋ถํ ๊น๊ฒ ์ถ๋ก ํ์ง ๋ชปํ ์ ์์ต๋๋ค.
|
189 |
+
์ ์ ํ ๊ฐ์ 100์์ 512์
๋๋ค.
|
190 |
+
์ต์ข
๋ต๋ณ์ ๋ ์ ์ ์์ ํ ํฐ์ ์ฌ์ฉํ๋ฉด ๋ชจ๋ธ์
|
191 |
+
์๋ต์ด ๋ ์ฅํฉํด์ง์ง๋ง, ์์ ํ ๋ต๋ณ์ ์ ๊ณตํ์ง ๋ชปํ ์ ์์ต๋๋ค.
|
192 |
+
์ ์ ํ ๊ฐ์ 512์์ 1024์
๋๋ค.
|
193 |
+
**์ํ๋ง ์ฌ์ฉ**์ ๋ต๋ณ์ ์์ฑํ๊ธฐ ์ํด ๋ค์ ํ ํฐ์ ์ ํํ๋ ๋ค๋ฅธ ์ ๋ต์ ์ฌ์ฉํฉ๋๋ค.
|
194 |
+
์ผ๋ฐ์ ์ผ๋ก ์ด ์ต์
์ ์ฒดํฌํด ๋๋ ๊ฒ์ด ์ข์ต๋๋ค.
|
195 |
+
**์จ๋**๋ ๋ชจ๋ธ์ด ์ผ๋ง๋ "์ฐฝ์์ "์ผ ์ ์๋์ง๋ฅผ ๋ํ๋
๋๋ค. 0.7์ด ์ผ๋ฐ์ ์ธ ๊ฐ์
๋๋ค.
|
196 |
+
๋๋ฌด ๋์ ๊ฐ(์: 1.0)์ ์ค์ ํ๋ฉด ๋ชจ๋ธ์ด ์ผ๊ด์ฑ์ด ์์ ์ ์์ต๋๋ค. ๋ฎ์ ๊ฐ(์: 0.3)์ผ๋ก
|
197 |
+
์ค์ ํ๋ฉด ๋ชจ๋ธ์ ๋งค์ฐ ์์ธก ๊ฐ๋ฅํ ๋ต๋ณ์ ์์ฑํฉ๋๋ค.
|
198 |
+
""")
|
199 |
+
gr.Markdown("""
|
200 |
+
์ด ์ธํฐํ์ด์ค๋ 6GB VRAM์ ๊ฐ์ง ๊ฐ์ธ ์ปดํจํฐ์์ ์๋ํ ์ ์์ต๋๋ค(์: ๋
ธํธ๋ถ์ NVidia 3050/3060).
|
201 |
+
์์ ๋กญ๊ฒ ์ ํ๋ฆฌ์ผ์ด์
์ ํฌํฌํ์ฌ ๋ค๋ฅธ instruct ๋ชจ๋ธ์ ์๋ํด ๋ณด์ธ์.
|
202 |
+
""")
|
203 |
+
|
204 |
+
# ์ฌ์ฉ์๊ฐ ๋ฉ์์ง๋ฅผ ์ ์ถํ๋ฉด ๋ด์ด ์๋ตํฉ๋๋ค
|
205 |
+
msg.submit(
|
206 |
+
user_input,
|
207 |
+
[msg, chatbot], # ์
๋ ฅ
|
208 |
+
[msg, chatbot], # ์ถ๋ ฅ
|
209 |
+
).then(
|
210 |
+
bot,
|
211 |
+
[
|
212 |
+
chatbot,
|
213 |
+
num_tokens,
|
214 |
+
final_num_tokens,
|
215 |
+
do_sample,
|
216 |
+
temperature,
|
217 |
+
], # ์ค์ ๋ก๋ "history" ์
๋ ฅ
|
218 |
+
chatbot, # ์ถ๋ ฅ์์ ์ ํ์คํ ๋ฆฌ ์ ์ฅ
|
219 |
+
)
|
220 |
+
|
221 |
+
if __name__ == "__main__":
|
222 |
+
demo.queue().launch()
|