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

Update deployer/gradio_generator.py

Browse files
Files changed (1) hide show
  1. deployer/gradio_generator.py +38 -23
deployer/gradio_generator.py CHANGED
@@ -2,52 +2,67 @@ import gradio as gr
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)
 
2
  from .simulator_interface import VirtualRobot
3
  import logging
4
 
5
+ def create_standalone_interface(title="RoboSage", description="Your virtual assistant"):
6
+ """
7
+ Creates a completely self-contained interface that avoids component reference issues
8
+ by defining and using all components in the same context.
9
+ """
10
  robot = VirtualRobot()
11
 
12
+ # Define the command processor
13
+ def process_command(cmd):
14
  try:
15
+ return robot.perform_action(cmd)
16
  except Exception as e:
17
+ logging.error(f"Command processing error: {e}")
18
  return f"⚠️ Error: {str(e)}"
19
 
20
+ # Build the interface in one atomic operation
21
+ with gr.Blocks(title=title, analytics_enabled=False) as interface:
22
  gr.Markdown(f"# 🤖 {title}\n\n{description}")
23
 
24
+ # Create and immediately use components
25
  with gr.Row():
26
+ cmd_input = gr.Textbox(
27
+ label="Command Input",
28
+ placeholder="Try 'wave' or 'say hello'...",
29
+ elem_id="cmd_input"
30
+ )
31
+ submit_btn = gr.Button(
32
+ "Submit",
33
+ variant="primary",
34
+ elem_id="submit_btn"
35
+ )
36
 
37
+ response_output = gr.Textbox(
38
+ label="Robot Response",
39
+ interactive=False,
40
+ elem_id="response_output"
41
+ )
42
 
43
  # Define all event handlers in the same context
44
  submit_btn.click(
45
  fn=process_command,
46
+ inputs=cmd_input,
47
+ outputs=response_output
48
  )
49
 
50
+ cmd_input.submit(
51
  fn=process_command,
52
+ inputs=cmd_input,
53
+ outputs=response_output
54
  )
55
 
56
+ # Add clear functionality
57
+ gr.Button("Clear").click(
58
+ lambda: ("", ""),
59
  inputs=None,
60
+ outputs=[cmd_input, response_output]
61
  )
62
 
63
  return interface
64
 
65
  def launch_gradio_app(title="RoboSage", description="Your virtual assistant"):
66
+ """Public interface that guarantees proper initialization"""
67
  logging.basicConfig(level=logging.INFO)
68
+ return create_standalone_interface(title, description)