Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# filename: request_router.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from typing import Any, Dict
|
| 5 |
+
|
| 6 |
+
# Example methods to demonstrate routing
|
| 7 |
+
def method_one(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 8 |
+
# Process input_data for method_one
|
| 9 |
+
return {"result": "Method One executed", "input": input_data}
|
| 10 |
+
|
| 11 |
+
def method_two(input_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 12 |
+
# Process input_data for method_two
|
| 13 |
+
return {"result": "Method Two executed", "input": input_data}
|
| 14 |
+
|
| 15 |
+
# Main request router method
|
| 16 |
+
def request_router(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]:
|
| 17 |
+
# Mapping method names to actual functions
|
| 18 |
+
method_mapping = {
|
| 19 |
+
"method_one": method_one,
|
| 20 |
+
"method_two": method_two
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# Retrieve the appropriate method based on the name
|
| 24 |
+
method = method_mapping.get(name)
|
| 25 |
+
if method is None:
|
| 26 |
+
return {"error": "Method not found"}
|
| 27 |
+
|
| 28 |
+
# Call the method with the provided input data
|
| 29 |
+
output = method(input_data)
|
| 30 |
+
return output
|
| 31 |
+
|
| 32 |
+
# Gradio Interface
|
| 33 |
+
def launch_gradio_app():
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# Request Router API")
|
| 36 |
+
with gr.Row():
|
| 37 |
+
name_input = gr.Textbox(label="Method Name")
|
| 38 |
+
input_data_input = gr.Textbox(label="Input Data (JSON format)")
|
| 39 |
+
output_output = gr.JSON(label="Output")
|
| 40 |
+
|
| 41 |
+
submit_button = gr.Button("Submit")
|
| 42 |
+
submit_button.click(
|
| 43 |
+
fn=lambda name, input_data: request_router(name, eval(input_data)),
|
| 44 |
+
inputs=[name_input, input_data_input],
|
| 45 |
+
outputs=output_output
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
demo.launch()
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
launch_gradio_app()
|