# filename: request_router.py import gradio as gr from typing import Any, Dict # Example methods to demonstrate routing def method_one(input_data: Dict[str, Any]) -> Dict[str, Any]: # Process input_data for method_one return {"result": "Method One executed", "input": input_data} def method_two(input_data: Dict[str, Any]) -> Dict[str, Any]: # Process input_data for method_two return {"result": "Method Two executed", "input": input_data} # Main request router method def request_router(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]: # Mapping method names to actual functions method_mapping = { "method_one": method_one, "method_two": method_two } # Retrieve the appropriate method based on the name method = method_mapping.get(name) if method is None: return {"error": "Method not found"} # Call the method with the provided input data output = method(input_data) return output # Gradio Interface def launch_gradio_app(): with gr.Blocks() as demo: gr.Markdown("# Request Router API") with gr.Row(): name_input = gr.Textbox(label="Method Name") input_data_input = gr.Textbox(label="Input Data (JSON format)") output_output = gr.JSON(label="Output") submit_button = gr.Button("Submit") submit_button.click( fn=lambda name, input_data: request_router(name, eval(input_data)), inputs=[name_input, input_data_input], outputs=output_output ) demo.launch() if __name__ == "__main__": launch_gradio_app()