import json, time, urllib.request, sys URL = "http://localhost:11434/api/chat" MODEL = sys.argv[1] if len(sys.argv) > 1 else "hymt2-1.8b" FRASES = [ "Bom dia a todos, vamos começar a live de hoje.", "A gente vai falar sobre inteligência artificial e soberania de dados.", "Se vocês estiverem me ouvindo, mandem um oi no chat.", "O projeto roda no meu computador, sem depender de nenhuma empresa grande.", "Isso não funcionou da outra vez, então eu resolvi criar o meu próprio sistema.", "Pessoal, o link da pesquisa está na descrição, não esqueçam de responder.", ] def traduz(t): body = {"model": MODEL, "stream": False, "keep_alive": "30m", "messages": [{"role": "user", "content": f"Translate the following segment into Spanish, without additional explanation.\n\n{t}"}]} r = urllib.request.Request(URL, json.dumps(body).encode(), {"Content-Type": "application/json"}) t0 = time.perf_counter() out = json.load(urllib.request.urlopen(r, timeout=120))["message"]["content"].strip() return out, time.perf_counter() - t0 for i, f in enumerate(FRASES): out, dt = traduz(f) print(f"[{dt:5.2f}s]{' (frio)' if i == 0 else ''} {f}\n -> {out}")