Spaces:
Sleeping
Sleeping
Commit
·
5c2060f
1
Parent(s):
af2c4a4
test deepseek
Browse files
app.py
CHANGED
|
@@ -1,33 +1,60 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
txt.submit(
|
| 27 |
-
|
| 28 |
-
inputs=[txt, chatbot,
|
| 29 |
-
|
| 30 |
-
outputs=[chatbot, md_output]
|
| 31 |
)
|
| 32 |
|
|
|
|
| 33 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import call_api
|
| 3 |
|
| 4 |
+
# Hàm format và respond giữ nguyên như trước
|
| 5 |
+
def format_reply(raw_reply: str) -> str:
|
| 6 |
+
lines = raw_reply.splitlines()
|
| 7 |
+
out_lines = []
|
| 8 |
+
for line in lines:
|
| 9 |
+
line = line.strip()
|
| 10 |
+
if not line:
|
| 11 |
+
continue
|
| 12 |
+
if line.startswith("Bước"):
|
| 13 |
+
out_lines.append(f"**{line}**")
|
| 14 |
+
else:
|
| 15 |
+
out_lines.append(line)
|
| 16 |
+
return "\n\n".join(out_lines)
|
| 17 |
|
| 18 |
+
def respond(
|
| 19 |
+
message, history, system_message, max_tokens, temperature, top_p,
|
| 20 |
+
file_upload=None, image_upload=None
|
| 21 |
+
):
|
| 22 |
+
raw = call_api.call_deepseek(
|
| 23 |
+
message, history, system_message, max_tokens, temperature, top_p,
|
| 24 |
+
file_upload=file_upload, image_upload=image_upload
|
| 25 |
+
)
|
| 26 |
+
return format_reply(raw)
|
| 27 |
|
| 28 |
+
with gr.Blocks() as demo: # Bắt đầu Blocks
|
| 29 |
+
# Thành phần hiển thị lịch sử chat
|
| 30 |
+
chatbot = gr.Chatbot(type="messages") # hỗ trợ subset Markdown:contentReference[oaicite:3]{index=3}
|
| 31 |
|
| 32 |
+
# Thành phần Markdown để render kết quả đã format (Markdown + LaTeX)
|
| 33 |
+
markdown_out = gr.Markdown(
|
| 34 |
+
latex_delimiters=[
|
| 35 |
+
{"left": "$$", "right": "$$", "display": True},
|
| 36 |
+
{"left": "$", "right": "$", "display": False},
|
| 37 |
+
]
|
| 38 |
+
) # cho phép render LaTeX:contentReference[oaicite:4]{index=4}
|
| 39 |
|
| 40 |
+
# Các input controls
|
| 41 |
+
with gr.Row():
|
| 42 |
+
txt = gr.Textbox(label="Nhập câu hỏi")
|
| 43 |
+
sys_msg = gr.Textbox(value="Bạn là một chatbot tiếng Việt thân thiện.", label="System message")
|
| 44 |
+
with gr.Row():
|
| 45 |
+
max_t = gr.Slider(1, 2048, value=500, step=1, label="Max new tokens")
|
| 46 |
+
temp = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature")
|
| 47 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 48 |
+
# Inputs tuỳ chọn
|
| 49 |
+
file_up = gr.File(label="Tải lên file (tuỳ chọn)", file_count="single", optional=True)
|
| 50 |
+
img_up = gr.Image(type="pil", label="Tải lên ảnh (tuỳ chọn)", optional=True)
|
| 51 |
+
|
| 52 |
+
# Sự kiện submit
|
| 53 |
txt.submit(
|
| 54 |
+
fn=respond,
|
| 55 |
+
inputs=[txt, chatbot, sys_msg, max_t, temp, top_p, file_up, img_up],
|
| 56 |
+
outputs=[chatbot, markdown_out]
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Chạy app
|
| 60 |
demo.launch()
|