File size: 1,628 Bytes
a4cd24c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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()