Search code examples
pythonfastapi

FastAPI run config commands before serving application?


Does FastAPI automatically configure environment, imports, ML model instantiation, etc. when you run uvicorn Package.API:app --reload --port 8001 ?

The result I'm seeing is

INFO:     Will watch for changes in these directories: ['/Users/me/Documents/application']
INFO:     Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
INFO:     Started reloader process [35866] using StatReload

However, when I navigate to http://127.0.0.1:8001/docs, the browser cannot connect. This seems inconsistent.

import logging
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from .Inference import TextClassifier

logging.basicConfig(level=logging.INFO)

# Instantiate the model
try:
    model = TextClassifier("./Package/model_artifacts.joblib")
except Exception as e:
    logging.error(f"Failed to initialize TextClassifier: {str(e)}")
    raise

app = FastAPI(title="mlApp")

class MlRequest(BaseModel):
    text: str

@app.post("/ml_request")
async def ml_request(request: MlRequest):
    try:
        text = request.text
        predicted_class = model.predict_labels(text)
        return {"predicted_class": predicted_class}
    except Exception as e:
        logging.error(f"Error in ml_request: {str(e)}")
        raise HTTPException(status_code=400, detail=str(e))

So my question is really, do I need to tell my machine, "import these packages, instantiate this ML model, then serve the API?"

If yes, maybe this behavior is expected. If no, then maybe I need to troubleshoot my machine.


Solution

  • If I understood your question correctly, then yes, everything you listed should be configured automatically. I've commented a few lines and started the app with command you mentioned and everything works fine.

    It seems like you just don't have an access to 8001 port because of firewall or you machine configuration.