"""Read an ASR/TTS request from stdin and print the checked QVikhr result."""
import json
import sys
from urllib.request import Request, urlopen
from editor import finish, model_input
from prompts import split

def edit(text):
    if not split(text)[3]:
        return ""
    prompt, content = model_input(text)
    body = {
        "model": "qvikhr-3:4b-q4_k_m",
        "stream": False,
        "messages": [{"role": "system", "content": prompt}, {"role": "user", "content": content}],
        "options": {"seed": 42, "temperature": 0, "num_predict": 160},
    }
    req = Request("http://ollama.h100.local/api/chat", json.dumps(body).encode(),
                  {"Content-Type": "application/json"})
    with urlopen(req, timeout=30) as response:
        candidate = json.load(response)["message"]["content"].strip()
    return finish(text, candidate)[0]

if __name__ == "__main__":
    print(edit(sys.stdin.read()))
