Spaces:
Sleeping
Sleeping
Commit
·
d86ac9a
1
Parent(s):
d11e8de
làm mọi thứ đẹp lại
Browse files- app.py +1 -75
- call_api.py +43 -1
- test.py +141 -0
app.py
CHANGED
@@ -2,80 +2,6 @@
|
|
2 |
import gradio as gr # type: ignore
|
3 |
import os
|
4 |
|
5 |
-
# import openai # type: ignore
|
6 |
-
# # openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
-
# client = openai.OpenAI()
|
8 |
-
# def respond(
|
9 |
-
# message,
|
10 |
-
# history: list[tuple[str, str]],
|
11 |
-
# system_message,
|
12 |
-
# max_tokens,
|
13 |
-
# temperature,
|
14 |
-
# top_p,
|
15 |
-
# image_uploaded,
|
16 |
-
# file_uploaded
|
17 |
-
# ):
|
18 |
-
|
19 |
-
# #read system message
|
20 |
-
# messages = [{"role": "system", "content": system_message}]
|
21 |
-
|
22 |
-
# #read history
|
23 |
-
# for val in history:
|
24 |
-
# if val[0]:
|
25 |
-
# messages.append({"role": "user", "content": val[0]})
|
26 |
-
# if val[1]:
|
27 |
-
# messages.append({"role": "assistant", "content": val[1]})
|
28 |
-
|
29 |
-
# #read output
|
30 |
-
# messages.append({"role": "user", "content": message})
|
31 |
-
# print("## Messages: \n", messages) #debug output
|
32 |
-
|
33 |
-
# #create output
|
34 |
-
# response = client.responses.create(
|
35 |
-
# model="gpt-4.1-nano",
|
36 |
-
# input=messages,
|
37 |
-
# temperature=temperature,
|
38 |
-
# top_p=top_p,
|
39 |
-
# max_output_tokens=max_tokens
|
40 |
-
# )
|
41 |
-
|
42 |
-
# #read output
|
43 |
-
# response = response.output_text
|
44 |
-
# print("## Response: ", response) #debug output
|
45 |
-
# print("\n")
|
46 |
-
# yield response #chat reply
|
47 |
-
|
48 |
-
# import torch
|
49 |
-
# from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
50 |
-
# model_name = "deepseek-ai/deepseek-math-7b-base"
|
51 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_name)
|
52 |
-
# model = AutoModelForCausalLM.from_pretrained(model_name)
|
53 |
-
# # model.generation_config = GenerationConfig.from_pretrained(model_name)
|
54 |
-
# # model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
55 |
-
# def deepseek(
|
56 |
-
# message,
|
57 |
-
# history: list[tuple[str, str]],
|
58 |
-
# system_message,
|
59 |
-
# max_tokens,
|
60 |
-
# temperature,
|
61 |
-
# top_p):
|
62 |
-
|
63 |
-
|
64 |
-
# # messages = [
|
65 |
-
# # {"role": "user", "content": "what is the integral of x^2 from 0 to 2?\nPlease reason step by step, and put your final answer within \\boxed{}."}
|
66 |
-
# # ]
|
67 |
-
# messages = [
|
68 |
-
# {"role": "user", "content": message}
|
69 |
-
# ]
|
70 |
-
|
71 |
-
# input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
72 |
-
# outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
|
73 |
-
# print(outputs)
|
74 |
-
# print("\n")
|
75 |
-
# result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
76 |
-
# print(result)
|
77 |
-
# return result
|
78 |
-
|
79 |
# import replicate
|
80 |
# def deepseek_api_replicate(
|
81 |
# user_message,
|
@@ -125,7 +51,7 @@ chat = gr.ChatInterface(
|
|
125 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
126 |
# gr.Image(type="pil", label="Attach an image (optional)"),
|
127 |
# gr.File(label="Upload a file (optional)"),
|
128 |
-
]
|
129 |
examples=[
|
130 |
# Mỗi item: [message, system_message, max_tokens, temperature, top_p]
|
131 |
["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],
|
|
|
2 |
import gradio as gr # type: ignore
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# import replicate
|
6 |
# def deepseek_api_replicate(
|
7 |
# user_message,
|
|
|
51 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
52 |
# gr.Image(type="pil", label="Attach an image (optional)"),
|
53 |
# gr.File(label="Upload a file (optional)"),
|
54 |
+
],
|
55 |
examples=[
|
56 |
# Mỗi item: [message, system_message, max_tokens, temperature, top_p]
|
57 |
["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],
|
call_api.py
CHANGED
@@ -38,4 +38,46 @@ def respond(
|
|
38 |
response = response.output_text
|
39 |
print("## Response: ", response) #debug output
|
40 |
print("\n")
|
41 |
-
yield response #chat reply
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
response = response.output_text
|
39 |
print("## Response: ", response) #debug output
|
40 |
print("\n")
|
41 |
+
yield response #chat reply
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
"""
|
47 |
+
Không có billing nên không xài được replicate
|
48 |
+
"""
|
49 |
+
# import replicate
|
50 |
+
|
51 |
+
# def deepseek_api_replicate(
|
52 |
+
# user_message,
|
53 |
+
# history: list[tuple[str, str]],
|
54 |
+
# system_message,
|
55 |
+
# max_new_tokens,
|
56 |
+
# temperature,
|
57 |
+
# top_p):
|
58 |
+
# """
|
59 |
+
# Gọi DeepSeek Math trên Replicate và trả ngay kết quả.
|
60 |
+
|
61 |
+
# Trả về:
|
62 |
+
# str hoặc [bytes]: output model sinh ra
|
63 |
+
# """
|
64 |
+
# # 1. Khởi tạo client và xác thực
|
65 |
+
# # token = os.getenv("REPLICATE_API_TOKEN")
|
66 |
+
# # if not token:
|
67 |
+
# # raise RuntimeError("Missing REPLICATE_API_TOKEN") # bảo mật bằng biến môi trường
|
68 |
+
# client = replicate.Client(api_token="REPLICATE_API_TOKEN")
|
69 |
+
|
70 |
+
# # 2. Gọi model
|
71 |
+
# output = client.run(
|
72 |
+
# "deepseek-ai/deepseek-math-7b-base:61f572dae0985541cdaeb4a114fd5d2d16cb40dac3894da10558992fc60547c7",
|
73 |
+
# input={
|
74 |
+
# "system_prompt": system_message,
|
75 |
+
# "user_prompt": user_message,
|
76 |
+
# "max_new_tokens": max_new_tokens,
|
77 |
+
# "temperature": temperature,
|
78 |
+
# "top_p": top_p
|
79 |
+
# }
|
80 |
+
# )
|
81 |
+
|
82 |
+
# # 3. Trả kết quả
|
83 |
+
# return output
|
test.py
CHANGED
@@ -62,3 +62,144 @@ demo = gr.ChatInterface(
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
###########################
|
69 |
+
|
70 |
+
# app.py
|
71 |
+
import gradio as gr # type: ignore
|
72 |
+
import os
|
73 |
+
|
74 |
+
# import openai # type: ignore
|
75 |
+
# # openai.api_key = os.getenv("OPENAI_API_KEY")
|
76 |
+
# client = openai.OpenAI()
|
77 |
+
# def respond(
|
78 |
+
# message,
|
79 |
+
# history: list[tuple[str, str]],
|
80 |
+
# system_message,
|
81 |
+
# max_tokens,
|
82 |
+
# temperature,
|
83 |
+
# top_p,
|
84 |
+
# image_uploaded,
|
85 |
+
# file_uploaded
|
86 |
+
# ):
|
87 |
+
|
88 |
+
# #read system message
|
89 |
+
# messages = [{"role": "system", "content": system_message}]
|
90 |
+
|
91 |
+
# #read history
|
92 |
+
# for val in history:
|
93 |
+
# if val[0]:
|
94 |
+
# messages.append({"role": "user", "content": val[0]})
|
95 |
+
# if val[1]:
|
96 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
97 |
+
|
98 |
+
# #read output
|
99 |
+
# messages.append({"role": "user", "content": message})
|
100 |
+
# print("## Messages: \n", messages) #debug output
|
101 |
+
|
102 |
+
# #create output
|
103 |
+
# response = client.responses.create(
|
104 |
+
# model="gpt-4.1-nano",
|
105 |
+
# input=messages,
|
106 |
+
# temperature=temperature,
|
107 |
+
# top_p=top_p,
|
108 |
+
# max_output_tokens=max_tokens
|
109 |
+
# )
|
110 |
+
|
111 |
+
# #read output
|
112 |
+
# response = response.output_text
|
113 |
+
# print("## Response: ", response) #debug output
|
114 |
+
# print("\n")
|
115 |
+
# yield response #chat reply
|
116 |
+
|
117 |
+
# import torch
|
118 |
+
# from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
119 |
+
# model_name = "deepseek-ai/deepseek-math-7b-base"
|
120 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_name)
|
121 |
+
# model = AutoModelForCausalLM.from_pretrained(model_name)
|
122 |
+
# # model.generation_config = GenerationConfig.from_pretrained(model_name)
|
123 |
+
# # model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
124 |
+
# def deepseek(
|
125 |
+
# message,
|
126 |
+
# history: list[tuple[str, str]],
|
127 |
+
# system_message,
|
128 |
+
# max_tokens,
|
129 |
+
# temperature,
|
130 |
+
# top_p):
|
131 |
+
|
132 |
+
|
133 |
+
# # messages = [
|
134 |
+
# # {"role": "user", "content": "what is the integral of x^2 from 0 to 2?\nPlease reason step by step, and put your final answer within \\boxed{}."}
|
135 |
+
# # ]
|
136 |
+
# messages = [
|
137 |
+
# {"role": "user", "content": message}
|
138 |
+
# ]
|
139 |
+
|
140 |
+
# input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
141 |
+
# outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
|
142 |
+
# print(outputs)
|
143 |
+
# print("\n")
|
144 |
+
# result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
145 |
+
# print(result)
|
146 |
+
# return result
|
147 |
+
|
148 |
+
# import replicate
|
149 |
+
# def deepseek_api_replicate(
|
150 |
+
# user_message,
|
151 |
+
# history: list[tuple[str, str]],
|
152 |
+
# system_message,
|
153 |
+
# max_new_tokens,
|
154 |
+
# temperature,
|
155 |
+
# top_p):
|
156 |
+
# """
|
157 |
+
# Gọi DeepSeek Math trên Replicate và trả ngay kết quả.
|
158 |
+
|
159 |
+
# Trả về:
|
160 |
+
# str hoặc [bytes]: output model sinh ra
|
161 |
+
# """
|
162 |
+
# # 1. Khởi tạo client và xác thực
|
163 |
+
# # token = os.getenv("REPLICATE_API_TOKEN")
|
164 |
+
# # if not token:
|
165 |
+
# # raise RuntimeError("Missing REPLICATE_API_TOKEN") # bảo mật bằng biến môi trường
|
166 |
+
# client = replicate.Client(api_token="REPLICATE_API_TOKEN")
|
167 |
+
|
168 |
+
# # 2. Gọi model
|
169 |
+
# output = client.run(
|
170 |
+
# "deepseek-ai/deepseek-math-7b-base:61f572dae0985541cdaeb4a114fd5d2d16cb40dac3894da10558992fc60547c7",
|
171 |
+
# input={
|
172 |
+
# "system_prompt": system_message,
|
173 |
+
# "user_prompt": user_message,
|
174 |
+
# "max_new_tokens": max_new_tokens,
|
175 |
+
# "temperature": temperature,
|
176 |
+
# "top_p": top_p
|
177 |
+
# }
|
178 |
+
# )
|
179 |
+
|
180 |
+
# # 3. Trả kết quả
|
181 |
+
# return output
|
182 |
+
|
183 |
+
import call_api
|
184 |
+
|
185 |
+
|
186 |
+
chat = gr.ChatInterface(
|
187 |
+
call_api.respond, #chat
|
188 |
+
title="Trợ lý Học Tập AI",
|
189 |
+
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!",
|
190 |
+
additional_inputs=[
|
191 |
+
gr.Textbox("Bạn là một chatbot tiếng Việt thân thiện.", label="System message"),
|
192 |
+
gr.Slider(1, 2048, value=200, step=1, label="Max new tokens"),
|
193 |
+
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
|
194 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
195 |
+
# gr.Image(type="pil", label="Attach an image (optional)"),
|
196 |
+
# gr.File(label="Upload a file (optional)"),
|
197 |
+
],
|
198 |
+
examples=[
|
199 |
+
# Mỗi item: [message, system_message, max_tokens, temperature, top_p]
|
200 |
+
["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],
|
201 |
+
],
|
202 |
+
)
|
203 |
+
|
204 |
+
if __name__ == "__main__":
|
205 |
+
chat.launch()
|