Update core_creator/code_generator.py
Browse files
core_creator/code_generator.py
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# code_generator.py - Turns app blueprint into executable Python code using OpenAI
|
| 2 |
+
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
def generate_app_code(blueprint: dict) -> str:
|
| 7 |
+
base_prompt = f"""
|
| 8 |
+
You are an expert Python developer and Gradio engineer.
|
| 9 |
+
Generate a Hugging Face-ready app that does the following:
|
| 10 |
+
|
| 11 |
+
Title: {blueprint.get("title")}
|
| 12 |
+
Description: {blueprint.get("description")}
|
| 13 |
+
|
| 14 |
+
Inputs: {blueprint.get("inputs")}
|
| 15 |
+
Outputs: {blueprint.get("outputs")}
|
| 16 |
+
Voice Commands: {blueprint.get("voice_commands")}
|
| 17 |
+
|
| 18 |
+
Requirements:
|
| 19 |
+
- Use Gradio Blocks.
|
| 20 |
+
- Accept user voice or text input.
|
| 21 |
+
- Use placeholder code for hardware control (e.g., robot.move_arm()).
|
| 22 |
+
- Print logs for each user command detected.
|
| 23 |
+
- Keep it in a single Python file.
|
| 24 |
+
- Wrap robot behavior in a function named `robot_behavior()`.
|
| 25 |
+
- Return only the Python code, no extra explanation.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
response = OpenAI().chat.completions.create(
|
| 29 |
+
model="gpt-4o",
|
| 30 |
+
messages=[
|
| 31 |
+
{"role": "system", "content": "You write production-quality Hugging Face Spaces code."},
|
| 32 |
+
{"role": "user", "content": base_prompt},
|
| 33 |
+
],
|
| 34 |
+
temperature=0.3
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
return response.choices[0].message.content.strip()
|
| 38 |
+
|
| 39 |
+
# Example
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
sample_blueprint = {
|
| 42 |
+
"title": "WaveBot",
|
| 43 |
+
"description": "A robot that waves and greets customers.",
|
| 44 |
+
"inputs": ["camera", "microphone"],
|
| 45 |
+
"outputs": ["arm wave", "voice greeting"],
|
| 46 |
+
"voice_commands": ["hello", "hi", "good morning"],
|
| 47 |
+
"monetization": ["Retail subscription", "Affiliate for motion sensors"]
|
| 48 |
+
}
|
| 49 |
+
code = generate_app_code(sample_blueprint)
|
| 50 |
+
print(code[:1000])
|