from flask import Flask, request, render_template, jsonify, send_file import os, json, importlib, importlib.util, sys, re from markdown import markdown app = Flask(__name__) # FOLDERS UPLOAD_FOLDER = "scripts" MODELS_FOLDER = "models" APKS_FOLDER = "apks" os.makedirs(UPLOAD_FOLDER, exist_ok=True) os.makedirs(MODELS_FOLDER, exist_ok=True) sys.path.insert(0, os.path.join(os.getcwd(), APKS_FOLDER)) # GLOBAL MODELS MODELS = {} # ---- Load models at startup ---- def load_models(): global MODELS MODELS = {} for folder in os.listdir(MODELS_FOLDER): path = os.path.join(MODELS_FOLDER, folder) if not os.path.isdir(path): continue meta_path = os.path.join(path, "meta.json") if not os.path.exists(meta_path): continue with open(meta_path) as f: meta = json.load(f) func_name = meta.get("function_name") if not func_name: continue py_file = os.path.join(path, "model.py") spec = importlib.util.spec_from_file_location("model_module", py_file) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) func = getattr(module, func_name) MODELS[folder] = { "func": func, "inputs": meta.get("inputs", []), "meta": meta, "path": path } load_models() # ---------------- DASHBOARD ---------------- @app.route("/") def dashboard(): models_with_md = {} for name, model in MODELS.items(): md_content = "" try: md_path = os.path.join(model["path"], model["meta"]["description_file"]) with open(md_path, "r", encoding="utf-8") as f: md_content = markdown(f.read()) except: md_content = "

Brak opisu.

" models_with_md[name] = {**model, "md_content": md_content} return render_template("user.html", models=models_with_md) # ---------------- EDITOR ---------------- @app.route("/editor/") def editor(model_name): return render_template("editor.html", model_name=model_name) # ---------------- RUN SCRIPT (MKScript) ---------------- @app.route("/run_script", methods=["POST"]) def run_script(): code = request.json.get("code", "") output = [] variables = {} modules = {} try: lines = code.split(";") for line in lines: line = line.strip() if not line: continue # use mk if line.startswith("use "): name = line.split()[1] mod = importlib.import_module(name) modules[name] = mod variables[name] = mod continue # print(...) if line.startswith("print"): inside = line[line.find("(")+1:line.rfind(")")] output.append(str(eval_expr(inside, variables))) continue # typy int/float/bool if any(line.startswith(t) for t in ["int ", "float ", "bool "]): _, rest = line.split(" ", 1) var, expr = rest.split("=") variables[var.strip()] = eval_expr(expr.strip(), variables) continue # wywołanie funkcji np mk.add(1,2) if "." in line and "(" in line: obj, rest = line.split(".", 1) fname = rest[:rest.find("(")] args = rest[rest.find("(")+1:rest.find(")")] args = [eval_expr(a.strip(), variables) for a in args.split(",")] result = getattr(modules[obj], fname)(*args) variables["_"] = result continue except Exception as e: return jsonify({"error": str(e)}) return jsonify({ "output": output, "variables": {k: str(v) for k, v in variables.items()} }) def eval_expr(expr, variables): expr = expr.replace("true", "True").replace("false", "False") return eval(expr, {}, variables) # ---------------- PREDICT ---------------- @app.route("/predict/", methods=["POST"]) def predict(model_name): model_entry = MODELS.get(model_name) if not model_entry: return jsonify({"output": "Model nie istnieje"}), 404 func = model_entry["func"] inputs = model_entry["inputs"] kwargs = {inp: request.form.get(inp) for inp in inputs} try: for k in kwargs: try: kwargs[k] = float(kwargs[k]) except: pass output = func(**kwargs) except Exception as e: output = str(e) return jsonify({"output": str(output)}) # ---------------- DOWNLOAD SCRIPT ---------------- @app.route("/download_script/") def download_script(filename): path = os.path.join(UPLOAD_FOLDER, filename) if os.path.exists(path): return send_file(path, as_attachment=True) return "Plik nie istnieje", 404 # ---------------- UPLOAD SCRIPT ---------------- @app.route("/upload_script", methods=["POST"]) def upload_script(): file = request.files.get("file") if not file: return "Nie przesłano pliku", 400 filename = file.filename path = os.path.join(UPLOAD_FOLDER, filename) file.save(path) return "OK" # ---------------- MAIN ---------------- if __name__ == "__main__": app.run(port=4500)