mgbam commited on
Commit
84fba92
·
verified ·
1 Parent(s): 37927c4

Update deployer/gradio_generator.py

Browse files
Files changed (1) hide show
  1. deployer/gradio_generator.py +28 -26
deployer/gradio_generator.py CHANGED
@@ -2,50 +2,52 @@ import gradio as gr
2
  from .simulator_interface import VirtualRobot
3
  import logging
4
 
5
- def create_robot_app(title="RoboSage", description="Your virtual assistant"):
6
- """Creates a fully self-contained robot interface"""
7
  robot = VirtualRobot()
8
 
9
- def execute_command(command):
10
- """Process commands with error handling"""
11
  try:
12
  return robot.perform_action(command)
13
  except Exception as e:
14
- logging.error(f"Command failed: {e}")
15
  return f"⚠️ Error: {str(e)}"
16
 
17
  # Build interface in one atomic operation
18
- with gr.Blocks(title=title) as app:
19
  gr.Markdown(f"# 🤖 {title}\n\n{description}")
20
 
21
- # Define components and handlers together
22
- command_box = gr.Textbox(label="Command", placeholder="Try 'wave' or 'say hello'")
23
- response_box = gr.Textbox(label="Response", interactive=False)
 
 
24
 
25
- # Primary interaction
26
- gr.Button("Submit").click(
27
- execute_command,
28
- inputs=command_box,
29
- outputs=response_box
 
 
30
  )
31
 
32
- # Alternate submission method
33
- command_box.submit(
34
- execute_command,
35
- inputs=command_box,
36
- outputs=response_box
37
  )
38
 
39
- # Utility button
40
- gr.Button("Clear").click(
41
- lambda: ("", ""),
42
  inputs=None,
43
- outputs=[command_box, response_box]
44
  )
45
 
46
- return app
47
 
48
  def launch_gradio_app(title="RoboSage", description="Your virtual assistant"):
49
- """Public interface that guarantees proper initialization"""
50
  logging.basicConfig(level=logging.INFO)
51
- return create_robot_app(title, description)
 
2
  from .simulator_interface import VirtualRobot
3
  import logging
4
 
5
+ def build_robot_interface(title="RoboSage", description="Your virtual assistant"):
6
+ """Creates a completely self-contained interface without external references"""
7
  robot = VirtualRobot()
8
 
9
+ # Define all processing logic first
10
+ def process_command(command):
11
  try:
12
  return robot.perform_action(command)
13
  except Exception as e:
14
+ logging.error(f"Command processing failed: {e}")
15
  return f"⚠️ Error: {str(e)}"
16
 
17
  # Build interface in one atomic operation
18
+ with gr.Blocks(title=title) as interface:
19
  gr.Markdown(f"# 🤖 {title}\n\n{description}")
20
 
21
+ # Create and use components immediately in the same context
22
+ with gr.Row():
23
+ input_box = gr.Textbox(label="Command", placeholder="Try 'wave' or 'say hello'")
24
+ submit_btn = gr.Button("Submit", variant="primary")
25
+ clear_btn = gr.Button("Clear")
26
 
27
+ output_box = gr.Textbox(label="Response", interactive=False)
28
+
29
+ # Define all event handlers in the same context
30
+ submit_btn.click(
31
+ fn=process_command,
32
+ inputs=input_box,
33
+ outputs=output_box
34
  )
35
 
36
+ input_box.submit(
37
+ fn=process_command,
38
+ inputs=input_box,
39
+ outputs=output_box
 
40
  )
41
 
42
+ clear_btn.click(
43
+ fn=lambda: ("", ""),
 
44
  inputs=None,
45
+ outputs=[input_box, output_box]
46
  )
47
 
48
+ return interface
49
 
50
  def launch_gradio_app(title="RoboSage", description="Your virtual assistant"):
51
+ """Public interface that ensures proper initialization"""
52
  logging.basicConfig(level=logging.INFO)
53
+ return build_robot_interface(title, description)