Spaces:
Sleeping
Sleeping
File size: 6,978 Bytes
e2f0d06 | 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | """
Lazy-loading inference wrappers for the 3 trained LoRA agents.
Each model is loaded on first call and cached for the session.
Falls back gracefully to None if GPU unavailable or models fail to load.
"""
from __future__ import annotations
import json
import re
import threading
from typing import Optional
# HF Hub model IDs
EXTRACTOR_HUB = "ps2181/extractor-lora-qwen2.5-1.5b"
AUDITOR_HUB = "ps2181/auditor-lora-qwen2.5-1.5b"
GENERATOR_HUB = "ps2181/generator-lora-qwen2.5-1.5b"
BASE_MODEL = "unsloth/Qwen2.5-1.5B-Instruct"
EXTRACTOR_SYSTEM = (
"Extract invoice fields. Return JSON only: "
"{vendor, date (YYYY-MM-DD), currency (USD/EUR/GBP), total (float), "
"line_items [{description, qty, unit_price, amount}]}"
)
AUDITOR_SYSTEM = (
"You are an invoice fraud auditor. Review each invoice and output a JSON array.\n"
"For each invoice output: "
"{\"invoice_id\": \"INV-XXXXX\", \"verdict\": \"approved\" or \"flagged\", "
"\"fraud_type\": null or one of [\"phantom_vendor\",\"price_gouging\","
"\"math_fraud\",\"duplicate_submission\"], \"confidence\": 0.0-1.0}\n"
"Output ONLY valid JSON: {\"audit_results\": [...]}"
)
GENERATOR_SYSTEM = (
"You are an invoice generator. Create a realistic fraudulent invoice as JSON.\n"
"Output ONLY valid JSON with keys: vendor, date, currency, total, line_items, invoice_id."
)
_lock = threading.Lock()
# Maps name -> (model, tokenizer, device) | None
_cache: dict = {}
_load_errors: dict = {}
def _load(hub_id: str):
"""Load base model + LoRA adapter. Returns (model, tokenizer, device) or None."""
try:
import torch
from peft import PeftModel
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
has_gpu = torch.cuda.is_available()
device = "cuda" if has_gpu else "cpu"
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
if has_gpu:
bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16)
base = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
quantization_config=bnb,
device_map="auto",
trust_remote_code=True,
)
else:
base = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
device_map="cpu",
torch_dtype="auto",
trust_remote_code=True,
)
model = PeftModel.from_pretrained(base, hub_id)
model.eval()
return model, tokenizer, device
except Exception as exc:
print(f"[agents] Could not load {hub_id}: {exc}")
return None
def _get(name: str, hub_id: str):
with _lock:
if name not in _cache:
print(f"[agents] Loading {name}…")
result = _load(hub_id)
_cache[name] = result
if result is None:
_load_errors[name] = f"load failed for {hub_id}"
return _cache[name]
def _generate(model, tokenizer, device: str, system: str, user: str, max_new_tokens: int = 400) -> str:
import torch
messages = [
{"role": "system", "content": system},
{"role": "user", "content": user},
]
inputs = tokenizer.apply_chat_template(
messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
)
if device == "cuda":
inputs = inputs.to("cuda")
with torch.no_grad():
out = model.generate(
inputs,
max_new_tokens=max_new_tokens,
temperature=0.3,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
return tokenizer.decode(out[0][inputs.shape[1]:], skip_special_tokens=True)
def _parse(text: str):
text = text.strip()
if text.startswith("```"):
text = re.sub(r"^```[a-z]*\n?", "", text)
text = re.sub(r"```$", "", text).strip()
try:
return json.loads(text)
except json.JSONDecodeError:
return {}
# ---------------------------------------------------------------------------
# Public inference functions
# ---------------------------------------------------------------------------
def run_extractor(raw_text: str, ref_data: str = "") -> tuple[dict, bool]:
"""
Run trained Extractor LoRA on raw invoice text.
Returns (extracted_dict, used_model).
used_model=False means model unavailable, caller should use rule-based fallback.
"""
result = _get("extractor", EXTRACTOR_HUB)
if result is None:
return {}, False
model, tokenizer, device = result
user = (f"REF:\n{ref_data[:200]}\n\nINVOICE:\n{raw_text[:600]}"
if ref_data else raw_text[:600])
text = _generate(model, tokenizer, device, EXTRACTOR_SYSTEM, user)
parsed = _parse(text)
if isinstance(parsed, list):
parsed = parsed[0] if parsed else {}
return (parsed if isinstance(parsed, dict) else {}), True
def run_auditor(raw_text: str, ref_data: str, n_invoices: int) -> tuple[list, bool]:
"""
Run trained Auditor LoRA on invoice batch text.
Returns (audit_results_list, used_model).
"""
result = _get("auditor", AUDITOR_HUB)
if result is None:
return [], False
model, tokenizer, device = result
user = (
f"INVOICE BATCH:\n{raw_text[:800]}\n\n"
f"REFERENCE DATA:\n{ref_data[:400]}\n\n"
'Audit all invoices. Output: {"audit_results": [...]}'
)
text = _generate(model, tokenizer, device, AUDITOR_SYSTEM, user)
parsed = _parse(text)
if isinstance(parsed, dict):
results = parsed.get("audit_results", [])
elif isinstance(parsed, list):
results = parsed
else:
results = []
return results, True
def run_generator(fraud_type: str, blind_spots: list | None = None) -> tuple[dict, bool]:
"""
Run trained Generator LoRA to create a fraudulent invoice.
Returns (invoice_dict, used_model).
"""
result = _get("generator", GENERATOR_HUB)
if result is None:
return {}, False
model, tokenizer, device = result
ctx = f"Regulator blind spots: {blind_spots}" if blind_spots else ""
user = (
f"Generate a realistic invoice with {fraud_type} fraud. {ctx}\n"
"Output JSON only: {{vendor, date, currency, total, line_items, invoice_id}}"
)
text = _generate(model, tokenizer, device, GENERATOR_SYSTEM, user)
parsed = _parse(text)
return (parsed if isinstance(parsed, dict) else {}), True
def models_status() -> dict[str, str]:
"""Return load status for all 3 agents."""
names = ["extractor", "auditor", "generator"]
status = {}
for n in names:
if n in _cache:
status[n] = "loaded ✅" if _cache[n] is not None else f"failed ❌ ({_load_errors.get(n,'')})"
else:
status[n] = "not loaded yet"
return status
|