great-ai/docs/tutorial/deploy.ipynb

130 lines
4.6 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Harden and deploy your app\n",
"\n",
"Finally, it's time to deploy your model. But before that, you have to make sure you follow AI deployment [best practices](https://se-ml.github.io/). In the past, this step was too often either the source of unexpected struggle, or worse, simply ignored.\n",
"\n",
"GreatAI's decorators add tracing and API scaffolding with a few lines of code. This brings the service nearly to production readiness, but does not establish the model's accuracy, fairness, or trustworthiness. Evaluate the model for its intended use, configure a production-suitable tracing database and storage, and review the remaining deployment best practices. The local TinyDB backend used below is for development, as the output warns."
],
"id": "cell-0"
},
{
"id": "94c0cd76",
"cell_type": "markdown",
"source": [
"## Local setup\n",
"\n",
"Use Python 3.12 or 3.13 and install this checkout with `uv pip install --editable '.[notebooks]'`.\n",
"See **Run notebooks locally** in the repository README for the kernel, execution order,\n",
"and full-data options. Run related notebooks from the same working directory\n",
"to share the default `.cache/` model store and `tracing_database.json` database.\n"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from great_ai import GreatAI, use_model\n",
"from great_ai.utilities import clean\n",
"\n",
"\n",
"@GreatAI.create\n",
"@use_model(\"my-domain-predictor\")\n",
"def predict_domain(sentence, model):\n",
" inputs = [clean(sentence)]\n",
" return str(model.predict(inputs)[0])"
],
"id": "cell-3"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predict_domain(\"Mountains are just big rocks.\")"
],
"id": "cell-4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how the original return value is under the `.output` key, alongside\n",
"tracing metadata. Run `train.ipynb` first, using the same local/full mode.\n",
"\n",
"Start the development server in a separate terminal from the checkout root:\n",
"\n",
"```sh\n",
"source .venv/bin/activate\n",
"great-ai docs/tutorial/deploy.ipynb\n",
"```\n",
"\n",
"Open http://127.0.0.1:6060/dashboard. Stop the server with Ctrl+C.\n",
"The next cell tests the HTTP API in-process so that **Run All** can finish."
],
"id": "cell-5"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fastapi.testclient import TestClient\n",
"\n",
"with TestClient(predict_domain.app) as client:\n",
" response = client.post(\n",
" \"/predict\", json={\"sentence\": \"Mountains are just big rocks.\"}\n",
" )\n",
" response.raise_for_status()\n",
" assert isinstance(response.json()[\"output\"], str)\n",
" print(response.json()[\"output\"])"
],
"id": "cell-6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Congrats, you've just created your first GreatAI service! 🎉\n",
"\n",
"Your service now has tracing and API scaffolding. Before deploying, replace the default local TinyDB tracing backend, configure storage and infrastructure, evaluate the model for its intended use, and review the remaining best practices. Model trustworthiness requires evidence and ongoing oversight beyond wrapping the inference function. You (or your organisation) remain responsible for deploying and operating the service.\n",
"\n",
"There are three main approaches to deploy a GreatAI service: For more info about them, check out [the deployment how-to](https://great-ai.schmelczer.dev/how-to-guides/use-service).\n",
"\n",
"For more thorough examples, see the [examples page](https://great-ai.schmelczer.dev/examples/simple/data).\n",
"\n",
"### [Go back to the summary](https://great-ai.schmelczer.dev/tutorial/#summary)"
],
"id": "cell-7"
}
],
"metadata": {
"kernelspec": {
"display_name": "GreatAI (local)",
"language": "python",
"name": "great-ai-local"
},
"language_info": {
"name": "python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}