openfree commited on
Commit
46ef1e4
ยท
verified ยท
1 Parent(s): 2fcd8c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -22
app.py CHANGED
@@ -52,9 +52,11 @@ def reformat_math(text):
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
 
@@ -75,14 +77,63 @@ def rebuild_messages(history: list):
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(
@@ -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=5):
146
-
147
- chatbot = gr.Chatbot(
148
  scale=1,
149
  type="messages",
150
  latex_delimiters=latex_delimiters,
 
151
  )
152
- msg = gr.Textbox(
153
- submit_btn=True,
154
- label="",
155
- show_label=False,
156
- placeholder="์—ฌ๊ธฐ์— ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”.",
157
- autofocus=True,
 
 
158
  )
159
- with gr.Column(scale=1):
 
 
 
 
 
 
 
 
 
 
 
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, chatbot], # ์ž…๋ ฅ
185
- [msg, chatbot], # ์ถœ๋ ฅ
 
 
 
 
 
 
 
 
 
186
  ).then(
187
- bot,
188
  [
189
- chatbot,
190
  num_tokens,
191
  final_num_tokens,
192
  do_sample,
193
  temperature,
194
- ], # ์‹ค์ œ๋กœ๋Š” "history" ์ž…๋ ฅ
195
- chatbot, # ์ถœ๋ ฅ์—์„œ ์ƒˆ ํžˆ์Šคํ† ๋ฆฌ ์ €์žฅ
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__":