Created
May 5, 2023 19:29
-
-
Save sarony/a0f83f53eb8445ed23f04b3053d8c3eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aiohttp | |
import asyncio | |
import uvicorn | |
from fastai import * | |
from fastai.vision import * | |
from fastai.vision.all import * | |
from io import BytesIO | |
from starlette.applications import Starlette | |
from starlette.middleware.cors import CORSMiddleware | |
from starlette.responses import HTMLResponse, JSONResponse | |
from starlette.staticfiles import StaticFiles | |
from PIL import Image | |
import urllib.request | |
app = Starlette() | |
app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_headers=['X-Requested-With', 'Content-Type']) | |
learn = load_learner('export.pkl') | |
@app.route('/analyze', methods=['POST']) | |
async def analyze(request): | |
img_data = await request.form() | |
img = Image.open(requests.get(img_data['file'], stream=True, timeout = 10).raw) | |
pil_img = PILImage.create(img) | |
prediction = learn.predict(pil_img)[0] | |
return JSONResponse({'result': str(prediction)}) | |
if __name__ == '__main__': | |
if 'serve' in sys.argv: | |
uvicorn.run(app=app, host='0.0.0.0', port=5500, log_level="info") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment