Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,50 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
|
| 4 |
-
|
| 5 |
-
model_name = "Helsinki-NLP/opus-mt-en-zh"
|
| 6 |
-
model = MarianMTModel.from_pretrained(model_name).to("cpu") # 强制使用 CPU
|
| 7 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
demo.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
model_name = "deepseek-ai/deepseek-llm-7b-chat"
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# 加载 tokenizer 和模型
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_name,
|
| 11 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 12 |
+
device_map="auto" if torch.cuda.is_available() else None
|
| 13 |
+
)
|
| 14 |
+
model.generation_config = GenerationConfig.from_pretrained(model_name)
|
| 15 |
+
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
| 16 |
|
| 17 |
+
# 推理函数
|
| 18 |
+
def chat(prompt, max_new_tokens=256, temperature=0.7, top_p=0.9):
|
| 19 |
+
messages = [{"role": "user", "content": prompt}]
|
| 20 |
+
input_tensor = tokenizer.apply_chat_template(
|
| 21 |
+
messages, add_generation_prompt=True, return_tensors="pt"
|
| 22 |
+
).to(model.device)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model.generate(
|
| 25 |
+
input_tensor,
|
| 26 |
+
max_new_tokens=max_new_tokens,
|
| 27 |
+
temperature=temperature,
|
| 28 |
+
top_p=top_p,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
eos_token_id=tokenizer.eos_token_id
|
| 31 |
+
)
|
| 32 |
+
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
|
| 33 |
+
return result
|
| 34 |
|
| 35 |
+
# Gradio 界面
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=chat,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Textbox(lines=4, label="请输入您的问题"),
|
| 40 |
+
gr.Slider(32, 1024, step=16, value=256, label="最大生成长度"),
|
| 41 |
+
gr.Slider(0.1, 1.5, step=0.1, value=0.7, label="Temperature"),
|
| 42 |
+
gr.Slider(0.1, 1.0, step=0.05, value=0.9, label="Top-p")
|
| 43 |
+
],
|
| 44 |
+
outputs=gr.Textbox(label="模型回复"),
|
| 45 |
+
title="DeepSeek LLM 7B Chat 演示",
|
| 46 |
+
description="基于 Hugging Face Spaces 的部署示例"
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
iface.launch()
|
|
|