feat: Ollama und Modell vor dem Start prüfen, kurze Meldung statt Traceback

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Dieter Schlüter 2026-10-11 19:20:46 +02:00
commit 6aa4887f22

View file

@ -8,6 +8,7 @@ import re
import shutil
import sys
import time
import urllib.error
import urllib.request
from aenderungen import REVIEW_MARK, print_summary, report_path, write_report
@ -17,6 +18,7 @@ from yt_untertitel import YT_DLP, base_language, default_name, original_language
MODEL = "qwen3:32b"
OLLAMA_URL = "http://127.0.0.1:11434/api/chat"
OLLAMA_TAGS_URL = "http://127.0.0.1:11434/api/tags"
CHUNK_SIZE = 1200 # Zielgröße eines Abschnitts in Wörtern
MAX_EXTRA_WORDS = 300 # so weit darf ein Abschnitt bis zum nächsten Satzende wachsen
@ -237,6 +239,31 @@ def chat_request(payload):
)
def ollama_open(request, timeout=600):
"""urlopen() for Ollama; a missing server ends the program with a short message."""
try:
return urllib.request.urlopen(request, timeout=timeout)
except urllib.error.HTTPError:
raise
except urllib.error.URLError as error:
raise SystemExit(
f"Ollama nicht erreichbar ({error.reason}). Läuft der Dienst? "
"Starten mit: sudo systemctl start ollama (oder: ollama serve)"
) from None
def check_ollama(model):
"""Stop early, before any download, if Ollama is down or lacks the model."""
with ollama_open(OLLAMA_TAGS_URL, timeout=10) as response:
installed = {m.get("name") for m in json.load(response).get("models", [])}
wanted = model if ":" in model else f"{model}:latest"
if wanted not in installed:
raise SystemExit(
f"Modell „{model}“ ist in Ollama nicht installiert. "
f"Installieren mit: ollama pull {model}"
)
def correct_chunk(chunk, index, system):
payload = {
"model": MODEL,
@ -263,7 +290,7 @@ def correct_chunk(chunk, index, system):
words = 0
result = {}
with urllib.request.urlopen(request, timeout=600) as response:
with ollama_open(request) as response:
for line in response:
if not line.strip():
continue
@ -328,7 +355,7 @@ def find_paragraph_starts(sentences, first, system):
"num_predict": 2048,
},
}
with urllib.request.urlopen(chat_request(payload), timeout=600) as response:
with ollama_open(chat_request(payload)) as response:
result = json.load(response)
content = result["message"]["content"]
@ -542,6 +569,7 @@ def main():
# reuses cached parts of another
MODEL = args.model
print(f"Modell: {MODEL}")
check_ollama(MODEL)
raw, target, lang = read_source(args)
print(f"Sprache der Rede: {lang}")
parts_dir = target.with_suffix(".parts")