thanglekdi commited on
Commit
16126d1
·
1 Parent(s): 66b81f9

test deepseek

Browse files
Files changed (3) hide show
  1. README.md +0 -5
  2. app.py +49 -30
  3. call_api.py +41 -0
README.md CHANGED
@@ -10,8 +10,3 @@ pinned: false
10
  ---
11
 
12
  An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
13
-
14
- #reqirment
15
- torch
16
- einops
17
- transformers==4.38.1
 
10
  ---
11
 
12
  An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
app.py CHANGED
@@ -76,43 +76,62 @@ import os
76
  # print(result)
77
  # return result
78
 
79
- import replicate
80
- def deepseek_api_replicate(
81
- user_message,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  history: list[tuple[str, str]],
83
  system_message,
84
- max_new_tokens,
85
  temperature,
86
- top_p):
87
- """
88
- Gọi DeepSeek Math trên Replicate và trả ngay kết quả.
89
-
90
- Trả về:
91
- str hoặc [bytes]: output model sinh ra
92
- """
93
- # 1. Khởi tạo client và xác thực
94
- # token = os.getenv("REPLICATE_API_TOKEN")
95
- # if not token:
96
- # raise RuntimeError("Missing REPLICATE_API_TOKEN") # bảo mật bằng biến môi trường
97
- client = replicate.Client(api_token="REPLICATE_API_TOKEN")
98
-
99
- # 2. Gọi model
100
- output = client.run(
101
- "deepseek-ai/deepseek-math-7b-base:61f572dae0985541cdaeb4a114fd5d2d16cb40dac3894da10558992fc60547c7",
102
- input={
103
- "system_prompt": system_message,
104
- "user_prompt": user_message,
105
- "max_new_tokens": max_new_tokens,
106
- "temperature": temperature,
107
- "top_p": top_p
108
- }
109
  )
110
 
111
- # 3. Trả kết quả
112
- return output
113
 
114
  chat = gr.ChatInterface(
115
- deepseek_api_replicate, #chat
116
  # title="Trợ lý Học Tập AI",
117
  # 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!",
118
  additional_inputs=[
 
76
  # print(result)
77
  # return result
78
 
79
+ # import replicate
80
+ # def deepseek_api_replicate(
81
+ # user_message,
82
+ # history: list[tuple[str, str]],
83
+ # system_message,
84
+ # max_new_tokens,
85
+ # temperature,
86
+ # top_p):
87
+ # """
88
+ # Gọi DeepSeek Math trên Replicate và trả ngay kết quả.
89
+
90
+ # Trả về:
91
+ # str hoặc [bytes]: output model sinh ra
92
+ # """
93
+ # # 1. Khởi tạo client và xác thực
94
+ # # token = os.getenv("REPLICATE_API_TOKEN")
95
+ # # if not token:
96
+ # # raise RuntimeError("Missing REPLICATE_API_TOKEN") # bảo mật bằng biến môi trường
97
+ # client = replicate.Client(api_token="REPLICATE_API_TOKEN")
98
+
99
+ # # 2. Gọi model
100
+ # output = client.run(
101
+ # "deepseek-ai/deepseek-math-7b-base:61f572dae0985541cdaeb4a114fd5d2d16cb40dac3894da10558992fc60547c7",
102
+ # input={
103
+ # "system_prompt": system_message,
104
+ # "user_prompt": user_message,
105
+ # "max_new_tokens": max_new_tokens,
106
+ # "temperature": temperature,
107
+ # "top_p": top_p
108
+ # }
109
+ # )
110
+
111
+ # # 3. Trả kết quả
112
+ # return output
113
+
114
+ import call_api
115
+ def respond(
116
+ message,
117
  history: list[tuple[str, str]],
118
  system_message,
119
+ max_tokens,
120
  temperature,
121
+ top_p
122
+ ):
123
+ return call_api.respond(
124
+ message,
125
+ history: list[tuple[str, str]],
126
+ system_message,
127
+ max_tokens,
128
+ temperature,
129
+ top_p
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  )
131
 
 
 
132
 
133
  chat = gr.ChatInterface(
134
+ respond, #chat
135
  # title="Trợ lý Học Tập AI",
136
  # 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!",
137
  additional_inputs=[
call_api.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai # type: ignore
2
+ # openai.api_key = os.getenv("OPENAI_API_KEY")
3
+
4
+ client = openai.OpenAI()
5
+ def respond(
6
+ message,
7
+ history: list[tuple[str, str]],
8
+ system_message,
9
+ max_tokens,
10
+ temperature,
11
+ top_p
12
+ ):
13
+
14
+ #read system message
15
+ messages = [{"role": "system", "content": system_message}]
16
+
17
+ #read history
18
+ for val in history:
19
+ if val[0]:
20
+ messages.append({"role": "user", "content": val[0]})
21
+ if val[1]:
22
+ messages.append({"role": "assistant", "content": val[1]})
23
+
24
+ #read output
25
+ messages.append({"role": "user", "content": message})
26
+ print("## Messages: \n", messages) #debug output
27
+
28
+ #create output
29
+ response = client.responses.create(
30
+ model="gpt-4.1-nano",
31
+ input=messages,
32
+ temperature=temperature,
33
+ top_p=top_p,
34
+ max_output_tokens=max_tokens
35
+ )
36
+
37
+ #read output
38
+ response = response.output_text
39
+ print("## Response: ", response) #debug output
40
+ print("\n")
41
+ yield response #chat reply