Spaces:
Runtime error

humanizer-engine / app /api /models.py
faseehsandhu's picture
stage 1 completed
f66da21
Raw History Blame Contribute Delete
3.56 kB
"""
Pydantic request/response models for the Humanization Engine API.
"""
from __future__ import annotations
from typing import Any, Literal, Optional
from pydantic import BaseModel, Field, field_validator
# ---------------------------------------------------------------------------
# Request models
# ---------------------------------------------------------------------------
class HumanizeRequest(BaseModel):
"""POST /humanize request body."""
text: str = Field(
...,
description="The AI-generated text to humanize (50–5000 words)",
min_length=10,
)
mode: Literal["standard", "thorough"] = Field(
default="standard",
description=(
"'standard' skips Stage 3 perplexity re-scoring; "
"'thorough' enables it for a better result at ~200ms extra cost"
),
)
@field_validator("text")
@classmethod
def text_must_not_be_blank(cls, v: str) -> str:
"""Reject whitespace-only input."""
if not v.strip():
raise ValueError("text must not be blank")
return v
class AnalyzeRequest(BaseModel):
"""POST /analyze request body."""
text: str = Field(
...,
description="The text to analyse (returns Stage 1 analysis without LLM rewrite)",
min_length=10,
)
@field_validator("text")
@classmethod
def text_must_not_be_blank(cls, v: str) -> str:
if not v.strip():
raise ValueError("text must not be blank")
return v
# ---------------------------------------------------------------------------
# Shared sub-models
# ---------------------------------------------------------------------------
class ProcessingStats(BaseModel):
"""Processing statistics returned with every successful /humanize response."""
input_word_count: int
output_word_count: int
expansion_percentage: float
input_avg_perplexity: Optional[float] = None
output_avg_perplexity: Optional[float] = None
input_burstiness_cv: Optional[float] = None
output_burstiness_cv: Optional[float] = None
banned_words_found_in_output: list[str] = Field(default_factory=list)
banned_words_auto_fixed: int = 0
instructions_generated: int = 0
high_risk_sentences: int = 0
processing_time_ms: float = 0.0
stage1_time_ms: float = 0.0
stage2_time_ms: float = 0.0
stage3_time_ms: float = 0.0
prompt_variant_used: str = ""
llm_used: str = ""
# ---------------------------------------------------------------------------
# Response models
# ---------------------------------------------------------------------------
class HumanizeResponse(BaseModel):
"""POST /humanize success response."""
success: Literal[True] = True
output_text: str
stats: ProcessingStats
class HumanizeErrorResponse(BaseModel):
"""POST /humanize error response."""
success: Literal[False] = False
error: str
code: Literal[
"INPUT_TOO_SHORT",
"INPUT_TOO_LONG",
"LLM_TIMEOUT",
"PROCESSING_ERROR",
"INVALID_INPUT",
]
class AnalyzeResponse(BaseModel):
"""POST /analyze success response."""
success: Literal[True] = True
analysis: dict[str, Any]
class AnalyzeErrorResponse(BaseModel):
"""POST /analyze error response."""
success: Literal[False] = False
error: str
code: str
class HealthResponse(BaseModel):
"""GET /health response."""
status: Literal["healthy", "degraded", "starting"]
models_loaded: bool
uptime_seconds: float