Skip to content

Instantly share code, notes, and snippets.

@sarony
Created May 5, 2023 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarony/a0f83f53eb8445ed23f04b3053d8c3eb to your computer and use it in GitHub Desktop.
Save sarony/a0f83f53eb8445ed23f04b3053d8c3eb to your computer and use it in GitHub Desktop.
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