This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_api.py
88 lines (72 loc) · 2.74 KB
/
model_api.py
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
"""For training a custom mlflow model for detector api access."""
import click
import mlflow
import mlflow.pyfunc
import pandas as pd
import pprint
from ableist_language_detector.detector import find_ableist_language
class MLflowLanguageModel(mlflow.pyfunc.PythonModel):
def __init__(self, func):
self.func = func
def predict(self, context, model_input):
properties = ["lemma", "text", "start", "end",
"alternative_verbs", "example"]
result = self.func(model_input['data'][0])
terms = {}
print(f"Found {len(result)} instances of ableist language.\n")
if len(result) > 0:
for i, ableist_term in enumerate(result):
print(
f"Match #{i+1}\n"
f"PHRASE: {ableist_term} | LEMMA: {ableist_term.lemma} | "
f"POSITION: {ableist_term.start}:{ableist_term.end} | "
f"ALTERNATIVES: {ableist_term.data.alternative_verbs} | "
f"EXAMPLE: {ableist_term.data.example}\n"
)
for ableist_term in result:
terms[str(ableist_term.start)] = dict()
if len(properties) > 0:
for p in properties:
try:
terms[str(ableist_term.start)][p] = str(getattr(ableist_term, p))
except AttributeError:
terms[str(ableist_term.start)][p] = str(getattr(ableist_term.data, p))
return terms
@click.command()
@click.option(
"--train_only",
"-t",
type=bool,
required=True,
default=True,
help="Option to only train model and without testing on local file.",
)
@click.option(
"--job_description_file",
"-j",
type=str,
required=False,
help="Path to file containing the job description text.",
)
def main(train_only, job_description_file):
model_path = "detector_model"
# Construct and save the model if one does not exist
try:
analyzer = MLflowLanguageModel(find_ableist_language)
mlflow.pyfunc.save_model(path=model_path, python_model=analyzer)
print("Generating new model in path {}".format(model_path))
except:
print("Existing model in path {}".format(model_path))
pass
if train_only is False:
# Load the model in `python_function` format
local_model = mlflow.pyfunc.load_model(model_uri=model_path)
with open(job_description_file, "r") as jd_file:
job_description_text = jd_file.read()
data = pd.DataFrame({'data': [job_description_text]})
local_output = local_model.predict(data)
pprint.pprint(local_output)
else:
pass
if __name__ == "__main__":
main()