Spaces:
Sleeping
Sleeping
Commit
·
b10cbb3
1
Parent(s):
7ac42ec
test deepseek
Browse files
app.py
CHANGED
|
@@ -1,24 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
with gr.Blocks() as demo: # Bắt đầu Blocks
|
| 24 |
# Thành phần hiển thị lịch sử chat
|
|
@@ -40,12 +66,12 @@ with gr.Blocks() as demo: # Bắt đầu Blocks
|
|
| 40 |
|
| 41 |
# Sự kiện submit
|
| 42 |
txt.submit(
|
| 43 |
-
|
| 44 |
inputs=[txt, chatbot],
|
| 45 |
-
outputs=chatbot,
|
| 46 |
)
|
| 47 |
|
| 48 |
|
| 49 |
# Chạy app
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from openai import OpenAI # type: ignore
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# chat = gr.ChatInterface(
|
| 6 |
+
# call_api.call_deepseek, #chat
|
| 7 |
+
# title="Trợ lý Học Tập AI",
|
| 8 |
+
# 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!",
|
| 9 |
+
# additional_inputs=[
|
| 10 |
+
# gr.Textbox("Bạn là một chatbot tiếng Việt thân thiện.", label="System message"),
|
| 11 |
+
# gr.Slider(1, 2048, value=200, step=1, label="Max new tokens"),
|
| 12 |
+
# gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
| 13 |
+
# gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 14 |
|
| 15 |
+
# gr.Image(label="Attach an image (optional)"), # báo lỗi
|
| 16 |
+
# gr.File(label="Upload a file (optional)"), # báo lỗi
|
| 17 |
+
# ],
|
| 18 |
+
# # examples=[
|
| 19 |
+
# # # Mỗi item: [message, system_message, max_tokens, temperature, top_p]
|
| 20 |
+
# # ["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],
|
| 21 |
+
# # ],
|
| 22 |
+
# )
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
deepseek = OpenAI(api_key = os.getenv("DEEPSEEK_API_KEY"), base_url="https://api.deepseek.com")
|
| 27 |
+
def call_deepseek_new(
|
| 28 |
+
user_prompt,
|
| 29 |
+
chat_history
|
| 30 |
+
):
|
| 31 |
+
history = chat_history or []
|
| 32 |
+
history.append({"role": "user", "content": user_prompt})
|
| 33 |
+
|
| 34 |
+
# Gọi API DeepSeek Chat (OpenAI-compatible, không stream)
|
| 35 |
+
response = deepseek.chat.completions.create(
|
| 36 |
+
model = "deepseek-chat", # hoặc model bạn đã config
|
| 37 |
+
messages = history,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Lấy nội dung assistant trả về
|
| 41 |
+
reply = response.choices[0].message.content
|
| 42 |
+
|
| 43 |
+
# Append vào history
|
| 44 |
+
history.append({"role": "assistant", "content": reply})
|
| 45 |
+
|
| 46 |
+
# Trả về 2 outputs: toàn bộ history và đúng reply để render Markdown
|
| 47 |
+
return history, reply
|
| 48 |
|
| 49 |
with gr.Blocks() as demo: # Bắt đầu Blocks
|
| 50 |
# Thành phần hiển thị lịch sử chat
|
|
|
|
| 66 |
|
| 67 |
# Sự kiện submit
|
| 68 |
txt.submit(
|
| 69 |
+
call_deepseek_new,
|
| 70 |
inputs=[txt, chatbot],
|
| 71 |
+
outputs=[chatbot, markdown],
|
| 72 |
)
|
| 73 |
|
| 74 |
|
| 75 |
# Chạy app
|
| 76 |
if __name__ == "__main__":
|
| 77 |
+
demo.launch(show_error=True)
|