K00B404 commited on
Commit
c453d15
·
verified ·
1 Parent(s): a4cd24c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -16
app.py CHANGED
@@ -1,33 +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():
 
1
  # filename: request_router.py
2
 
3
  import gradio as gr
4
+ import os
5
+ import importlib.util
6
  from typing import Any, Dict
7
 
8
+ # Define the path to the tools folder
9
+ TOOLS_DIR = './tools'
 
 
10
 
11
+ # Function to dynamically load methods from files in the ./tools folder
12
+ def load_methods_from_tools():
13
+ method_mapping = {}
14
+
15
+ # List all .py files in the tools directory
16
+ for filename in os.listdir(TOOLS_DIR):
17
+ if filename.endswith('.py'):
18
+ method_name = filename[:-3].lower() # Remove '.py' and convert to lowercase
19
+
20
+ # Load the module
21
+ module_path = os.path.join(TOOLS_DIR, filename)
22
+ spec = importlib.util.spec_from_file_location(method_name, module_path)
23
+ module = importlib.util.module_from_spec(spec)
24
+ spec.loader.exec_module(module)
25
+
26
+ # Map all functions from the module to the method mapping
27
+ for attr in dir(module):
28
+ if callable(getattr(module, attr)) and not attr.startswith("__"):
29
+ method_mapping[method_name] = getattr(module, attr)
30
+
31
+ return method_mapping
32
+
33
+ # Load the method mapping at the start
34
+ method_mapping = load_methods_from_tools()
35
 
36
  # Main request router method
37
  def request_router(name: str, input_data: Dict[str, Any]) -> Dict[str, Any]:
 
 
 
 
 
 
38
  # Retrieve the appropriate method based on the name
39
  method = method_mapping.get(name)
40
  if method is None:
41
  return {"error": "Method not found"}
42
+
43
  # Call the method with the provided input data
44
+ try:
45
+ output = method(input_data)
46
+ return output
47
+ except Exception as e:
48
+ return {"error": str(e)}
49
 
50
  # Gradio Interface
51
  def launch_gradio_app():