Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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=
|
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 |
|