mgbam commited on
Commit
70154bd
Β·
verified Β·
1 Parent(s): 7186830

Update deployer/gradio_generator.py

Browse files
Files changed (1) hide show
  1. deployer/gradio_generator.py +80 -60
deployer/gradio_generator.py CHANGED
@@ -1,77 +1,97 @@
1
  import os
2
- import asyncio
3
  import gradio as gr
4
-
5
  from core_creator.voice_to_app import VoiceToAppCreator
6
  from deployer.simulator_interface import VirtualRobot
7
- from deployer.revenue_tracker import package_artifacts
8
-
9
- # --- Async pipeline runner ---
10
- async def _run_pipeline_async(idea: str) -> str:
11
- creator = VoiceToAppCreator(idea)
12
- assets = creator.run_pipeline()
13
- # blueprint title fallback
14
- return assets.get("blueprint", {}).get("title", "<unknown>")
15
-
16
- # --- Sync wrappers for Gradio ---
17
- def generate_app(idea: str) -> str:
18
- title = asyncio.run(_run_pipeline_async(idea))
19
- return f"βœ… Generated app: {title}"
20
 
21
- def get_app_zip() -> str:
22
- zip_path = package_artifacts()
23
- if not os.path.isfile(zip_path):
24
- raise FileNotFoundError(f"Could not find ZIP at {zip_path}")
25
- return zip_path
 
 
26
 
27
- def robot_behavior(command: str) -> str:
28
- return VirtualRobot().perform_action(command)
29
 
30
- # --- Build & launch Gradio UI ---
31
- def launch_gradio_app():
32
- css = r"""
33
- .gradio-container { max-width: 800px; margin: auto; }
34
- .gradio-box { padding: 1em; margin-bottom: 2em; border: 1px solid #e2e2e2; border-radius: 8px; }
35
  """
 
 
 
 
 
36
 
37
- with gr.Blocks(css=css) as demo:
38
- gr.Markdown("# πŸš€ RoboSage")
39
- gr.Markdown("Generate your custom robot app and test it live.")
40
-
41
- # Generate App section
42
- with gr.Box(elem_id="generate-app", css_class="gradio-box"):
43
- gr.Markdown("## 1️⃣ Generate App")
44
- idea_input = gr.Textbox(label="Your Robot Idea", placeholder="e.g. A friendly greeting robot", lines=1)
45
- gen_btn = gr.Button("Generate App")
46
- status_out = gr.Textbox(label="Status", interactive=False)
47
-
48
- gen_btn.click(fn=generate_app, inputs=[idea_input], outputs=[status_out])
49
 
50
- # Download ZIP section
51
- with gr.Box(elem_id="download-app", css_class="gradio-box"):
52
- gr.Markdown("## πŸ“¦ Download Packaged App")
53
- dl_btn = gr.Button("Download App ZIP")
54
- dl_file = gr.File(label="App ZIP File")
 
 
 
 
 
 
55
 
56
- dl_btn.click(fn=get_app_zip, inputs=None, outputs=[dl_file])
57
 
58
- # Simulator section
59
- with gr.Box(elem_id="robot-sim", css_class="gradio-box"):
60
- gr.Markdown("## 2️⃣ Robot Simulator")
61
- cmd_input = gr.Textbox(label="Command", placeholder="hello or say You rock!", lines=1)
62
- sim_btn = gr.Button("Send")
63
- sim_output = gr.Textbox(label="Robot Response", lines=4, interactive=False)
64
 
65
- sim_btn.click(fn=robot_behavior, inputs=[cmd_input], outputs=[sim_output])
66
 
67
- demo.launch(
68
- server_name="0.0.0.0",
69
- server_port=int(os.environ.get("PORT", 7860)),
70
- share=False
71
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  return demo
74
 
75
- # Expose for external import
76
- robot_behavior = robot_behavior
77
- launch_gradio_app = launch_gradio_app
 
1
  import os
 
2
  import gradio as gr
3
+ import openai
4
  from core_creator.voice_to_app import VoiceToAppCreator
5
  from deployer.simulator_interface import VirtualRobot
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Ensure API key is set
8
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
9
+ if not OPENAI_API_KEY:
10
+ raise EnvironmentError(
11
+ "Please set the OPENAI_API_KEY environment variable for audio transcription."
12
+ )
13
+ openai.api_key = OPENAI_API_KEY
14
 
 
 
15
 
16
+ def deploy_app(text_input: str) -> str:
 
 
 
 
17
  """
18
+ Generate the app package based on text input.
19
+ """
20
+ assets = VoiceToAppCreator(text_input).run_pipeline()
21
+ title = assets.get("blueprint", {}).get("title", "<unknown>")
22
+ return f"βœ… Generated app: {title}"
23
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ def transcribe_audio(audio_path: str) -> str:
26
+ """
27
+ Transcribe audio file to text using OpenAI Whisper.
28
+ """
29
+ if not audio_path:
30
+ return ""
31
+ response = openai.Audio.transcribe(
32
+ model="whisper-1",
33
+ file=audio_path
34
+ )
35
+ return response.get("text", "")
36
 
 
37
 
38
+ def robot_behavior(user_input: str) -> str:
39
+ """
40
+ Simulate robot action based on user command.
41
+ """
42
+ return VirtualRobot().perform_action(user_input)
 
43
 
 
44
 
45
+ def launch_gradio_app():
46
+ """
47
+ Builds and returns the Gradio Blocks UI for the RoboSage app.
48
+ """
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("# πŸš€ RoboSage\nGenerate your custom robot app and test it live.")
51
+
52
+ with gr.Row():
53
+ with gr.Column():
54
+ gr.Markdown("## 1️⃣ Generate App")
55
+ text_idea = gr.Textbox(
56
+ label="Your Robot Idea",
57
+ placeholder="e.g. A friendly greeting robot."
58
+ )
59
+ audio_idea = gr.Audio(
60
+ source="microphone",
61
+ type="filepath",
62
+ label="Or speak your robot idea"
63
+ )
64
+ gen_btn = gr.Button("Generate App")
65
+ status = gr.Textbox(label="Status")
66
+
67
+ # Combine transcription or text input
68
+ def on_generate(text, audio):
69
+ transcript = transcribe_audio(audio)
70
+ final_input = transcript if transcript else text
71
+ return deploy_app(final_input)
72
+
73
+ gen_btn.click(
74
+ fn=on_generate,
75
+ inputs=[text_idea, audio_idea],
76
+ outputs=status
77
+ )
78
+
79
+ with gr.Column():
80
+ gr.Markdown("## 2️⃣ Robot Simulator")
81
+ cmd_input = gr.Textbox(
82
+ label="Command",
83
+ placeholder="hello or say You rock!"
84
+ )
85
+ sim_btn = gr.Button("Send")
86
+ resp = gr.Textbox(label="Robot Response", lines=4)
87
+ sim_btn.click(
88
+ fn=robot_behavior,
89
+ inputs=cmd_input,
90
+ outputs=resp
91
+ )
92
 
93
  return demo
94
 
95
+
96
+ if __name__ == "__main__":
97
+ launch_gradio_app().launch()