File size: 3,154 Bytes
74aa6b2 1a4c139 74aa6b2 1a4c139 74aa6b2 f577279 1a4c139 f577279 1a4c139 f577279 74aa6b2 f577279 74aa6b2 7733ce5 74aa6b2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
from allennlp.models.archival import archive_model, load_archive
from allennlp.predictors.text_classifier import TextClassifierPredictor
import gradio as gr
import platform
from project_settings import project_path
from toolbox.allennlp.data.dataset_readers.text_classification_json import TextClassificationJsonReader
from toolbox.os.command import Command
def get_args():
parser = argparse.ArgumentParser()
args = parser.parse_args()
return args
model_names = {
"allennlp_text_classification": {
"qgyd2021/language_identification": "https://huggingface.co/qgyd2021/language_identification"
}
}
trained_model_dir = project_path / "trained_models/huggingface"
trained_model_dir.mkdir(parents=True, exist_ok=True)
def click_button_allennlp_text_classification(text: str, model_name: str):
model_path = trained_model_dir / model_name
if not model_path.exists():
model_path.parent.mkdir(exist_ok=True)
Command.cd(model_path.parent.as_posix())
Command.popen("git clone https://huggingface.co/{}".format(model_name))
archive = load_archive(archive_file=model_path.as_posix())
predictor = TextClassifierPredictor(
model=archive.model,
dataset_reader=archive.dataset_reader,
)
json_dict = {
"sentence": text
}
outputs = predictor.predict_json(
json_dict
)
label = outputs["label"]
probs = outputs["probs"]
return label, round(max(probs), 4)
def main():
args = get_args()
brief_description = """
## NLP Tools
NLP Tools Demo
"""
# ui
with gr.Blocks() as blocks:
gr.Markdown(value=brief_description)
with gr.Tabs():
with gr.TabItem("AllenNLP Text Classification"):
with gr.Row():
with gr.Column(scale=3):
text = gr.Text(label="text")
ground_true = gr.Text(label="ground_true")
model_name = gr.Dropdown(
choices=list(model_names["allennlp_text_classification"].keys())
)
button = gr.Button("infer", variant="primary")
with gr.Column(scale=3):
label = gr.Text(label="label")
prob = gr.Text(label="prob")
gr.Examples(
examples=[
["你好", "zh", "qgyd2021/language_identification"]
],
inputs=[text, ground_true, model_name],
outputs=[label, prob],
)
button.click(
click_button_allennlp_text_classification,
inputs=[text, model_name],
outputs=[label, prob]
)
blocks.queue().launch(
share=False if platform.system() == "Windows" else False,
server_name="127.0.0.1" if platform.system() == "Windows" else "0.0.0.0",
server_port=7860
)
return
if __name__ == '__main__':
main()
|