Spaces:
Sleeping
Sleeping
File size: 3,321 Bytes
64c9151 a7f240e 64c9151 a7f240e 64c9151 a7f240e 64c9151 a7f240e 0bcce8b a7f240e 0bcce8b a7f240e 0bcce8b a7f240e 0bcce8b a7f240e a817787 a7f240e 64c9151 a7f240e a817787 a7f240e a817787 a7f240e a817787 a7f240e a817787 64c9151 a7f240e f60efb7 a7f240e f60efb7 64c9151 a7f240e 0bcce8b a7f240e 64c9151 a7f240e a817787 a7f240e a817787 a7f240e 0bcce8b a7f240e 64c9151 a7f240e | 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 | """
ccpu_parser.extract
-------------------
NOUVEAU PIPELINE SIMPLIFIÉ (VISION UNIQUEMENT) :
fichier (PDF/JPG/PNG/...) --(file_bytes_to_jpeg)--> JPEG unique
JPEG + prompt JSON CCPU --(call_vision_llm_to_json)--> texte JSON
texte JSON --(json.loads)--> dict Python
Responsabilité :
- Orchestrer la conversion du fichier en image.
- Construire le prompt CCPU adapté au JSON cible.
- Appeler le modèle multimodal DeepInfra.
- Parser le JSON et le retourner directement à l'UI Gradio.
Fonctions fournies :
- extract_ccpu_from_pdf(file_bytes: bytes) -> dict
(Nom conservé pour compatibilité avec app.py, mais gère PDF *et* images.
Renvoie un dict Python correspondant exactement au JSON produit par le LLM.)
"""
from __future__ import annotations
import json
from typing import Any, Dict
from .pdf_loader import file_bytes_to_jpeg
from .prompting import build_ccpu_vision_prompt
from .llm_client import call_vision_llm_to_json
from .utils import logger
def extract_ccpu_from_pdf(file_bytes: bytes) -> Dict[str, Any]:
"""
Point d'entrée principal pour l'analyse CCPU.
Entrée :
- file_bytes : contenu binaire du fichier uploadé (PDF, JPG, PNG, ...).
Étapes :
1. Normalisation du fichier en un JPEG unique (1ère page pour les PDFs).
2. Construction du prompt qui impose le schéma JSON.
3. Appel du modèle vision DeepInfra (Llama-4 Maverick).
4. Parsing du JSON renvoyé.
Sortie :
- Un dict Python correspondant exactement au JSON renvoyé par le LLM.
Exceptions :
- ValueError si le fichier est vide ou non exploitable.
- json.JSONDecodeError si la sortie du LLM n'est pas un JSON valide.
- LLMClientError en cas d'erreur d'appel LLM (voir llm_client).
"""
if not file_bytes:
raise ValueError("extract_ccpu_from_pdf: fichier binaire vide")
logger.info(
"extract_ccpu_from_pdf: début traitement (taille fichier=%d octets)",
len(file_bytes),
)
# 1. Fichier -> JPEG
image_jpeg = file_bytes_to_jpeg(file_bytes)
logger.info(
"extract_ccpu_from_pdf: normalisation OK (JPEG=%d octets)",
len(image_jpeg),
)
# 2. Prompt JSON CCPU
prompt = build_ccpu_vision_prompt()
logger.info(
"extract_ccpu_from_pdf: prompt CCPU construit (len=%d caractères)",
len(prompt),
)
# 3. Appel LLM vision
raw_text = call_vision_llm_to_json(image_jpeg, prompt)
# 4. Parsing JSON strict
try:
data = json.loads(raw_text)
except json.JSONDecodeError as exc:
logger.error(
"extract_ccpu_from_pdf: échec de json.loads: %s\nRéponse brute (1k chars): %s",
exc,
raw_text[:1000],
)
# On laisse remonter l'exception, app.py la transformera en gr.Error
raise
if not isinstance(data, dict):
logger.error(
"extract_ccpu_from_pdf: le JSON racine n'est pas un objet (type=%r)",
type(data),
)
raise ValueError("Le LLM n'a pas renvoyé un objet JSON au niveau racine.")
logger.info(
"extract_ccpu_from_pdf: analyse terminée avec succès (clés racine=%s)",
list(data.keys()),
)
return data
|