Update deployer/gradio_generator.py
Browse files- 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
def robot_behavior(command: str) -> str:
|
| 28 |
-
return VirtualRobot().perform_action(command)
|
| 29 |
|
| 30 |
-
|
| 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 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
dl_btn.click(fn=get_app_zip, inputs=None, outputs=[dl_file])
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 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 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
return demo
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
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()
|