#!/usr/bin/env python3
import requests
import json
import sys
from datetime import datetime


def log_status(message):
    """Print status messages to stderr with timestamp"""
    timestamp = datetime.now().strftime("%H:%M:%S")
    print(f"[{timestamp}] {message}", file=sys.stderr)

def convert_to_markdown(text):
    log_status("Starting markdown conversion...")

    url = "http://localhost:11434/api/generate"
    headers = {"Content-Type": "application/json"}
    data = {
        # "model": "llama3.2",
        # "model": "gemma",
        # "model": "llama3.3",
        # "model": "deepseek-coder-v2",
        # "model": "dolphin-mixtral",
        "model": "hf.co/sm54/FuseO1-DeepSeekR1-QwQ-SkyT1-Flash-32B-Preview-Q4_K_M-GGUF",
        # "model": "llama3.1:8b",
        # "model": "command-r",
        # "model": "granite3.1-dense",
        "system": "Write large outputs and convert all text to markdown in their entirity, dont leave out anything and absolutely do not summarize. Do not ask questions about the contents. Do processing only. You only take input and convert it to markdown. Proceed with processing without asking any questions. Convert input to markdown. Do not converse with the user. Only output english. Use markdown syntax and tables.",
        "prompt": f"Convert the following text to markdown format. Convert all the text and dont leave out any information for any reason! I NEED ALL THE TEXT CONVERTED! Only return the markdown syntax and keep all tables! Here is the file to process:\n\n```{text}```",

        "stream": False,
    }

    try:
        log_status("Sending request to Ollama server...")
        response = requests.post(url, headers=headers, data=json.dumps(data))
        response.raise_for_status()
        log_status("Successfully received response from server")
        return json.loads(response.text)["response"]
    except requests.exceptions.RequestException as e:
        log_status(f"Error: Failed to communicate with Ollama server: {e}")
        sys.exit(1)
    except json.JSONDecodeError as e:
        log_status(f"Error: Failed to parse JSON response: {e}")
        sys.exit(1)


def main():
    if len(sys.argv) != 2:
        log_status("Error: Incorrect number of arguments")
        print("Usage: ./script.py file.txt", file=sys.stderr)
        sys.exit(1)

    input_file = sys.argv[1]
    log_status(f"Processing file: {input_file}")

    try:
        with open(input_file, "r") as f:
            content = f.read()

        markdown_content = convert_to_markdown(content)
        print(markdown_content)  # This will go to stdout/file
        log_status("Conversion completed successfully")

    except IOError as e:
        log_status(f"Error: Failed to read file {input_file}: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()

Generated by Getz using scpaste at Wed Feb 5 23:51:55 2025. CET. (original)