I'm doing the followong task frequently and i want to automate it :
I'm working on Unbuntu 24, i have to convert .odt documents written using libreoffice to markdown using pandoc. Then i use a local running ministral model to translate the document. then the translated document is converted back to .odt file using pandoc.
------
what is the best approach to automate this task so that i give the file and get the final result.
May 29, 7:53am

To automate your workflow on Ubuntu 24—converting .odt to Markdown, translating with a local Mistral model, and converting back to .odt—you can create a Bash script or a Python script. Both approaches are efficient, but a Bash script is the simplest for this linear, file-based task.

Here’s how you can do it:


Option 1: Bash Script (Recommended for Simplicity)

Create a script that chains the commands together. This is ideal for a one-off or repetitive task.

Steps:

  1. Install dependencies (if not already installed):

    bash
    sudo apt update
    sudo apt install pandoc libreoffice

    Ensure your local Mistral model is accessible via a CLI tool (e.g., curl to a local API or a Python script).

  2. Create the script (e.g., convert_translate.sh):

    bash
    #!/bin/bash
    
    # Check if input file is provided
    if [ "$#" -ne 1 ]; then
        echo "Usage: $0 <input.odt>"
        exit 1
    fi
    
    INPUT_FILE="$1"
    BASENAME=$(basename "$INPUT_FILE" .odt)
    OUTPUT_MD="${BASENAME}.md"
    OUTPUT_TRANSLATED_MD="${BASENAME}_translated.md"
    OUTPUT_ODT="${BASENAME}_translated.odt"
    
    # Step 1: Convert ODT to Markdown
    echo "Converting $INPUT_FILE to Markdown..."
    pandoc "$INPUT_FILE" -o "$OUTPUT_MD" || { echo "Pandoc conversion failed"; exit 1; }
    
    # Step 2: Translate Markdown using local Mistral model
    echo "Translating $OUTPUT_MD..."
    # Replace this with your actual Mistral CLI command.
    # Example: If using a local API, you might do:
    # curl -X POST http://localhost:8000/translate -H "Content-Type: text/plain" --data-binary "@$OUTPUT_MD" > "$OUTPUT_TRANSLATED_MD"
    # Or use a Python script if Mistral is accessed via Python.
    # For now, assume a placeholder command:
    mistral-cli translate --input "$OUTPUT_MD" --output "$OUTPUT_TRANSLATED_MD" || { echo "Translation failed"; exit 1; }
    
    # Step 3: Convert translated Markdown back to ODT
    echo "Converting $OUTPUT_TRANSLATED_MD to ODT..."
    pandoc "$OUTPUT_TRANSLATED_MD" -o "$OUTPUT_ODT" || { echo "Pandoc conversion failed"; exit 1; }
    
    echo "Done! Output: $OUTPUT_ODT"
  3. Make the script executable:

    bash
    chmod +x convert_translate.sh
  4. Run the script:

    bash
    ./convert_translate.sh your_document.odt

Option 2: Python Script (More Flexible)

If you need more control (e.g., error handling, logging, or batch processing), use Python.

Steps:

  1. Install Python dependencies:

    bash
    pip install pypandoc requests
  2. Create the script (e.g., convert_translate.py):

    python
    import os
    import subprocess
    import requests
    
    def convert_odt_to_md(input_file, output_md):
        try:
            subprocess.run(["pandoc", input_file, "-o", output_md], check=True)
        except subprocess.CalledProcessError as e:
            print(f"Pandoc conversion failed: {e}")
            exit(1)
    
    def translate_md(input_md, output_translated_md):
        # Replace with your actual Mistral API call.
        # Example: Send the file content to a local Mistral API.
        with open(input_md, "r") as f:
            text = f.read()
        # Example: Assuming Mistral is running locally on port 8000
        response = requests.post(
            "http://localhost:8000/translate",
            headers={"Content-Type": "text/plain"},
            data=text
        )
        if response.status_code != 200:
            print("Translation failed")
            exit(1)
        with open(output_translated_md, "w") as f:
            f.write(response.text)
    
    def convert_md_to_odt(input_md, output_odt):
        try:
            subprocess.run(["pandoc", input_md, "-o", output_odt], check=True)
        except subprocess.CalledProcessError as e:
            print(f"Pandoc conversion failed: {e}")
            exit(1)
    
    if __name__ == "__main__":
        import sys
        if len(sys.argv) != 2:
            print("Usage: python convert_translate.py <input.odt>")
            exit(1)
    
        input_file = sys.argv[1]
        base = os.path.splitext(input_file)[0]
        md_file = f"{base}.md"
        translated_md_file = f"{base}_translated.md"
        output_odt = f"{base}_translated.odt"
    
        convert_odt_to_md(input_file, md_file)
        translate_md(md_file, translated_md_file)
        convert_md_to_odt(translated_md_file, output_odt)
        print(f"Done! Output: {output_odt}")
  3. Run the script:

    bash
    python3 convert_translate.py your_document.odt

Option 3: Makefile (For Batch Processing)

If you have many files, a Makefile can help automate the process for all .odt files in a directory.

Example Makefile:

makefile
all: convert translate reconvert

convert:
	pandoc *.odt -o %.md

translate:
	# Add your Mistral translation command here for each .md file
	mistral-cli translate --input %.md --output %_translated.md

reconvert:
	pandoc *_translated.md -o %_translated.odt

Run with:

bash
make

How to Handle Mistral Translation

  • If your Mistral model is running locally (e.g., via llama.cpp or a FastAPI server), replace the translation step with the actual command or API call.
  • For example, if you’re using curl to send text to a local Mistral API:
    bash
    curl -X POST http://localhost:8000/translate -H "Content-Type: text/plain" --data-binary "@input.md" > output_translated.md

Which Approach to Choose?

Bash
Quick, one-off tasks
Low
Low
Python
More control, error handling
Medium
High
Makefile
Batch processing
Medium
Medium

Next Steps

  • If you already have a CLI command for Mistral, replace the placeholder in the Bash or Python script.
  • Test with one file first, then scale up.

Would you like me to adapt one of these scripts to your exact Mistral CLI/API setup? Or do you need help setting up the Mistral model for local translation?

Memory updated

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more