ag added service API using docker and fastAPI
This commit is contained in:
1 parent
759f4c50d0
commit
08c5f672be
16 files changed
+3450
-264
No files matched your search
@@ -0,0 +1,18 @@
|
||||
|
||||
#
|
||||
FROM python:3.9
|
||||
|
||||
#
|
||||
WORKDIR /code
|
||||
|
||||
#
|
||||
COPY ./requirements.txt /code/requirements.txt
|
||||
|
||||
#
|
||||
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
||||
|
||||
#
|
||||
COPY ./app /code/app
|
||||
|
||||
#
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
|
||||
Whitespace-only changes.
@@ -0,0 +1,27 @@
|
||||
from typing import Union
|
||||
from .predict import predict
|
||||
from fastapi import FastAPI
|
||||
import numpy as np
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"Hello": "World"}
|
||||
|
||||
|
||||
@app.get("/predict/{data}")
|
||||
def read_item(data: str):
|
||||
final_json = {'probabilities':{}}
|
||||
|
||||
try:
|
||||
res = predict(data)
|
||||
for i in range(5):
|
||||
final_json['probabilities'][f"india_{i}"] = round(float(res[i]),2)
|
||||
final_json['success'] = True
|
||||
except Exception as e:
|
||||
final_json['success'] = False
|
||||
final_json['error'] = repr(e)
|
||||
|
||||
|
||||
return final_json
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
{"learner":{"attributes":{"best_iteration":"516","best_score":"0.6680812335220442"},"feature_names":["age","diagnosis","injury_general_location","destination","country","difficulty","cause","equipment","injury_side","helicopter","akja","ambulance","snowmobile_sled","snowmobile","helmet","indipendently","ski_lift","gender"],"feature_types":["float","c","c","c","c","c","c","c","c","c","c","c","c","c","c","c","c","c"],"gradient_booster":{"model":{"gbtree_model_param":{"num_parallel_tree":"1","num_trees":"3085"},"iteration_indptr":[0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,205,210,215,220,225,230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,310,315,320,325,330,335,340,345,350,355,360,365,370,375,380,385,390,395,400,405,410,415,420,425,430,435,440,445,450,455,460,465,470,475,480,485,490,495,500,505,510,515,520,525,530,535,540,545,550,555,560,565,570,575,580,585,590,595,600,605,610,615,620,625,630,635,640,645,650,655,660,665,670,675,680,685,690,695,700,705,710,715,720,725,730,735,740,745,750,755,760,765,770,775,780,785,790,795,800,805,810,815,820,825,830,835,840,845,850,855,860,865,870,875,880,885,890,895,900,905,910,915,920,925,930,935,940,945,950,955,960,965,970,975,980,985,990,995,1000,1005,1010,1015,1020,1025,1030,1035,1040,1045,1050,1055,1060,1065,1070,1075,1080,1085,1090,1095,1100,1105,1110,1115,1120,1125,1130,1135,1140,1145,1150,1155,1160,1165,1170,1175,1180,1185,1190,1195,1200,1205,1210,1215,1220,1225,1230,1235,1240,1245,1250,1255,1260,1265,1270,1275,1280,1285,1290,1295,1300,1305,1310,1315,1320,1325,1330,1335,1340,1345,1350,1355,1360,1365,1370,1375,1380,1385,1390,1395,1400,1405,1410,1415,1420,1425,1430,1435,1440,1445,1450,1455,1460,1465,1470,1475,1480,1485,1490,1495,1500,1505,1510,1515,1520,1525,1530,1535,1540,1545,1550,1555,1560,1565,1570,1575,1580,1585,1590,1595,1600,1605,1610,1615,1620,1625,1630,1635,1640,1645,1650,1655,1660,1665,1670,1675,1680,1685,1690,1695,1700,1705,1710,1715,1720,1725,1730,1735,1740,1745,1750,1755,1760,1765,1770,1775,1780,1785,1790,1795,1800,1805,1810,1815,1820,1825,1830,1835,1840,1845,1850,1855,1860,1865,1870,1875,1880,1885,1890,1895,1900,1905,1910,1915,1920,1925,1930,1935,1940,1945,1950,1955,1960,1965,1970,1975,1980,1985,1990,1995,2000,2005,2010,2015,2020,2025,2030,2035,2040,2045,2050,2055,2060,2065,2070,2075,2080,2085,2090,2095,2100,2105,2110,2115,2120,2125,2130,2135,2140,2145,2150,2155,2160,2165,2170,2175,2180,2185,2190,2195,2200,2205,2210,2215,2220,2225,2230,2235,2240,2245,2250,2255,2260,2265,2270,2275,2280,2285,2290,2295,2300,2305,2310,2315,2320,2325,2330,2335,2340,2345,2350,2355,2360,2365,2370,2375,2380,2385,2390,2395,2400,2405,2410,2415,2420,2425,2430,2435,2440,2445,2450,2455,2460,2465,2470,2475,2480,2485,2490,2495,2500,2505,2510,2515,2520,2525,2530,2535,2540,2545,2550,2555,2560,2565,2570,2575,2580,2585,2590,2595,2600,2605,2610,2615,2620,2625,2630,2635,2640,2645,2650,2655,2660,2665,2670,2675,2680,2685,2690,2695,2700,2705,2710,2715,2720,2725,2730,2735,2740,2745,2750,2755,2760,2765,2770,2775,2780,2785,2790,2795,2800,2805,2810,2815,2820,2825,2830,2835,2840,2845,2850,2855,2860,2865,2870,2875,2880,2885,2890,2895,2900,2905,2910,2915,2920,2925,2930,2935,2940,2945,2950,2955,2960,2965,2970,2975,2980,2985,2990,2995,3000,3005,3010,3015,3020,3025,3030,3035,3040,3045,3050,3055,3060,3065,3070,3075,3080,3085],"tree_info":[0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,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2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1Line truncated
|
||||
@@ -0,0 +1,60 @@
|
||||
|
||||
|
||||
import json
|
||||
import xgboost as xgb
|
||||
import pickle
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
def predict(json_input:str):
|
||||
|
||||
with open('app/metadata.pkl','rb') as f:
|
||||
to_remove,use_small,evacuations,encoders = pickle.load(f)
|
||||
|
||||
|
||||
data = pd.DataFrame(json.loads(json_input[1:-1]),index=[0])
|
||||
|
||||
data.drop(columns=['dateandtime','skiarea_id','day_of_year','minute_of_day','year'], inplace=True, errors='ignore')
|
||||
|
||||
##evacuation_vehicles must be explicitated and workaround for other!
|
||||
evacuation = data.evacuation_vehicles.values[0]
|
||||
if isinstance(evacuation,str):
|
||||
evacuation = [evacuation]
|
||||
for c in evacuations:
|
||||
data[c] = False
|
||||
for c in evacuation:
|
||||
data[c] = True
|
||||
|
||||
for c in evacuation:
|
||||
if c not in evacuations:
|
||||
data['other'] = True
|
||||
break
|
||||
|
||||
data.drop(columns=['town','province','evacuation_vehicles'],inplace=True, errors='ignore')
|
||||
|
||||
|
||||
data['age'] = data['age'].astype(np.float32)
|
||||
|
||||
|
||||
|
||||
for c in data.columns:
|
||||
if c not in ['india','age','season','skiarea_name','destination']:
|
||||
data[c] = data[c].astype('str')
|
||||
if use_small:
|
||||
for c in to_remove.keys():
|
||||
for k in to_remove[c]:
|
||||
data.loc[data[c]==k,c] = 'other'
|
||||
for c in data.columns:
|
||||
if c not in ['age','season','skiarea_name','india']:
|
||||
data[c] = data[c].fillna('None')
|
||||
if use_small:
|
||||
data[c] = pd.Categorical( encoders['small'][c].transform(data[c]), categories=list(range(len(encoders['small'][c].classes_))), ordered=False)
|
||||
else:
|
||||
data[c] = pd.Categorical( encoders['normal'][c].transform(data[c]), categories=list(range(len(encoders['normal'][c].classes_))), ordered=False)
|
||||
|
||||
bst_FS = xgb.Booster()
|
||||
bst_FS.load_model("app/model.json")
|
||||
|
||||
dtest_FS = xgb.DMatrix(data[bst_FS.feature_names],enable_categorical=True)
|
||||
preds = bst_FS.predict(dtest_FS)
|
||||
|
||||
return preds[0]*100
|
||||
@@ -0,0 +1,31 @@
|
||||
annotated-types==0.6.0
|
||||
anyio==4.3.0
|
||||
click==8.1.7
|
||||
fastapi==0.110.0
|
||||
h11==0.14.0
|
||||
httptools==0.6.1
|
||||
idna==3.6
|
||||
joblib==1.3.2
|
||||
numpy==1.26.4
|
||||
pandas==2.2.1
|
||||
pydantic==2.6.4
|
||||
pydantic_core==2.16.3
|
||||
python-dateutil==2.9.0.post0
|
||||
python-dotenv==1.0.1
|
||||
pytz==2024.1
|
||||
PyYAML==6.0.1
|
||||
scikit-learn==1.4.1.post1
|
||||
scipy==1.12.0
|
||||
setuptools==68.2.2
|
||||
six==1.16.0
|
||||
sniffio==1.3.1
|
||||
starlette==0.36.3
|
||||
threadpoolctl==3.3.0
|
||||
typing_extensions==4.10.0
|
||||
tzdata==2024.1
|
||||
uvicorn==0.28.0
|
||||
uvloop==0.19.0
|
||||
watchfiles==0.21.0
|
||||
websockets==12.0
|
||||
wheel==0.41.2
|
||||
xgboost==2.0.3
|
||||
Reference in new issue
Block a user