Commit ·
38bc0de
1
Parent(s): 5e6d15e
Fixed minor typos
Browse files- Dockerfile +1 -1
- app.py +142 -3
- requirements.txt +1 -1
- server.py +0 -136
Dockerfile
CHANGED
|
@@ -16,4 +16,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
|
| 16 |
|
| 17 |
COPY . /code
|
| 18 |
|
| 19 |
-
CMD ["uvicorn", "app
|
|
|
|
| 16 |
|
| 17 |
COPY . /code
|
| 18 |
|
| 19 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -1,4 +1,143 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
|
| 3 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi.logger import logger
|
| 6 |
+
|
| 7 |
+
from model import Model
|
| 8 |
+
import base64
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
from pydantic import BaseModel
|
| 11 |
+
from config import CONFIG
|
| 12 |
+
|
| 13 |
+
from predict import predict
|
| 14 |
+
|
| 15 |
+
# About
|
| 16 |
+
import torch
|
| 17 |
+
import os
|
| 18 |
+
import sys
|
| 19 |
+
|
| 20 |
+
# Server API
|
| 21 |
+
import uvicorn
|
| 22 |
+
|
| 23 |
+
app = FastAPI(
|
| 24 |
+
title="AdVisual MaskCut Model",
|
| 25 |
+
description="Description of the ML Model",
|
| 26 |
+
version="0.0.1",
|
| 27 |
+
terms_of_service=None,
|
| 28 |
+
contact=None,
|
| 29 |
+
license_info=None,
|
| 30 |
+
docs_url="/",
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Allow CORS for local debugging
|
| 34 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"])
|
| 35 |
+
|
| 36 |
+
@app.on_event("startup")
|
| 37 |
+
async def startup_event():
|
| 38 |
+
"""
|
| 39 |
+
Initialize FastAPI and add variables
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
logger.info('Running envirnoment: {}'.format(CONFIG['ENV']))
|
| 43 |
+
logger.info('PyTorch using device: {}'.format(CONFIG['DEVICE']))
|
| 44 |
+
|
| 45 |
+
# Initialize the pytorch model
|
| 46 |
+
model = Model()
|
| 47 |
+
|
| 48 |
+
# add model and other preprocess tools too app state
|
| 49 |
+
app.package = {
|
| 50 |
+
"model": model
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
@app.get("/ping")
|
| 54 |
+
def ping():
|
| 55 |
+
return {"ok": True, "message": "Pong"}
|
| 56 |
+
|
| 57 |
+
@app.get("/about")
|
| 58 |
+
def show_about():
|
| 59 |
+
"""
|
| 60 |
+
Get deployment information, for debugging
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
logger.info('API /about called')
|
| 64 |
+
|
| 65 |
+
def bash(command):
|
| 66 |
+
output = os.popen(command).read()
|
| 67 |
+
return output
|
| 68 |
+
|
| 69 |
+
return {
|
| 70 |
+
"sys.version": sys.version,
|
| 71 |
+
"torch.__version__": torch.__version__,
|
| 72 |
+
"torch.cuda.is_available()": torch.cuda.is_available(),
|
| 73 |
+
"torch.version.cuda": torch.version.cuda,
|
| 74 |
+
"torch.backends.cudnn.version()": torch.backends.cudnn.version(),
|
| 75 |
+
"torch.backends.cudnn.enabled": torch.backends.cudnn.enabled,
|
| 76 |
+
"nvidia-smi": bash('nvidia-smi')
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
class ImageBody(BaseModel):
|
| 80 |
+
image: str
|
| 81 |
+
threshold: float = 0.15
|
| 82 |
+
num_objects: int = 1
|
| 83 |
+
|
| 84 |
+
@app.post("/predict")
|
| 85 |
+
async def do_predict(body: ImageBody):
|
| 86 |
+
"""
|
| 87 |
+
Perform prediction on input data
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
logger.info('API predict called')
|
| 91 |
+
|
| 92 |
+
image: str = body.image
|
| 93 |
+
threshold: float = body.threshold
|
| 94 |
+
num_objects: int = body.num_objects
|
| 95 |
+
|
| 96 |
+
# Run the algorithm
|
| 97 |
+
result = predict(app.package, image, threshold, num_objects)
|
| 98 |
+
|
| 99 |
+
# Convert the result to base64 and send the json back
|
| 100 |
+
buffered = BytesIO()
|
| 101 |
+
result.save(buffered, format="JPEG")
|
| 102 |
+
img_str = 'data:image/jpeg;base64,' + base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 103 |
+
|
| 104 |
+
return {"ok": True, "status": "FINISHED", "result": img_str}
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@app.websocket("/ws")
|
| 108 |
+
async def websocket_endpoint(websocket: WebSocket):
|
| 109 |
+
await websocket.accept()
|
| 110 |
+
while True:
|
| 111 |
+
try:
|
| 112 |
+
data = await websocket.receive_json()
|
| 113 |
+
image: str = data.get('image')
|
| 114 |
+
threshold: float = data.get('threshold') or 0.15
|
| 115 |
+
num_objects: int = data.get('num_objects') or 1
|
| 116 |
+
|
| 117 |
+
await websocket.send_json({"ok": True, "status": "STARTED"})
|
| 118 |
+
|
| 119 |
+
if image == None:
|
| 120 |
+
await websocket.send_json({
|
| 121 |
+
"ok": False,
|
| 122 |
+
"status": "ERROR",
|
| 123 |
+
"message": "No image provided"
|
| 124 |
+
})
|
| 125 |
+
break
|
| 126 |
+
|
| 127 |
+
# Run the algorithm
|
| 128 |
+
result = predict(app.package, image, threshold, num_objects)
|
| 129 |
+
|
| 130 |
+
# Convert the result to base64 and send the json back
|
| 131 |
+
buffered = BytesIO()
|
| 132 |
+
result.save(buffered, format="JPEG")
|
| 133 |
+
img_str = 'data:image/jpeg;base64,' + base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 134 |
+
|
| 135 |
+
await websocket.send_json({"ok": True, "status": "FINISHED", "result": img_str})
|
| 136 |
+
|
| 137 |
+
await websocket.close()
|
| 138 |
+
except WebSocketDisconnect:
|
| 139 |
+
break
|
| 140 |
+
|
| 141 |
+
if __name__ == '__main__':
|
| 142 |
+
# server api
|
| 143 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
requirements.txt
CHANGED
|
@@ -9,4 +9,4 @@ tqdm==4.64.1
|
|
| 9 |
pycocotools==2.0.6
|
| 10 |
fastapi==0.94.0
|
| 11 |
pydantic==1.8.2
|
| 12 |
-
uvicorn==0.21.0
|
|
|
|
| 9 |
pycocotools==2.0.6
|
| 10 |
fastapi==0.94.0
|
| 11 |
pydantic==1.8.2
|
| 12 |
+
uvicorn[standard]==0.21.0
|
server.py
DELETED
|
@@ -1,136 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python
|
| 2 |
-
|
| 3 |
-
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
from fastapi.logger import logger
|
| 6 |
-
|
| 7 |
-
from model import Model
|
| 8 |
-
import base64
|
| 9 |
-
from io import BytesIO
|
| 10 |
-
from pydantic import BaseModel
|
| 11 |
-
from config import CONFIG
|
| 12 |
-
|
| 13 |
-
from predict import predict
|
| 14 |
-
|
| 15 |
-
# About
|
| 16 |
-
import torch
|
| 17 |
-
import os
|
| 18 |
-
import sys
|
| 19 |
-
|
| 20 |
-
app = FastAPI(
|
| 21 |
-
title="AdVisual MaskCut Model",
|
| 22 |
-
description="Description of the ML Model",
|
| 23 |
-
version="0.0.1",
|
| 24 |
-
terms_of_service=None,
|
| 25 |
-
contact=None,
|
| 26 |
-
license_info=None,
|
| 27 |
-
docs_url="/",
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
# Allow CORS for local debugging
|
| 31 |
-
app.add_middleware(CORSMiddleware, allow_origins=["*"])
|
| 32 |
-
|
| 33 |
-
@app.on_event("startup")
|
| 34 |
-
async def startup_event():
|
| 35 |
-
"""
|
| 36 |
-
Initialize FastAPI and add variables
|
| 37 |
-
"""
|
| 38 |
-
|
| 39 |
-
logger.info('Running envirnoment: {}'.format(CONFIG['ENV']))
|
| 40 |
-
logger.info('PyTorch using device: {}'.format(CONFIG['DEVICE']))
|
| 41 |
-
|
| 42 |
-
# Initialize the pytorch model
|
| 43 |
-
model = Model()
|
| 44 |
-
|
| 45 |
-
# add model and other preprocess tools too app state
|
| 46 |
-
app.package = {
|
| 47 |
-
"model": model
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
@app.get("/ping")
|
| 51 |
-
def ping():
|
| 52 |
-
return {"ok": True, "message": "Pong"}
|
| 53 |
-
|
| 54 |
-
@app.get("/about")
|
| 55 |
-
def show_about():
|
| 56 |
-
"""
|
| 57 |
-
Get deployment information, for debugging
|
| 58 |
-
"""
|
| 59 |
-
|
| 60 |
-
logger.info('API /about called')
|
| 61 |
-
|
| 62 |
-
def bash(command):
|
| 63 |
-
output = os.popen(command).read()
|
| 64 |
-
return output
|
| 65 |
-
|
| 66 |
-
return {
|
| 67 |
-
"sys.version": sys.version,
|
| 68 |
-
"torch.__version__": torch.__version__,
|
| 69 |
-
"torch.cuda.is_available()": torch.cuda.is_available(),
|
| 70 |
-
"torch.version.cuda": torch.version.cuda,
|
| 71 |
-
"torch.backends.cudnn.version()": torch.backends.cudnn.version(),
|
| 72 |
-
"torch.backends.cudnn.enabled": torch.backends.cudnn.enabled,
|
| 73 |
-
"nvidia-smi": bash('nvidia-smi')
|
| 74 |
-
}
|
| 75 |
-
|
| 76 |
-
class ImageBody(BaseModel):
|
| 77 |
-
image: str
|
| 78 |
-
threshold: float = 0.15
|
| 79 |
-
num_objects: int = 1
|
| 80 |
-
|
| 81 |
-
@app.post("/predict")
|
| 82 |
-
async def do_predict(body: ImageBody):
|
| 83 |
-
"""
|
| 84 |
-
Perform prediction on input data
|
| 85 |
-
"""
|
| 86 |
-
|
| 87 |
-
logger.info('API predict called')
|
| 88 |
-
|
| 89 |
-
image: str = body.image
|
| 90 |
-
threshold: float = body.threshold
|
| 91 |
-
num_objects: int = body.num_objects
|
| 92 |
-
|
| 93 |
-
# Run the algorithm
|
| 94 |
-
result = predict(app.package, image, threshold, num_objects)
|
| 95 |
-
|
| 96 |
-
# Convert the result to base64 and send the json back
|
| 97 |
-
buffered = BytesIO()
|
| 98 |
-
result.save(buffered, format="JPEG")
|
| 99 |
-
img_str = 'data:image/jpeg;base64,' + base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 100 |
-
|
| 101 |
-
return {"ok": True, "status": "FINISHED", "result": img_str}
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
@app.websocket("/ws")
|
| 105 |
-
async def websocket_endpoint(websocket: WebSocket):
|
| 106 |
-
await websocket.accept()
|
| 107 |
-
while True:
|
| 108 |
-
try:
|
| 109 |
-
data = await websocket.receive_json()
|
| 110 |
-
image: str = data.get('image')
|
| 111 |
-
threshold: float = data.get('threshold') or 0.15
|
| 112 |
-
num_objects: int = data.get('num_objects') or 1
|
| 113 |
-
|
| 114 |
-
await websocket.send_json({"ok": True, "status": "STARTED"})
|
| 115 |
-
|
| 116 |
-
if image == None:
|
| 117 |
-
await websocket.send_json({
|
| 118 |
-
"ok": False,
|
| 119 |
-
"status": "ERROR",
|
| 120 |
-
"message": "No image provided"
|
| 121 |
-
})
|
| 122 |
-
break
|
| 123 |
-
|
| 124 |
-
# Run the algorithm
|
| 125 |
-
result = predict(app.package, image, threshold, num_objects)
|
| 126 |
-
|
| 127 |
-
# Convert the result to base64 and send the json back
|
| 128 |
-
buffered = BytesIO()
|
| 129 |
-
result.save(buffered, format="JPEG")
|
| 130 |
-
img_str = 'data:image/jpeg;base64,' + base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 131 |
-
|
| 132 |
-
await websocket.send_json({"ok": True, "status": "FINISHED", "result": img_str})
|
| 133 |
-
|
| 134 |
-
await websocket.close()
|
| 135 |
-
except WebSocketDisconnect:
|
| 136 |
-
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|