Update app.py
Browse files
app.py
CHANGED
|
@@ -13,33 +13,49 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 13 |
low_cpu_mem_usage=True
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# Helper function to
|
| 17 |
def format_reasoning(text):
|
| 18 |
"""
|
| 19 |
-
Parses <think>
|
| 20 |
-
|
| 21 |
"""
|
|
|
|
|
|
|
| 22 |
if "<think>" in text:
|
| 23 |
if "</think>" in text:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
answer_content = parts[1].strip()
|
| 28 |
-
|
| 29 |
-
return f"""<details open><summary style="color: #ff7a00; font-weight: bold; cursor: pointer;">π€ Thinking Process (Click to collapse)</summary>
|
| 30 |
-
<div style="color: #666; font-style: italic; background-color: #f9f9f9; padding: 10px; border-left: 3px solid #ff7a00; margin: 5px 0 15px 0;">
|
| 31 |
{thinking_content}
|
| 32 |
-
</div></details>
|
| 33 |
-
|
| 34 |
-
{answer_content}"""
|
| 35 |
else:
|
| 36 |
-
# Currently
|
| 37 |
-
thinking_content = text.
|
| 38 |
-
|
| 39 |
-
<div style="color: #
|
| 40 |
{thinking_content}
|
| 41 |
</div></details>"""
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return text
|
| 44 |
|
| 45 |
# 2. Main Generation Logic Handler
|
|
@@ -54,13 +70,19 @@ def generate_response(message, history, system_prompt, temperature, top_p, top_k
|
|
| 54 |
if role == "user":
|
| 55 |
prompt += f"User: {content}\n"
|
| 56 |
elif role == "assistant":
|
| 57 |
-
# Strip out our custom HTML formatting back to raw
|
| 58 |
clean_content = content
|
| 59 |
-
if "π€" in content:
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
prompt += f"Assistant: {clean_content}\n"
|
| 65 |
|
| 66 |
# Inject active raw message with Quasar's default target sequence patterns
|
|
@@ -94,7 +116,6 @@ with gr.Blocks() as demo:
|
|
| 94 |
with gr.Row():
|
| 95 |
# Left Workspace Column: Chat Layout
|
| 96 |
with gr.Column(scale=3):
|
| 97 |
-
# REMOVED type="messages" to solve the init crash
|
| 98 |
chatbot = gr.Chatbot(height=500)
|
| 99 |
msg_input = gr.Textbox(placeholder="Enter reasoning prompt...", label="Your Message")
|
| 100 |
with gr.Row():
|
|
|
|
| 13 |
low_cpu_mem_usage=True
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Helper function to parse both <think> and <answer> tag structures
|
| 17 |
def format_reasoning(text):
|
| 18 |
"""
|
| 19 |
+
Parses <think>...</think> and <answer>...</answer> tags
|
| 20 |
+
to apply highly distinct, clean visual wrappers.
|
| 21 |
"""
|
| 22 |
+
# Extract Thinking Content
|
| 23 |
+
thinking_html = ""
|
| 24 |
if "<think>" in text:
|
| 25 |
if "</think>" in text:
|
| 26 |
+
thinking_content = text.split("<think>")[1].split("</think>")[0].strip()
|
| 27 |
+
thinking_html = f"""<details open><summary style="color: #ff7a00; font-weight: bold; cursor: pointer; margin-bottom: 4px;">π€ Thinking Process (Click to collapse)</summary>
|
| 28 |
+
<div style="color: #555555; font-style: italic; background-color: #fdfdfd; padding: 12px; border-left: 4px solid #ff7a00; border-radius: 4px; margin: 4px 0 16px 0; font-size: 0.95em; line-height: 1.5;">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
{thinking_content}
|
| 30 |
+
</div></details>"""
|
|
|
|
|
|
|
| 31 |
else:
|
| 32 |
+
# Currently streaming thought process
|
| 33 |
+
thinking_content = text.split("<think>")[1].strip()
|
| 34 |
+
thinking_html = f"""<details open><summary style="color: #ff7a00; font-weight: bold;">π€ Thinking...</summary>
|
| 35 |
+
<div style="color: #555555; font-style: italic; background-color: #fdfdfd; padding: 12px; border-left: 4px solid #ff7a00; border-radius: 4px; margin: 4px 0 16px 0; font-size: 0.95em; line-height: 1.5;">
|
| 36 |
{thinking_content}
|
| 37 |
</div></details>"""
|
| 38 |
+
|
| 39 |
+
# Extract Answer Content
|
| 40 |
+
answer_html = ""
|
| 41 |
+
if "<answer>" in text:
|
| 42 |
+
if "</answer>" in text:
|
| 43 |
+
answer_content = text.split("<answer>")[1].split("</answer>")[0].strip()
|
| 44 |
+
answer_html = f"""<div style="background-color: #f4f6f8; border-left: 4px solid #2e4053; padding: 16px; border-radius: 4px; color: #1c2833; box-shadow: inset 0 1px 3px rgba(0,0,0,0.05); line-height: 1.6;">
|
| 45 |
+
{answer_content}
|
| 46 |
+
</div>"""
|
| 47 |
+
else:
|
| 48 |
+
# Currently streaming final answer output
|
| 49 |
+
answer_content = text.split("<answer>")[1].strip()
|
| 50 |
+
answer_html = f"""<div style="background-color: #f4f6f8; border-left: 4px solid #2e4053; padding: 16px; border-radius: 4px; color: #1c2833; line-height: 1.6;">
|
| 51 |
+
<span style="color: #2e4053; font-weight: bold; display: block; margin-bottom: 8px;">π Writing Response...</span>
|
| 52 |
+
{answer_content}
|
| 53 |
+
</div>"""
|
| 54 |
+
|
| 55 |
+
# Combine them for the streaming UI wrapper display
|
| 56 |
+
if thinking_html or answer_html:
|
| 57 |
+
return f"{thinking_html}\n{answer_html}"
|
| 58 |
+
|
| 59 |
return text
|
| 60 |
|
| 61 |
# 2. Main Generation Logic Handler
|
|
|
|
| 70 |
if role == "user":
|
| 71 |
prompt += f"User: {content}\n"
|
| 72 |
elif role == "assistant":
|
| 73 |
+
# Strip out our custom HTML formatting back to raw tags for context history
|
| 74 |
clean_content = content
|
| 75 |
+
if "π€" in content or "background-color" in content:
|
| 76 |
+
# RegEx conversion logic to revert beautiful layout code back to raw tags for model's context awareness
|
| 77 |
+
import re
|
| 78 |
+
# Check for completed or uncompleted think elements
|
| 79 |
+
think_match = re.search(r'<div style="color: #555555;.*?>(.*?)</div>', clean_content, re.DOTALL)
|
| 80 |
+
ans_match = re.search(r'</div></details>\s*<div style="background-color: #f4f6f8;.*?>.*?</span>(.*?)</div>', clean_content, re.DOTALL) or re.search(r'</div></details>\s*<div style="background-color: #f4f6f8;.*?>(.*?)</div>', clean_content, re.DOTALL)
|
| 81 |
+
|
| 82 |
+
raw_think = f"<think>{think_match.group(1).strip()}</think>" if think_match else ""
|
| 83 |
+
raw_ans = f"<answer>{ans_match.group(1).strip()}</answer>" if ans_match else ""
|
| 84 |
+
clean_content = f"{raw_think}\n{raw_ans}".strip()
|
| 85 |
+
|
| 86 |
prompt += f"Assistant: {clean_content}\n"
|
| 87 |
|
| 88 |
# Inject active raw message with Quasar's default target sequence patterns
|
|
|
|
| 116 |
with gr.Row():
|
| 117 |
# Left Workspace Column: Chat Layout
|
| 118 |
with gr.Column(scale=3):
|
|
|
|
| 119 |
chatbot = gr.Chatbot(height=500)
|
| 120 |
msg_input = gr.Textbox(placeholder="Enter reasoning prompt...", label="Your Message")
|
| 121 |
with gr.Row():
|