Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,35 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 12 |
low_cpu_mem_usage=True
|
| 13 |
)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# 2. Main Generation Logic Handler
|
| 16 |
def generate_response(message, history, system_prompt, temperature, top_p, top_k, max_tokens):
|
| 17 |
# Setup standard base template structures
|
|
@@ -24,7 +53,17 @@ def generate_response(message, history, system_prompt, temperature, top_p, top_k
|
|
| 24 |
if role == "user":
|
| 25 |
prompt += f"User: {content}\n"
|
| 26 |
elif role == "assistant":
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Inject active raw message with Quasar's default target sequence patterns
|
| 30 |
prompt += f"User: {message}\nAssistant: <think>"
|
|
@@ -48,7 +87,8 @@ def generate_response(message, history, system_prompt, temperature, top_p, top_k
|
|
| 48 |
partial_text = "<think>"
|
| 49 |
for new_text in streamer:
|
| 50 |
partial_text += new_text
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
# 3. Clean Interface Assembly
|
| 54 |
with gr.Blocks() as demo:
|
|
@@ -57,7 +97,8 @@ with gr.Blocks() as demo:
|
|
| 57 |
with gr.Row():
|
| 58 |
# Left Workspace Column: Chat Layout
|
| 59 |
with gr.Column(scale=3):
|
| 60 |
-
|
|
|
|
| 61 |
msg_input = gr.Textbox(placeholder="Enter reasoning prompt...", label="Your Message")
|
| 62 |
with gr.Row():
|
| 63 |
submit_btn = gr.Button("Send", variant="primary")
|
|
@@ -84,8 +125,8 @@ with gr.Blocks() as demo:
|
|
| 84 |
def bot_side_inference(history, system_p, temp, t_p, t_k, max_t):
|
| 85 |
user_message = history[-1]["content"]
|
| 86 |
|
| 87 |
-
# Initialize assistant message
|
| 88 |
-
history.append({"role": "assistant", "content": "
|
| 89 |
|
| 90 |
for partial_reply in generate_response(user_message, history[:-2], system_p, temp, t_p, t_k, max_t):
|
| 91 |
history[-1]["content"] = partial_reply
|
|
|
|
| 12 |
low_cpu_mem_usage=True
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# Helper function to format the streaming tokens with custom HTML/Markdown styling
|
| 16 |
+
def format_reasoning(text):
|
| 17 |
+
"""
|
| 18 |
+
Parses <think> tags and applies custom styles.
|
| 19 |
+
Uses HTML <details> for a clean, distinct 'accordion' look for reasoning.
|
| 20 |
+
"""
|
| 21 |
+
if "<think>" in text:
|
| 22 |
+
if "</think>" in text:
|
| 23 |
+
# Thinking phase is complete
|
| 24 |
+
parts = text.split("</think>")
|
| 25 |
+
thinking_content = parts[0].replace("<think>", "").strip()
|
| 26 |
+
answer_content = parts[1].strip()
|
| 27 |
+
|
| 28 |
+
return f"""<details open><summary style="color: #ff7a00; font-weight: bold; cursor: pointer;">🤔 Thinking Process (Click to collapse)</summary>
|
| 29 |
+
<div style="color: #666; font-style: italic; background-color: #f9f9f9; padding: 10px; border-left: 3px solid #ff7a00; margin: 5px 0 15px 0;">
|
| 30 |
+
{thinking_content}
|
| 31 |
+
</div></details>
|
| 32 |
+
|
| 33 |
+
{answer_content}"""
|
| 34 |
+
else:
|
| 35 |
+
# Currently thinking
|
| 36 |
+
thinking_content = text.replace("<think>", "").strip()
|
| 37 |
+
return f"""<details open><summary style="color: #ff7a00; font-weight: bold;">🤔 Thinking...</summary>
|
| 38 |
+
<div style="color: #666; font-style: italic; background-color: #f9f9f9; padding: 10px; border-left: 3px solid #ff7a00; margin: 5px 0 15px 0;">
|
| 39 |
+
{thinking_content}
|
| 40 |
+
</div></details>"""
|
| 41 |
+
|
| 42 |
+
return text
|
| 43 |
+
|
| 44 |
# 2. Main Generation Logic Handler
|
| 45 |
def generate_response(message, history, system_prompt, temperature, top_p, top_k, max_tokens):
|
| 46 |
# Setup standard base template structures
|
|
|
|
| 53 |
if role == "user":
|
| 54 |
prompt += f"User: {content}\n"
|
| 55 |
elif role == "assistant":
|
| 56 |
+
# Strip out our custom HTML formatting back to raw tokens for context history
|
| 57 |
+
# (Simple fallback: if the history already has html, you might want to keep a raw text history,
|
| 58 |
+
# but for this generation block we clean basic tags if present)
|
| 59 |
+
clean_content = content
|
| 60 |
+
if "🤔" in content:
|
| 61 |
+
# Reconstruct rough raw tags for the model's history context
|
| 62 |
+
import re
|
| 63 |
+
clean_content = re.sub(r'<details.*?>.*?<\/summary>', '<think>', clean_content)
|
| 64 |
+
clean_content = clean_content.replace('</div></details>', '</think>')
|
| 65 |
+
clean_content = re.sub(r'<div.*?>', '', clean_content)
|
| 66 |
+
prompt += f"Assistant: {clean_content}\n"
|
| 67 |
|
| 68 |
# Inject active raw message with Quasar's default target sequence patterns
|
| 69 |
prompt += f"User: {message}\nAssistant: <think>"
|
|
|
|
| 87 |
partial_text = "<think>"
|
| 88 |
for new_text in streamer:
|
| 89 |
partial_text += new_text
|
| 90 |
+
# Yield the beautifully formatted text instead of raw markdown tags
|
| 91 |
+
yield format_reasoning(partial_text)
|
| 92 |
|
| 93 |
# 3. Clean Interface Assembly
|
| 94 |
with gr.Blocks() as demo:
|
|
|
|
| 97 |
with gr.Row():
|
| 98 |
# Left Workspace Column: Chat Layout
|
| 99 |
with gr.Column(scale=3):
|
| 100 |
+
# Line wrapping and markdown parsing enabled by default
|
| 101 |
+
chatbot = gr.Chatbot(height=500, type="messages")
|
| 102 |
msg_input = gr.Textbox(placeholder="Enter reasoning prompt...", label="Your Message")
|
| 103 |
with gr.Row():
|
| 104 |
submit_btn = gr.Button("Send", variant="primary")
|
|
|
|
| 125 |
def bot_side_inference(history, system_p, temp, t_p, t_k, max_t):
|
| 126 |
user_message = history[-1]["content"]
|
| 127 |
|
| 128 |
+
# Initialize assistant message element with a loading state style
|
| 129 |
+
history.append({"role": "assistant", "content": "🤔 *Thinking...*"})
|
| 130 |
|
| 131 |
for partial_reply in generate_response(user_message, history[:-2], system_p, temp, t_p, t_k, max_t):
|
| 132 |
history[-1]["content"] = partial_reply
|