Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,45 +58,87 @@ def parse_think_blocks(text: str) -> tuple[str, str]:
|
|
| 58 |
m = re.search(r"<think>(.*?)</think>\s*", text, re.DOTALL)
|
| 59 |
return (m.group(1).strip(), text[m.end():].strip()) if m else ("", text)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def format_response(raw: str) -> str:
|
| 62 |
-
# 1. <think>...</think> ์๋ฃ
|
| 63 |
chain, answer = parse_think_blocks(raw)
|
| 64 |
if chain:
|
| 65 |
-
|
| 66 |
-
quoted = "\n".join(f"> {l}" for l in lines)
|
| 67 |
-
block = (
|
| 68 |
"<details>\n"
|
| 69 |
"<summary>๐ง Reasoning Chain โ click to expand</summary>\n\n"
|
| 70 |
-
f"{
|
| 71 |
"</details>\n\n"
|
|
|
|
| 72 |
)
|
| 73 |
-
|
| 74 |
-
# 2. <think> ์ด๋ฆผ but ๋ซํ์ง ์์ โ ์คํธ๋ฆฌ๋ฐ ์ค
|
| 75 |
if "<think>" in raw and "</think>" not in raw:
|
| 76 |
think_len = len(raw) - raw.index("<think>") - 7
|
| 77 |
return f"๐ง Reasoning... ({think_len} chars)"
|
| 78 |
-
# 3. NIPA ์๋ํฌ์ธํธ
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
"
|
| 86 |
-
"\n\n
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
think_part = raw[:idx].strip()
|
| 92 |
-
answer_part = raw[idx:].strip()
|
| 93 |
-
return (
|
| 94 |
-
f"<details>\n"
|
| 95 |
-
f"<summary>๐ง Reasoning Chain ({len(think_part)} chars)</summary>\n\n"
|
| 96 |
-
f"{think_part}\n\n"
|
| 97 |
-
f"</details>\n\n"
|
| 98 |
-
f"{answer_part}"
|
| 99 |
-
)
|
| 100 |
# ์์ง ๋ต๋ณ ์ ๋์ด โ ์บ๋ฆญํฐ ์๋ง
|
| 101 |
return f"๐ง Reasoning... ({len(raw)} chars)"
|
| 102 |
return raw
|
|
|
|
| 58 |
m = re.search(r"<think>(.*?)</think>\s*", text, re.DOTALL)
|
| 59 |
return (m.group(1).strip(), text[m.end():].strip()) if m else ("", text)
|
| 60 |
|
| 61 |
+
def _is_thinking_line(line: str) -> bool:
|
| 62 |
+
"""ํ ์ค์ด reasoning/thinking์ธ์ง ๊ฐ์ง"""
|
| 63 |
+
l = line.strip()
|
| 64 |
+
if not l:
|
| 65 |
+
return True # ๋น ์ค์ thinking ๋ธ๋ก์ ํฌํจ
|
| 66 |
+
# ์์ด reasoning ํจํด
|
| 67 |
+
think_starts = [
|
| 68 |
+
"The user", "the user", "This is", "this is", "I should", "I need to",
|
| 69 |
+
"Let me", "let me", "My task", "my task", "I'll ", "I will",
|
| 70 |
+
"Since ", "since ", "Now,", "now,", "So,", "so,", "First,", "first,",
|
| 71 |
+
"Okay", "okay", "Alright", "Hmm", "Wait", "Actually",
|
| 72 |
+
"The question", "the question", "The input", "the input",
|
| 73 |
+
"The request", "the request", "The prompt", "the prompt",
|
| 74 |
+
"Thinking Process", "Thinking process", "**Thinking",
|
| 75 |
+
"Step ", "step ", "Approach:", "Analysis:", "Reasoning:",
|
| 76 |
+
"1. **", "2. **", "3. **", "4. **", "5. **",
|
| 77 |
+
]
|
| 78 |
+
for s in think_starts:
|
| 79 |
+
if l.startswith(s):
|
| 80 |
+
return True
|
| 81 |
+
# ๊ธ๋จธ๋ฆฌ ๊ธฐํธ + ์์ด reasoning
|
| 82 |
+
if l.startswith(("- ", "* ", "โ ")) and any(c.isascii() and c.isalpha() for c in l[:20]):
|
| 83 |
+
if not any(ord(c) > 0x1100 for c in l[:30]): # ํ๊ธ ์์ผ๋ฉด thinking
|
| 84 |
+
return True
|
| 85 |
+
return False
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def _split_thinking_answer(raw: str) -> tuple:
|
| 89 |
+
"""์๋ต์์ thinking ๋ถ๋ถ๊ณผ ์ค์ ๋ต๋ณ์ ๋ถ๋ฆฌ"""
|
| 90 |
+
lines = raw.split("\n")
|
| 91 |
+
answer_start = -1
|
| 92 |
+
|
| 93 |
+
for i, line in enumerate(lines):
|
| 94 |
+
if not _is_thinking_line(line):
|
| 95 |
+
# ํ๊ธ/๋นASCII๊ฐ ํฌํจ๋ ์ค์ ๋ต๋ณ ์์์
|
| 96 |
+
if any(ord(c) > 0x1100 for c in line.strip()[:10]):
|
| 97 |
+
answer_start = i
|
| 98 |
+
break
|
| 99 |
+
# ๋๋ ๋น์ค 2๊ฐ ์ดํ ์์ด ๋ต๋ณ
|
| 100 |
+
if i > 2 and not _is_thinking_line(line):
|
| 101 |
+
# ์์ 2์ค์ด ๋น์ค์ด๋ฉด ๋ต๋ณ ์์
|
| 102 |
+
if all(not lines[j].strip() for j in range(max(0,i-2), i)):
|
| 103 |
+
answer_start = i
|
| 104 |
+
break
|
| 105 |
+
|
| 106 |
+
if answer_start > 0:
|
| 107 |
+
thinking = "\n".join(lines[:answer_start]).strip()
|
| 108 |
+
answer = "\n".join(lines[answer_start:]).strip()
|
| 109 |
+
return thinking, answer
|
| 110 |
+
|
| 111 |
+
return "", raw
|
| 112 |
+
|
| 113 |
+
|
| 114 |
def format_response(raw: str) -> str:
|
| 115 |
+
# 1. <think>...</think> ์๋ฃ
|
| 116 |
chain, answer = parse_think_blocks(raw)
|
| 117 |
if chain:
|
| 118 |
+
return (
|
|
|
|
|
|
|
| 119 |
"<details>\n"
|
| 120 |
"<summary>๐ง Reasoning Chain โ click to expand</summary>\n\n"
|
| 121 |
+
f"{chain}\n\n"
|
| 122 |
"</details>\n\n"
|
| 123 |
+
f"{answer}"
|
| 124 |
)
|
| 125 |
+
# 2. <think> ์ด๋ฆผ but ๋ซํ์ง ์์
|
|
|
|
| 126 |
if "<think>" in raw and "</think>" not in raw:
|
| 127 |
think_len = len(raw) - raw.index("<think>") - 7
|
| 128 |
return f"๐ง Reasoning... ({think_len} chars)"
|
| 129 |
+
# 3. ํ๋ฌธ thinking ๊ฐ์ง (NIPA ์๋ํฌ์ธํธ)
|
| 130 |
+
first_line = raw.strip().split("\n")[0] if raw.strip() else ""
|
| 131 |
+
if _is_thinking_line(first_line) and len(raw) > 20:
|
| 132 |
+
thinking, answer = _split_thinking_answer(raw)
|
| 133 |
+
if thinking and answer:
|
| 134 |
+
return (
|
| 135 |
+
f"<details>\n"
|
| 136 |
+
f"<summary>๐ง Reasoning Chain ({len(thinking)} chars)</summary>\n\n"
|
| 137 |
+
f"{thinking}\n\n"
|
| 138 |
+
f"</details>\n\n"
|
| 139 |
+
f"{answer}"
|
| 140 |
+
)
|
| 141 |
+
elif thinking and not answer:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
# ์์ง ๋ต๋ณ ์ ๋์ด โ ์บ๋ฆญํฐ ์๋ง
|
| 143 |
return f"๐ง Reasoning... ({len(raw)} chars)"
|
| 144 |
return raw
|