thanglekdi's picture
test deepseek
102aa00
raw
history blame
2.3 kB
# app.py
import gradio as gr # type: ignore
import call_api
def format_reply(raw_reply: str) -> str:
"""
Format lại chuỗi raw_reply thành Markdown-friendly:
- Chèn xuống dòng đôi để tạo paragraph rõ ràng
- In đậm các bước ('Bước 1:', 'Bước 2:', …)
- Giữ nguyên các công thức LaTeX ở dạng [$…$] hoặc [$$…$$]
"""
lines = raw_reply.splitlines()
out_lines = []
for line in lines:
line = line.strip()
if not line:
continue
# In đậm tiêu đề bước
if line.startswith("Bước"):
out_lines.append(f"**{line}**")
else:
out_lines.append(line)
# Nối với paragraph spacing
return "\n\n".join(out_lines)
def respond(
message,
history,
system_message,
max_tokens,
temperature,
top_p,
file_upload=None,
image_upload=None
):
# 1. Gọi API gốc, lấy raw text
raw = call_api.call_deepseek(
message, history, system_message, max_tokens, temperature, top_p,
file_upload=file_upload, image_upload=image_upload
)
# 2. Format lại
formatted = format_reply(raw)
return formatted
chat = gr.ChatInterface(
respond, #chat
title="Trợ lý Học Tập AI",
description="Nhập câu hỏi của bạn về Toán, Lý, Hóa, Văn… và nhận giải đáp chi tiết ngay lập tức!",
additional_inputs=[
gr.Textbox("Bạn là một chatbot tiếng Việt thân thiện.", label="System message"),
gr.Slider(1, 2048, value=200, step=1, label="Max new tokens"),
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
# gr.Image(type="pil", label="Attach an image (optional)"),
# gr.File(label="Upload a file (optional)"),
],
# examples=[
# # Mỗi item: [message, system_message, max_tokens, temperature, top_p]
# ["tích phân của x^2 từ 0 đến 2 là gì? vui lòng lập luận từng bước", "bạn là nhà toán học, hãy viết dạng chữ bình thường, không dùng kí tự đặt biết", 500, 0.7, 0.95],
# ],
)
if __name__ == "__main__":
chat.launch()