File size: 4,481 Bytes
d0fdbcd | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | """
Web interface for the code generation system.
Simple Flask app with UI for prompt input and JSON output visualization.
"""
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
import json
import os
import sys
from datetime import datetime
# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
from pipeline import Pipeline
from runtime_simulator import validate_config_executable
app = Flask(__name__)
CORS(app)
# Initialize pipeline
pipeline = Pipeline(use_llm=False) # Use rule-based for now, can enable LLM
class GenerationRequest:
"""Track generation requests."""
def __init__(self, prompt: str):
self.prompt = prompt
self.timestamp = datetime.now().isoformat()
self.config = None
self.execution_log = None
self.executable_report = None
self.errors = []
# Store recent requests for demo
recent_requests = []
@app.route("/")
def index():
"""Main page."""
return render_template("index.html")
@app.route("/api/generate", methods=["POST"])
def generate():
"""Generate config from prompt."""
try:
data = request.json
prompt = data.get("prompt", "").strip()
if not prompt:
return jsonify({"error": "Prompt is required"}), 400
if len(prompt) > 2000:
return jsonify({"error": "Prompt is too long (max 2000 chars)"}), 400
# Create request tracker
req = GenerationRequest(prompt)
# Generate
config, exec_log = pipeline.generate(prompt)
req.config = config
req.execution_log = exec_log
# Check executability
is_executable, exec_report = validate_config_executable(config)
req.executable_report = exec_report
# Store request
recent_requests.append(req)
if len(recent_requests) > 20:
recent_requests.pop(0)
return jsonify({
"success": True,
"config": config,
"execution_log": exec_log,
"executable_report": exec_report,
"is_executable": is_executable,
})
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 500
@app.route("/api/validate", methods=["POST"])
def validate():
"""Validate a config."""
try:
data = request.json
config = data.get("config", {})
# Validate
is_executable, report = validate_config_executable(config)
return jsonify({
"success": True,
"is_executable": is_executable,
"report": report,
})
except Exception as e:
return jsonify({
"success": False,
"error": str(e)
}), 500
@app.route("/api/recent", methods=["GET"])
def get_recent():
"""Get recent requests."""
recent = []
for req in recent_requests[-10:]:
recent.append({
"timestamp": req.timestamp,
"prompt": req.prompt[:100],
"success": req.config is not None,
"executable": req.executable_report.get("is_executable", False) if req.executable_report else False,
})
return jsonify({"recent": recent})
@app.route("/api/example", methods=["GET"])
def get_example():
"""Get an example generation."""
example_prompt = "Build a CRM with login, contacts, dashboard, role-based access, and premium plan with payments."
config, exec_log = pipeline.generate(example_prompt)
is_executable, exec_report = validate_config_executable(config)
return jsonify({
"prompt": example_prompt,
"config": config,
"executable": is_executable,
})
@app.route("/api/health", methods=["GET"])
def health():
"""Health check."""
return jsonify({
"status": "healthy",
"timestamp": datetime.now().isoformat(),
"total_requests": len(recent_requests),
})
# Error handlers
@app.errorhandler(404)
def not_found(e):
return jsonify({"error": "Not found"}), 404
@app.errorhandler(500)
def internal_error(e):
return jsonify({"error": "Internal server error"}), 500
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8080))
debug_mode = os.environ.get("DEBUG", "false").lower() == "true"
app.run(host="0.0.0.0", port=port, debug=debug_mode)
|