Hoctar77 commited on
Commit
dec3aa4
·
verified ·
1 Parent(s): 0b6b372

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -74
app.py CHANGED
@@ -4,6 +4,7 @@ import re
4
  from docx import Document
5
  import tempfile
6
  import os
 
7
 
8
  def setup_logging():
9
  """Initialize logging configuration."""
@@ -642,63 +643,41 @@ def format_results_for_gradio(heading_valid, headings_found, acronyms_valid, und
642
 
643
  return "\n".join(results)
644
 
645
- # Modify the process_document function to return formatted results instead of writing to file
646
- def process_document(file_obj, doc_type, template_type):
647
- """Process the document and perform checks based on document type and template type."""
648
- logger = logging.getLogger(__name__)
 
 
649
 
650
- # Create a temporary file to save the uploaded file
651
- with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as tmp_file:
652
- tmp_file.write(file_obj.read())
653
- tmp_path = tmp_file.name
654
 
655
  try:
656
- # Step 1: Read the Word document
657
- doc = read_word_document(tmp_path)
658
- logger.info("Document read successfully.")
659
-
660
- # Get required headings for document type and template type
661
- checks = get_document_checks(doc_type, template_type)
662
- required_headings = checks.get("required_headings", [])
663
 
664
- # Step 2: Perform all checks
665
- heading_valid, headings_found = heading_title_check(doc, required_headings)
666
- acronyms_valid, undefined_acronyms = acronym_check(doc)
667
- legal_valid, incorrect_legal_references = legal_check(doc)
668
- table_valid, incorrect_captions = table_caption_check(doc, doc_type)
669
- figure_valid, incorrect_fig_captions = figure_caption_check(doc, doc_type)
670
- references_valid, incorrect_table_figure_references = table_figure_reference_check(doc, doc_type)
671
- title_style_valid, incorrect_titles = document_title_check(tmp_path, doc_type)
672
- double_period_valid, incorrect_sentences = double_period_check(doc)
673
- spacing_valid, incorrect_spacing = spacing_check(doc)
674
- abbreviation_issues = check_abbreviation_usage(doc)
675
- date_issues = check_date_formats(doc)
676
- placeholder_issues = check_placeholders(doc)
677
-
678
- # Format results for Gradio
679
- results = format_results_for_gradio(
680
- heading_valid, headings_found,
681
- acronyms_valid, undefined_acronyms,
682
- legal_valid, incorrect_legal_references,
683
- table_valid, incorrect_captions,
684
- figure_valid, incorrect_fig_captions,
685
- references_valid, incorrect_table_figure_references,
686
- title_style_valid, incorrect_titles,
687
- required_headings, doc_type,
688
- double_period_valid, incorrect_sentences,
689
- spacing_valid, incorrect_spacing,
690
- abbreviation_issues, date_issues,
691
- placeholder_issues
692
- )
693
 
694
- return results
695
-
696
- finally:
697
- # Clean up the temporary file
698
- os.unlink(tmp_path)
699
 
700
- # Create the Gradio interface
701
- def create_gradio_interface():
 
 
702
  document_types = [
703
  "Advisory Circular", "Airworthiness Criteria", "Deviation Memo", "Exemption",
704
  "Federal Register Notice", "Handbook/Manual", "Order", "Policy Statement",
@@ -707,30 +686,66 @@ def create_gradio_interface():
707
 
708
  template_types = ["Short AC template AC", "Long AC template AC"]
709
 
710
- def process_file(file_obj, doc_type, template_type):
711
- if doc_type != "Advisory Circular":
712
- template_type = "N/A"
713
- return process_document(file_obj, doc_type, template_type)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714
 
715
- # Create the interface
716
- iface = gr.Interface(
717
  fn=process_file,
718
- inputs=[
719
- gr.File(label="Upload Word Document (.docx)", type="binary"),
720
- gr.Dropdown(choices=document_types, label="Document Type"),
721
- gr.Radio(choices=template_types, label="Template Type (Only for Advisory Circular)", visible=True)
722
- ],
723
- outputs=gr.Markdown(label="Check Results"),
724
- title="FAA Document Checker",
725
- description="Upload a Word document to check for compliance with FAA documentation standards.",
726
- article="This tool checks document formatting, headings, acronyms, legal references, and more.",
727
- theme="default"
728
  )
729
 
730
- return iface
731
-
732
- # Launch the Gradio interface
733
- if __name__ == "__main__":
734
- setup_logging()
735
- iface = create_gradio_interface()
736
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from docx import Document
5
  import tempfile
6
  import os
7
+ import traceback
8
 
9
  def setup_logging():
10
  """Initialize logging configuration."""
 
643
 
644
  return "\n".join(results)
645
 
646
+ def process_file(file_obj, doc_type, template_type):
647
+ """
648
+ Process the uploaded file and return results with error handling
649
+ """
650
+ if file_obj is None:
651
+ return "Please upload a document first."
652
 
653
+ if not hasattr(file_obj, 'name') or not file_obj.name.endswith('.docx'):
654
+ return "Please upload a valid .docx file."
 
 
655
 
656
  try:
657
+ # Process the document and get results
658
+ results = process_document(file_obj, doc_type, template_type)
659
+ return results
660
+ except Exception as e:
661
+ error_trace = traceback.format_exc()
662
+ error_message = f"""An error occurred while processing the document:
 
663
 
664
+ Error: {str(e)}
665
+
666
+ Please ensure:
667
+ 1. The file is a valid Word document (.docx)
668
+ 2. The file is not corrupted
669
+ 3. The file is not password protected"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
670
 
671
+ logging.error(f"Error processing document: {error_trace}")
672
+ return error_message
673
+
674
+ # Create the interface with simplified layout for Hugging Face Spaces
675
+ demo = gr.Blocks(title="FAA Document Checker")
676
 
677
+ with demo:
678
+ gr.Markdown("# FAA Document Checker")
679
+ gr.Markdown("Upload a Word document to check for compliance with FAA documentation standards.")
680
+
681
  document_types = [
682
  "Advisory Circular", "Airworthiness Criteria", "Deviation Memo", "Exemption",
683
  "Federal Register Notice", "Handbook/Manual", "Order", "Policy Statement",
 
686
 
687
  template_types = ["Short AC template AC", "Long AC template AC"]
688
 
689
+ with gr.Row():
690
+ with gr.Column(scale=1):
691
+ file_input = gr.File(
692
+ label="Upload Word Document (.docx)",
693
+ file_types=[".docx"],
694
+ type="binary"
695
+ )
696
+ doc_type = gr.Dropdown(
697
+ choices=document_types,
698
+ label="Document Type",
699
+ value="Advisory Circular"
700
+ )
701
+ template_type = gr.Radio(
702
+ choices=template_types,
703
+ label="Template Type (Only for Advisory Circular)",
704
+ visible=True,
705
+ value="Short AC template AC"
706
+ )
707
+ submit_btn = gr.Button("Check Document", variant="primary")
708
+
709
+ with gr.Column(scale=2):
710
+ output = gr.Markdown(
711
+ label="Check Results",
712
+ value="Results will appear here after processing..."
713
+ )
714
+
715
+ # Update template type visibility based on document type
716
+ def update_template_visibility(doc_type):
717
+ return gr.update(visible=doc_type == "Advisory Circular")
718
+
719
+ doc_type.change(
720
+ fn=update_template_visibility,
721
+ inputs=[doc_type],
722
+ outputs=[template_type]
723
+ )
724
 
725
+ # Process file when submit button is clicked
726
+ submit_btn.click(
727
  fn=process_file,
728
+ inputs=[file_input, doc_type, template_type],
729
+ outputs=[output]
 
 
 
 
 
 
 
 
730
  )
731
 
732
+ gr.Markdown("""
733
+ ## Instructions
734
+ 1. Upload your Word document (.docx format)
735
+ 2. Select the document type
736
+ 3. If it's an Advisory Circular, select the template type
737
+ 4. Click 'Check Document' to process
738
+ 5. Review the results in the output panel
739
+
740
+ The checker will verify:
741
+ - Required headings
742
+ - Acronym definitions
743
+ - Legal terminology
744
+ - Table and figure captions
745
+ - Document title styling
746
+ - Spacing and formatting
747
+ - And more...
748
+ """)
749
+
750
+ # For Hugging Face Spaces, we just need to expose the 'demo'
751
+ demo.launch()