Download web/app.py from 2008robocode-crypto/code-generation-system: direct link, hf CLI and curl.
- Browser
- Download file 4.48 kB
-
https://huggingface.co/spaces/2008robocode-crypto/code-generation-system/resolve/main/web/app.py
- Command line
-
hf download hf://spaces/2008robocode-crypto/code-generation-system/web/app.py
-
curl -L -o app.py https://huggingface.co/spaces/2008robocode-crypto/code-generation-system/resolve/main/web/app.py
4.48 kB
| """ | |
| 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 = [] | |
| def index(): | |
| """Main page.""" | |
| return render_template("index.html") | |
| 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 | |
| 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 | |
| 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}) | |
| 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, | |
| }) | |
| def health(): | |
| """Health check.""" | |
| return jsonify({ | |
| "status": "healthy", | |
| "timestamp": datetime.now().isoformat(), | |
| "total_requests": len(recent_requests), | |
| }) | |
| # Error handlers | |
| def not_found(e): | |
| return jsonify({"error": "Not found"}), 404 | |
| 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) | |