Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -52,9 +52,11 @@ def reformat_math(text):
|
|
52 |
return text
|
53 |
|
54 |
|
55 |
-
def user_input(message,
|
56 |
"""์ฌ์ฉ์ ์
๋ ฅ์ ํ์คํ ๋ฆฌ์ ์ถ๊ฐํ๊ณ ์
๋ ฅ ํ
์คํธ ์์ ๋น์ฐ๊ธฐ"""
|
57 |
-
return "",
|
|
|
|
|
58 |
gr.ChatMessage(role="user", content=message.replace(ANSWER_MARKER, ""))
|
59 |
]
|
60 |
|
@@ -75,14 +77,63 @@ def rebuild_messages(history: list):
|
|
75 |
|
76 |
|
77 |
@spaces.GPU
|
78 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
@@ -142,21 +193,35 @@ def bot(
|
|
142 |
|
143 |
with gr.Blocks(fill_height=True, title="๋ชจ๋ LLM ๋ชจ๋ธ์ ์ถ๋ก ๋ฅ๋ ฅ ๋ถ์ฌํ๊ธฐ") as demo:
|
144 |
with gr.Row(scale=1):
|
145 |
-
with gr.Column(scale=
|
146 |
-
|
147 |
-
|
148 |
scale=1,
|
149 |
type="messages",
|
150 |
latex_delimiters=latex_delimiters,
|
|
|
151 |
)
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
158 |
)
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
gr.Markdown("""## ๋งค๊ฐ๋ณ์ ์กฐ์ """)
|
161 |
num_tokens = gr.Slider(
|
162 |
50,
|
@@ -177,22 +242,30 @@ with gr.Blocks(fill_height=True, title="๋ชจ๋ LLM ๋ชจ๋ธ์ ์ถ๋ก ๋ฅ๋ ฅ ๋ถ
|
|
177 |
do_sample = gr.Checkbox(True, label="์ํ๋ง ์ฌ์ฉ")
|
178 |
temperature = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="์จ๋")
|
179 |
|
180 |
-
|
181 |
-
# ์ฌ์ฉ์๊ฐ ๋ฉ์์ง๋ฅผ ์ ์ถํ๋ฉด ๋ด์ด ์๋ตํฉ๋๋ค
|
182 |
msg.submit(
|
183 |
user_input,
|
184 |
-
[msg,
|
185 |
-
[msg,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
).then(
|
187 |
-
|
188 |
[
|
189 |
-
|
190 |
num_tokens,
|
191 |
final_num_tokens,
|
192 |
do_sample,
|
193 |
temperature,
|
194 |
-
],
|
195 |
-
|
196 |
)
|
197 |
|
198 |
if __name__ == "__main__":
|
|
|
52 |
return text
|
53 |
|
54 |
|
55 |
+
def user_input(message, history_original, history_thinking):
|
56 |
"""์ฌ์ฉ์ ์
๋ ฅ์ ํ์คํ ๋ฆฌ์ ์ถ๊ฐํ๊ณ ์
๋ ฅ ํ
์คํธ ์์ ๋น์ฐ๊ธฐ"""
|
57 |
+
return "", history_original + [
|
58 |
+
gr.ChatMessage(role="user", content=message.replace(ANSWER_MARKER, ""))
|
59 |
+
], history_thinking + [
|
60 |
gr.ChatMessage(role="user", content=message.replace(ANSWER_MARKER, ""))
|
61 |
]
|
62 |
|
|
|
77 |
|
78 |
|
79 |
@spaces.GPU
|
80 |
+
def bot_original(
|
81 |
+
history: list,
|
82 |
+
max_num_tokens: int,
|
83 |
+
do_sample: bool,
|
84 |
+
temperature: float,
|
85 |
+
):
|
86 |
+
"""์๋ณธ ๋ชจ๋ธ์ด ์ง๋ฌธ์ ๋ต๋ณํ๋๋ก ํ๊ธฐ (์ถ๋ก ๊ณผ์ ์์ด)"""
|
87 |
+
|
88 |
+
# ๋์ค์ ์ค๋ ๋์์ ํ ํฐ์ ์คํธ๋ฆผ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ ์ํจ
|
89 |
+
streamer = transformers.TextIteratorStreamer(
|
90 |
+
pipe.tokenizer, # pyright: ignore
|
91 |
+
skip_special_tokens=True,
|
92 |
+
skip_prompt=True,
|
93 |
+
)
|
94 |
+
|
95 |
+
# ๋ณด์กฐ์ ๋ฉ์์ง ์ค๋น
|
96 |
+
history.append(
|
97 |
+
gr.ChatMessage(
|
98 |
+
role="assistant",
|
99 |
+
content=str(""),
|
100 |
+
)
|
101 |
+
)
|
102 |
+
|
103 |
+
# ํ์ฌ ์ฑํ
์ ํ์๋ ๋ฉ์์ง
|
104 |
+
messages = rebuild_messages(history[:-1]) # ๋ง์ง๋ง ๋น ๋ฉ์์ง ์ ์ธ
|
105 |
+
|
106 |
+
# ์๋ณธ ๋ชจ๋ธ์ ์ถ๋ก ์์ด ๋ฐ๋ก ๋ต๋ณ
|
107 |
+
t = threading.Thread(
|
108 |
+
target=pipe,
|
109 |
+
args=(messages,),
|
110 |
+
kwargs=dict(
|
111 |
+
max_new_tokens=max_num_tokens,
|
112 |
+
streamer=streamer,
|
113 |
+
do_sample=do_sample,
|
114 |
+
temperature=temperature,
|
115 |
+
),
|
116 |
+
)
|
117 |
+
t.start()
|
118 |
+
|
119 |
+
for token in streamer:
|
120 |
+
history[-1].content += token
|
121 |
+
history[-1].content = reformat_math(history[-1].content)
|
122 |
+
yield history
|
123 |
+
t.join()
|
124 |
+
|
125 |
+
yield history
|
126 |
+
|
127 |
+
|
128 |
+
@spaces.GPU
|
129 |
+
def bot_thinking(
|
130 |
history: list,
|
131 |
max_num_tokens: int,
|
132 |
final_num_tokens: int,
|
133 |
do_sample: bool,
|
134 |
temperature: float,
|
135 |
):
|
136 |
+
"""์ถ๋ก ๊ณผ์ ์ ํฌํจํ์ฌ ๋ชจ๋ธ์ด ์ง๋ฌธ์ ๋ต๋ณํ๋๋ก ํ๊ธฐ"""
|
137 |
|
138 |
# ๋์ค์ ์ค๋ ๋์์ ํ ํฐ์ ์คํธ๋ฆผ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ ์ํจ
|
139 |
streamer = transformers.TextIteratorStreamer(
|
|
|
193 |
|
194 |
with gr.Blocks(fill_height=True, title="๋ชจ๋ LLM ๋ชจ๋ธ์ ์ถ๋ก ๋ฅ๋ ฅ ๋ถ์ฌํ๊ธฐ") as demo:
|
195 |
with gr.Row(scale=1):
|
196 |
+
with gr.Column(scale=2):
|
197 |
+
gr.Markdown("## Before (Original)")
|
198 |
+
chatbot_original = gr.Chatbot(
|
199 |
scale=1,
|
200 |
type="messages",
|
201 |
latex_delimiters=latex_delimiters,
|
202 |
+
label="Original Model (No Reasoning)"
|
203 |
)
|
204 |
+
|
205 |
+
with gr.Column(scale=2):
|
206 |
+
gr.Markdown("## After (Thinking)")
|
207 |
+
chatbot_thinking = gr.Chatbot(
|
208 |
+
scale=1,
|
209 |
+
type="messages",
|
210 |
+
latex_delimiters=latex_delimiters,
|
211 |
+
label="Model with Reasoning"
|
212 |
)
|
213 |
+
|
214 |
+
with gr.Row():
|
215 |
+
msg = gr.Textbox(
|
216 |
+
submit_btn=True,
|
217 |
+
label="",
|
218 |
+
show_label=False,
|
219 |
+
placeholder="์ฌ๊ธฐ์ ์ง๋ฌธ์ ์
๋ ฅํ์ธ์.",
|
220 |
+
autofocus=True,
|
221 |
+
)
|
222 |
+
|
223 |
+
with gr.Row():
|
224 |
+
with gr.Column():
|
225 |
gr.Markdown("""## ๋งค๊ฐ๋ณ์ ์กฐ์ """)
|
226 |
num_tokens = gr.Slider(
|
227 |
50,
|
|
|
242 |
do_sample = gr.Checkbox(True, label="์ํ๋ง ์ฌ์ฉ")
|
243 |
temperature = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="์จ๋")
|
244 |
|
245 |
+
# ์ฌ์ฉ์๊ฐ ๋ฉ์์ง๋ฅผ ์ ์ถํ๋ฉด ๋ ๋ด์ด ๋์์ ์๋ตํฉ๋๋ค
|
|
|
246 |
msg.submit(
|
247 |
user_input,
|
248 |
+
[msg, chatbot_original, chatbot_thinking], # ์
๋ ฅ
|
249 |
+
[msg, chatbot_original, chatbot_thinking], # ์ถ๋ ฅ
|
250 |
+
).then(
|
251 |
+
bot_original,
|
252 |
+
[
|
253 |
+
chatbot_original,
|
254 |
+
num_tokens,
|
255 |
+
do_sample,
|
256 |
+
temperature,
|
257 |
+
],
|
258 |
+
chatbot_original, # ์ถ๋ ฅ์์ ์ ํ์คํ ๋ฆฌ ์ ์ฅ
|
259 |
).then(
|
260 |
+
bot_thinking,
|
261 |
[
|
262 |
+
chatbot_thinking,
|
263 |
num_tokens,
|
264 |
final_num_tokens,
|
265 |
do_sample,
|
266 |
temperature,
|
267 |
+
],
|
268 |
+
chatbot_thinking, # ์ถ๋ ฅ์์ ์ ํ์คํ ๋ฆฌ ์ ์ฅ
|
269 |
)
|
270 |
|
271 |
if __name__ == "__main__":
|