Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Give me code with 3 APIs where I upload a docx file and then in second API the user will…

  1. User

    Give me code with 3 APIs where I upload a docx file and then in second API the user will provide instructions and in third API it should return us drafted document. In the output docx file there should be comments in the documents on some lines in the document based on the instructions provided in the second API. And I want code with FLASK APIs.

  2. Assistant

    Certainly! Below is a complete Flask application that provides three APIs to:

    1. Upload a DOCX file.
    2. Submit instructions for adding comments to the document.
    3. Generate and download the annotated DOCX file with comments based on the provided instructions.

    Overview

    • Dependencies:

      • Flask: Web framework to create APIs.
      • python-docx: To manipulate DOCX files.
      • lxml: To handle advanced XML manipulations required for adding comments.
      • uuid: To generate unique identifiers for file and instruction storage.
      • os: To handle file system operations.
    • Endpoints:

      1. /upload (POST): Uploads a DOCX file.
      2. /instructions (POST): Submits instructions to add comments.
      3. /draft (GET): Generates and returns the annotated DOCX file.
    • Storage:

      • Uploaded files and instructions are stored temporarily on the server using unique IDs. For a production environment, consider using a persistent storage solution.

    Setup

    1. Install Required Libraries:

      pip install Flask python-docx lxml
      
    2. Run the Flask Application: Save the below code in a file, e.g., app.py, and run:

      python app.py
      

    Complete Code

    from flask import Flask, request, jsonify, send_file
    from werkzeug.utils import secure_filename
    from docx import Document
    from docx.opc.constants import RELATIONSHIP_TYPE as RT
    from docx.oxml import OxmlElement
    from docx.oxml.ns import qn
    import os
    import uuid
    import json
    from lxml import etree
    
    app = Flask(__name__)
    
    # Configuration
    UPLOAD_FOLDER = 'uploads'
    ANNOTATED_FOLDER = 'annotated'
    if not os.path.exists(UPLOAD_FOLDER):
        os.makedirs(UPLOAD_FOLDER)
    if not os.path.exists(ANNOTATED_FOLDER):
        os.makedirs(ANNOTATED_FOLDER)
    
    # In-memory storage (For demo purposes. Use persistent storage in production)
    file_storage = {}
    instruction_storage = {}
    
    
    def add_comment_to_document(doc_path, instructions, output_path):
        """
        Adds comments to the DOCX file based on instructions.
        Instructions format:
        [
            {
                "search_text": "Text to search in the document",
                "comment": "Comment to add"
            },
            ...
        ]
        """
        # Load the document
        doc = Document(doc_path)
    
        # Access the underlying XML to add comments
        # Initialize comment parts if not present
        package = doc.part.package
        comments_part = None
        for rel in doc.part.rels.values():
            if rel.reltype == RT.COMMENTS:
                comments_part = rel.target_part
                break
        if not comments_part:
            comments_part = doc.part.get_or_add_comments_part()
        comments = comments_part.element
    
        # Get existing comment IDs
        existing_comment_ids = [
            int(comment.get(qn('w:id'))) for comment in comments.findall('w:comment', namespaces=comments.nsmap)
        ]
        next_comment_id = max(existing_comment_ids, default=0) + 1
    
        # Iterate through paragraphs and add comments based on instructions
        for para in doc.paragraphs:
            for instr in instructions:
                if instr['search_text'] in para.text:
                    # Find the run containing the search_text
                    for run in para.runs:
                        if instr['search_text'] in run.text:
                            # Split the run text to isolate the search_text
                            parts = run.text.split(instr['search_text'])
                            if len(parts) >= 2:
                                run.text = parts[0]
                                new_run = para.add_run(instr['search_text'])
                                italic_run = para.add_run(parts[1])
                                
                                # Create the comment reference
                                comment_ref = OxmlElement('w:commentReference')
                                comment_ref.set(qn('w:id'), str(next_comment_id))
                                new_run._r.append(comment_ref)
                                
                                # Add the comment to the comments part
                                comment_element = OxmlElement('w:comment')
                                comment_element.set(qn('w:id'), str(next_comment_id))
                                comment_element.set(qn('w:author'), 'FlaskApp')
                                comment_element.set(qn('w:date'), '2023-10-10T00:00:00Z')
                                comment_text = OxmlElement('w:p')
                                comment_run = OxmlElement('w:r')
                                comment_t = OxmlElement('w:t')
                                comment_t.text = instr['comment']
                                comment_run.append(comment_t)
                                comment_text.append(comment_run)
                                comment_element.append(comment_text)
                                comments.append(comment_element)
                                
                                next_comment_id += 1
        # Save the annotated document
        doc.save(output_path)
    
    
    @app.route('/upload', methods=['POST'])
    def upload_docx():
        """
        Upload a DOCX file.
        Expects a form-data with 'file' key.
        Returns a unique file_id.
        """
        if 'file' not in request.files:
            return jsonify({'error': 'No file part in the request'}), 400
        file = request.files['file']
        if file.filename == '':
            return jsonify({'error': 'No selected file'}), 400
        if file and file.filename.endswith('.docx'):
            filename = secure_filename(file.filename)
            file_id = str(uuid.uuid4())
            filepath = os.path.join(UPLOAD_FOLDER, f"{file_id}_{filename}")
            file.save(filepath)
            file_storage[file_id] = filepath
            return jsonify({'file_id': file_id}), 200
        else:
            return jsonify({'error': 'Invalid file type. Only .docx files are allowed.'}), 400
    
    
    @app.route('/instructions', methods=['POST'])
    def submit_instructions():
        """
        Submit instructions for adding comments.
        Expects a JSON payload with:
        {
            "file_id": "uploaded_file_id",
            "instructions": [
                {
                    "search_text": "Text to search",
                    "comment": "Comment to add"
                },
                ...
            ]
        }
        """
        data = request.get_json()
        if not data:
            return jsonify({'error': 'Invalid JSON payload'}), 400
        file_id = data.get('file_id')
        instructions = data.get('instructions')
        if not file_id or not instructions:
            return jsonify({'error': 'file_id and instructions are required'}), 400
        if file_id not in file_storage:
            return jsonify({'error': 'Invalid file_id'}), 400
        # Store instructions
        instruction_id = str(uuid.uuid4())
        instruction_storage[instruction_id] = {
            'file_id': file_id,
            'instructions': instructions
        }
        return jsonify({'instruction_id': instruction_id}), 200
    
    
    @app.route('/draft/<instruction_id>', methods=['GET'])
    def generate_draft(instruction_id):
        """
        Generate and return the annotated DOCX file.
        """
        if instruction_id not in instruction_storage:
            return jsonify({'error': 'Invalid instruction_id'}), 400
        instruction_data = instruction_storage[instruction_id]
        file_id = instruction_data['file_id']
        instructions = instruction_data['instructions']
        original_file = file_storage[file_id]
        
        # Generate output file path
        filename = os.path.basename(original_file)
        annotated_filename = f"annotated_{filename}"
        output_path = os.path.join(ANNOTATED_FOLDER, annotated_filename)
        
        # Add comments based on instructions
        add_comment_to_document(original_file, instructions, output_path)
        
        # Return the annotated file
        return send_file(output_path, as_attachment=True, attachment_filename=annotated_filename)
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    Detailed Explanation

    1. Endpoints:

      • /upload (POST):

        • Purpose: Upload a DOCX file to the server.
        • Request: Form data with a key file containing the DOCX file.
        • Response: JSON containing a unique file_id to reference the uploaded file.
        • Usage:
          curl -F 'file=@your_document.docx' http://localhost:5000/upload
          
      • /instructions (POST):

        • Purpose: Submit instructions for adding comments to the uploaded DOCX file.
        • Request: JSON payload containing file_id and a list of instructions.
        • Instruction Format:
          {
              "file_id": "uploaded_file_id",
              "instructions": [
                  {
                      "search_text": "Specific text in the document",
                      "comment": "Your comment here"
                  },
                  ...
              ]
          }
          
        • Response: JSON containing a unique instruction_id to reference the submitted instructions.
        • Usage:
          curl -H "Content-Type: application/json" -d '{
              "file_id": "your_file_id",
              "instructions": [
                  {
                      "search_text": "Introduction",
                      "comment": "This section needs more detail."
                  },
                  {
                      "search_text": "Conclusion",
                      "comment": "Great summary!"
                  }
              ]
          }' http://localhost:5000/instructions
          
      • /draft/<instruction_id> (GET):

        • Purpose: Generate the annotated DOCX file based on the provided instructions and return it as a downloadable file.
        • Response: The annotated DOCX file with comments.
        • Usage:
          curl -O http://localhost:5000/draft/your_instruction_id
          
    2. Adding Comments to DOCX:

      The add_comment_to_document function handles the addition of comments based on the instructions provided. Here's how it works:

      • Load the DOCX File: Uses python-docx to load and manipulate the DOCX file.

      • Access and Initialize Comments Part: DOCX files have a separate XML part for comments. The function checks if the document already has a comments part. If not, it creates one.

      • Iterate Through Instructions: For each instruction, it searches for the specified search_text within the paragraphs of the document.

      • Add Comment Reference: When search_text is found, it splits the run (a contiguous region of text with the same style) to isolate the search_text and appends a comment reference.

      • Create Comment Element: A new comment is created in the comments part with the provided comment text.

      • Save the Annotated Document: The modified DOCX is saved to the specified output path.

      Note: This implementation is a simplified example. In complex documents, handling multiple runs and ensuring accurate placement of comments might require more sophisticated parsing.

    3. Storage Considerations:

      • In-Memory Storage: This example uses Python dictionaries (file_storage and instruction_storage) to temporarily store file paths and instructions. This approach is suitable for demonstration purposes but not for production.

      • Persistent Storage: For a production environment, consider using a database (like PostgreSQL, MongoDB) and a file storage solution (like AWS S3) to store files and instructions persistently.

    4. Security Considerations:

      • File Validation: The /upload endpoint validates that only .docx files are accepted. Additional validations can be added as needed.

      • Rate Limiting & Authentication: To secure the APIs, implement authentication (e.g., JWT) and rate limiting to prevent abuse.

      • Input Sanitization: Ensure that the search_text and comment fields are sanitized to prevent injection attacks or malformed comments.

    5. Enhancements:

      • Advanced Comment Placement: Enhance the add_comment_to_document function to handle complex scenarios, such as multiple occurrences of search_text, comments on specific lines, or different sections.

      • UI Interface: Develop a frontend interface to interact with these APIs, allowing users to upload files, input instructions, and download the annotated documents seamlessly.

      • Error Handling: Improve error handling to cover more edge cases and provide more descriptive error messages.

    Testing the APIs

    Here's how you can test the APIs using curl:

    1. Upload a DOCX File:

      curl -F 'file=@example.docx' http://localhost:5000/upload
      

      Response:

      {
          "file_id": "123e4567-e89b-12d3-a456-426614174000"
      }
      
    2. Submit Instructions:

      curl -H "Content-Type: application/json" -d '{
          "file_id": "123e4567-e89b-12d3-a456-426614174000",
          "instructions": [
              {
                  "search_text": "Introduction",
                  "comment": "Please elaborate on this section."
              },
              {
                  "search_text": "Conclusion",
                  "comment": "Well summarized!"
              }
          ]
      }' http://localhost:5000/instructions
      

      Response:

      {
          "instruction_id": "789e4567-e89b-12d3-a456-426614174001"
      }
      
    3. Generate and Download Annotated DOCX:

      curl -O http://localhost:5000/draft/789e4567-e89b-12d3-a456-426614174001
      

      This command downloads the annotated_example.docx file with comments added as per the instructions.

    Conclusion

    This Flask application provides a foundational structure to upload DOCX files, submit instructions for adding comments, and generate annotated documents. Depending on your specific use case, you might need to enhance the comment placement logic, storage mechanisms, and security features. Additionally, integrating a frontend interface can provide a more user-friendly experience.