File size: 1,773 Bytes
7319700 1ac5f1d 7319700 1ac5f1d 7319700 1ac5f1d 7319700 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# code_generator.py - Turns app blueprint into executable Python code using OpenAI
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()
# Example
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])
|