Update README.md
Browse files
README.md
CHANGED
|
@@ -11,4 +11,70 @@ metrics:
|
|
| 11 |
---
|
| 12 |
## Language Identification
|
| 13 |
|
| 14 |
-
该模型是基于 AllenNLP 在 [qgyd2021/language_identification](https://huggingface.co/datasets/qgyd2021/language_identification) 数据集上训练的语种识别模型。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
## Language Identification
|
| 13 |
|
| 14 |
+
该模型是基于 AllenNLP 在 [qgyd2021/language_identification](https://huggingface.co/datasets/qgyd2021/language_identification) 数据集上训练的语种识别模型。
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
测试代码:
|
| 18 |
+
```python
|
| 19 |
+
#!/usr/bin/python3
|
| 20 |
+
# -*- coding: utf-8 -*-
|
| 21 |
+
import argparse
|
| 22 |
+
import time
|
| 23 |
+
|
| 24 |
+
from allennlp.models.archival import archive_model, load_archive
|
| 25 |
+
from allennlp.predictors.text_classifier import TextClassifierPredictor
|
| 26 |
+
|
| 27 |
+
from project_settings import project_path
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_args():
|
| 31 |
+
"""
|
| 32 |
+
python3 step_5_predict_by_archive.py
|
| 33 |
+
:return:
|
| 34 |
+
"""
|
| 35 |
+
parser = argparse.ArgumentParser()
|
| 36 |
+
parser.add_argument(
|
| 37 |
+
"--text",
|
| 38 |
+
default="hello guy.",
|
| 39 |
+
type=str
|
| 40 |
+
)
|
| 41 |
+
parser.add_argument(
|
| 42 |
+
"--archive_file",
|
| 43 |
+
default=(project_path / "trained_models/language_identification").as_posix(),
|
| 44 |
+
type=str
|
| 45 |
+
)
|
| 46 |
+
args = parser.parse_args()
|
| 47 |
+
return args
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def main():
|
| 51 |
+
args = get_args()
|
| 52 |
+
|
| 53 |
+
archive = load_archive(archive_file=args.archive_file)
|
| 54 |
+
|
| 55 |
+
predictor = TextClassifierPredictor(
|
| 56 |
+
model=archive.model,
|
| 57 |
+
dataset_reader=archive.dataset_reader,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
json_dict = {
|
| 61 |
+
"sentence": args.text
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
begin_time = time.time()
|
| 65 |
+
outputs = predictor.predict_json(
|
| 66 |
+
json_dict
|
| 67 |
+
)
|
| 68 |
+
label = outputs["label"]
|
| 69 |
+
prob = round(max(outputs["probs"]), 4)
|
| 70 |
+
print(label)
|
| 71 |
+
print(prob)
|
| 72 |
+
|
| 73 |
+
print('time cost: {}'.format(time.time() - begin_time))
|
| 74 |
+
return
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == '__main__':
|
| 78 |
+
main()
|
| 79 |
+
|
| 80 |
+
```
|