Forums

How to deploy a FastAPI endpoint using pythonanywhere?

https://stackoverflow.com/questions/75495360/how-to-deploy-a-fastapi-endpoint-using-pythonanywhere

I have been trying to deploy EasyOCR (it's fine if it works on CPU) with FastAPI endpoints such that anyone can use it via https POST request. It runs fine on my local host but when I have been facing challenges in deploying it using pythonanywhere. I added the additional requirements like pip install easyocr, pip install python-multipart==0.0.5.

The following is my code-

import io
import logging
import re

from fastapi import FastAPI, APIRouter, Request, File, UploadFile
from fastapi.responses import FileResponse, StreamingResponse
import easyocr

import PIL
from PIL import Image, ImageOps
import numpy

app = FastAPI()
router = APIRouter()
ocr = easyocr.Reader(["en"])
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("ocr")


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.post("/ocr")
async def do_ocr(request: Request, file: UploadFile = File(...)):
    if file is not None:
        imgFile = numpy.array(PIL.Image.open(file.file).convert("RGB"))
        res = ocr.readtext(imgFile)

        # return array of strings
        return [item[1] for item in res]

    return {"error": "missing file"}


app.include_router(router)
if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app)

I am getting the error in the logs-

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/somyatomar15/main.py", line 82, in <module>
    uvicorn.run(app)
  File "/usr/local/lib/python3.10/site-packages/uvicorn/main.py", line 463, in run
    server.run()
  File "/usr/local/lib/python3.10/site-packages/uvicorn/server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 633, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 600, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1896, in _run_once
    handle._run()
  File "/usr/local/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.10/site-packages/uvicorn/server.py", line 77, in serve
    await self.startup(sockets=sockets)
  File "/usr/local/lib/python3.10/site-packages/uvicorn/server.py", line 158, in startup
    sys.exit(1)
SystemExit: 1

PythonAnywhere supports currently only WSGI web apps, ASGI is at the top of our priority list, but it's not available yet, so FastAPI would not work. We'll let you know, when it will be ready.

Hi, I just signed up for PythonAnywhere with the intention to use it as a small API server for some internal functions. Not having FastAPI was quite a disappointment. Any update on the timeline for having it available?

It's very very high on our list. No timeline right now though

It's very high on the list for literally ages now. At least a plan/timeline is really needed now.

Unfortunately we can't provide a timeline at this point.

How about now? Is it available?

We are currently doing a private, limited test of FastAPI apps that can be created by API only. If you are interested in helping with testing, email us at support@pythonanywhere.com

I just wasted 2 hours on trying to understand what the hell was going on and why i couldn't run my little endpoint using FastAPI. JUST WRITE SOMEWHERE "FastAPI is not yet supported".

@mvavassori sorry for the confusion! FastAPI is not supported officialy via our UI yet, but it's in ALPHA stage of testing. If you'd like to participate in the testing, please contact us at support@pythonanywhere.com.