Skip to content

Commit

Permalink
update README and documentation to include API request examples for s…
Browse files Browse the repository at this point in the history
…tructured and binary data
  • Loading branch information
Aleksandr Movchan committed Dec 12, 2024
1 parent 0fe0c92 commit ccdeb48
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,75 @@ aana_app.deploy() # Deploys the application.

All you need to do is define the deployments and endpoints you want to use in your application, and Aana SDK will take care of the rest.


## API

Aana SDK uses form data for API requests, which allows sending both binary data and structured fields in a single request. The request body is sent as a JSON string in the `body` field, and any binary data is sent as files.

### Making API Requests

You can send requests to the SDK endpoints with only structured data or a combination of structured data and binary data.

#### Only Structured Data
When your request includes only structured data, you can send it as a JSON string in the `body` field.

- **cURL Example:**
```bash
curl http://127.0.0.1:8000/endpoint \
-F body='{"input": "data", "param": "value"}'
```

- **Python Example:**
```python
import json, requests
url = "http://127.0.0.1:8000/endpoint"
body = {
"input": "data",
"param": "value"
}
response = requests.post(
url,
data={"body": json.dumps(body)}
)
print(response.json())
```

#### With Binary Data
When your request includes binary files (images, audio, etc.), you can send them as files in the request and include the names of the files in the `body` field as a reference.

For example, if you want to send an image, you can use [`aana.core.models.image.ImageInput`](https://mobiusml.github.io/aana_sdk/reference/models/media/#aana.core.models.ImageInput) as the input type that supports binary data upload. The `content` field in the input type should be set to the name of the file you are sending.

- **cURL Example:**
```bash
curl http://127.0.0.1:8000/process_images \
-H "Content-Type: multipart/form-data" \
-F body='{"image": {"content": "file1"}}' \
-F file1="@image.jpeg"
```

- **Python Example:**
```python
import json, requests
url = "http://127.0.0.1:8000/process_images"
body = {
"image": {"content": "file1"}
}
with open("image.jpeg", "rb") as file:
files = {"file1": file}
response = requests.post(
url,
data={"body": json.dumps(body)},
files=files
)
print(response.text)
```

## Serve Config Files

The [Serve Config Files](https://docs.ray.io/en/latest/serve/production-guide/config.html#serve-config-files) is the recommended way to deploy and update your applications in production. Aana SDK provides a way to build the Serve Config Files for the Aana applications. See the [Serve Config Files documentation](https://mobiusml.github.io/aana_sdk/pages/serve_config_files/) on how to build and deploy the applications using the Serve Config Files.
Expand Down
1 change: 0 additions & 1 deletion aana/tests/units/test_image_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ def test_imagelistinput():

def test_imagelistinput_set_files():
"""Test that the files can be set for the images."""
# files = [b"image data 1", b"image data 2"]
files = {
"file": b"image data 1",
"numpy_file": b"image data 2",
Expand Down
70 changes: 70 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,73 @@ aana_app.deploy() # Deploys the application.

All you need to do is define the deployments and endpoints you want to use in your application, and Aana SDK will take care of the rest.


## API

Aana SDK uses form data for API requests, which allows sending both binary data and structured fields in a single request. The request body is sent as a JSON string in the `body` field, and any binary data is sent as files.

### Making API Requests

You can send requests to the SDK endpoints with only structured data or a combination of structured data and binary data.

#### Only Structured Data
When your request includes only structured data, you can send it as a JSON string in the `body` field.

- **cURL Example:**
```bash
curl http://127.0.0.1:8000/endpoint \
-F body='{"input": "data", "param": "value"}'
```

- **Python Example:**
```python
import json, requests
url = "http://127.0.0.1:8000/endpoint"
body = {
"input": "data",
"param": "value"
}
response = requests.post(
url,
data={"body": json.dumps(body)}
)
print(response.json())
```

#### With Binary Data
When your request includes binary files (images, audio, etc.), you can send them as files in the request and include the names of the files in the `body` field as a reference.

For example, if you want to send an image, you can use [`aana.core.models.image.ImageInput`](reference/models/media.md#aana.core.models.ImageInput) as the input type that supports binary data upload. The `content` field in the input type should be set to the name of the file you are sending.

You can send multiple files in a single request by including multiple files in the request and referencing them in the `body` field even if they are of different types.

- **cURL Example:**
```bash
curl http://127.0.0.1:8000/process_images \
-H "Content-Type: multipart/form-data" \
-F body='{"image": {"content": "file1"}}' \
-F file1="@image.jpeg"
```

- **Python Example:**
```python
import json, requests
url = "http://127.0.0.1:8000/process_images"
body = {
"image": {"content": "file1"}
}
with open("image.jpeg", "rb") as file:
files = {"file1": file}
response = requests.post(
url,
data={"body": json.dumps(body)},
files=files
)
print(response.text)
```

0 comments on commit ccdeb48

Please sign in to comment.