Spaces:
Sleeping
Sleeping
File size: 2,984 Bytes
af2c4a4 b10cbb3 68f58b6 b10cbb3 a20a68f b10cbb3 9284d7f b10cbb3 e8f320c b10cbb3 af2c4a4 e7e2827 af2c4a4 e7e2827 58549c9 e7e2827 102aa00 e7e2827 5c2060f e7e2827 b10cbb3 f1b344d b10cbb3 e7e2827 c263170 d83956b 5c2060f 6d423fe b10cbb3 |
1 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import gradio as gr
from openai import OpenAI # type: ignore
import os
# chat = gr.ChatInterface(
# call_api.call_deepseek, #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(label="Attach an image (optional)"), # báo lỗi
# gr.File(label="Upload a file (optional)"), # báo lỗi
# ],
# # 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, và đặt kết quả cuối cùng trong \boxed{}", "bạn là nhà toán học", 100, 0.7, 0.95],
# # ],
# )
def call_deepseek_new(
user_prompt,
chat_history
):
history = chat_history or []
history.append({"role": "user", "content": user_prompt})
# # Gọi API DeepSeek Chat (OpenAI-compatible, không stream)
# response = deepseek.chat.completions.create(
# model = "deepseek-chat", # hoặc model bạn đã config
# messages = history,
# )
# Lấy nội dung assistant trả về
reply = "response.choices[0].message.content"
# Append vào history
history.append({"role": "assistant", "content": reply})
# Trả về 2 outputs: toàn bộ history và đúng reply để render Markdown
return history, reply
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 = gr.Markdown(
latex_delimiters=[{"left": "$$", "right": "$$", "display": True}]
) # 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")
# Sự kiện submit
txt.submit(
call_deepseek_new,
inputs=[txt, chatbot],
outputs=[chatbot, markdown],
)
# Chạy app
if __name__ == "__main__":
demo.launch(show_error=True)
|