# Spirit-GPU
- [Spirit-GPU](#spirit-gpu)
- [Install](#install)
- [Usage example](#usage-example)
- [Logging](#logging)
- [API](#api)
## Install
```
pip install spirit-gpu
```
## Usage example
```python
from spirit_gpu import start
from spirit_gpu.env import Env
from typing import Dict, Any
def handler(request: Dict[str, Any], env: Env):
"""
request: Dict[str, Any], from client http request body.
request["input"]: Required.
request["webhook"]: Optional string for asynchronous requests.
returned object to be serialized into JSON and sent to the client.
in this case: '{"output": "hello"}'
"""
return {"output": "hello"}
def gen_handler(request: Dict[str, Any], env: Env):
"""
append yield output to array, serialize into JSON and send to client.
in this case: [0, 1, 2, 3, 4]
"""
for i in range(5):
yield i
async def async_handler(request: Dict[str, Any], env: Env):
"""
returned object to be serialized into JSON and sent to the client.
"""
return {"output": "hello"}
async def async_gen_handler(request: Dict[str, Any], env: Env):
"""
append yield output to array, serialize into JSON and send to client.
"""
for i in range(10):
yield i
def concurrency_modifier(current_allowed_concurrency: int) -> int:
"""
Adjusts the allowed concurrency level based on the current state.
For example, if the current allowed concurrency is 3 and resources are sufficient,
it can be increased to 5, allowing 5 tasks to run concurrently.
"""
allowed_concurrency = ...
return allowed_concurrency
"""
Register the handler with serverless.start().
Handlers can be synchronous, asynchronous, generators, or asynchronous generators.
"""
start({
"handler": async_handler, "concurrency_modifier": concurrency_modifier
})
```
## Logging
We provide a tool to log information. Default logging level is "INFO", you can call `logger.set_level(logging.DEBUG)` to change it.
> Please make sure you update to the `latest` version to use this feature.
```python
from spirit_gpu import start, logger
from spirit_gpu.env import Env
from typing import Dict, Any
def handler(request: Dict[str, Any], env: Env):
"""
request: Dict[str, Any], from client http request body.
request["input"]: Required.
request["webhook"]: Optional string for asynchronous requests.
we will only add request["meta"]["requestID"] if it not exist in your request.
"""
request_id = request["meta"]["requestID"]
logger.info("start to handle", request_id = request_id, caller=True)
return {"output": "hello"}
start({"handler": handler})
```
## API
Please read [API](https://github.com/datastone-spirit/spirit-gpu/blob/main/API.md) or [中文 API](https://github.com/datastone-spirit/spirit-gpu/blob/main/API.zh.md) for how to use spirit-gpu serverless apis and some other import policies.
Raw data
{
"_id": null,
"home_page": "https://github.com/datastone-spirit",
"name": "spirit-gpu",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "serverless, ai, gpu, machine learning, SDK, library, python, API",
"author": "spirit",
"author_email": "Spirit <pypi@datastone.cn>",
"download_url": "https://files.pythonhosted.org/packages/03/60/f341c5ee9842317dcf11c22e3ddef5919ff077634c4194277183cbc81163/spirit_gpu-0.0.4.tar.gz",
"platform": null,
"description": "# Spirit-GPU\n\n- [Spirit-GPU](#spirit-gpu)\n - [Install](#install)\n - [Usage example](#usage-example)\n - [Logging](#logging)\n - [API](#api)\n\n## Install\n```\npip install spirit-gpu\n```\n\n## Usage example\n\n```python\nfrom spirit_gpu import start\nfrom spirit_gpu.env import Env\nfrom typing import Dict, Any\n\ndef handler(request: Dict[str, Any], env: Env):\n \"\"\"\n request: Dict[str, Any], from client http request body.\n request[\"input\"]: Required.\n request[\"webhook\"]: Optional string for asynchronous requests.\n\n returned object to be serialized into JSON and sent to the client.\n in this case: '{\"output\": \"hello\"}'\n \"\"\"\n return {\"output\": \"hello\"}\n\n\ndef gen_handler(request: Dict[str, Any], env: Env):\n \"\"\"\n append yield output to array, serialize into JSON and send to client.\n in this case: [0, 1, 2, 3, 4]\n \"\"\"\n for i in range(5):\n yield i\n\n\nasync def async_handler(request: Dict[str, Any], env: Env):\n \"\"\"\n returned object to be serialized into JSON and sent to the client.\n \"\"\"\n return {\"output\": \"hello\"}\n\n\nasync def async_gen_handler(request: Dict[str, Any], env: Env):\n \"\"\"\n append yield output to array, serialize into JSON and send to client.\n \"\"\"\n for i in range(10):\n yield i\n\n\ndef concurrency_modifier(current_allowed_concurrency: int) -> int:\n \"\"\"\n Adjusts the allowed concurrency level based on the current state.\n For example, if the current allowed concurrency is 3 and resources are sufficient,\n it can be increased to 5, allowing 5 tasks to run concurrently.\n \"\"\"\n allowed_concurrency = ...\n return allowed_concurrency\n\n\n\"\"\"\nRegister the handler with serverless.start().\nHandlers can be synchronous, asynchronous, generators, or asynchronous generators.\n\"\"\"\nstart({\n \"handler\": async_handler, \"concurrency_modifier\": concurrency_modifier\n})\n```\n\n## Logging\nWe provide a tool to log information. Default logging level is \"INFO\", you can call `logger.set_level(logging.DEBUG)` to change it.\n\n> Please make sure you update to the `latest` version to use this feature.\n```python\nfrom spirit_gpu import start, logger\nfrom spirit_gpu.env import Env\nfrom typing import Dict, Any\n\n\ndef handler(request: Dict[str, Any], env: Env):\n \"\"\"\n request: Dict[str, Any], from client http request body.\n request[\"input\"]: Required.\n request[\"webhook\"]: Optional string for asynchronous requests.\n\n we will only add request[\"meta\"][\"requestID\"] if it not exist in your request.\n \"\"\"\n request_id = request[\"meta\"][\"requestID\"]\n logger.info(\"start to handle\", request_id = request_id, caller=True)\n return {\"output\": \"hello\"}\n\nstart({\"handler\": handler})\n```\n\n## API\nPlease read [API](https://github.com/datastone-spirit/spirit-gpu/blob/main/API.md) or [\u4e2d\u6587 API](https://github.com/datastone-spirit/spirit-gpu/blob/main/API.zh.md) for how to use spirit-gpu serverless apis and some other import policies.\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "Python serverless framework for Datastone Spirit GPU.",
"version": "0.0.4",
"project_urls": {
"BugTracker": "https://github.com/datastone-spirit/spirit-gpu/issues",
"Documentation": "https://github.com/datastone-spirit/spirit-gpu/blob/main/README.md",
"Homepage": "https://github.com/datastone-spirit",
"Repository": "https://github.com/datastone-spirit/spirit-gpu"
},
"split_keywords": [
"serverless",
" ai",
" gpu",
" machine learning",
" sdk",
" library",
" python",
" api"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e16862645ea93d78715e52f34dec15ce0675932faa598c64c144b6b788d75f51",
"md5": "1f13a9942ee8d133eaefaac88fd85843",
"sha256": "d0ca7d4927f64f5012788bdc4f52142e664bba88b55d2b2195769efeec24ec49"
},
"downloads": -1,
"filename": "spirit_gpu-0.0.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1f13a9942ee8d133eaefaac88fd85843",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 15718,
"upload_time": "2024-09-27T06:26:29",
"upload_time_iso_8601": "2024-09-27T06:26:29.387605Z",
"url": "https://files.pythonhosted.org/packages/e1/68/62645ea93d78715e52f34dec15ce0675932faa598c64c144b6b788d75f51/spirit_gpu-0.0.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0360f341c5ee9842317dcf11c22e3ddef5919ff077634c4194277183cbc81163",
"md5": "ba5d9502894534a1d7f066dd531f883b",
"sha256": "89e86c198ab2e6270d48ccb31ecad4b38fcbad702dc59599be3fa488ce99f4a8"
},
"downloads": -1,
"filename": "spirit_gpu-0.0.4.tar.gz",
"has_sig": false,
"md5_digest": "ba5d9502894534a1d7f066dd531f883b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 18991,
"upload_time": "2024-09-27T06:26:30",
"upload_time_iso_8601": "2024-09-27T06:26:30.773308Z",
"url": "https://files.pythonhosted.org/packages/03/60/f341c5ee9842317dcf11c22e3ddef5919ff077634c4194277183cbc81163/spirit_gpu-0.0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-27 06:26:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "datastone-spirit",
"github_project": "spirit-gpu",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "requests",
"specs": [
[
">=",
"2.31.0"
]
]
},
{
"name": "PyYAML",
"specs": [
[
">=",
"6.0.1"
]
]
},
{
"name": "backoff",
"specs": [
[
">=",
"2.2.1"
]
]
},
{
"name": "aiohttp",
"specs": [
[
">=",
"3.9.1"
]
]
},
{
"name": "pydantic",
"specs": [
[
">=",
"2.5.2"
]
]
}
],
"lcname": "spirit-gpu"
}