cafe3310's picture
refactor: Optimize thinking token display using Markdown
fdbc2bf
raw
history blame
1.12 kB
import gradio as gr
from comp import generate_response
# --- Gradio UI ---
with gr.Blocks() as demo:
gr.Markdown("# Ling Playground")
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.ClearButton([msg, chatbot])
def user(user_message, history):
return "", history + [[user_message, None]]
def bot(history):
user_message = history[-1][0]
history[-1][1] = ""
for response in generate_response(user_message, history[:-1]):
if "</think>" in response:
parts = response.split("</think>", 1)
thinking_text = parts[0].replace("<think>", "")
body_text = parts[1]
md_output = f"**Thinking...**\n```\n{thinking_text}\n```\n\n{body_text}"
history[-1][1] = md_output
else:
history[-1][1] = response
yield history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()