Spaces:
Sleeping
Sleeping
Download services/export.py from DerXanity/farient-deckcheck: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/services/export.py
- Command line
-
hf download hf://spaces/DerXanity/farient-deckcheck/services/export.py
-
curl -L -o export.py https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/services/export.py
3.48 kB
| """Export helpers for the strict-PRD audit shape. | |
| Two formats: | |
| * ``to_json(audit_export)`` β returns a plain dict from | |
| ``AuditExportJson.model_dump()``. Because ``AuditExportJson`` references | |
| ``SlideEnvelope`` which references ``Finding`` (NOT ``FindingExtended``), | |
| the resulting JSON automatically excludes ``finding_meta``. | |
| * ``to_markdown(audit_export)`` β produces a human-readable report with | |
| one section per slide and a severity-badged sub-section per finding. | |
| """ | |
| from __future__ import annotations | |
| from typing import Any | |
| from schemas.api import AuditExportJson, Finding, SlideEnvelope | |
| # --------------------------------------------------------------------------- | |
| # JSON export | |
| # --------------------------------------------------------------------------- | |
| def to_json(audit_export: AuditExportJson) -> dict[str, Any]: | |
| """Return the strict-PRD JSON payload for ``/export?fmt=json``. | |
| ``AuditExportJson.model_dump()`` already drops ``finding_meta`` because | |
| the model uses ``Finding`` (no ``finding_meta``) and ``SlideEnvelope`` | |
| (no ``image_url``). Callers may JSON-encode the dict directly. | |
| """ | |
| return audit_export.model_dump() | |
| # --------------------------------------------------------------------------- | |
| # Markdown export | |
| # --------------------------------------------------------------------------- | |
| _SEVERITY_BADGE = { | |
| "high": "π΄ HIGH", | |
| "medium": "π‘ MEDIUM", | |
| "low": "π’ LOW", | |
| } | |
| def _badge(severity: str) -> str: | |
| return _SEVERITY_BADGE.get(severity, severity.upper()) | |
| def _render_finding(f: Finding) -> list[str]: | |
| lines: list[str] = [] | |
| lines.append(f"#### {_badge(f.severity)} β {f.title}") | |
| lines.append("") | |
| lines.append(f"- **Category:** {f.category}") | |
| lines.append(f"- **Confidence:** {f.confidence:.2f}") | |
| lines.append(f"- **Guideline:** {f.guideline_reference}") | |
| lines.append("") | |
| lines.append(f"**Description:** {f.description}") | |
| lines.append("") | |
| lines.append("**Evidence:**") | |
| lines.append("") | |
| for line in f.evidence.splitlines() or [f.evidence]: | |
| lines.append(f"> {line}" if line else ">") | |
| lines.append("") | |
| lines.append(f"**Suggested fix:** {f.suggested_fix}") | |
| lines.append("") | |
| return lines | |
| def _render_slide(slide: SlideEnvelope) -> list[str]: | |
| lines: list[str] = [] | |
| lines.append(f"### Slide {slide.slide_number}") | |
| lines.append("") | |
| if not slide.findings: | |
| lines.append("_No findings on this slide._") | |
| lines.append("") | |
| return lines | |
| for f in slide.findings: | |
| lines.extend(_render_finding(f)) | |
| return lines | |
| def to_markdown(audit_export: AuditExportJson) -> str: | |
| """Render the strict-PRD audit envelope as a Markdown report.""" | |
| lines: list[str] = [] | |
| lines.append(f"# Brand Compliance Audit β {audit_export.file_name}") | |
| lines.append("") | |
| lines.append(f"- **Job ID:** `{audit_export.job_id}`") | |
| lines.append(f"- **Slide count:** {audit_export.slide_count}") | |
| lines.append( | |
| f"- **Summary:** " | |
| f"{audit_export.summary.high} high Β· " | |
| f"{audit_export.summary.medium} medium Β· " | |
| f"{audit_export.summary.low} low" | |
| ) | |
| lines.append("") | |
| lines.append("---") | |
| lines.append("") | |
| lines.append("## Findings by slide") | |
| lines.append("") | |
| for slide in audit_export.slides: | |
| lines.extend(_render_slide(slide)) | |
| return "\n".join(lines).rstrip() + "\n" | |
| __all__ = ["to_json", "to_markdown"] | |