albin commited on
Commit
a87b3ec
·
1 Parent(s): aab2638

add all files

Browse files
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ RUN useradd -m -u 1000 user
10
+
11
+ USER user
12
+
13
+ ENV HOME=/home/user \
14
+ PATH=/home/user/.local/bin:$PATH
15
+
16
+ WORKDIR $HOME/app
17
+
18
+ COPY --chown=user . $HOME/app
19
+
20
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
LICENSE.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Himesh Samarasekera
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
Procfile ADDED
@@ -0,0 +1 @@
 
 
1
+ web: uvicorn main:app --host 0.0.0.0 --port $PORT
__pycache__/main.cpython-310.pyc ADDED
Binary file (3.8 kB). View file
 
insurance_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ce5baecaec7aa0475bc4f500227df4d42dadaa242c2c95ac1a930b8281b755d1
3
+ size 633
main.cpython-310.pyc ADDED
Binary file (3.8 kB). View file
 
main.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # encoding: utf-8
3
+
4
+ from fastapi import FastAPI, Form, Depends, Request
5
+ from fastapi.encoders import jsonable_encoder
6
+ from fastapi.responses import JSONResponse
7
+ from fastapi.middleware.cors import CORSMiddleware
8
+ from pydantic import BaseModel
9
+ import pickle
10
+
11
+ app = FastAPI()
12
+
13
+ # Add CORS middleware
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"], # Replace with the list of allowed origins for production
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+ model_file = open('insurance_model.pkl', 'rb')
23
+ model = pickle.load(model_file, encoding='bytes')
24
+
25
+
26
+ class Msg(BaseModel):
27
+ msg: str
28
+
29
+
30
+ class Req(BaseModel):
31
+ age: int
32
+ sex: int
33
+ smoker: int
34
+ bmi: float
35
+ children: int
36
+ region: int
37
+
38
+ class Resp(BaseModel):
39
+ age: int
40
+ sex: str
41
+ smoker: str
42
+ bmi: float
43
+ children: int
44
+ region: str
45
+ insurance_cost: float
46
+
47
+
48
+
49
+ @app.get("/")
50
+ async def root():
51
+ return {"message": "Hello World. Welcome to FastAPI!"}
52
+
53
+ def form_req(age: str = Form(...), sex: str = Form(...), smoker: str = Form(...),
54
+ bmi: str = Form(...), children: str = Form(...), region: str = Form(...)):
55
+ sBmi = bmi.replace(",", ".")
56
+ return Req(age=int(age), sex=int(sex), smoker=int(smoker), bmi=float(sBmi), children=int(children), region=int(region))
57
+
58
+
59
+ @app.get("/path")
60
+ async def demo_get():
61
+ return {"message": "This is /path endpoint, use a post request to transform the text to uppercase"}
62
+
63
+
64
+ @app.post("/path")
65
+ async def demo_post(inp: Msg):
66
+ return {"message": inp.msg.upper()}
67
+
68
+
69
+ @app.get("/path/{path_id}")
70
+ async def demo_get_path_id(path_id: int):
71
+ return {"message": f"This is /path/{path_id} endpoint, use post request to retrieve result"}
72
+
73
+
74
+ @app.get("/predict/{path_id}")
75
+ async def predict(path_id: int):
76
+ return {"message": f"This is /predict/{path_id} endpoint, use post request to retrieve result"}
77
+
78
+ def get_region_name(region_code):
79
+ region_mapping = {
80
+ 0: "Northeast",
81
+ 1: "Northwest",
82
+ 2: "Southeast",
83
+ 3: "Southwest"
84
+ }
85
+ return region_mapping.get(region_code, "Unknown")
86
+
87
+ @app.post("/predict")
88
+ async def predict(request: Request, requess: Req = Depends(form_req)):
89
+ '''
90
+ Predict the insurance cost based on user inputs
91
+ and render the result to the html page
92
+ '''
93
+ age = requess.age
94
+ sex = requess.sex
95
+ smoker = requess.smoker
96
+ bmi = requess.bmi
97
+ children = requess.children
98
+ region = requess.region
99
+ data = []
100
+
101
+ data.append(int(age))
102
+ data.extend([int(sex)])
103
+ data.extend([float(bmi)])
104
+ data.extend([int(children)])
105
+ data.extend([int(smoker)])
106
+ data.extend([int(region)])
107
+
108
+ prediction = model.predict([data])
109
+ output = round(prediction[0], 2)
110
+
111
+ sex = "Male" if requess.sex == 1 else "Female"
112
+ smoker = "Yes" if requess.smoker == 1 else "No"
113
+
114
+ # Render index.html with prediction results
115
+ json_compatible_resp_data = jsonable_encoder(Resp(age=requess.age, sex=sex, smoker=smoker,
116
+ bmi=requess.bmi, children=requess.children, region=get_region_name(requess.region), insurance_cost=output))
117
+ return JSONResponse(content=json_compatible_resp_data)
118
+
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0e2966701ef9bda29bde5d34b776789a0f614589c179d44bf33dbde8b3362f0
3
+ size 630
pyproject.toml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "railway-fastapi-template"
3
+ version = "0.1.0"
4
+ description = "This example starts up a FastAPI server"
5
+ authors = ["DeviousLab <deviouslab@gmail.com>"]
6
+ license = "MIT"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.10"
10
+ fastapi = "^0.80.0"
11
+ uvicorn = "^0.18.3"
12
+
13
+ [tool.poetry.dev-dependencies]
14
+
15
+ [build-system]
16
+ requires = ["poetry-core>=1.0.0"]
17
+ build-backend = "poetry.core.masonry.api"