thanglekdi commited on
Commit
5c2060f
·
1 Parent(s): af2c4a4

test deepseek

Browse files
Files changed (1) hide show
  1. app.py +50 -23
app.py CHANGED
@@ -1,33 +1,60 @@
1
  import gradio as gr
 
2
 
3
- def generate_markdown_response(message, history, system_message, max_tokens, temperature, top_p):
4
- # Giả sử đây là kết quả gồm Markdown + LaTeX
5
- md = f"""
6
- **Bước 1**: Viết lại tích phân
7
- $$\\int_0^2 x^2 \\,dx$$
 
 
 
 
 
 
 
 
8
 
9
- **Bước 2**: Nguyên hàm
10
- $$F(x)=\\frac{x^3}{3}$$
 
 
 
 
 
 
 
11
 
12
- **Bước 3**: Giá trị
13
- $$F(2)-F(0)=\\frac{8}{3}$$
 
14
 
15
- **Kết luận**:
16
- $$\\boxed{{\\int_0^2 x^2 \\,dx=\\frac{8}{3}}}$$
17
- """
18
- return (None, md)
 
 
 
19
 
20
- with gr.Blocks() as demo:
21
- chatbot = gr.Chatbot(type="messages")
22
- md_output = gr.Markdown(
23
- latex_delimiters=[{"left":"$$","right":"$$","display":True}]
24
- )
25
- txt = gr.Textbox(label="Nhập câu hỏi")
 
 
 
 
 
 
 
26
  txt.submit(
27
- generate_markdown_response,
28
- inputs=[txt, chatbot, gr.Textbox(value="Bạn chatbot toán học"),
29
- gr.Slider(1,2048,200), gr.Slider(0.1,4.0,0.7), gr.Slider(0.1,1.0,0.95)],
30
- outputs=[chatbot, md_output]
31
  )
32
 
 
33
  demo.launch()
 
1
  import gradio as gr
2
+ import call_api
3
 
4
+ # Hàm format 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()