Commit ·
3a752ea
1
Parent(s): 4d673a4
Revert "feat: format fallback PDF headings from markdown markers"
Browse filesThis reverts commit 1599b38fd7310355a90a3585ad7a088a06839b94.
- ai-service/cam/cam_assembler.py +20 -57
ai-service/cam/cam_assembler.py
CHANGED
|
@@ -3,7 +3,6 @@ import json
|
|
| 3 |
import logging
|
| 4 |
import subprocess
|
| 5 |
import re as _re
|
| 6 |
-
import textwrap
|
| 7 |
from datetime import datetime
|
| 8 |
from typing import List, Dict, Any
|
| 9 |
|
|
@@ -399,80 +398,44 @@ def generate_fallback_pdf(cam_data: dict, job_id: str):
|
|
| 399 |
pdf_path = os.path.join(base_dir, "cam_final.pdf")
|
| 400 |
|
| 401 |
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 402 |
-
|
| 403 |
y = height - 2 * cm
|
| 404 |
|
| 405 |
-
def
|
| 406 |
nonlocal y
|
| 407 |
if y < 2 * cm:
|
| 408 |
c.showPage()
|
| 409 |
y = height - 2 * cm
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
def write_line(
|
| 413 |
-
text: str,
|
| 414 |
-
size: int = 10,
|
| 415 |
-
leading: float = 1.35,
|
| 416 |
-
font: str = "Helvetica",
|
| 417 |
-
spacer: float = 0,
|
| 418 |
-
):
|
| 419 |
-
nonlocal y
|
| 420 |
-
c.setFont(font, size)
|
| 421 |
for line in str(text).splitlines() or [""]:
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
c.setFont(
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
if spacer > 0:
|
| 429 |
-
y -= spacer
|
| 430 |
-
|
| 431 |
-
def strip_inline_markers(line: str) -> str:
|
| 432 |
-
line = _re.sub(r"\*\*(.*?)\*\*", r"\1", line)
|
| 433 |
-
line = _re.sub(r"\*(.*?)\*", r"\1", line)
|
| 434 |
-
return line
|
| 435 |
-
|
| 436 |
-
def write_markdownish_block(text: str):
|
| 437 |
-
nonlocal y
|
| 438 |
-
for raw in (text or "").splitlines():
|
| 439 |
-
line = raw.strip()
|
| 440 |
-
if not line:
|
| 441 |
-
y -= 4
|
| 442 |
-
continue
|
| 443 |
-
|
| 444 |
-
h1 = _re.match(r"^\*\*(.+)\*\*$", line)
|
| 445 |
-
if h1:
|
| 446 |
-
write_line(h1.group(1).strip(), size=12, font="Helvetica-Bold", spacer=2)
|
| 447 |
-
continue
|
| 448 |
-
|
| 449 |
-
h2 = _re.match(r"^\*(.+)\*$", line)
|
| 450 |
-
if h2:
|
| 451 |
-
write_line(h2.group(1).strip(), size=11, font="Helvetica-Bold", spacer=1)
|
| 452 |
-
continue
|
| 453 |
-
|
| 454 |
-
write_line(strip_inline_markers(line), size=10, font="Helvetica")
|
| 455 |
-
|
| 456 |
-
def write_section(title: str, body: str):
|
| 457 |
-
nonlocal y
|
| 458 |
-
write_line(title, size=12, font="Helvetica-Bold", spacer=2)
|
| 459 |
-
write_markdownish_block(body)
|
| 460 |
-
y -= 4
|
| 461 |
|
| 462 |
company = cam_data.get("company_name", "Company")
|
| 463 |
cro_out = cam_data.get("cro_output", {})
|
| 464 |
acc_out = cam_data.get("accountant_output", {})
|
| 465 |
comp_out = cam_data.get("compliance_output", {})
|
| 466 |
|
| 467 |
-
write_line(f"Credit Appraisal Memo - {company}", size=14
|
| 468 |
write_line(f"Date: {cam_data.get('date', datetime.utcnow().strftime('%Y-%m-%d'))}")
|
| 469 |
write_line(f"Decision: {cro_out.get('final_decision', 'UNKNOWN')}")
|
| 470 |
write_line(f"Final Score: {cam_data.get('final_score', 0)} / 100")
|
| 471 |
y -= 6
|
| 472 |
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
|
| 477 |
c.save()
|
| 478 |
logger.info(f"Generated fallback PDF for job {job_id}: {pdf_path}")
|
|
|
|
| 3 |
import logging
|
| 4 |
import subprocess
|
| 5 |
import re as _re
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
from typing import List, Dict, Any
|
| 8 |
|
|
|
|
| 398 |
pdf_path = os.path.join(base_dir, "cam_final.pdf")
|
| 399 |
|
| 400 |
c = canvas.Canvas(pdf_path, pagesize=A4)
|
| 401 |
+
width, height = A4
|
| 402 |
y = height - 2 * cm
|
| 403 |
|
| 404 |
+
def write_line(text: str, size: int = 10, leading: float = 1.35):
|
| 405 |
nonlocal y
|
| 406 |
if y < 2 * cm:
|
| 407 |
c.showPage()
|
| 408 |
y = height - 2 * cm
|
| 409 |
+
c.setFont("Helvetica", size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
for line in str(text).splitlines() or [""]:
|
| 411 |
+
if y < 2 * cm:
|
| 412 |
+
c.showPage()
|
| 413 |
+
y = height - 2 * cm
|
| 414 |
+
c.setFont("Helvetica", size)
|
| 415 |
+
c.drawString(2 * cm, y, line[:160])
|
| 416 |
+
y -= size * leading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
company = cam_data.get("company_name", "Company")
|
| 419 |
cro_out = cam_data.get("cro_output", {})
|
| 420 |
acc_out = cam_data.get("accountant_output", {})
|
| 421 |
comp_out = cam_data.get("compliance_output", {})
|
| 422 |
|
| 423 |
+
write_line(f"Credit Appraisal Memo - {company}", size=14)
|
| 424 |
write_line(f"Date: {cam_data.get('date', datetime.utcnow().strftime('%Y-%m-%d'))}")
|
| 425 |
write_line(f"Decision: {cro_out.get('final_decision', 'UNKNOWN')}")
|
| 426 |
write_line(f"Final Score: {cam_data.get('final_score', 0)} / 100")
|
| 427 |
y -= 6
|
| 428 |
|
| 429 |
+
write_line("Executive Recommendation", size=12)
|
| 430 |
+
write_line(cro_out.get("content", "No CRO recommendation available."))
|
| 431 |
+
y -= 6
|
| 432 |
+
|
| 433 |
+
write_line("Financial Assessment", size=12)
|
| 434 |
+
write_line(acc_out.get("content", "No accountant narrative available."))
|
| 435 |
+
y -= 6
|
| 436 |
+
|
| 437 |
+
write_line("Legal and Governance", size=12)
|
| 438 |
+
write_line(comp_out.get("content", "No compliance narrative available."))
|
| 439 |
|
| 440 |
c.save()
|
| 441 |
logger.info(f"Generated fallback PDF for job {job_id}: {pdf_path}")
|