Spaces:
Sleeping
Sleeping
import gradio as gr | |
import call_api | |
# Hàm format và respond giữ nguyên như trước | |
def format_reply(raw_reply: str) -> str: | |
lines = raw_reply.splitlines() | |
out_lines = [] | |
for line in lines: | |
line = line.strip() | |
if not line: | |
continue | |
if line.startswith("Bước"): | |
out_lines.append(f"**{line}**") | |
else: | |
out_lines.append(line) | |
return "\n\n".join(out_lines) | |
def respond( | |
message, history, system_message, max_tokens, temperature, top_p, | |
file_upload=None, image_upload=None | |
): | |
raw = call_api.call_deepseek( | |
message, history, system_message, max_tokens, temperature, top_p, | |
file_upload=file_upload, image_upload=image_upload | |
) | |
return format_reply(raw) | |
with gr.Blocks() as demo: # Bắt đầu Blocks | |
# Thành phần hiển thị lịch sử chat | |
chatbot = gr.Chatbot(type="messages") # hỗ trợ subset Markdown:contentReference[oaicite:3]{index=3} | |
# Thành phần Markdown để render kết quả đã format (Markdown + LaTeX) | |
markdown_out = gr.Markdown( | |
latex_delimiters=[ | |
{"left": "$$", "right": "$$", "display": True}, | |
{"left": "$", "right": "$", "display": False}, | |
] | |
) # cho phép render LaTeX:contentReference[oaicite:4]{index=4} | |
# Các input controls | |
with gr.Row(): | |
txt = gr.Textbox(label="Nhập câu hỏi") | |
sys_msg = gr.Textbox(value="Bạn là một chatbot tiếng Việt thân thiện.", label="System message") | |
with gr.Row(): | |
max_t = gr.Slider(1, 2048, value=500, step=1, label="Max new tokens") | |
temp = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature") | |
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p") | |
# Inputs tuỳ chọn | |
file_up = gr.File(label="Tải lên file (tuỳ chọn)", file_count="single", optional=True) | |
img_up = gr.Image(type="pil", label="Tải lên ảnh (tuỳ chọn)", optional=True) | |
# Sự kiện submit | |
txt.submit( | |
fn=respond, | |
inputs=[txt, chatbot, sys_msg, max_t, temp, top_p, file_up, img_up], | |
outputs=[chatbot, markdown_out] | |
) | |
# Chạy app | |
demo.launch() | |