diff --git a/app/services/filler.py b/app/services/filler.py index 148b2ff..86c4c07 100644 --- a/app/services/filler.py +++ b/app/services/filler.py @@ -1,6 +1,6 @@ from datetime import datetime -from pdfrw import PdfReader, PdfWriter +from pdfrw import PdfReader, PdfWriter, PdfName from app.services.llm import LLM @@ -41,8 +41,31 @@ def fill_form(self, pdf_form: str, llm: LLM): for annot in sorted_annots: if annot.Subtype == "/Widget" and annot.T: if i < len(answers_list): - annot.V = f"{answers_list[i]}" - annot.AP = None + val = answers_list[i] + + # Inspection du type de champ pour identifier les boutons / cases à cocher (/Btn) + ft = getattr(annot, 'FT', None) + if ft == '/Btn': + # Traduction de la réponse de l'IA en état booléen + is_checked = False + if isinstance(val, bool): + is_checked = val + elif isinstance(val, str): + is_checked = val.lower() in ['true', 'yes', '1', 'on', 'checked', 'oui'] + + # Attribution de la valeur et de l'état d'affichage (Appearance State) + if is_checked: + annot.V = PdfName('/Yes') + annot.AS = PdfName('/Yes') + else: + annot.V = PdfName('/Off') + annot.AS = PdfName('/Off') + annot.AP = None + else: + # Logique standard pour les champs de texte (/Tx) + annot.V = f"{val}" + annot.AP = None + i += 1 else: # Stop if we run out of answers @@ -51,4 +74,4 @@ def fill_form(self, pdf_form: str, llm: LLM): PdfWriter().write(output_pdf, pdf) # Your main.py expects this function to return the path - return output_pdf + return output_pdf \ No newline at end of file diff --git a/app/services/llm.py b/app/services/llm.py index 85e168c..4e36469 100644 --- a/app/services/llm.py +++ b/app/services/llm.py @@ -36,7 +36,8 @@ def main_loop(self): total_fields = len(self._target_fields) for i, (field, field_type) in enumerate(self._target_fields.items(), 1): - prompt = self.build_prompt(field, field_type if isinstance(field_type, str) else "string") + f_type = field_type if isinstance(field_type, str) else "string" + prompt = self.build_prompt(field, f_type) ollama_url = f"{OLLAMA_HOST}/api/generate" ollama_model = self._model or OLLAMA_MODEL @@ -70,30 +71,34 @@ def main_loop(self): raise RuntimeError("Failed to get response from Ollama after retries.") else: parsed_response = json_data["response"] - self.add_response_to_json(field, parsed_response) + # Passage du type de champ pour permettre le mapping booléen + self.add_response_to_json(field, parsed_response, field_type=f_type) logger.info("[%d/%d] Extracted data for field '%s' successfully.", i, total_fields, field) logger.info("Resulting JSON created from the input text:\n%s", json.dumps(self._json, indent=2)) return self - def add_response_to_json(self, field: str, value: str): + def add_response_to_json(self, field: str, value: str, field_type: str = "string"): """ - this method adds the following value under the specified field, - or under a new field if the field doesn't exist, to the json dict + This method adds the following value under the specified field, + or under a new field if the field doesn't exist, to the json dict. + Includes boolean mapping for checkbox/button fields. """ value = value.strip().replace('"', "") parsed_value = None if value != "-1": - parsed_value = value + # Mapping des types booléens / cases à cocher + if field_type.lower() in ["boolean", "bool", "checkbox", "btn"]: + parsed_value = value.lower() in ["true", "yes", "1", "on", "checked", "oui"] + else: + parsed_value = value if field in self._json.keys(): self._json[field].append(parsed_value) else: self._json[field] = parsed_value - - def get_data(self): - return self._json + return self._json \ No newline at end of file