Lightning-AI / LitServe

Lightning-fast serving engine for any AI model of any size. Flexible. Easy. Enterprise-scale.
https://lightning.ai/docs/litserve
Apache License 2.0
2.5k stars 158 forks source link

Supporting query params alongside file upload #272

Closed energydrink9 closed 2 months ago

energydrink9 commented 2 months ago

⚠️ BEFORE SUBMITTING, READ:

We're excited for your request! However, here are things we are not interested in:


🚀 Feature

Hello team,

Please correct me if I'm wrong but I couldn't find a way to specify query parameters for my endpoint when using a request annotated with FileUpload in decode_request. Is it possible to add support for them?

Motivation

It's sometimes useful to specify additional parameters in the request in order to control inference.

aniketmaurya commented 2 months ago

Hi @energydrink9, you can send multiple pieces of data along with a file upload, as shown in the following example. You can send the additional data as JSON and access it directly using the corresponding keys. To access the file, you can retrieve the filename as follows:

Server example

from PIL import Image
import litserve as ls

class ImageAPI(ls.LitAPI):
    def setup(self, device):
        self.model = lambda x: x.size

    def decode_request(self, request):
        print(request["model_name"])
        return Image.open(request["request"].filename)

    def predict(self, x):
        return self.model(x)

    def encode_response(self, output):
        return {"output": output}

if __name__ == "__main__":
    api = ImageAPI()
    server = ls.LitServer(api)
    server.run(port=8000)

Client

import requests

url = "http://127.0.0.1:8000/predict"
image_path = "image.jpg"

# Open the image file in binary mode
with open(image_path, 'rb') as image_file:
    files = {'request': image_file}
    response = requests.post(url, files=files, data={"model_name": "resnet50"})
print(response.json())
aniketmaurya commented 2 months ago

Added a new section for this in the documentation here. Please let me know if it helps!

aniketmaurya commented 2 months ago

Closing the issue for now, feel free to reopen if you are still facing this issue!

energydrink9 commented 2 months ago

Thank you Aniket, I managed to implement the parameterization following your advice