Spaces:
Runtime error
Runtime error
File size: 3,023 Bytes
aad7814 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | """Medium AI: full past-report paragraph with proofread expansion."""
from __future__ import annotations
from docx import Document
from backend.config import settings
from backend.core import ingest, section_mapper
from backend.core.medium_expand import expand_medium_ai
from backend.core.minimum_weave import map_minimum_reference
from backend.core.rag_store import TIER_REFERENCE, get_rag_store
from backend.models.schema import TemplateSchema
def _write_past_roof_report(path):
doc = Document()
doc.add_paragraph("Section E2 - Roof Coverings")
doc.add_paragraph(
"The roof covering comprised slate tiles laid to pitched timber rafters. "
"Inspection was limited to ground level using binoculars."
)
doc.save(str(path))
def test_medium_keeps_full_reference_paragraph():
reference = (
"The roof covering comprised slate tiles laid to pitched timber rafters. "
"Inspection was limited to ground level using binoculars."
)
out = expand_medium_ai(["slate tile slipped south slope"], reference)
assert "•" not in out
assert out.count(".") >= 2
assert "slate" in out.lower()
assert "south slope" in out.lower()
assert "binoculars" in out.lower()
assert len(out.split()) >= len(reference.split()) * 0.9
def test_medium_at_least_as_long_as_minimum():
reference = (
"The roof covering comprised slate tiles laid to pitched timber rafters. "
"Inspection was limited to ground level using binoculars."
)
obs = ["slate tile slipped south slope"]
schema = TemplateSchema()
minimum = map_minimum_reference(reference, obs, schema)
medium = expand_medium_ai(obs, reference, schema=schema)
assert len(medium.split()) >= len(minimum.split()) * 0.9
assert medium.count(".") >= minimum.count(".")
def test_medium_mode_full_paragraph_in_report(tmp_path, monkeypatch):
monkeypatch.setattr(settings, "data_dir", str(tmp_path / "data"))
tpl = tmp_path / "template.docx"
doc = Document()
doc.add_paragraph("D2 Roof coverings")
doc.save(str(tpl))
master = tmp_path / "master.docx"
doc2 = Document()
doc2.add_paragraph("Section E2 - Roof Coverings")
doc2.add_paragraph("The roof covering comprises slate laid to pitched timber rafters.")
doc2.save(str(master))
past = tmp_path / "past.docx"
_write_past_roof_report(past)
tenant = "t-med-ref"
ingest.ingest_report_template(tenant, tpl)
ingest.ingest_standard_paragraphs(tenant, master)
ingest.ingest_reference(tenant, past)
assert get_rag_store().count(tenant, TIER_REFERENCE) >= 1
result = section_mapper.generate_report(
tenant,
"slate tile slipped south slope",
interference_level="medium",
)
d3 = next(s for s in result.sections if s.section_id == "D2")
assert d3.status == "OK"
assert "•" not in d3.text
assert d3.text.count(".") >= 2
assert "binoculars" in d3.text.lower()
assert "south slope" in d3.text.lower()
|