thanglekdi commited on
Commit
5b70050
·
1 Parent(s): 1005960

làm mọi thứ đẹp lại

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. call_api.py +57 -2
app.py CHANGED
@@ -3,7 +3,7 @@ import gradio as gr # type: ignore
3
  import call_api
4
 
5
  chat = gr.ChatInterface(
6
- call_api.respond, #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=[
 
3
  import call_api
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=[
call_api.py CHANGED
@@ -1,4 +1,4 @@
1
- import openai # type: ignore
2
  def call_openai(
3
  user_prompt,
4
  chat_history: list[tuple[str, str]],
@@ -37,7 +37,7 @@ def call_openai(
37
  print("## Messages: \n", messages) #debug output
38
 
39
  #create output
40
- response = openai.OpenAI().responses.create(
41
  model="gpt-4.1-nano",
42
  input=messages,
43
  temperature=temperature,
@@ -51,7 +51,62 @@ def call_openai(
51
  print("\n")
52
  yield response #chat reply
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
 
 
 
55
 
56
 
57
  """
 
1
+ from openai import OpenAI # type: ignore
2
  def call_openai(
3
  user_prompt,
4
  chat_history: list[tuple[str, str]],
 
37
  print("## Messages: \n", messages) #debug output
38
 
39
  #create output
40
+ response = OpenAI().responses.create(
41
  model="gpt-4.1-nano",
42
  input=messages,
43
  temperature=temperature,
 
51
  print("\n")
52
  yield response #chat reply
53
 
54
+ def call_deepseek(
55
+ user_prompt: str,
56
+ chat_history: list[tuple[str, str]],
57
+ system_prompt: str,
58
+ max_tokens: int,
59
+ temperature: float,
60
+ top_p: float,
61
+ file_upload=None,
62
+ image_upload=None
63
+ ) -> str:
64
+ """
65
+ Gọi DeepSeek Chat qua OpenAI-compatible API (không stream).
66
+ - file_upload và image_upload tùy chọn (None để bỏ qua xử lý).
67
+ Trả về:
68
+ - reply (str): nội dung model sinh ra.
69
+ """
70
+ # 1. Xử lý tùy chọn file (nếu có)
71
+ if file_upload is not None:
72
+ try:
73
+ with open(file_upload.name, "rb") as f:
74
+ file_bytes = f.read()
75
+ # TODO: xử lý hoặc đính kèm file_bytes vào prompt nếu cần
76
+ except Exception:
77
+ pass
78
+
79
+ # 2. Xử lý tùy chọn image (nếu có)
80
+ if image_upload is not None:
81
+ try:
82
+ # image_upload là PIL.Image.Image
83
+ img = image_upload
84
+ # TODO: xử lý img nếu cần
85
+ except Exception:
86
+ pass
87
+
88
+ # 3. Xây dựng messages lịch sử chat
89
+ messages = [{"role": "system", "content": system_prompt}]
90
+ for user_msg, ai_msg in chat_history:
91
+ if user_msg:
92
+ messages.append({"role": "user", "content": user_msg})
93
+ if ai_msg:
94
+ messages.append({"role": "assistant", "content": ai_msg})
95
+ # Thêm prompt hiện tại của user
96
+ messages.append({"role": "user", "content": user_prompt})
97
+
98
+ # 4. Gọi API DeepSeek Chat (OpenAI-compatible)
99
+ response = OpenAI().chat.completions.create(
100
+ model="deepseek-chat", # hoặc model bạn cấu hình
101
+ messages=messages,
102
+ temperature=temperature,
103
+ top_p=top_p,
104
+ max_tokens=max_tokens
105
+ )
106
 
107
+ # 5. Trích xuất kết quả trả về
108
+ reply = response.choices[0].message.content
109
+ return reply
110
 
111
 
112
  """