Processes API calls from the frontend. First preprocesses the data, then runs the relevant function, then postprocesses the output. Parameters: fn_index: Index of function to run. inputs: input data received from the frontend username: nam
(
self,
fn_index: int,
inputs: List[Any],
username: str = None,
state: Dict[int, Any] | List[Dict[int, Any]] | None = None,
iterators: Dict[int, Any] | None = None,
)
| 932 | return output |
| 933 | |
| 934 | async def process_api( |
| 935 | self, |
| 936 | fn_index: int, |
| 937 | inputs: List[Any], |
| 938 | username: str = None, |
| 939 | state: Dict[int, Any] | List[Dict[int, Any]] | None = None, |
| 940 | iterators: Dict[int, Any] | None = None, |
| 941 | ) -> Dict[str, Any]: |
| 942 | """ |
| 943 | Processes API calls from the frontend. First preprocesses the data, |
| 944 | then runs the relevant function, then postprocesses the output. |
| 945 | Parameters: |
| 946 | fn_index: Index of function to run. |
| 947 | inputs: input data received from the frontend |
| 948 | username: name of user if authentication is set up (not used) |
| 949 | state: data stored from stateful components for session (key is input block id) |
| 950 | iterators: the in-progress iterators for each generator function (key is function index) |
| 951 | Returns: None |
| 952 | """ |
| 953 | block_fn = self.fns[fn_index] |
| 954 | batch = self.dependencies[fn_index]["batch"] |
| 955 | |
| 956 | if batch: |
| 957 | max_batch_size = self.dependencies[fn_index]["max_batch_size"] |
| 958 | batch_sizes = [len(inp) for inp in inputs] |
| 959 | batch_size = batch_sizes[0] |
| 960 | if inspect.isasyncgenfunction(block_fn.fn) or inspect.isgeneratorfunction( |
| 961 | block_fn.fn |
| 962 | ): |
| 963 | raise ValueError("Gradio does not support generators in batch mode.") |
| 964 | if not all(x == batch_size for x in batch_sizes): |
| 965 | raise ValueError( |
| 966 | f"All inputs to a batch function must have the same length but instead have sizes: {batch_sizes}." |
| 967 | ) |
| 968 | if batch_size > max_batch_size: |
| 969 | raise ValueError( |
| 970 | f"Batch size ({batch_size}) exceeds the max_batch_size for this function ({max_batch_size})" |
| 971 | ) |
| 972 | |
| 973 | inputs = [self.preprocess_data(fn_index, i, state) for i in zip(*inputs)] |
| 974 | result = await self.call_function(fn_index, zip(*inputs), None) |
| 975 | preds = result["prediction"] |
| 976 | data = [self.postprocess_data(fn_index, o, state) for o in zip(*preds)] |
| 977 | data = list(zip(*data)) |
| 978 | is_generating, iterator = None, None |
| 979 | else: |
| 980 | inputs = self.preprocess_data(fn_index, inputs, state) |
| 981 | iterator = iterators.get(fn_index, None) if iterators else None |
| 982 | result = await self.call_function(fn_index, inputs, iterator) |
| 983 | data = self.postprocess_data(fn_index, result["prediction"], state) |
| 984 | is_generating, iterator = result["is_generating"], result["iterator"] |
| 985 | |
| 986 | block_fn.total_runtime += result["duration"] |
| 987 | block_fn.total_runs += 1 |
| 988 | |
| 989 | return { |
| 990 | "data": data, |
| 991 | "is_generating": is_generating, |
nothing calls this directly
no test coverage detected