|
|
|
|
|
import os |
|
import json |
|
import requests |
|
import gradio as gr |
|
from deployer.simulator_interface import VirtualRobot |
|
from deployer.revenue_tracker import get_revenue_stats, package_artifacts |
|
from core_creator.voice_to_app import VoiceToAppCreator |
|
|
|
|
|
HF_STT_URL = "https://api-inference.huggingface.co/models/openai/whisper-small" |
|
HK_API_KEY = os.getenv("HK_API_KEY") |
|
if not HK_API_KEY: |
|
raise EnvironmentError( |
|
"Please set the HK_API_KEY environment variable for HuggingFace Inference API.") |
|
HEADERS = {"Authorization": f"Bearer {HK_API_KEY}"} |
|
|
|
|
|
def robot_behavior(user_input: str) -> str: |
|
bot = VirtualRobot() |
|
text = user_input.strip().lower() |
|
if any(greet in text for greet in ["hello", "hi", "hey", "welcome"]): |
|
return bot.perform_action("wave") + "\n" + bot.perform_action("say Hello there!") |
|
if text.startswith("say "): |
|
return bot.perform_action(text) |
|
return bot.perform_action("say I'm sorry, I didn't understand that.") |
|
|
|
|
|
def transcribe_audio(audio_file: str) -> str: |
|
with open(audio_file, "rb") as f: |
|
data = f.read() |
|
response = requests.post(HF_STT_URL, headers=HEADERS, data=data) |
|
if response.status_code != 200: |
|
return f"β Transcription error: {response.status_code}" |
|
result = response.json() |
|
|
|
return result.get("text", "") |
|
|
|
|
|
def transcribe_and_respond(audio_file: str) -> str: |
|
text = transcribe_audio(audio_file) |
|
return robot_behavior(text) |
|
|
|
|
|
def on_generate(i: str): |
|
creator = VoiceToAppCreator(i) |
|
assets = creator.run_pipeline() |
|
zip_path = package_artifacts(assets) |
|
blueprint = assets.get("blueprint", {}) |
|
code_files = assets.get("code", {}) |
|
code_preview = "\n\n".join([f"# {fname}\n{content}" for fname, content in code_files.items()]) |
|
return ( |
|
"β
App generated successfully!", |
|
zip_path, |
|
blueprint, |
|
code_preview |
|
) |
|
|
|
|
|
def launch_gradio_app( |
|
title: str = "RoboSage App", |
|
description: str = "Your robot, your voice, monetized." |
|
) -> gr.Blocks: |
|
with gr.Blocks() as demo: |
|
gr.Markdown(f"# π {title}\n\n{description}") |
|
|
|
|
|
with gr.Accordion("π¨ Generate App & Download Artifacts", open=True): |
|
idea = gr.Textbox(label="Robot Idea", placeholder="e.g. A friendly greeting robot.") |
|
gen_btn = gr.Button("Generate & Package", key="gen-app-btn") |
|
status = gr.Textbox(label="Generation Status", interactive=False) |
|
download_zip = gr.File(label="Download Artifacts (.zip)") |
|
blueprint_view = gr.JSON(label="App Blueprint") |
|
code_view = gr.Code(label="Generated Code Preview", language="python") |
|
gen_btn.click( |
|
fn=on_generate, |
|
inputs=[idea], |
|
outputs=[status, download_zip, blueprint_view, code_view] |
|
) |
|
|
|
|
|
with gr.Accordion("π€ Test Your Robot", open=False): |
|
text_input = gr.Textbox(label="Text Command", placeholder="Type 'hello' or 'say Good job!'" ) |
|
text_btn = gr.Button("Send Text", key="send-text-btn") |
|
text_output = gr.Textbox(label="Robot Response", lines=3, interactive=False) |
|
text_btn.click(fn=robot_behavior, inputs=[text_input], outputs=[text_output]) |
|
|
|
gr.Markdown("---") |
|
|
|
audio_input = gr.Audio(source="microphone", type="filepath", label="Record Command") |
|
audio_btn = gr.Button("Send Audio", key="send-audio-btn") |
|
audio_output = gr.Textbox(label="Robot Response (via voice)", lines=3, interactive=False) |
|
audio_btn.click(fn=transcribe_and_respond, inputs=[audio_input], outputs=[audio_output]) |
|
|
|
|
|
with gr.Accordion("π° Monetization Dashboard", open=False): |
|
subscribe_btn = gr.Button("Subscribe to Pro Plan", key="subscribe-btn") |
|
subscribe_msg = gr.Textbox(label="Subscription Status", interactive=False) |
|
rev_btn = gr.Button("View Revenue Stats", key="rev-stats-btn") |
|
rev_table = gr.Dataframe(label="Revenue & Usage Metrics") |
|
subscribe_btn.click(fn=lambda: "β
Subscribed!", inputs=None, outputs=[subscribe_msg]) |
|
rev_btn.click(fn=get_revenue_stats, inputs=None, outputs=[rev_table]) |
|
|
|
return demo |
|
|
|
if __name__ == "__main__": |
|
app = launch_gradio_app() |
|
app.launch() |
|
|