This message was deleted.
# ask-for-help
s
This message was deleted.
и
client.py
Copy code
import json
from collections import defaultdict
from pathlib import Path

from bentoml.client import Client
from loguru import logger


BENTOML_URL = "<http://0.0.0.0:8996>"
IMG_PATH = "./image_example"


def call(json_data):
    client = Client.from_url(BENTOML_URL)
    return client.process_batch(folder_name=IMG_PATH, results_container=json_data)

if __name__ == "__main__":
    results_container = defaultdict(lambda: {"idx": None, "name": None, "price": None, "file": None})

    images = list(Path(IMG_PATH).glob("*.jpg"))

    for idx in range(0,15):
        results_container[idx]["idx"] = idx
        results_container[idx]["file"] = images.name

    # Convert defaultdict to JSON
    json_data = json.dumps(dict(results_container), indent=2)

    logger.info(call(json_data))
service.py
- this version works fine
Copy code
input_spec = Multipart(folder_name=Text(), results_container=JSON())
@svc.api(input=input_spec, output=JSON())
def process_batch(folder_name: str, results_container: json):
    loaded_dict = json.loads(results_container)
client.py
this version doesn't work
Copy code
#imports

BENTOML_URL = "<http://0.0.0.0:8996>"
IMG_PATH = "./image_example"


def call(json_data):
    client = Client.from_url(BENTOML_URL)
    return client.process_batch(input=json_data)

if __name__ == "__main__":
    results_container = defaultdict(lambda: {"idx": None, "name": None, "price": None, "file": None})

    images = list(Path(IMG_PATH).glob("*.jpg"))

    for idx in range(0,15):
        results_container[idx]["idx"] = idx
        results_container[idx]["file"] = images.name

    # Convert defaultdict to JSON
    json_data = json.dumps(dict(results_container), indent=2)

    logger.info(call(json_data))
service.py
this version doesn't work
Copy code
@svc.api(input=JSON(), output=JSON())
def process_batch(input: json):
    loaded_dict = json.loads(input)
t
• ist of all use Multipart when you need different types of inputs like image and json as input. In this case you'll write something like:
img_json_input_spec = Multipart(img=Image(), data=JSON())
@svc.api(input=img_json_input_spec, output=Image())
the
output=Image()
means the output from endpoint will be image. • If you only need a json as input then its more advisable to create a pydantic model of your json like:
from pydantic import BaseModel
import typing as t
from <http://bentoml.io|bentoml.io> import Image, JSON
class JsonArgs(BaseModel):
prompt: str
height: t.Optional[int] = 1024
width: t.Optional[int] = 1024
class Config:
extra = "allow"
sample = JsonArgs(prompt="custom text message")
then for service endpoint:
@svc.api(input=JSON.from_sample(sample), output=Image())
def predict_sentiment(input_data):
request_dict = input_data.dict()
it will ensure that json is consistent and help to avoid errors.
и
Great, thanks a lot!