Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# 修改为你上传的模型文件夹路径
|
| 6 |
+
model_path = "./my-deepseek"
|
| 7 |
+
|
| 8 |
+
# 加载模型和分词器
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
model_path,
|
| 12 |
+
trust_remote_code=True,
|
| 13 |
+
torch_dtype=torch.float32 # 若使用 GPU,可改为 torch.float16
|
| 14 |
+
)
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
def chat(prompt, max_new_tokens=512):
|
| 18 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model.generate(
|
| 21 |
+
**inputs,
|
| 22 |
+
max_new_tokens=max_new_tokens,
|
| 23 |
+
do_sample=True,
|
| 24 |
+
top_p=0.95,
|
| 25 |
+
temperature=0.8,
|
| 26 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 27 |
+
)
|
| 28 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
+
return response[len(prompt):] # 返回去除原始prompt后的回答部分
|
| 30 |
+
|
| 31 |
+
# 创建 Gradio 界面
|
| 32 |
+
gr.Interface(
|
| 33 |
+
fn=chat,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Textbox(label="你的提问", lines=3, placeholder="请输入 prompt..."),
|
| 36 |
+
gr.Slider(128, 1024, step=64, value=512, label="最大生成长度")
|
| 37 |
+
],
|
| 38 |
+
outputs="text",
|
| 39 |
+
title="🧠 DeepSeek-R1 Chat Demo",
|
| 40 |
+
description="使用你本地上传的 DeepSeek-R1 模型运行的聊天机器人。"
|
| 41 |
+
).launch()
|