Q&A 5 How do you run and test your FastAPI app using Uvicorn and Swagger UI?

5.1 Explanation

After creating your FastAPI app, you need to run it locally to test prediction endpoints. The standard way to run FastAPI is through Uvicorn, an ASGI server that supports fast, async APIs.

When you run your app, FastAPI automatically provides an interactive, browser-based Swagger UI at /docs, where you can test endpoints and submit real input.

5.2 Run Command (Terminal)

Assuming your FastAPI file is located at scripts/model_api.py and your app instance is named app, run:

uvicorn scripts.model_api:app --reload
  • script.model_api is the Python path: folder + filename (without .py)
  • :app refers to the FastAPI() instance inside the file
  • –reload enables live reloading during development

5.3 Output (Sample)

INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process…

5.4 Test in Your Browser

  • Go: http://127.0.0.1:8000/docs

  • You’ll see:

    • /models → GET endpoint to list all available models
    • /predict/{model_name} → POST endpoint with a form to test predictions

Click “Try it out” on any route, fill in the form, and hit Execute to see the response.

5.5 Example JSON Input

{ “Pclass”: 1, “Sex”: 1, “Age”: 32.0, “Fare”: 100.0, “Embarked”: 2 }

5.6 Troubleshooting

If you get Address already in use:

lsof -i :8000     # Find the PID
kill -9 <PID>     # Kill the process using the port

Or use an alternate port: like so:

uvicorn script.model_api:app --reload --port 8001

✅ Takeaway: FastAPI’s auto-generated Swagger UI lets you run and test prediction APIs right from your browser. Uvicorn runs the app, and /docs gives you an instant frontend for debugging.