Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +77 -104
deployer/gradio_generator.py
CHANGED
@@ -1,117 +1,90 @@
|
|
1 |
-
|
2 |
-
|
3 |
import os
|
4 |
-
import
|
5 |
-
import requests
|
6 |
import gradio as gr
|
7 |
-
|
8 |
-
from deployer.revenue_tracker import get_revenue_stats, package_artifacts
|
9 |
from core_creator.voice_to_app import VoiceToAppCreator
|
|
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
if not HK_API_KEY:
|
15 |
-
raise EnvironmentError(
|
16 |
-
"Please set the HK_API_KEY environment variable for HuggingFace Inference API.")
|
17 |
-
HEADERS = {"Authorization": f"Bearer {HK_API_KEY}"}
|
18 |
-
|
19 |
-
# Core robot logic
|
20 |
-
def robot_behavior(user_input: str) -> str:
|
21 |
-
bot = VirtualRobot()
|
22 |
-
text = user_input.strip().lower()
|
23 |
-
if any(greet in text for greet in ["hello", "hi", "hey", "welcome"]):
|
24 |
-
return bot.perform_action("wave") + "\n" + bot.perform_action("say Hello there!")
|
25 |
-
if text.startswith("say "):
|
26 |
-
return bot.perform_action(text)
|
27 |
-
return bot.perform_action("say I'm sorry, I didn't understand that.")
|
28 |
-
|
29 |
-
# Transcribe via Hugging Face Inference API
|
30 |
-
def transcribe_audio(audio_file: str) -> str:
|
31 |
-
with open(audio_file, "rb") as f:
|
32 |
-
data = f.read()
|
33 |
-
response = requests.post(HF_STT_URL, headers=HEADERS, data=data)
|
34 |
-
if response.status_code != 200:
|
35 |
-
return f"β Transcription error: {response.status_code}"
|
36 |
-
result = response.json()
|
37 |
-
return result.get("text", "")
|
38 |
-
|
39 |
-
# Combined flow
|
40 |
-
def transcribe_and_respond(audio_file: str) -> str:
|
41 |
-
text = transcribe_audio(audio_file)
|
42 |
-
return robot_behavior(text)
|
43 |
-
|
44 |
-
# Package generation pipeline
|
45 |
-
def on_generate(i: str):
|
46 |
-
creator = VoiceToAppCreator(i)
|
47 |
assets = creator.run_pipeline()
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
gen_btn.click(
|
76 |
-
fn=
|
77 |
-
inputs=[
|
78 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
)
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
file_count="multiple",
|
86 |
-
file_types=None,
|
87 |
-
value="/data"
|
88 |
)
|
89 |
|
90 |
-
|
91 |
-
with gr.Accordion("π€ Test Your Robot", open=False):
|
92 |
-
text_input = gr.Textbox(label="Text Command", placeholder="Type 'hello' or 'say Good job!'" )
|
93 |
-
text_btn = gr.Button("Send Text", key="send-text-btn")
|
94 |
-
text_output = gr.Textbox(label="Robot Response", lines=3, interactive=False)
|
95 |
-
text_btn.click(fn=robot_behavior, inputs=[text_input], outputs=[text_output])
|
96 |
-
|
97 |
-
gr.Markdown("---")
|
98 |
-
|
99 |
-
audio_input = gr.Audio(source="microphone", type="filepath", label="Record Command")
|
100 |
-
audio_btn = gr.Button("Send Audio", key="send-audio-btn")
|
101 |
-
audio_output = gr.Textbox(label="Robot Response (via voice)", lines=3, interactive=False)
|
102 |
-
audio_btn.click(fn=transcribe_and_respond, inputs=[audio_input], outputs=[audio_output])
|
103 |
-
|
104 |
-
# 3. Monetization & Revenue Dashboard
|
105 |
-
with gr.Accordion("π° Monetization Dashboard", open=False):
|
106 |
-
subscribe_btn = gr.Button("Subscribe to Pro Plan", key="subscribe-btn")
|
107 |
-
subscribe_msg = gr.Textbox(label="Subscription Status", interactive=False)
|
108 |
-
rev_btn = gr.Button("View Revenue Stats", key="rev-stats-btn")
|
109 |
-
rev_table = gr.Dataframe(label="Revenue & Usage Metrics")
|
110 |
-
subscribe_btn.click(fn=lambda: "β
Subscribed!", inputs=None, outputs=[subscribe_msg])
|
111 |
-
rev_btn.click(fn=get_revenue_stats, inputs=None, outputs=[rev_table])
|
112 |
|
113 |
return demo
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
1 |
+
import asyncio
|
|
|
2 |
import os
|
3 |
+
import shutil
|
|
|
4 |
import gradio as gr
|
5 |
+
|
|
|
6 |
from core_creator.voice_to_app import VoiceToAppCreator
|
7 |
+
from deployer.simulator_interface import VirtualRobot
|
8 |
+
from deployer.revenue_tracker import package_artifacts
|
9 |
|
10 |
+
# Async pipeline runner for generating the robot app
|
11 |
+
async def run_pipeline_async(idea: str) -> str:
|
12 |
+
creator = VoiceToAppCreator(idea)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
assets = creator.run_pipeline()
|
14 |
+
title = assets.get("blueprint", {}).get("title", "<unknown>")
|
15 |
+
return title
|
16 |
+
|
17 |
+
# Synchronous wrapper for Gradio
|
18 |
+
def generate_app(idea: str) -> str:
|
19 |
+
title = asyncio.run(run_pipeline_async(idea))
|
20 |
+
return f"β
Generated app: {title}"
|
21 |
+
|
22 |
+
# Package artifacts into a ZIP and return the path
|
23 |
+
def get_app_zip() -> str:
|
24 |
+
# package_artifacts should return the absolute path to the ZIP
|
25 |
+
zip_path = package_artifacts()
|
26 |
+
if not os.path.isfile(zip_path):
|
27 |
+
raise FileNotFoundError(f"Expected ZIP at {zip_path}, but not found.")
|
28 |
+
return zip_path
|
29 |
+
|
30 |
+
# Simulated robot behavior
|
31 |
+
def robot_behavior(command: str) -> str:
|
32 |
+
return VirtualRobot().perform_action(command)
|
33 |
+
|
34 |
+
# Launch the Gradio interface
|
35 |
+
def launch_gradio_app():
|
36 |
+
with gr.Blocks(css=".gradio-container { width: 800px; margin: auto; }
|
37 |
+
.section { margin-bottom: 20px; }") as demo:
|
38 |
+
gr.Markdown("# π RoboSage\nGenerate your custom robot app and test it live.")
|
39 |
+
|
40 |
+
with gr.Group(elem_id="generate-app"), gr.Box():
|
41 |
+
gr.Markdown("## 1οΈβ£ Generate App")
|
42 |
+
idea_input = gr.Textbox(
|
43 |
+
label="Your Robot Idea",
|
44 |
+
placeholder="e.g. A friendly greeting robot",
|
45 |
+
lines=1
|
46 |
+
)
|
47 |
+
gen_btn = gr.Button("Generate App")
|
48 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
49 |
+
|
50 |
gen_btn.click(
|
51 |
+
fn=generate_app,
|
52 |
+
inputs=[idea_input],
|
53 |
+
outputs=[status_output]
|
54 |
+
)
|
55 |
+
|
56 |
+
with gr.Group(elem_id="download-app"), gr.Box():
|
57 |
+
gr.Markdown("## π¦ Download Packaged App")
|
58 |
+
dl_btn = gr.Button("Download App ZIP")
|
59 |
+
dl_file = gr.File(label="App ZIP File")
|
60 |
+
|
61 |
+
dl_btn.click(
|
62 |
+
fn=get_app_zip,
|
63 |
+
inputs=None,
|
64 |
+
outputs=[dl_file]
|
65 |
+
)
|
66 |
+
|
67 |
+
with gr.Group(elem_id="robot-sim"), gr.Box():
|
68 |
+
gr.Markdown("## 2οΈβ£ Robot Simulator")
|
69 |
+
cmd_input = gr.Textbox(
|
70 |
+
label="Command",
|
71 |
+
placeholder="hello or say You rock!",
|
72 |
+
lines=1
|
73 |
)
|
74 |
+
sim_btn = gr.Button("Send")
|
75 |
+
sim_output = gr.Textbox(label="Robot Response", lines=4, interactive=False)
|
76 |
|
77 |
+
sim_btn.click(
|
78 |
+
fn=robot_behavior,
|
79 |
+
inputs=[cmd_input],
|
80 |
+
outputs=[sim_output]
|
|
|
|
|
|
|
81 |
)
|
82 |
|
83 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)), share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
return demo
|
86 |
|
87 |
+
|
88 |
+
# Expose interface for external imports
|
89 |
+
robot_behavior = robot_behavior
|
90 |
+
launch_gradio_app = launch_gradio_app
|