commit a0da2689444a5f76416162af4d8b1f6bb40526ae Author: Victor Giers Date: Sat Jun 14 14:06:26 2025 +0200 Initial commit (generated by auto-git) diff --git a/README.md b/README.md new file mode 100644 index 0000000..2f9fdd9 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Projekt in text-to-markdown diff --git a/text-to-markdown.py b/text-to-markdown.py new file mode 100644 index 0000000..01248f9 --- /dev/null +++ b/text-to-markdown.py @@ -0,0 +1,53 @@ +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() \ No newline at end of file