mgbam commited on
Commit
3a39257
Β·
verified Β·
1 Parent(s): a475429

Update deployer/gradio_generator.py

Browse files
Files changed (1) hide show
  1. deployer/gradio_generator.py +59 -23
deployer/gradio_generator.py CHANGED
@@ -1,7 +1,29 @@
1
- # gradio_generator.py - CPU & Spaces-compatible Gradio interface for RoboSage
2
 
 
3
  import gradio as gr
4
  from deployer.simulator_interface import VirtualRobot
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
 
7
  def robot_behavior(user_input: str) -> str:
@@ -23,42 +45,56 @@ def robot_behavior(user_input: str) -> str:
23
  return bot.perform_action("say I'm sorry, I didn't understand that.")
24
 
25
 
 
 
 
 
 
 
 
 
26
  def launch_gradio_app(
27
  title: str = "RoboSage App",
28
  description: str = "Your robot, your voice."
29
  ) -> gr.Blocks:
30
  """
31
- Constructs and returns a Gradio Blocks app with two sections:
32
- 1. Create your robot app (stubbed)
33
- 2. Test your robot in simulator
34
  """
35
  with gr.Blocks() as demo:
36
- gr.Markdown(f"# πŸš€ {title}\n\n{description}")
37
-
38
- # Section: App creation (stubbed)
39
- with gr.Accordion("🎨 Create your robot app", open=True):
40
- idea = gr.Textbox(label="Robot Idea", placeholder="e.g. A friendly greeting robot.")
41
- gen_btn = gr.Button("Generate App", key="gen-app-btn")
42
- gen_status = gr.Textbox(label="Generation Status", interactive=False)
43
- gen_btn.click(
44
- fn=lambda i: "βœ… App generation pipeline executed.",
45
- inputs=[idea],
46
- outputs=[gen_status]
47
- )
48
 
49
- # Section: Robot simulation
50
  with gr.Accordion("πŸ€– Test your robot", open=True):
51
- inp = gr.Textbox(
52
- label="Speak or Type Command",
 
53
  placeholder="Type 'hello' or 'say Have a great day!'"
54
  )
55
- out = gr.Textbox(label="Robot Response", lines=4, interactive=False)
56
- btn = gr.Button("Send", key="simulate-btn")
57
- btn.click(fn=robot_behavior, inputs=[inp], outputs=[out])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  return demo
60
 
61
 
62
  if __name__ == "__main__":
63
  app = launch_gradio_app()
64
- app.launch()
 
1
+ # gradio_generator.py - CPU & Spaces-compatible Gradio interface with voice recording for RoboSage
2
 
3
+ import os
4
  import gradio as gr
5
  from deployer.simulator_interface import VirtualRobot
6
+ from openai import OpenAI
7
+
8
+ # Initialize OpenAI client for transcription
9
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
10
+ if not OPENAI_API_KEY:
11
+ raise EnvironmentError("Please set the OPENAI_API_KEY environment variable for audio transcription.
12
+ ")
13
+ openai_client = OpenAI(api_key=OPENAI_API_KEY)
14
+
15
+
16
+ def transcribe_audio(audio_file: str) -> str:
17
+ """
18
+ Transcribe recorded audio using OpenAI Whisper API.
19
+ Returns the transcribed text.
20
+ """
21
+ with open(audio_file, "rb") as f:
22
+ response = openai_client.audio.transcriptions.create(
23
+ file=f,
24
+ model="whisper-1"
25
+ )
26
+ return response.text
27
 
28
 
29
  def robot_behavior(user_input: str) -> str:
 
45
  return bot.perform_action("say I'm sorry, I didn't understand that.")
46
 
47
 
48
+ def transcribe_and_respond(audio_file: str) -> str:
49
+ """
50
+ Transcribe audio input and run robot_behavior.
51
+ """
52
+ text = transcribe_audio(audio_file)
53
+ return robot_behavior(text)
54
+
55
+
56
  def launch_gradio_app(
57
  title: str = "RoboSage App",
58
  description: str = "Your robot, your voice."
59
  ) -> gr.Blocks:
60
  """
61
+ Constructs and returns a Gradio Blocks app with voice and text input for simulation.
 
 
62
  """
63
  with gr.Blocks() as demo:
64
+ gr.Markdown(f"# πŸ€– {title}\n\n{description}")
 
 
 
 
 
 
 
 
 
 
 
65
 
 
66
  with gr.Accordion("πŸ€– Test your robot", open=True):
67
+ # Text input section
68
+ text_input = gr.Textbox(
69
+ label="Text Command",
70
  placeholder="Type 'hello' or 'say Have a great day!'"
71
  )
72
+ text_btn = gr.Button("Send Text", key="send-text-btn")
73
+ text_output = gr.Textbox(label="Robot Response", lines=4, interactive=False)
74
+ text_btn.click(
75
+ fn=robot_behavior,
76
+ inputs=[text_input],
77
+ outputs=[text_output]
78
+ )
79
+
80
+ gr.Markdown("---")
81
+ # Voice input section
82
+ audio_input = gr.Audio(
83
+ source="microphone",
84
+ type="filepath",
85
+ label="Record Command"
86
+ )
87
+ audio_btn = gr.Button("Send Audio", key="send-audio-btn")
88
+ audio_output = gr.Textbox(label="Robot Response (via voice)", lines=4, interactive=False)
89
+ audio_btn.click(
90
+ fn=transcribe_and_respond,
91
+ inputs=[audio_input],
92
+ outputs=[audio_output]
93
+ )
94
 
95
  return demo
96
 
97
 
98
  if __name__ == "__main__":
99
  app = launch_gradio_app()
100
+ app.launch()