openfree commited on
Commit
9e7af9a
ยท
verified ยท
1 Parent(s): 47cd8f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -13
app.py CHANGED
@@ -29,11 +29,19 @@ rethink_prepends = [
29
  "๋˜ ๋‹ค๋ฅธ ์ฃผ๋ชฉํ•  ์ ์€ ",
30
  "๊ทธ๋ฆฌ๊ณ  ์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‚ฌ์‹ค๋„ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค ",
31
  "์ด์ œ ์ถฉ๋ถ„ํžˆ ์ดํ•ดํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค ",
32
- "์ง€๊ธˆ๊นŒ์ง€์˜ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ, ์›๋ž˜ ์งˆ๋ฌธ์— ์‚ฌ์šฉ๋œ ์–ธ์–ด๋กœ ๋‹ต๋ณ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค:"
33
- "\n{question}\n"
34
- f"\n{ANSWER_MARKER}\n",
35
  ]
36
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  # ์ˆ˜์‹ ํ‘œ์‹œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์„ค์ •
39
  latex_delimiters = [
@@ -69,7 +77,7 @@ def rebuild_messages(history: list):
69
  messages.append(h)
70
  elif (
71
  isinstance(h, gr.ChatMessage)
72
- and h.metadata.get("title")
73
  and isinstance(h.content, str)
74
  ):
75
  messages.append({"role": h.role, "content": h.content})
@@ -156,19 +164,21 @@ def bot_thinking(
156
 
157
  # ํ˜„์žฌ ์ฑ„ํŒ…์— ํ‘œ์‹œ๋  ์ถ”๋ก  ๊ณผ์ •
158
  messages = rebuild_messages(history)
 
 
 
 
 
159
  for i, prepend in enumerate(rethink_prepends):
160
  if i > 0:
161
  messages[-1]["content"] += "\n\n"
162
  messages[-1]["content"] += prepend.format(question=question)
163
 
164
- num_tokens = int(
165
- max_num_tokens if ANSWER_MARKER not in prepend else final_num_tokens
166
- )
167
  t = threading.Thread(
168
  target=pipe,
169
  args=(messages,),
170
  kwargs=dict(
171
- max_new_tokens=num_tokens,
172
  streamer=streamer,
173
  do_sample=do_sample,
174
  temperature=temperature,
@@ -178,21 +188,59 @@ def bot_thinking(
178
 
179
  # ์ƒˆ ๋‚ด์šฉ์œผ๋กœ ํžˆ์Šคํ† ๋ฆฌ ์žฌ๊ตฌ์„ฑ
180
  history[-1].content += prepend.format(question=question)
181
- if ANSWER_MARKER in prepend:
182
- history[-1].metadata = {"title": "๐Ÿ’ญ ์‚ฌ๊ณ  ๊ณผ์ •", "status": "done"}
183
- # ์ƒ๊ฐ ์ข…๋ฃŒ, ์ด์ œ ๋‹ต๋ณ€์ž…๋‹ˆ๋‹ค (์ค‘๊ฐ„ ๋‹จ๊ณ„์— ๋Œ€ํ•œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์—†์Œ)
184
- history.append(gr.ChatMessage(role="assistant", content=""))
185
  for token in streamer:
186
  history[-1].content += token
187
  history[-1].content = reformat_math(history[-1].content)
188
  yield history
189
  t.join()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
  yield history
192
 
193
 
194
  with gr.Blocks(fill_height=True, title="Vidraft ThinkFlow") as demo:
195
- # ์ œ๋ชฉ๊ณผ ์„ค๋ช…
196
  gr.Markdown("# Vidraft ThinkFlow")
197
  gr.Markdown("### ์ถ”๋ก  ๊ธฐ๋Šฅ์ด ์—†๋Š” LLM ๋ชจ๋ธ์˜ ์ˆ˜์ • ์—†์ด๋„ ์ถ”๋ก  ๊ธฐ๋Šฅ์„ ์ž๋™์œผ๋กœ ์ ์šฉํ•˜๋Š” LLM ์ถ”๋ก  ์ƒ์„ฑ ํ”Œ๋žซํผ")
198
 
 
29
  "๋˜ ๋‹ค๋ฅธ ์ฃผ๋ชฉํ•  ์ ์€ ",
30
  "๊ทธ๋ฆฌ๊ณ  ์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‚ฌ์‹ค๋„ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค ",
31
  "์ด์ œ ์ถฉ๋ถ„ํžˆ ์ดํ•ดํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค ",
 
 
 
32
  ]
33
 
34
+ # ์ตœ์ข… ๋‹ต๋ณ€ ์ƒ์„ฑ์„ ์œ„ํ•œ ํ”„๋กฌํ”„ํŠธ ์ถ”๊ฐ€
35
+ final_answer_prompt = """
36
+ ์ง€๊ธˆ๊นŒ์ง€์˜ ์ถ”๋ก  ๊ณผ์ •์„ ๋ฐ”ํƒ•์œผ๋กœ, ์›๋ž˜ ์งˆ๋ฌธ์— ์‚ฌ์šฉ๋œ ์–ธ์–ด๋กœ ๋‹ต๋ณ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค:
37
+ {question}
38
+
39
+ ์•„๋ž˜๋Š” ๋‚ด๊ฐ€ ์ถ”๋ก ํ•œ ๊ฒฐ๋ก ์ž…๋‹ˆ๋‹ค:
40
+ {reasoning_conclusion}
41
+
42
+ ์œ„ ์ถ”๋ก ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ตœ์ข… ๋‹ต๋ณ€:
43
+ {ANSWER_MARKER}
44
+ """
45
 
46
  # ์ˆ˜์‹ ํ‘œ์‹œ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ์„ค์ •
47
  latex_delimiters = [
 
77
  messages.append(h)
78
  elif (
79
  isinstance(h, gr.ChatMessage)
80
+ and h.metadata.get("title", None) is None
81
  and isinstance(h.content, str)
82
  ):
83
  messages.append({"role": h.role, "content": h.content})
 
164
 
165
  # ํ˜„์žฌ ์ฑ„ํŒ…์— ํ‘œ์‹œ๋  ์ถ”๋ก  ๊ณผ์ •
166
  messages = rebuild_messages(history)
167
+
168
+ # ์ „์ฒด ์ถ”๋ก  ๊ณผ์ •์„ ์ €์žฅํ•  ๋ณ€์ˆ˜
169
+ full_reasoning = ""
170
+
171
+ # ์ถ”๋ก  ๋‹จ๊ณ„ ์‹คํ–‰
172
  for i, prepend in enumerate(rethink_prepends):
173
  if i > 0:
174
  messages[-1]["content"] += "\n\n"
175
  messages[-1]["content"] += prepend.format(question=question)
176
 
 
 
 
177
  t = threading.Thread(
178
  target=pipe,
179
  args=(messages,),
180
  kwargs=dict(
181
+ max_new_tokens=max_num_tokens,
182
  streamer=streamer,
183
  do_sample=do_sample,
184
  temperature=temperature,
 
188
 
189
  # ์ƒˆ ๋‚ด์šฉ์œผ๋กœ ํžˆ์Šคํ† ๋ฆฌ ์žฌ๊ตฌ์„ฑ
190
  history[-1].content += prepend.format(question=question)
 
 
 
 
191
  for token in streamer:
192
  history[-1].content += token
193
  history[-1].content = reformat_math(history[-1].content)
194
  yield history
195
  t.join()
196
+
197
+ # ๊ฐ ์ถ”๋ก  ๋‹จ๊ณ„์˜ ๊ฒฐ๊ณผ๋ฅผ full_reasoning์— ์ €์žฅ
198
+ full_reasoning = history[-1].content
199
+
200
+ # ์ถ”๋ก  ์™„๋ฃŒ, ์ด์ œ ์ตœ์ข… ๋‹ต๋ณ€์„ ์ƒ์„ฑ
201
+ history[-1].metadata = {"title": "๐Ÿ’ญ ์‚ฌ๊ณ  ๊ณผ์ •", "status": "done"}
202
+
203
+ # ์ถ”๋ก  ๊ณผ์ •์—์„œ ๊ฒฐ๋ก  ๋ถ€๋ถ„์„ ์ถ”์ถœ (๋งˆ์ง€๋ง‰ 1-2 ๋ฌธ๋‹จ ์ •๋„)
204
+ reasoning_parts = full_reasoning.split("\n\n")
205
+ reasoning_conclusion = "\n\n".join(reasoning_parts[-2:]) if len(reasoning_parts) > 2 else full_reasoning
206
+
207
+ # ์ตœ์ข… ๋‹ต๋ณ€ ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
208
+ history.append(gr.ChatMessage(role="assistant", content=""))
209
+
210
+ # ์ตœ์ข… ๋‹ต๋ณ€์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€ ๊ตฌ์„ฑ
211
+ final_messages = rebuild_messages(history[:-1]) # ๋งˆ์ง€๋ง‰ ๋นˆ ๋ฉ”์‹œ์ง€ ์ œ์™ธ
212
+ final_prompt = final_answer_prompt.format(
213
+ question=question,
214
+ reasoning_conclusion=reasoning_conclusion,
215
+ ANSWER_MARKER=ANSWER_MARKER
216
+ )
217
+ final_messages[-1]["content"] += final_prompt
218
+
219
+ # ์ตœ์ข… ๋‹ต๋ณ€ ์ƒ์„ฑ
220
+ t = threading.Thread(
221
+ target=pipe,
222
+ args=(final_messages,),
223
+ kwargs=dict(
224
+ max_new_tokens=final_num_tokens,
225
+ streamer=streamer,
226
+ do_sample=do_sample,
227
+ temperature=temperature,
228
+ ),
229
+ )
230
+ t.start()
231
+
232
+ # ์ตœ์ข… ๋‹ต๋ณ€ ์ŠคํŠธ๋ฆฌ๋ฐ
233
+ for token in streamer:
234
+ history[-1].content += token
235
+ history[-1].content = reformat_math(history[-1].content)
236
+ yield history
237
+ t.join()
238
 
239
  yield history
240
 
241
 
242
  with gr.Blocks(fill_height=True, title="Vidraft ThinkFlow") as demo:
243
+ # ์ œ๋ชฉ๊ณผ ์„ค๋ช…
244
  gr.Markdown("# Vidraft ThinkFlow")
245
  gr.Markdown("### ์ถ”๋ก  ๊ธฐ๋Šฅ์ด ์—†๋Š” LLM ๋ชจ๋ธ์˜ ์ˆ˜์ • ์—†์ด๋„ ์ถ”๋ก  ๊ธฐ๋Šฅ์„ ์ž๋™์œผ๋กœ ์ ์šฉํ•˜๋Š” LLM ์ถ”๋ก  ์ƒ์„ฑ ํ”Œ๋žซํผ")
246