mgbam commited on
Commit
edfe384
·
verified ·
1 Parent(s): eada831

Update deployer/gradio_generator.py

Browse files
Files changed (1) hide show
  1. deployer/gradio_generator.py +74 -65
deployer/gradio_generator.py CHANGED
@@ -1,107 +1,116 @@
1
- # gradio_generator.py - Robust Gradio Interface
2
  import gradio as gr
3
- from simulator_interface import VirtualRobot
4
- from typing import Optional
5
  import logging
6
 
7
- logging.basicConfig(level=logging.INFO)
8
-
9
- class RoboSageInterface:
10
- """Main application interface with lifecycle management."""
11
 
12
  def __init__(self):
 
13
  self.robot = VirtualRobot()
14
- self.interface = self._create_interface()
15
-
16
- def _robot_response(self, user_input: str) -> str:
17
- """Process user input with error handling."""
18
- if not user_input or not isinstance(user_input, str):
19
- logging.warning("Invalid input received")
20
- return "❌ Please enter valid text"
21
-
22
- try:
23
- logging.info("Processing input: %s", user_input)
24
- return self.robot.perform_action(user_input)
25
- except Exception as e:
26
- logging.error("Response generation failed: %s", str(e))
27
- return f"❌ System error: {str(e)}"
28
-
29
- def _create_interface(self) -> gr.Blocks:
30
- """Build Gradio interface with proper component binding."""
31
  with gr.Blocks(
32
- title="RoboSage",
33
- css=".gradio-container {max-width: 600px !important}"
 
 
 
34
  ) as interface:
35
 
36
  # Header section
37
  gr.Markdown("""
38
- # 🤖 RoboSage
39
- ### Your personal virtual assistant
40
  """)
41
 
42
  # Interaction panel
43
  with gr.Row():
44
- with gr.Column(scale=4):
45
- self.input_box = gr.Textbox(
46
- label="Command Input",
47
- placeholder="Try 'hello' or 'say something'...",
48
- max_lines=3
49
- )
50
- with gr.Column(scale=1):
51
- self.submit_btn = gr.Button(
52
- "Send",
53
- variant="primary",
54
- size="lg"
55
- )
 
 
 
 
 
56
 
57
  # Output section
58
- self.output_box = gr.Textbox(
59
  label="Robot Response",
60
  interactive=False,
61
- lines=5
 
62
  )
63
 
64
- # History section
65
- with gr.Accordion("Session History", open=False):
66
- self.history = gr.JSON(
67
- label="Command History",
68
- value={"commands": []}
69
- )
70
-
71
  # Event handlers
72
- self.submit_btn.click(
73
- fn=self._robot_response,
74
- inputs=self.input_box,
75
- outputs=self.output_box
76
  )
77
 
78
- self.input_box.submit(
79
- fn=self._robot_response,
80
- inputs=self.input_box,
81
- outputs=self.output_box
82
  )
83
 
 
 
 
 
 
 
84
  return interface
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- def launch_application() -> gr.Blocks:
87
  """Initialize and return the application interface."""
88
  try:
89
- logging.info("Initializing RoboSage application")
90
- app = RoboSageInterface()
91
  return app.interface
92
  except Exception as e:
93
  logging.critical("Application failed to initialize: %s", str(e))
94
  raise
95
 
96
  if __name__ == "__main__":
97
- # Production configuration
98
- app = launch_application()
99
- app.launch(
100
  server_name="0.0.0.0",
101
  server_port=7860,
102
  share=False,
103
  favicon_path=None,
104
- auth=None,
105
- ssl_verify=True,
106
  debug=False
107
  )
 
 
1
  import gradio as gr
2
+ from .simulator_interface import VirtualRobot # Relative import
 
3
  import logging
4
 
5
+ class RoboSageApp:
6
+ """Main application class for Gradio interface."""
 
 
7
 
8
  def __init__(self):
9
+ """Initialize application components."""
10
  self.robot = VirtualRobot()
11
+ self._setup_logging()
12
+ self.interface = self._build_interface()
13
+
14
+ def _setup_logging(self):
15
+ """Configure application logging."""
16
+ logging.basicConfig(
17
+ level=logging.INFO,
18
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
19
+ )
20
+ self.logger = logging.getLogger(__name__)
21
+
22
+ def _build_interface(self) -> gr.Blocks:
23
+ """Construct the Gradio interface."""
 
 
 
 
24
  with gr.Blocks(
25
+ title="RoboSage Assistant",
26
+ css="""
27
+ .gradio-container {max-width: 600px !important}
28
+ .dark {background: #f9f9f9}
29
+ """
30
  ) as interface:
31
 
32
  # Header section
33
  gr.Markdown("""
34
+ # 🤖 RoboSage
35
+ ### Your Personal Virtual Assistant
36
  """)
37
 
38
  # Interaction panel
39
  with gr.Row():
40
+ self.input_txt = gr.Textbox(
41
+ label="Your Command",
42
+ placeholder="Try 'wave' or 'say hello'...",
43
+ max_lines=3,
44
+ interactive=True
45
+ )
46
+
47
+ with gr.Row():
48
+ self.send_btn = gr.Button(
49
+ "Submit",
50
+ variant="primary",
51
+ size="lg"
52
+ )
53
+ self.clear_btn = gr.Button(
54
+ "Clear",
55
+ variant="secondary"
56
+ )
57
 
58
  # Output section
59
+ self.output_txt = gr.Textbox(
60
  label="Robot Response",
61
  interactive=False,
62
+ lines=5,
63
+ show_copy_button=True
64
  )
65
 
 
 
 
 
 
 
 
66
  # Event handlers
67
+ self.send_btn.click(
68
+ fn=self._process_command,
69
+ inputs=self.input_txt,
70
+ outputs=self.output_txt
71
  )
72
 
73
+ self.input_txt.submit(
74
+ fn=self._process_command,
75
+ inputs=self.input_txt,
76
+ outputs=self.output_txt
77
  )
78
 
79
+ self.clear_btn.click(
80
+ fn=lambda: ("", ""),
81
+ inputs=None,
82
+ outputs=[self.input_txt, self.output_txt]
83
+ )
84
+
85
  return interface
86
+
87
+ def _process_command(self, command: str) -> str:
88
+ """Handle user commands with logging."""
89
+ self.logger.info("Processing command: %s", command)
90
+ try:
91
+ response = self.robot.perform_action(command)
92
+ self.logger.info("Generated response: %s", response)
93
+ return response
94
+ except Exception as e:
95
+ self.logger.error("Command processing failed: %s", str(e))
96
+ return f"⚠️ System error: {str(e)}"
97
 
98
+ def launch_app() -> gr.Blocks:
99
  """Initialize and return the application interface."""
100
  try:
101
+ app = RoboSageApp()
 
102
  return app.interface
103
  except Exception as e:
104
  logging.critical("Application failed to initialize: %s", str(e))
105
  raise
106
 
107
  if __name__ == "__main__":
108
+ # Local development configuration
109
+ interface = launch_app()
110
+ interface.launch(
111
  server_name="0.0.0.0",
112
  server_port=7860,
113
  share=False,
114
  favicon_path=None,
 
 
115
  debug=False
116
  )