|
|
|
|
|
import gradio as gr |
|
from typing import Any, Dict |
|
|
|
|
|
def method_one(input_data: Dict[str, Any]) -> Dict[str, Any]: |
|
|
|
return {"result": "Method One executed", "input": input_data} |
|
|
|
def method_two(input_data: Dict[str, Any]) -> Dict[str, Any]: |
|
|
|
return {"result": "Method Two executed", "input": input_data} |
|
|
|
|
|
def request_router(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]: |
|
|
|
method_mapping = { |
|
"method_one": method_one, |
|
"method_two": method_two |
|
} |
|
|
|
|
|
method = method_mapping.get(name) |
|
if method is None: |
|
return {"error": "Method not found"} |
|
|
|
|
|
output = method(input_data) |
|
return output |
|
|
|
|
|
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() |