v1.0.162: auto-classify backend — classify/grouping services, 7 new columns, /classify-batch, /api/groups, /apply-groups
This commit is contained in:
+48
-4
@@ -2,12 +2,12 @@
|
||||
from .connection import query, execute, execute_returning
|
||||
|
||||
|
||||
def insert(filename, mime_type, original_bytes, status="uploaded"):
|
||||
def insert(filename, mime_type, original_bytes, status="uploaded", batch_id=None):
|
||||
"""Insert document, return row dict."""
|
||||
return execute_returning(
|
||||
"""INSERT INTO documents (filename, mime_type, original_bytes, status)
|
||||
VALUES (%s, %s, %s, %s) RETURNING *""",
|
||||
(filename, mime_type, original_bytes, status),
|
||||
"""INSERT INTO documents (filename, mime_type, original_bytes, status, batch_id)
|
||||
VALUES (%s, %s, %s, %s, %s) RETURNING *""",
|
||||
(filename, mime_type, original_bytes, status, batch_id),
|
||||
)
|
||||
|
||||
|
||||
@@ -34,3 +34,47 @@ def set_error(doc_id, error_message):
|
||||
|
||||
def delete(doc_id):
|
||||
return execute("DELETE FROM documents WHERE id = %s", (doc_id,))
|
||||
|
||||
|
||||
def set_classification(doc_id, doc_type, own_number, parent_number, doc_date, counterparty):
|
||||
"""Store LLM classification results."""
|
||||
return execute(
|
||||
"""UPDATE documents SET doc_type=%s, own_number=%s, parent_number=%s,
|
||||
doc_date=%s, counterparty=%s, classify_status='classified'
|
||||
WHERE id=%s""",
|
||||
(doc_type, own_number, parent_number, doc_date, counterparty, doc_id),
|
||||
)
|
||||
|
||||
|
||||
def set_classify_failed(doc_id, error):
|
||||
return execute(
|
||||
"UPDATE documents SET classify_status='failed', error_message=%s WHERE id=%s",
|
||||
(error, doc_id),
|
||||
)
|
||||
|
||||
|
||||
def list_pending(batch_id):
|
||||
"""Documents waiting for classification."""
|
||||
return query(
|
||||
"SELECT id, filename, elements_json FROM documents WHERE batch_id=%s AND classify_status='pending' AND status='parsed'",
|
||||
(batch_id,),
|
||||
)
|
||||
|
||||
|
||||
def list_by_batch(batch_id):
|
||||
"""All documents in a batch with classification fields."""
|
||||
return query(
|
||||
"""SELECT id, filename, status, doc_type, own_number, parent_number,
|
||||
doc_date, counterparty, classify_status, error_message
|
||||
FROM documents WHERE batch_id=%s ORDER BY created_at""",
|
||||
(batch_id,),
|
||||
)
|
||||
|
||||
|
||||
def count_by_status(batch_id):
|
||||
"""Count documents by classify_status."""
|
||||
rows = query(
|
||||
"SELECT classify_status, count(*) as cnt FROM documents WHERE batch_id=%s GROUP BY classify_status",
|
||||
(batch_id,),
|
||||
)
|
||||
return {r["classify_status"]: r["cnt"] for r in rows}
|
||||
|
||||
Reference in New Issue
Block a user