from typing import Any, Coroutine from fastapi import Request, Response, status from fastapi.responses import JSONResponse from .blackbox import Blackbox from injector import singleton @singleton class TextAndImage(Blackbox): def __call__(self, *args, **kwargs): return self.processing(*args, **kwargs) def processing(self, text: str, image: bytes): # TODO: implement the processing logic return "text and image processing..." def valid(self, *args, **kwargs) -> bool: return True async def fast_api_handler(self, request: Request) -> Response: # If the request is a form data, you can use the following code data = (await request.form()).get("image") if data is None: return JSONResponse(content={"error": "image is required"}, status_code=status.HTTP_400_BAD_REQUEST) imageBytes = await data.read() text = (await request.form()).get("text") result = self.processing(text, imageBytes) return JSONResponse(content={"result": result }, status_code=status.HTTP_200_OK)