Spaces:
Runtime error
Runtime error
Upload 23 files
Browse files- Procfile +1 -0
- app.py +58 -0
- application/__init__.py +0 -0
- application/__pycache__/__init__.cpython-38.pyc +0 -0
- application/__pycache__/schema.cpython-38.pyc +0 -0
- application/components/__init__.py +1 -0
- application/components/__pycache__/__init__.cpython-38.pyc +0 -0
- application/components/prediction/__pycache__/serve_model.cpython-38.pyc +0 -0
- application/components/prediction/__pycache__/symptom_check.cpython-38.pyc +0 -0
- application/components/prediction/serve_model.py +50 -0
- application/components/prediction/symptom_check.py +14 -0
- application/models/resnet_ct.h5 +3 -0
- application/schema.py +7 -0
- application/server/__pycache__/main.cpython-38.pyc +0 -0
- application/server/main.py +35 -0
- images/img/fomative assesement.jpg +0 -0
- images/img/form-summ-assess.png +0 -0
- images/img/learning602-1.png +0 -0
- images/main.png +0 -0
- images/response.png +0 -0
- images/terminal.png +0 -0
- requirements.txt +9 -0
- runtime.txt +1 -0
Procfile
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
web: uvicorn application.server.main:app --host 0.0.0.0 --port $PORT --workers 2
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import json
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
def load_image(image):
|
9 |
+
img = Image.open(image)
|
10 |
+
return img
|
11 |
+
|
12 |
+
def save_uploadedfile(uploadedfile):
|
13 |
+
with open(os.path.join("images/img",uploadedfile.name),"wb") as f:
|
14 |
+
f.write(uploadedfile.getbuffer())
|
15 |
+
uploaded_location = os.path.join("images/img",uploadedfile.name)
|
16 |
+
return uploaded_location#st.success("Saved File:{} to {}".format(uploadedfile.name, uploaded_location))
|
17 |
+
|
18 |
+
|
19 |
+
st.title("Covid Prediction App from CT Images")
|
20 |
+
|
21 |
+
|
22 |
+
#taking user inputs
|
23 |
+
|
24 |
+
st.write("")
|
25 |
+
|
26 |
+
#converting input to json
|
27 |
+
|
28 |
+
|
29 |
+
image = st.file_uploader("Upload CT Scan", type=["png","jpg","jpeg"])
|
30 |
+
|
31 |
+
if image is not None:
|
32 |
+
# To See details
|
33 |
+
file_details = {"filename":image.name, "filetype":image.type,
|
34 |
+
"filesize":image.size}
|
35 |
+
st.write(file_details)
|
36 |
+
#image1 = Image.open(image)
|
37 |
+
#img_array = np.array(image1)
|
38 |
+
|
39 |
+
#View Uploaded Image
|
40 |
+
st.image(load_image(image),width=250)
|
41 |
+
#save image to disk
|
42 |
+
saved = save_uploadedfile(image)
|
43 |
+
|
44 |
+
if st.button ('Analyze'):
|
45 |
+
test_file = open(os.path.join("images/img", image.name), "rb")
|
46 |
+
response = requests.post('http://127.0.0.1:8000/predict/image', files={'file': test_file })
|
47 |
+
prediction = response.json()##json_object["prediction"]
|
48 |
+
st.write(prediction)
|
49 |
+
st. subheader (f"Response from Covid Analyzer API = {prediction}")
|
50 |
+
|
51 |
+
#RUN BOTH...
|
52 |
+
#streamlit run app.py
|
53 |
+
#uvicorn application.server.main:app
|
54 |
+
|
55 |
+
#OPTION 2....
|
56 |
+
|
57 |
+
|
58 |
+
|
application/__init__.py
ADDED
File without changes
|
application/__pycache__/__init__.cpython-38.pyc
ADDED
Binary file (188 Bytes). View file
|
|
application/__pycache__/schema.cpython-38.pyc
ADDED
Binary file (532 Bytes). View file
|
|
application/components/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from .prediction.serve_model import predict, read_imagefile
|
application/components/__pycache__/__init__.cpython-38.pyc
ADDED
Binary file (278 Bytes). View file
|
|
application/components/prediction/__pycache__/serve_model.cpython-38.pyc
ADDED
Binary file (1.34 kB). View file
|
|
application/components/prediction/__pycache__/symptom_check.cpython-38.pyc
ADDED
Binary file (644 Bytes). View file
|
|
application/components/prediction/serve_model.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from io import BytesIO
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
from PIL import Image
|
6 |
+
#from tensorflow.keras.applications.imagenet_utils import decode_predictions
|
7 |
+
from tensorflow.keras.models import load_model
|
8 |
+
import os
|
9 |
+
|
10 |
+
|
11 |
+
model = None
|
12 |
+
|
13 |
+
def load_model2():
|
14 |
+
model = load_model('application/models/resnet_ct.h5')
|
15 |
+
#model = load_model(os.path.join(modelpath, 'resnet_ct.h5'))
|
16 |
+
print("Model loaded")
|
17 |
+
return model
|
18 |
+
|
19 |
+
|
20 |
+
def predict(image: Image.Image):
|
21 |
+
global model
|
22 |
+
if model is None:
|
23 |
+
model = load_model2()
|
24 |
+
|
25 |
+
image = np.asarray(image.resize((224, 224)))[..., :3]
|
26 |
+
image = np.expand_dims(image, 0)
|
27 |
+
image = image / 127.5 - 1.0
|
28 |
+
|
29 |
+
result = model.predict(image)
|
30 |
+
probability = result[0]
|
31 |
+
#print("Resnet Predictions:")
|
32 |
+
if probability[0] > 0.5:
|
33 |
+
resnet_chest_pred = str('%.2f' % (probability[0]*100) + '% COVID')
|
34 |
+
else:
|
35 |
+
resnet_chest_pred = str('%.2f' % ((1-probability[0])*100) + '% NonCOVID')
|
36 |
+
|
37 |
+
response = []
|
38 |
+
for i, res in enumerate(result):
|
39 |
+
resp = {}
|
40 |
+
resp["prediction"] = resnet_chest_pred
|
41 |
+
#resp["confidence"] = f"{res[2]*100:0.2f} %"
|
42 |
+
|
43 |
+
response.append(resp)
|
44 |
+
|
45 |
+
return response
|
46 |
+
|
47 |
+
|
48 |
+
def read_imagefile(file) -> Image.Image:
|
49 |
+
image = Image.open(BytesIO(file))
|
50 |
+
return image
|
application/components/prediction/symptom_check.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from application.schema import Symptom
|
2 |
+
|
3 |
+
def get_risk_level(symptom: Symptom):
|
4 |
+
if not (symptom.fever or symptom.dry_cough or symptom.tiredness or symptom.breathing_problem):
|
5 |
+
return 'Low risk level. THIS IS A DEMO APP'
|
6 |
+
|
7 |
+
if not (symptom.breathing_problem or symptom.dry_cough):
|
8 |
+
if symptom.fever:
|
9 |
+
return 'moderate risk level. THIS IS A DEMO APP'
|
10 |
+
|
11 |
+
if symptom.breathing_problem:
|
12 |
+
return 'High risk level. THIS IS A DEMO APP'
|
13 |
+
|
14 |
+
return 'THIS IS A DEMO APP'
|
application/models/resnet_ct.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6b20cd339ff029070d5a715ea2778143061a885bf1395cdc26464a8db8090825
|
3 |
+
size 59338824
|
application/schema.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel
|
2 |
+
|
3 |
+
class Symptom(BaseModel):
|
4 |
+
fever: bool = False
|
5 |
+
dry_cough: bool = False
|
6 |
+
tiredness: bool = False
|
7 |
+
breathing_problem: bool = False
|
application/server/__pycache__/main.cpython-38.pyc
ADDED
Binary file (1.37 kB). View file
|
|
application/server/main.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uvicorn
|
2 |
+
from fastapi import FastAPI, File, UploadFile
|
3 |
+
from starlette.responses import RedirectResponse
|
4 |
+
|
5 |
+
from application.components import predict, read_imagefile
|
6 |
+
from application.schema import Symptom
|
7 |
+
from application.components.prediction import symptom_check
|
8 |
+
|
9 |
+
app_desc = """<h2>Try this app by uploading any image with `predict/image`</h2>
|
10 |
+
<h2>Try Covid symptom checker api - it is just a learning app demo</h2>
|
11 |
+
<br>by Aniket Maurya"""
|
12 |
+
|
13 |
+
app = FastAPI(title='Tensorflow FastAPI Starter Pack', description=app_desc)
|
14 |
+
|
15 |
+
|
16 |
+
@app.get("/", include_in_schema=False)
|
17 |
+
async def index():
|
18 |
+
return RedirectResponse(url="/docs")
|
19 |
+
|
20 |
+
|
21 |
+
@app.post("/predict/image")
|
22 |
+
async def predict_api(file: UploadFile = File(...)):
|
23 |
+
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png")
|
24 |
+
if not extension:
|
25 |
+
return "Image must be jpg or png format!"
|
26 |
+
image = read_imagefile(await file.read())
|
27 |
+
prediction = predict(image)
|
28 |
+
|
29 |
+
return prediction
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
uvicorn.run(app, debug=True)
|
images/img/fomative assesement.jpg
ADDED
images/img/form-summ-assess.png
ADDED
images/img/learning602-1.png
ADDED
images/main.png
ADDED
images/response.png
ADDED
images/terminal.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow-cpu
|
2 |
+
fastapi
|
3 |
+
Pillow
|
4 |
+
starlette
|
5 |
+
gunicorn
|
6 |
+
uvicorn
|
7 |
+
numpy
|
8 |
+
python-multipart
|
9 |
+
streamlit==1.15.2
|
runtime.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python-3.7.8
|