https://github.com/pankajarm/clothing_finder_app
From DL to Web App in 50 Lines of Code, A web app created using https://www.starlette.io framework with Fast.AI and hosted on now.sh in a docker
https://github.com/pankajarm/clothing_finder_app
Last synced: 3 months ago
JSON representation
From DL to Web App in 50 Lines of Code, A web app created using https://www.starlette.io framework with Fast.AI and hosted on now.sh in a docker
- Host: GitHub
- URL: https://github.com/pankajarm/clothing_finder_app
- Owner: pankajarm
- Created: 2018-11-04T01:56:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-17T01:43:16.000Z (over 6 years ago)
- Last Synced: 2025-03-02T09:39:40.901Z (3 months ago)
- Language: CSS
- Size: 153 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cloth Finder App
From Deep Learning to Web App in 50 Lines of Code```
from starlette.applications import Starlette
from starlette.responses import HTMLResponse
from starlette.staticfiles import StaticFiles
from starlette.middleware.cors import CORSMiddleware
import uvicorn, aiohttp, asyncio
from io import BytesIO
from fastai import *
from fastai.vision import *
import base64model_file_url = 'https://github.com/pankymathur/cloth_finder_app/blob/master/data/cloth_categories/models/stage-1_sz-150.pth?raw=true'
model_file_name = 'model'
classes = ['Blouse', 'Blazer', 'Button-Down', 'Bomber', 'Anorak', 'Tee', 'Tank', 'Top', 'Sweater', 'Flannel', 'Hoodie', 'Cardigan', 'Jacket', 'Henley', 'Poncho', 'Jersey', 'Turtleneck', 'Parka', 'Peacoat', 'Halter', 'Skirt', 'Shorts', 'Jeans', 'Joggers', 'Sweatpants', 'Jeggings', 'Cutoffs', 'Sweatshorts', 'Leggings', 'Culottes', 'Chinos', 'Trunks', 'Sarong', 'Gauchos', 'Jodhpurs', 'Capris', 'Dress', 'Romper', 'Coat', 'Kimono', 'Jumpsuit', 'Robe', 'Caftan', 'Kaftan', 'Coverup', 'Onesie']path = Path(__file__).parent
app = Starlette()
app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_headers=['X-Requested-With', 'Content-Type'])
app.mount('/static', StaticFiles(directory='app/static'))async def download_file(url, dest):
if dest.exists(): return
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.read()
with open(dest, 'wb') as f: f.write(data)async def setup_learner():
await download_file(model_file_url, path/'models'/f'{model_file_name}.pth')
data_bunch = ImageDataBunch.single_from_classes(path, classes,
tfms=get_transforms(), size=150).normalize(imagenet_stats)
learn = create_cnn(data_bunch, models.resnet34, pretrained=False)
learn.load(model_file_name)
return learnloop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(setup_learner())]
learn = loop.run_until_complete(asyncio.gather(*tasks))[0]
loop.close()IMG_FILE_SRC = path/'static'/'saved_image.png'
PREDICTION_FILE_SRC = path/'static'/'predictions.txt'@app.route("/upload", methods=["POST"])
async def upload(request):
data = await request.form()
img_bytes = await (data["img"].read())
bytes = base64.b64decode(img_bytes)
return predict_from_bytes(bytes)def predict_from_bytes(bytes):
img = open_image(BytesIO(bytes))
_,_,losses = learn.predict(img)
img.save(IMG_FILE_SRC)
predictions = sorted(zip(classes, map(float, losses)), key=lambda p: p[1], reverse=True)
fh = open(PREDICTION_FILE_SRC, "w")
fh.write(str(predictions[0:3]));fh.close()
result_html = path/'static'/'result.html'
return HTMLResponse(result_html.open().read())@app.route("/")
def form(request):
index_html = path/'static'/'index.html'
return HTMLResponse(index_html.open().read())if __name__ == "__main__":
if "serve" in sys.argv: uvicorn.run(app, host="0.0.0.0", port=5042)
```This is repo for my article
This web app was created using https://www.starlette.io framework with Fast.AI and is hosted on now.sh in a docker