Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions app/services/filler.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
23 changes: 14 additions & 9 deletions app/services/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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