Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from optimum.onnxruntime import ORTModelForSequenceClassification
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# 转换 onnx 模型
|
| 6 |
+
def convert(path, onnx_path, onnx_path):
|
| 7 |
+
onnx_model = ORTModelForSequenceClassification.from_pretrained(path, from_transformers=True)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(path)
|
| 9 |
+
|
| 10 |
+
onnx_model.save_pretrained(onnx_path)
|
| 11 |
+
tokenizer.save_pretrained(onnx_path)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# 加载模型,用pipeline包装
|
| 15 |
+
def load_model(model_name):
|
| 16 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
+
text_classification_pipeline = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 19 |
+
print(text_classification_pipeline('这是一个简单的demo,用来防止忘记'))
|
| 20 |
+
return text_classification_pipeline
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# 加载 onnx 模型,用pipeline包装
|
| 24 |
+
def load_onnx_model(onnx_path):
|
| 25 |
+
lang_tokenizer = AutoTokenizer.from_pretrained(onnx_path)
|
| 26 |
+
lang_model = ORTModelForSequenceClassification.from_pretrained(onnx_path)
|
| 27 |
+
lang_detecter = pipeline("text-classification", model=lang_model, tokenizer=lang_tokenizer, truncation=True)
|
| 28 |
+
print(lang_detecter('这是一个简单的demo,用来防止忘记'))
|
| 29 |
+
return lang_detecter
|