-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
67 lines (56 loc) · 2.53 KB
/
app.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
from ner.components.model_architecture import XLMRobertaForTokenClassification
from ner.config.configurations import Configuration
from ner.exception.exception import CustomException
from ner.pipeline.train_pipeline import TrainPipeline
from typing import Any, Dict, List, ClassVar
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import uvicorn
import torch
import os
import sys
app = FastAPI()
class PredictPipeline:
def __init__(self, config):
self.predict_pipeline_config = config.get_model_predict_pipeline_config()
self.tokenizer = self.predict_pipeline_config.tokenizer
if len(os.listdir(self.predict_pipeline_config.output_dir)) == 0:
raise LookupError("Model not found : please Run Training Pipeline from pipeline/train_pipeline.py")
self.model = XLMRobertaForTokenClassification.from_pretrained(pretrained_model_name_or_path = self.predict_pipeline_config.output_dir)
def run_data_preparation(self, data: str):
try:
data = data.split()
input_ids = self.tokenizer(data, truncation=self.predict_pipeline_config.truncation,
is_split_into_words=self.predict_pipeline_config.is_split_into_words)
formatted_data = torch.tensor(input_ids["input_ids"]).reshape(-1, 1)
outputs = self.model(formatted_data).logits
predictions = torch.argmax(outputs, dim=-1)
pred_tags = [self.predict_pipeline_config.index2tag[i.item()] for i in predictions[1:-1]]
return pred_tags[1:-1]
except Exception as e:
raise CustomException(e, sys)
def run_pipeline(self, data):
predictions = self.run_data_preparation(data)
response = {
"Input_Data": data.split(),
"Tags": predictions
}
return response
@app.post("/train")
def train(request: Request):
try:
pipeline = TrainPipeline(Configuration())
pipeline.run_pipeline()
return JSONResponse(content="Training Completed", status_code=200)
except Exception as e:
return JSONResponse(content={"Error": str(e)}, status_code=500)
@app.post("/predict")
def predict(request: Request,data:str):
try:
pipeline = PredictPipeline(Configuration())
response = pipeline.run_pipeline(data)
return JSONResponse(content=response, status_code=200)
except Exception as e:
return JSONResponse(content={"Error": str(e)}, status_code=500)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)