Spaces:
Running
Running
update(utils): dynamic reasoning toggle behavior
Browse files- [refactor] Replace `open_block` with `open_block_open` and `open_block_closed` (utils.py:328-330)
- [update] Expand reasoning block when `</think>` tag is missing (render_with_reasoning_toggle)
- [update] Collapse reasoning block by default when `</think>` tag is present (render_with_reasoning_toggle)
utils.py
CHANGED
|
@@ -325,15 +325,23 @@ def render_with_reasoning_toggle(text: str, show_reasoning: bool) -> str:
|
|
| 325 |
return pattern_strip.sub("", text)
|
| 326 |
|
| 327 |
# Show reasoning: stream it as it arrives by converting tags into a collapsible details block
|
| 328 |
-
|
|
|
|
| 329 |
close_block = "\n```\n</details>\n"
|
| 330 |
|
| 331 |
-
#
|
| 332 |
-
if "
|
| 333 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
-
#
|
| 336 |
-
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
return text
|
|
|
|
| 325 |
return pattern_strip.sub("", text)
|
| 326 |
|
| 327 |
# Show reasoning: stream it as it arrives by converting tags into a collapsible details block
|
| 328 |
+
open_block_open = "<details open><summary>Reasoning</summary>\n\n```text\n"
|
| 329 |
+
open_block_closed = "<details><summary>Reasoning</summary>\n\n```text\n"
|
| 330 |
close_block = "\n```\n</details>\n"
|
| 331 |
|
| 332 |
+
# If the closing tag is not present yet, keep the block expanded while streaming
|
| 333 |
+
if "</think>" not in text:
|
| 334 |
+
# Replace any raw <think> with an expanded details block
|
| 335 |
+
text = re.sub(r"<think>", open_block_open, text, flags=re.IGNORECASE)
|
| 336 |
+
# If for any reason a closed details opening exists, switch it to open (expanded)
|
| 337 |
+
text = text.replace(open_block_closed, open_block_open)
|
| 338 |
+
return text
|
| 339 |
|
| 340 |
+
# If the closing tag is present, render a collapsed block by default
|
| 341 |
+
# 1) Ensure opening is the closed variant
|
| 342 |
+
text = re.sub(r"<think>", open_block_closed, text, flags=re.IGNORECASE)
|
| 343 |
+
text = text.replace(open_block_open, open_block_closed)
|
| 344 |
+
# 2) Close the block
|
| 345 |
+
text = re.sub(r"</think>", close_block, text, flags=re.IGNORECASE)
|
| 346 |
|
| 347 |
return text
|