Update deployer/gradio_generator.py
Browse files- deployer/gradio_generator.py +89 -67
deployer/gradio_generator.py
CHANGED
@@ -1,99 +1,121 @@
|
|
1 |
-
# gradio_generator.py - CPU & Spaces-compatible Gradio interface with
|
2 |
|
3 |
import os
|
|
|
4 |
import gradio as gr
|
|
|
5 |
from deployer.simulator_interface import VirtualRobot
|
6 |
-
from
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
with open(audio_file, "rb") as f:
|
21 |
-
response = openai_client.audio.transcriptions.create(
|
22 |
-
file=f,
|
23 |
-
model="whisper-1"
|
24 |
-
)
|
25 |
-
return response.text
|
26 |
-
|
27 |
-
|
28 |
def robot_behavior(user_input: str) -> str:
|
29 |
-
"""
|
30 |
-
Bridge between user input and VirtualRobot actions with basic NLU.
|
31 |
-
"""
|
32 |
bot = VirtualRobot()
|
33 |
text = user_input.strip().lower()
|
34 |
-
|
35 |
-
# Greeting detection
|
36 |
if any(greet in text for greet in ["hello", "hi", "hey", "welcome"]):
|
37 |
return bot.perform_action("wave") + "\n" + bot.perform_action("say Hello there!")
|
38 |
-
|
39 |
-
# Direct speech command
|
40 |
if text.startswith("say "):
|
41 |
return bot.perform_action(text)
|
42 |
-
|
43 |
-
# Fallback for unrecognized commands
|
44 |
return bot.perform_action("say I'm sorry, I didn't understand that.")
|
45 |
|
|
|
|
|
|
|
|
|
46 |
|
47 |
def transcribe_and_respond(audio_file: str) -> str:
|
48 |
-
"""
|
49 |
-
Transcribe audio input and run robot_behavior.
|
50 |
-
"""
|
51 |
text = transcribe_audio(audio_file)
|
52 |
return robot_behavior(text)
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
def launch_gradio_app(
|
56 |
title: str = "RoboSage App",
|
57 |
-
description: str = "Your robot, your voice."
|
58 |
) -> gr.Blocks:
|
59 |
-
"""
|
60 |
-
Constructs and returns a Gradio Blocks app with voice and text input for simulation.
|
61 |
-
"""
|
62 |
with gr.Blocks() as demo:
|
63 |
-
gr.Markdown(f"#
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
)
|
|
|
|
|
|
|
|
|
|
|
71 |
text_btn = gr.Button("Send Text", key="send-text-btn")
|
72 |
-
text_output = gr.Textbox(label="Robot Response", lines=
|
73 |
-
text_btn.click(
|
74 |
-
fn=robot_behavior,
|
75 |
-
inputs=[text_input],
|
76 |
-
outputs=[text_output]
|
77 |
-
)
|
78 |
|
79 |
gr.Markdown("---")
|
80 |
-
|
81 |
-
|
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=
|
89 |
-
audio_btn.click(
|
90 |
-
fn=transcribe_and_respond,
|
91 |
-
inputs=[audio_input],
|
92 |
-
outputs=[audio_output]
|
93 |
-
)
|
94 |
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
|
|
|
|
|
|
|
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
app = launch_gradio_app()
|
|
|
1 |
+
# gradio_generator.py - CPU & Spaces-compatible Gradio interface with full monetization & artifact management for RoboSage
|
2 |
|
3 |
import os
|
4 |
+
import json
|
5 |
import gradio as gr
|
6 |
+
from transformers import pipeline
|
7 |
from deployer.simulator_interface import VirtualRobot
|
8 |
+
from deployer.revenue_tracker import get_revenue_stats, package_artifacts
|
9 |
+
from core_creator.voice_to_app import VoiceToAppCreator
|
10 |
+
|
11 |
+
# Initialize Hugging Face ASR pipeline
|
12 |
+
HK_API_KEY = os.getenv("HK_API_KEY")
|
13 |
+
if not HK_API_KEY:
|
14 |
+
raise EnvironmentError("Please set the HK_API_KEY environment variable for audio transcription via HuggingFace API.")
|
15 |
+
asr = pipeline(
|
16 |
+
task="automatic-speech-recognition",
|
17 |
+
model="openai/whisper-small",
|
18 |
+
use_auth_token=HK_API_KEY
|
19 |
+
)
|
20 |
+
|
21 |
+
# Core actions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def robot_behavior(user_input: str) -> str:
|
|
|
|
|
|
|
23 |
bot = VirtualRobot()
|
24 |
text = user_input.strip().lower()
|
|
|
|
|
25 |
if any(greet in text for greet in ["hello", "hi", "hey", "welcome"]):
|
26 |
return bot.perform_action("wave") + "\n" + bot.perform_action("say Hello there!")
|
|
|
|
|
27 |
if text.startswith("say "):
|
28 |
return bot.perform_action(text)
|
|
|
|
|
29 |
return bot.perform_action("say I'm sorry, I didn't understand that.")
|
30 |
|
31 |
+
# Transcription helper
|
32 |
+
def transcribe_audio(audio_file: str) -> str:
|
33 |
+
result = asr(audio_file)
|
34 |
+
return result.get("text", "")
|
35 |
|
36 |
def transcribe_and_respond(audio_file: str) -> str:
|
|
|
|
|
|
|
37 |
text = transcribe_audio(audio_file)
|
38 |
return robot_behavior(text)
|
39 |
|
40 |
+
# Generation & packaging pipeline
|
41 |
+
def generate_and_package_app(idea: str):
|
42 |
+
creator = VoiceToAppCreator(idea)
|
43 |
+
assets = creator.run_pipeline()
|
44 |
+
# Package code & config into zip and return path
|
45 |
+
zip_path = package_artifacts(assets)
|
46 |
+
# Prepare blueprint and code previews
|
47 |
+
blueprint = assets.get("blueprint", {})
|
48 |
+
code_files = assets.get("code", {}) # dict of {filename: code_str}
|
49 |
+
# Concatenate code files for preview
|
50 |
+
code_preview = "\n\n".join([f"# {fname}\n{content}" for fname, content in code_files.items()])
|
51 |
+
return zip_path, blueprint, code_preview
|
52 |
+
|
53 |
+
# Build full-featured Gradio app
|
54 |
def launch_gradio_app(
|
55 |
title: str = "RoboSage App",
|
56 |
+
description: str = "Your robot, your voice, monetized."
|
57 |
) -> gr.Blocks:
|
|
|
|
|
|
|
58 |
with gr.Blocks() as demo:
|
59 |
+
gr.Markdown(f"# π {title}\n\n{description}")
|
60 |
+
|
61 |
+
# 1οΈβ£ Generate & Download Artifacts
|
62 |
+
with gr.Accordion("π¨ Generate App & Download Artifacts", open=True):
|
63 |
+
idea = gr.Textbox(label="Robot Idea", placeholder="e.g. A friendly greeting robot.")
|
64 |
+
gen_btn = gr.Button("Generate & Package", key="gen-app-btn")
|
65 |
+
status = gr.Textbox(label="Generation Status", interactive=False)
|
66 |
+
download_zip = gr.File(label="Download Artifacts (.zip)")
|
67 |
+
blueprint_view = gr.JSON(label="App Blueprint")
|
68 |
+
code_view = gr.Code(label="Generated Code Preview", language="python")
|
69 |
+
|
70 |
+
def on_generate(i):
|
71 |
+
zip_path, blueprint, code_preview = generate_and_package_app(i)
|
72 |
+
return (
|
73 |
+
"β
App generated successfully!",
|
74 |
+
zip_path,
|
75 |
+
blueprint,
|
76 |
+
code_preview
|
77 |
+
)
|
78 |
+
|
79 |
+
gen_btn.click(
|
80 |
+
fn=on_generate,
|
81 |
+
inputs=[idea],
|
82 |
+
outputs=[status, download_zip, blueprint_view, code_view]
|
83 |
)
|
84 |
+
|
85 |
+
# 2οΈβ£ Robot Simulation
|
86 |
+
with gr.Accordion("π€ Test Your Robot (Text & Voice)", open=False):
|
87 |
+
# Text command
|
88 |
+
text_input = gr.Textbox(label="Text Command", placeholder="Type 'hello' or 'say Great job!'" )
|
89 |
text_btn = gr.Button("Send Text", key="send-text-btn")
|
90 |
+
text_output = gr.Textbox(label="Robot Response", lines=3, interactive=False)
|
91 |
+
text_btn.click(fn=robot_behavior, inputs=[text_input], outputs=[text_output])
|
|
|
|
|
|
|
|
|
92 |
|
93 |
gr.Markdown("---")
|
94 |
+
# Voice command
|
95 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="Record Command")
|
|
|
|
|
|
|
|
|
|
|
96 |
audio_btn = gr.Button("Send Audio", key="send-audio-btn")
|
97 |
+
audio_output = gr.Textbox(label="Robot Response (via voice)", lines=3, interactive=False)
|
98 |
+
audio_btn.click(fn=transcribe_and_respond, inputs=[audio_input], outputs=[audio_output])
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
# 3οΈβ£ Monetization & Revenue
|
101 |
+
with gr.Accordion("π° Monetization Dashboard", open=False):
|
102 |
+
subscribe_btn = gr.Button("Subscribe to Pro Plan", key="subscribe-btn")
|
103 |
+
subscribe_msg = gr.Textbox(label="Subscription Status", interactive=False)
|
104 |
+
rev_btn = gr.Button("View Revenue Stats", key="rev-stats-btn")
|
105 |
+
rev_table = gr.Dataframe(label="Revenue & Usage Metrics")
|
106 |
+
|
107 |
+
def on_subscribe():
|
108 |
+
# Stubbed subscription activation
|
109 |
+
return "β
Subscribed! You now have access to premium features."
|
110 |
+
|
111 |
+
def on_view_revenue():
|
112 |
+
df = get_revenue_stats()
|
113 |
+
return df
|
114 |
|
115 |
+
subscribe_btn.click(fn=on_subscribe, inputs=None, outputs=[subscribe_msg])
|
116 |
+
rev_btn.click(fn=on_view_revenue, inputs=None, outputs=[rev_table])
|
117 |
+
|
118 |
+
return demo
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
app = launch_gradio_app()
|