VertinYi commited on
Commit
141ffe3
·
verified ·
1 Parent(s): 3ca75f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -43
app.py CHANGED
@@ -1,43 +1,24 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
-
4
- # 加载 Hugging Face 上的预训练模型(以 DeepSeek 为例)
5
- from huggingface_hub import login
6
- from transformers import pipeline
7
-
8
- # 登录 Hugging Face
9
- login(token="your_huggingface_token")
10
-
11
- # 加载模型
12
- model_name = "deepseek-ai/deepseek-7b"
13
- pipe = pipeline("text-generation", model=model_name, tokenizer=model_name)
14
-
15
-
16
- # 定义与模型对话的函数
17
- def chat_with_ai(prompt):
18
- # 使用模型生成文本
19
- response = pipe(prompt, max_length=100, do_sample=True)
20
- return response[0]["generated_text"]
21
-
22
- # 创建 Gradio 界面
23
- with gr.Blocks() as demo:
24
- gr.Markdown("# 🤖 AI Chatbot powered by DeepSeek")
25
-
26
- # 聊天框组件
27
- chatbot = gr.Chatbot()
28
- msg = gr.Textbox(label="Type your message:")
29
- clear = gr.Button("Clear")
30
-
31
- # 定义响应函数,处理用户输入并更新聊天历史
32
- def respond(message, chat_history):
33
- response = chat_with_ai(message)
34
- chat_history.append((message, response)) # 将对话记录添加到聊天历史
35
- return "", chat_history
36
-
37
- # 提交消息并更新聊天记录
38
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
39
- # 清空聊天记录
40
- clear.click(lambda: [], None, chatbot)
41
-
42
- # 启动 Gradio 界面
43
- demo.launch()
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
3
+
4
+ # 加载 DeepSeekMath 模型和分词器
5
+ model_name = "deepseek-ai/deepseek-math-7b-instruct"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
8
+ model.generation_config = GenerationConfig.from_pretrained(model_name)
9
+ model.generation_config.pad_token_id = model.generation_config.eos_token_id
10
+
11
+ # 定义带有链式推理的数学问题
12
+ messages = [
13
+ {"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{}."}
14
+ ]
15
+
16
+ # 将问题转换为模型输入格式
17
+ input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
18
+
19
+ # 生成模型的输出
20
+ outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
21
+
22
+ # 解码输出并打印结果
23
+ result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
24
+ print(result)