19 lines
621 B
Python
19 lines
621 B
Python
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||
|
|
import torch
|
||
|
|
|
||
|
|
# 加载模型和分词器
|
||
|
|
model_path = "path/to/your/model" # ModelScope 会自动设置路径
|
||
|
|
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
||
|
|
model = AutoModelForCausalLM.from_pretrained(model_path)
|
||
|
|
|
||
|
|
def inference(input_text):
|
||
|
|
inputs = tokenizer(input_text, return_tensors="pt")
|
||
|
|
outputs = model.generate(**inputs, max_length=50)
|
||
|
|
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
||
|
|
|
||
|
|
# 示例输入
|
||
|
|
if __name__ == "__main__":
|
||
|
|
input_text = "河北旅游推荐"
|
||
|
|
result = inference(input_text)
|
||
|
|
print(result)
|