File size: 6,888 Bytes
3afc718
 
 
 
 
 
 
 
 
d86ac9a
3afc718
 
 
 
 
 
 
d86ac9a
3afc718
d86ac9a
 
 
 
 
3afc718
d86ac9a
 
3afc718
 
 
 
 
 
d86ac9a
 
3afc718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d86ac9a
 
3afc718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# import gradio as gr # type: ignore
# from huggingface_hub import InferenceClient # type: ignore

# """
# For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
# """
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")


# def respond(
#     message,
#     history: list[tuple[str, str]],
#     system_message,
#     max_tokens,
#     temperature,
#     top_p,
# ):
#     messages = [{"role": "system", "content": system_message}]

#     for val in history:
#         if val[0]:
#             messages.append({"role": "user", "content": val[0]})
#         if val[1]:
#             messages.append({"role": "assistant", "content": val[1]})

#     messages.append({"role": "user", "content": message})

#     response = ""

#     for message in client.chat_completion(
#         messages,
#         max_tokens=max_tokens,
#         stream=True,
#         temperature=temperature,
#         top_p=top_p,
#     ):
#         token = message.choices[0].delta.content

#         response += token
#         yield response


# """
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
# """
# demo = gr.ChatInterface(
#     respond,
#     additional_inputs=[
#         gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
#         gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
#         gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
#         gr.Slider(
#             minimum=0.1,
#             maximum=1.0,
#             value=0.95,
#             step=0.05,
#             label="Top-p (nucleus sampling)",
#         ),
#     ],
# )


# if __name__ == "__main__":
#     demo.launch()



# ###########################

# # app.py
# import gradio as gr # type: ignore
# import os

# # import openai # type: ignore
# # # openai.api_key = os.getenv("OPENAI_API_KEY")
# # client = openai.OpenAI()
# # def respond(
# #             message, 
# #             history: list[tuple[str, str]], 
# #             system_message, 
# #             max_tokens, 
# #             temperature, 
# #             top_p, 
# #             image_uploaded,
# #             file_uploaded
# #     ):
    
# #     #read system message
# #     messages = [{"role": "system", "content": system_message}]
    
# #     #read history
# #     for val in history:
# #         if val[0]:
# #             messages.append({"role": "user", "content": val[0]})
# #         if val[1]:
# #             messages.append({"role": "assistant", "content": val[1]})
    
# #     #read output
# #     messages.append({"role": "user", "content": message})
# #     print("## Messages: \n", messages) #debug output

# #     #create output
# #     response = client.responses.create(
# #         model="gpt-4.1-nano", 
# #         input=messages,
# #         temperature=temperature,
# #         top_p=top_p,
# #         max_output_tokens=max_tokens
# #     )

# #     #read output
# #     response = response.output_text
# #     print("## Response: ", response) #debug output
# #     print("\n")
# #     yield response #chat reply

# # import torch
# # from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
# # model_name = "deepseek-ai/deepseek-math-7b-base"
# # tokenizer = AutoTokenizer.from_pretrained(model_name)
# # model = AutoModelForCausalLM.from_pretrained(model_name)
# # # model.generation_config = GenerationConfig.from_pretrained(model_name)
# # # model.generation_config.pad_token_id = model.generation_config.eos_token_id
# # def deepseek(
# #             message, 
# #             history: list[tuple[str, str]], 
# #             system_message, 
# #             max_tokens, 
# #             temperature, 
# #             top_p):
    
    
# #     # messages = [
# #     #     {"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{}."}
# #     # ]
# #     messages = [
# #         {"role": "user", "content": message}
# #     ]

# #     input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
# #     outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
# #     print(outputs)
# #     print("\n")
# #     result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
# #     print(result)
# #     return result

# # import replicate
# # def deepseek_api_replicate(
# #             user_message, 
# #             history: list[tuple[str, str]], 
# #             system_message, 
# #             max_new_tokens, 
# #             temperature, 
# #             top_p):
# #     """
# #     Gọi DeepSeek Math trên Replicate và trả ngay kết quả.

# #     Trả về:
# #         str hoặc [bytes]: output model sinh ra
# #     """
# #     # 1. Khởi tạo client và xác thực
# #     # token = os.getenv("REPLICATE_API_TOKEN")
# #     # if not token:
# #     #     raise RuntimeError("Missing REPLICATE_API_TOKEN")  # bảo mật bằng biến môi trường
# #     client = replicate.Client(api_token="REPLICATE_API_TOKEN")          

# #     # 2. Gọi model
# #     output = client.run(
# #         "deepseek-ai/deepseek-math-7b-base:61f572dae0985541cdaeb4a114fd5d2d16cb40dac3894da10558992fc60547c7",
# #         input={
# #             "system_prompt": system_message,
# #             "user_prompt":   user_message,
# #             "max_new_tokens": max_new_tokens,
# #             "temperature":    temperature,
# #             "top_p":          top_p
# #         }
# #     )

# #     # 3. Trả kết quả
# #     return output

# import call_api


# chat = gr.ChatInterface(
#     call_api.respond, #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(type="pil", label="Attach an image (optional)"),
#         # gr.File(label="Upload a file (optional)"),
#     ],
#     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],
#     ],
# )

# if __name__ == "__main__":
#     chat.launch()