import sys import requests import os OLLAMA_URL = "http://localhost:11434/api/generate" MODEL = "zongwei/gemma3-translator:4b" PROMPT = """Convert the following plain text into well-formatted Markdown. Do not change or omit any content. Only add Markdown structure (such as headings, lists, code blocks, tables, quotes, links, bold, italics, etc.) where appropriate, based on the meaning and structure of the original text. Keep the content itself unaltered and do not translate, summarize or rephrase. Only use your Markdown-formatting skills. Text: \"\"\"{text}\"\"\" Markdown:""" def get_output_path(inputfile): folder = os.path.dirname(inputfile) basename = os.path.splitext(os.path.basename(inputfile))[0] outputfile = os.path.join(folder, f"{basename}_markdown.md") counter = 1 while os.path.exists(outputfile): outputfile = os.path.join(folder, f"{basename}_markdown-{counter}.md") counter += 1 return outputfile def main(): if len(sys.argv) < 2: print(f"Usage: {os.path.basename(sys.argv[0])} ") sys.exit(1) inputfile = sys.argv[1] outputfile = get_output_path(inputfile) with open(inputfile, "r", encoding="utf-8") as f: text = f.read() payload = { "model": MODEL, "prompt": PROMPT.format(text=text), "stream": False } r = requests.post(OLLAMA_URL, json=payload) r.raise_for_status() markdown = r.json()["response"] with open(outputfile, "w", encoding="utf-8") as f: f.write(markdown) print(f"Markdown written to {outputfile}") if __name__ == "__main__": main()