Spaces:
Running on Zero

themis-playground / validation.py
lothnic's picture
Host both published Themis models on ZeroGPU
20d31c0 verified
Raw History Blame Contribute Delete
2.71 kB
"""Validate the public typed-question contract before model inference."""
def validate_request(request):
"""Reject training records and malformed options before downloading model weights."""
if not isinstance(request, dict) or set(request) != {"state", "questions"}:
raise ValueError(
"Request must contain only state and questions; keep labels outside inputs"
)
if not isinstance(request["state"], str) or not request["state"].strip():
raise ValueError("state must be a non-empty string")
questions = request["questions"]
if not isinstance(questions, dict) or not questions:
raise ValueError("questions must be a non-empty mapping")
for name, question in questions.items():
if not isinstance(name, str) or not name or not isinstance(question, dict):
raise ValueError("Every question needs a non-empty name and an object")
if not {"type", "instructions"} <= question.keys() or not question.keys() <= {
"type",
"instructions",
"criteria",
}:
raise ValueError(f"{name}: use type, instructions and optional criteria only")
instructions = question["instructions"]
if not isinstance(instructions, str) or not instructions.strip():
raise ValueError(f"{name}: instructions must be a non-empty string")
kind, criteria = question["type"], question.get("criteria")
if kind == "choice":
if not isinstance(criteria, dict) or not 2 <= len(criteria) <= 255:
raise ValueError(f"{name}: choice requires 2–255 named options")
if any(not isinstance(k, str) or not k for k in criteria):
raise ValueError(f"{name}: option names must be non-empty strings")
descriptions = list(criteria.values())
elif kind == "score":
if not isinstance(criteria, list) or not 2 <= len(criteria) <= 255:
raise ValueError(f"{name}: score requires 2–255 ordered levels")
descriptions = criteria
elif kind == "noul":
if criteria is not None and (
not isinstance(criteria, dict) or set(criteria) not in (set(), {"true", "false"})
):
raise ValueError(f"{name}: noul criteria must be empty or describe true and false")
descriptions = list((criteria or {}).values())
else:
raise ValueError(f"{name}: type must be choice, noul or score")
if any(not isinstance(item, str) or not item.strip() for item in descriptions):
raise ValueError(f"{name}: every option description must be a non-empty string")
return request