Deploy a FastAPI app
Here's the code for this specimen on Github
FastAPI is a modern, fast (high-performance), web framework for building APIs. It uses the popular Python web framework Starlette for handling requests, and the high-performance asynchronous web server ASGI (Asynchronous Server Gateway Interface) to handle the concurrency. This makes FastAPI ideal for building high-performance and high-concurrency APIs.
FastAPI is built on top of Pydantic, a library for data validation and settings management using Python type hints. this means that you as a developer do need to worry about the data validation and rather focus on the code and the logic that you are going to implement.
Let's create a simple FastAPI app like in the file server.py
:
from fastapi import FastAPI
app = FastAPI()
ITEMS = {
"foo": "The Foo Wrestlers",
"bar": "The Bar Fighters",
"wysiwyg": "What You See Is What You Get",
"memes": "Memes are the best",
"fastapi": "FastAPI is a server",
}
@app.get("/")
def read_root():
return '''<html>Hello World</html>'''
@app.get("/items/{item_id}")
def read_item(item_id: str):
if item_id in ITEMS:
return f'''<html>Item ID: {item_id} - {ITEMS[item_id]}</html>'''
else:
return f'''<html>Item ID: {item_id} - Not Found</html>'''
@app.post("/update_item_details")
def update_item_details(item_id: str, item_name: str):
if item_id in ITEMS:
ITEMS[item_id] = item_name
return f'''<html>Item ID: {item_id} - {ITEMS[item_id]}</html>'''
else:
return f'''<html>Item ID: {item_id} - Not Found</html>'''
Now to deploy this we can run this command on the Shell terminal:
nbx serve upload \
server:app \
--serving_type "fastapi" \
--id "<my_id>" \
--trigger
Once your model is deployed you can access your API endpoint using cURL like curl https://api.nimblebox.ai/ago7kguk/x/