mgbam commited on
Commit
157cb00
Β·
verified Β·
1 Parent(s): 871a880

Update deployer/gradio_generator.py

Browse files
Files changed (1) hide show
  1. deployer/gradio_generator.py +87 -38
deployer/gradio_generator.py CHANGED
@@ -1,58 +1,107 @@
 
1
  import gradio as gr
2
- from deployer.simulator_interface import VirtualRobot
 
 
3
 
4
- class RoboSageApp:
 
 
 
 
5
  def __init__(self):
6
- self.bot = VirtualRobot()
7
  self.interface = self._create_interface()
8
 
9
- def _robot_behavior(self, user_input: str):
10
- """Handle robot interaction logic"""
11
- text = user_input.lower().strip()
12
-
13
- if any(g in text for g in ["hello", "hi", "hey", "welcome"]):
14
- return self.bot.perform_action("wave") + "\n" + self.bot.perform_action("say Hello there!")
15
- if text.startswith("say"):
16
- return self.bot.perform_action(text)
17
- return self.bot.perform_action("say I didn't understand that. Try again!")
 
 
 
18
 
19
- def _create_interface(self):
20
- """Create the Gradio interface with proper component binding"""
21
- with gr.Blocks() as demo:
22
- gr.Markdown("# πŸ€– RoboSage App\n\nYour robot, your voice.")
 
 
 
 
 
 
 
 
23
 
 
24
  with gr.Row():
25
- self.input_text = gr.Textbox(label="Speak or Type", lines=1)
26
- self.send_button = gr.Button("Send", variant="primary")
 
 
 
 
 
 
 
 
 
 
27
 
28
- self.output_text = gr.Textbox(label="Robot Response", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- # Event handlers using bound methods
31
- self.send_button.click(
32
- fn=self._robot_behavior,
33
- inputs=self.input_text,
34
- outputs=self.output_text
35
  )
36
 
37
- self.input_text.submit(
38
- fn=self._robot_behavior,
39
- inputs=self.input_text,
40
- outputs=self.output_text
41
  )
42
-
43
- return demo
44
 
45
- def launch_gradio_app():
46
- """Launch the application with proper component lifecycle management"""
47
- app = RoboSageApp()
48
- return app.interface
 
 
 
 
 
49
 
50
  if __name__ == "__main__":
51
- # For local testing with explicit configuration
52
- interface = launch_gradio_app()
53
- interface.launch(
54
  server_name="0.0.0.0",
55
  server_port=7860,
56
  share=False,
57
- ssr_mode=False # Explicitly disable SSR if not needed
 
 
 
58
  )
 
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
  )