thanglekdi commited on
Commit
859b0d0
·
1 Parent(s): 10549dd

test deepseek

Browse files
Files changed (2) hide show
  1. app.py +59 -30
  2. requirements.txt +4 -3
app.py CHANGED
@@ -1,8 +1,8 @@
1
  # app.py
2
  import gradio as gr # type: ignore
3
- # import openai # type: ignore
4
- # import os
5
 
 
6
  # # openai.api_key = os.getenv("OPENAI_API_KEY")
7
  # client = openai.OpenAI()
8
  # def respond(
@@ -45,39 +45,68 @@ import gradio as gr # type: ignore
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
  chat = gr.ChatInterface(
80
- deepseek, #chat
81
  title="Trợ lý Học Tập AI",
82
  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!",
83
  additional_inputs=[
 
1
  # app.py
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(
 
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(user_message, system_message, max_new_tokens, temperature, top_p):
81
+ """
82
+ Gọi DeepSeek Math trên Replicate và trả ngay kết quả.
83
+
84
+ Trả về:
85
+ str hoặc [bytes]: output model sinh ra
86
+ """
87
+ # 1. Khởi tạo client và xác thực
88
+ token = os.getenv("REPLICATE_API_TOKEN")
89
+ if not token:
90
+ raise RuntimeError("Missing REPLICATE_API_TOKEN") # bảo mật bằng biến môi trường
91
+ client = replicate.Client(api_token=token)
92
+
93
+ # 2. Gọi model
94
+ output = client.run(
95
+ "deepseek-ai/deepseek-math-7b-instruct:latest",
96
+ input={
97
+ "system_prompt": system_message,
98
+ "user_prompt": user_message,
99
+ "max_new_tokens": max_new_tokens,
100
+ "temperature": temperature,
101
+ "top_p": top_p
102
+ }
103
+ )
104
 
105
+ # 3. Trả kết quả
106
+ return output
 
 
 
 
 
107
 
108
  chat = gr.ChatInterface(
109
+ deepseek_api_replicate, #chat
110
  title="Trợ lý Học Tập AI",
111
  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!",
112
  additional_inputs=[
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
  gradio
2
  # openai
3
- torch
4
- transformers
5
- accelerate
 
 
1
  gradio
2
  # openai
3
+ replicate
4
+ # torch
5
+ # transformers
6
+ # accelerate