|
| 1 | +import os |
| 2 | +from typing import Any, Dict |
| 3 | + |
| 4 | +from flask import Flask, request, jsonify |
| 5 | + |
| 6 | +from llm_router_services.guardrails.constants import SERVICES_API_PREFIX |
| 7 | +from llm_router_services.guardrails.inference.factory import ( |
| 8 | + GuardrailClassifierModelFactory, |
| 9 | +) |
| 10 | +from llm_router_services.guardrails.speakleash.config import SojkaModelConfig |
| 11 | + |
| 12 | +# ----------------------------------------------------------------------- |
| 13 | +# Environment prefix – all configuration keys start with this value |
| 14 | +# ----------------------------------------------------------------------- |
| 15 | +_ENV_PREFIX = "LLM_ROUTER_SOJKA_GUARD_" |
| 16 | + |
| 17 | +app = Flask(__name__) |
| 18 | + |
| 19 | +MODEL_PATH = os.getenv(f"{_ENV_PREFIX}MODEL_PATH", None) |
| 20 | +if not MODEL_PATH: |
| 21 | + raise Exception( |
| 22 | + f"Sojka guardrail model path is not set! " |
| 23 | + f"Export {_ENV_PREFIX}MODEL_PATH with proper model path" |
| 24 | + ) |
| 25 | + |
| 26 | +# Keep only a single constant for the device (CPU by default) |
| 27 | +DEFAULT_DEVICE = int(os.getenv(f"{_ENV_PREFIX}DEVICE", "-1")) |
| 28 | + |
| 29 | +# ----------------------------------------------------------------------- |
| 30 | +# Build the guardrail object via the factory, passing the Sojka‑specific config |
| 31 | +# ----------------------------------------------------------------------- |
| 32 | +guardrail = GuardrailClassifierModelFactory( |
| 33 | + model_type="text_classification", |
| 34 | + model_path=MODEL_PATH, |
| 35 | + device=DEFAULT_DEVICE, |
| 36 | + config=SojkaModelConfig(), |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +# ----------------------------------------------------------------------- |
| 41 | +# Endpoint: POST /api/guardrails/sojka_guard |
| 42 | +# ----------------------------------------------------------------------- |
| 43 | +@app.route(f"{SERVICES_API_PREFIX}/sojka_guard", methods=["POST"]) |
| 44 | +def sojka_guardrail(): |
| 45 | + """ |
| 46 | + Accepts a JSON payload, classifies the content and returns the aggregated results. |
| 47 | + """ |
| 48 | + if not request.is_json: |
| 49 | + return jsonify({"error": "Request body must be JSON"}), 400 |
| 50 | + |
| 51 | + payload: Dict[str, Any] = request.get_json() |
| 52 | + try: |
| 53 | + results = guardrail.classify_chunks(payload) |
| 54 | + return jsonify({"results": results}), 200 |
| 55 | + except Exception as exc: # pragma: no cover – safety net |
| 56 | + return jsonify({"error": str(exc)}), 500 |
| 57 | + |
| 58 | + |
| 59 | +# ----------------------------------------------------------------------- |
| 60 | +# Run the Flask server (only when executed directly) |
| 61 | +# ----------------------------------------------------------------------- |
| 62 | +if __name__ == "__main__": |
| 63 | + host = os.getenv(f"{_ENV_PREFIX}FLASK_HOST", "0.0.0.0") |
| 64 | + port = int(os.getenv(f"{_ENV_PREFIX}FLASK_PORT", "5000")) |
| 65 | + app.run(host=host, port=port, debug=False) |
0 commit comments