|
|
|
|
|
import os |
|
from openai import OpenAI |
|
|
|
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) |
|
|
|
def generate_app_code(blueprint: dict) -> str: |
|
base_prompt = f""" |
|
You are an expert Python developer and Gradio engineer. |
|
Generate a Hugging Face-ready app that does the following: |
|
|
|
Title: {blueprint.get("title")} |
|
Description: {blueprint.get("description")} |
|
|
|
Inputs: {blueprint.get("inputs")} |
|
Outputs: {blueprint.get("outputs")} |
|
Voice Commands: {blueprint.get("voice_commands")} |
|
|
|
Requirements: |
|
- Use Gradio Blocks. |
|
- Accept user voice or text input. |
|
- Use placeholder code for hardware control (e.g., robot.move_arm()). |
|
- Print logs for each user command detected. |
|
- Keep it in a single Python file. |
|
- Wrap robot behavior in a function named `robot_behavior()`. |
|
- Return only the Python code, no extra explanation. |
|
""" |
|
|
|
response = client.chat.completions.create( |
|
model="gpt-4o", |
|
messages=[ |
|
{"role": "system", "content": "You write production-quality Hugging Face Spaces code."}, |
|
{"role": "user", "content": base_prompt}, |
|
], |
|
temperature=0.3 |
|
) |
|
|
|
return response.choices[0].message.content.strip() |
|
|
|
|
|
if __name__ == "__main__": |
|
sample_blueprint = { |
|
"title": "WaveBot", |
|
"description": "A robot that waves and greets customers.", |
|
"inputs": ["camera", "microphone"], |
|
"outputs": ["arm wave", "voice greeting"], |
|
"voice_commands": ["hello", "hi", "good morning"], |
|
"monetization": ["Retail subscription", "Affiliate for motion sensors"] |
|
} |
|
code = generate_app_code(sample_blueprint) |
|
print(code[:1000]) |
|
|