Skip to content

Commit

Permalink
Use experimental FormFyxer interview generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nonprofittechy committed Dec 17, 2023
1 parent c1d31ed commit 97f6758
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion docassemble/ALWeaver/data/questions/assembly_line.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,10 @@ fields:
choices:
- Build step by step: False
- Use auto-drafting mode: True
- Use experimental GPT auto-drafting: interview.use_gpt_to_draft_json
datatype: yesno
disable others:
- interview.start_with_json
- Upload JSON file with draft screen arrangement (advanced): interview.start_with_json
help: |
If you have a JSON file that has a draft of the screens and their order, you can upload
Expand Down Expand Up @@ -1993,6 +1997,13 @@ code: |
yes_recognize_form_fields = False
interview_type = "regular"
if not has_safe_pdf_in_url:
if hasattr(interview, "use_gpt_to_draft_json") and interview.use_gpt_to_draft_json:
document_text = get_document_text(interview.uploaded_templates)
document_fields = get_fields(interview.uploaded_templates)
draft_screens = formfyxer.draft_interview_questions(document_fields, document_text)
interview.auto_assign_attributes(
screens=draft_screens
)
if hasattr(interview, "start_with_json") and interview.start_with_json:
#try: # Don't know yet why this always raises an exception. A name error that DA silently fixes?
interview.parsed_json = json.loads(interview.uploaded_json.slurp())
Expand All @@ -2004,7 +2015,7 @@ code: |
screens=interview.parsed_json["questions"],
interview_logic = interview.parsed_json["interview order"]
)
if isinstance(interview.parsed_json, dict) and "questions" in interview.parsed_json:
elif isinstance(interview.parsed_json, dict) and "questions" in interview.parsed_json:
# Let author upload just the "questions" dict without "interview order"
interview.auto_assign_attributes(screens=interview.parsed_json["questions"])
elif isinstance(interview.parsed_json, list) and next(iter(interview.parsed_json), None) and isinstance(next(iter(interview.parsed_json)), dict):
Expand Down
12 changes: 12 additions & 0 deletions docassemble/ALWeaver/interview_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def formfyxer_available():
"get_court_choices",
"get_docx_validation_errors",
"get_docx_variables",
"get_document_text",
"get_fields",
"get_question_file_variables",
"get_pdf_validation_errors",
Expand Down Expand Up @@ -2148,6 +2149,17 @@ def docx_variable_fix(variable: str) -> str:
return variable


def get_document_text(document: Union[DAFile, DAFileList]) -> str:
if isinstance(document, DAFileList):
if document[0].mimetype == "application/pdf":
return formfyxer.extract_text(document[0].path())
return docx2python(document.path()).text
else:
if document.mimetype == "application/pdf":
return formfyxer.extract_text(document.path())
return docx2python(document.path()).text


def get_fields(document: Union[DAFile, DAFileList]) -> Iterable:
"""Get the list of fields needed inside a template file (PDF or Docx Jinja
tags). This will include attributes referenced. Assumes a file that
Expand Down

0 comments on commit 97f6758

Please sign in to comment.