-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (121 loc) · 4.97 KB
/
main.py
File metadata and controls
144 lines (121 loc) · 4.97 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#-------------------------------------------------------------------------------
import uvicorn
from fastapi.openapi.utils import get_openapi
import json
from aiofile import async_open
from fastapi.openapi.utils import get_openapi
from fastapi import FastAPI, File, UploadFile,Depends, Form
from application import predict, read_imagefile, explain_lime#,read_model
from application import ShapModelExplainer
from application import OcclusionSensitityModelExplainer
from application import get_ClassName
ShapExplainer = ShapModelExplainer()
OcclusionExplainer = OcclusionSensitityModelExplainer()
####################FOR REQUEST BODY####################
from pydantic import BaseModel
description = """
xAI Microservices APIs helps you to understand the internal model structure and provide you explanation.
## Image Class Prediction Service
You can just pass an image to the Predict API and get prediction back as JSON
## LIME and SHAP Explainability Services
Just pass your image to the LIME Microservice and this service provide you the results in JSON
## Occlusion Sensitivity Explainability Service
* *Send Image True Label** (_cardboard,glass,metal,paper,plastic,trash_).
"""
# def create_application() -> FastAPI:
# application = FastAPI(openapi_url="/building/openapi.json", docs_url="/building/docs")
# application.include_router(create_building.router, prefix="/building", tags=["building"])
# application.include_router(modify_building.router, prefix="/building", tags=["building"]
# application.include_router(get_building.router, prefix="/building", tags=["building"])
# application.include_router(get_buildings.router, prefix="/building", tags=["building"])
# application.include_router(remove_building.router, prefix="/building", tags=["building"])
# application.include_router(assign_users_to_building.router, prefix="/building", tags=["building"])
# return application
# app = create_application()
app = FastAPI(
openapi_url="/building/openapi.json",
docs_url="/building/docs",
title="XAI Microservices",
description=description,
version="0.0.1",
terms_of_service="https://dps.cs.ut.ee/index.html",
contact={
"name": "Mehrdad Asadi, Ph.D.",
"url": "https://dps.cs.ut.ee/people.html",
"email": "mehrdad.asadi@ut.ee",
},
license_info={
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
},
servers=[{"url":"http://192.168.42.139"}],
# routes=app.routes,
)
#app = FastAPI()
#def my_schema():
# if app.openapi_schema:
# return app.openapi_schema
# openapi_schema = get_openapi(
# description="""testing""",
# title="XAI Microservices",
# terms_of_service="https://dps.cs.ut.ee/index.html",
# version="0.0.1",
# servers=[{"url": "http://192.168.42.93"}],
# routes=app.routes,
# )
# app.openapi_schema = openapi_schema
# return app.openapi_schema
#app.openapi = my_schema
#with open('openai.json', 'w') as f:
# json.dump(app.openapi(), f)
@app.get("/test")
def read_root():
print("GET api is running")
return {"Hello": "World"}
@app.post("/predict/image")
async def predict_api(file: UploadFile = File(...)):
print("POST api is running after asyncdef")
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png")
if not extension:
return "Image must be jpg or png format!"
image = read_imagefile(await file.read())
prediction = predict(image)
return prediction
@app.post("/explain_lime/image")
async def explain_api(file: UploadFile = File(...)):
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png")
if not extension:
return "Image must be jpg or png format!"
image = read_imagefile(await file.read())
explaination = explain_lime(image)
return explaination
@app.post("/explain_shap/image")
async def explain_api(file: UploadFile = File(...)):
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png")
if not extension:
return "Image must be jpg or png format!"
image = read_imagefile(await file.read())
explaination = ShapExplainer.explain_shap(image)
return explaination
class ClassLabel(BaseModel):
imagetype: str
@app.post("/explain_occlusion/image")
async def explain_api(file: UploadFile = File(...), base:ClassLabel = Depends()):
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png")
input = base.dict()
var = input['imagetype']
classNum = get_ClassName(var)
####################### FIND CLASS ########################
if not extension:
return "Image must be jpg or png format!"
label_number = get_ClassName(var)
# var1 = 4
image = read_imagefile(await file.read())
explaination = OcclusionExplainer.explain_occlusion(image,classNum)
return explaination
#print(app.openapi())
#schema = app.openapi()
with open('openapi.json', 'w') as f:
json.dump(app.openapi(), f)
if __name__ == "__main__":
uvicorn.run(app, port=8080)