{"id":15297510,"url":"https://github.com/deepai-org/ai_integration","last_synced_at":"2025-04-13T22:40:53.349Z","repository":{"id":34387343,"uuid":"175754400","full_name":"deepai-org/ai_integration","owner":"deepai-org","description":"AI Model Integration for python","archived":false,"fork":false,"pushed_at":"2023-09-29T16:57:33.000Z","size":191,"stargazers_count":18,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-27T12:52:41.866Z","etag":null,"topics":["ai","api","machine-learning","python","python27","python3"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deepai-org.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-15T05:28:35.000Z","updated_at":"2025-03-19T17:49:30.000Z","dependencies_parsed_at":"2024-10-23T02:04:17.240Z","dependency_job_id":null,"html_url":"https://github.com/deepai-org/ai_integration","commit_stats":{"total_commits":47,"total_committers":5,"mean_commits":9.4,"dds":"0.44680851063829785","last_synced_commit":"9b2c0f11fc926408e90779ce01e3f420bd8318c0"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepai-org%2Fai_integration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepai-org%2Fai_integration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepai-org%2Fai_integration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepai-org%2Fai_integration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deepai-org","download_url":"https://codeload.github.com/deepai-org/ai_integration/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248794234,"owners_count":21162611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","api","machine-learning","python","python27","python3"],"created_at":"2024-09-30T19:18:02.082Z","updated_at":"2025-04-13T22:40:53.291Z","avatar_url":"https://github.com/deepai-org.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ai_integration\n[![PyPI version](https://badge.fury.io/py/ai-integration.svg)](https://badge.fury.io/py/ai-integration)\nAI Model Integration for Python 2.7/3\n\n# Purpose\n### Expose your AI model under a standard interface so that you can run the model under a variety of usage modes and hosting platforms - all working seamlessly, automatically, with no code changes.\n### Designed to be as simple as possible to integrate.\n\n### Create a standard \"ai_integration Docker Container Format\" for interoperability.\n\n\n![Diagram showing integration modes](https://yuml.me/diagram/plain;dir:RL/class/[Your%20Model]%20-\u003e%20[AI%20Integration],%20[AI%20Integration]\u003c-[Many%20more%20coming!],%20[AI%20Integration]\u003c-[Pickle],%20[AI%20Integration]\u003c-[JSON],%20[AI%20Integration]\u003c-[HTTP%20API],%20[AI%20Integration]\u003c-[Command%20Line].jpg)\n\n## Table of Contents\n\n- [Purpose](#purpose)\n- [Built-In Usage Modes](#built-in-usage-modes)\n- [Example Models](#example-models)\n- [How to call the integration library from your code](#how-to-call-the-integration-library-from-your-code)\n  * [Simplest Usage Example](#simplest-usage-example)\n- [Docker Container Format Requirements](#docker-container-format-requirements)\n- [Inputs Dicts](#inputs-dicts)\n- [Result Dicts](#result-dicts)\n- [Error Handling](#error-handling)\n- [Inputs Schema](#inputs-schema)\n    + [Schema Data Types](#schema-data-types)\n    + [Schema Examples](#schema-examples)\n        * [Single Image](#single-image)\n        * [Multi-Image](#multi-image)\n        * [Text](#text)\n- [Creating Usage Modes](#creating-usage-modes)\n\n# Built-In Usage Modes\nThere are several built-in modes for testing:\n\n* Command Line using argparse (command_line)\n* HTTP Web UI / multipart POST API using Flask (http)\n* Pipe inputs dict as JSON (test_inputs_dict_json)\n* Pipe inputs dict as pickle (test_inputs_pickled_dict)\n* Pipe single image for models that take a single input named image (test_single_image)\n* Test single image models with a built-in solid gray image (test_model_integration)\n\n# Example Models\n* [Open NSFW](https://github.com/deepai-org/open_nsfw)\n* [DeOldify Black-and-white Image Colorizer](https://github.com/deepai-org/DeOldify)\n\n# Contribution\n\n`ai_integration` is a community project developed under the free Apache 2.0 license. We welcome any new modes, integrations, bugfixes, and your ideas.\n\n# How to call the integration library from your code\n\n(An older version of this library required the user to expose their model as an inference function, but this caused pain in users and is no longer needed.)\n\nRun a \"while True:\" loop in your code and call \"get_next_input\" to get inputs.\n\nPass an inputs_schema (see full docs below) to \"get_next_input\".\n\nSee the specification below for \"Inputs Dicts\"\n\n\"get_next_input\" needs to be called using a \"with\" block as demonstrated below.\n\nThen process the data. Format the result or error as described under \"Results Dicts\"\n\nThen send the result (or error back) with \"send_result\".\n\n## Simplest Usage Example\n\nThis example takes an image and returns a constant string without even looking at the input. It is a very bad AI algorithm for sure!\n\n```python\nimport ai_integration\n\nwhile True:\n    with ai_integration.get_next_input(inputs_schema={\"image\": {\"type\": \"image\"}}) as inputs_dict:\n        # If an exception happens in this 'with' block, it will be sent back to the ai_integration library\n        result_data = {\n            \"content-type\": 'text/plain',\n            \"data\": \"Fake output\",\n            \"success\": True\n        }\n        ai_integration.send_result(result_data)\n        \n        \n```\n\n# Docker Container Format Requirements:\n\n#### This library is intended to allow the creation of standardized docker containers. This is the standard:\n\n1. Use the ai_integration library\n\n2. You install this library with pip (or pip3)\n\n3. ENTRYPOINT is used to set your python code as the entry point into the container.\n\n4. No command line arguments will be passed to your python entrypoint. (Unless using the command line interface mode)\n\n5. Do not use argparse in your program as this will conflict with command line mode.\n\nTo test your finished container's integration, run:\n    * nvidia-docker run --rm -it -e MODE=test_model_integration YOUR_DOCKER_IMAGE_NAME\n    * use docker instead of nvidia-docker if you aren't using NVIDIA...\n    * You should see a bunch of happy messages. Any sad messages or exceptions indicate an error.\n    * It will try inference a few times. If you don't see this happening, something is not integrated right.\n\n\n# Inputs Dicts\n\ninputs_dict is a regular python dictionary.\n\n- Keys are input names (typically image, or style, content)\n- Values are the data itself. Either byte array of JPEG data (for images) or text string.\n- Any model options are also passed here and may be strings or numbers. Best to accept either strings/numbers in your model.\n\n\n\n# Result Dicts\n\nContent-type, a MIME type, inspired by HTTP, helps to inform the type of the \"data\" field\n\nsuccess is a boolean.\n\n\"error\" should be the error message if success is False.\n\n\n```python\n{\n    'content-type': 'application/json', # or image/jpeg\n    'data': \"{JSON data or image data as byte buffer}\",\n    'success': True,\n    'error': 'the error message (only if failed)'\n}   \n```\n\n# Error Handling\n\nIf there's an error that you can catch:\n- set content-type to text/plain\n- set success to False\n- set data to None\n- set error to the best description of the error (perhaps the output of traceback.format_exc())\n\n# Inputs Schema\n\nAn inputs schema is a simple python dict {} that documents the inputs required by your inference function.\n\nNot every integration mode looks at the inputs schema - think of it as a hint for telling the mode what data it needs to provide your function.\n\nAll mentioned inputs are assumed required by default.\n\nThe keys are names, the values specify properties of the input.\n\n### Schema Data Types\n- image\n- text\n- Suggest other types to add to the specification!\n\n### Schema Examples\n\n##### Single Image\nBy convention, name your input \"image\" if you accept a single image input\n```python\n{\n    \"image\": {\n        \"type\": \"image\"\n    }\n}\n```\n\n##### Multi-Image\nFor example, imagine a style transfer model that needs two input images.\n```python\n{\n    \"style\": {\n        \"type\": \"image\"\n    },\n    \"content\": {\n        \"type\": \"image\"\n    },    \n}\n```\n\n##### Text\n```python\n{\n    \"sentence\": {\n        \"type\": \"text\"\n    }\n}\n```\n\n# Creating Usage Modes\n\nA mode is a function that lives in a file in the modes folder of this library.\n\n\nTo create a new mode:\n\n1. Add a python file in this folder\n2. Add a python function to your file that takes two args:\n    \n    def http(inference_function=None, inputs_schema=None):\n3. Attach a hint to your function\n4. At the end of the file, declare the modes from your file (each python file could export multiple modes), for example:\n```python\nMODULE_MODES = {\n    'http': http\n}\n\n```\n\nYour mode will be called with the inference function and inference schema, the rest is up to you!\n\nThe sky is the limit, you can integrate with pretty much anything.\n\nSee existing modes for examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepai-org%2Fai_integration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeepai-org%2Fai_integration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepai-org%2Fai_integration/lists"}