import json, time, requests from pathlib import Path from concurrent.futures import ThreadPoolExecutor, as_completed BASE = Path("/opt/3cx-middleware/3cx-telefonie-middleware") SRC = BASE / "data/transcripts" SCREEN = BASE / "data/transcript_screening.jsonl" OUT = BASE / "data/transcript_relevance_final.jsonl" URL = "http://192.168.1.101:11434/api/chat" MODEL = "llama3.2:3b" WORKERS = 1 SYSTEM = """Du klassifizierst ein Telefontranskript ausschließlich nach Relevanz. RELEVANT: Ein echter Dialog mit Kunde, Interessent, Lieferant oder Geschäftspartner. IRRELEVANT: reine Telefonansage, Mailbox, Warteschleife, Fehlanruf ohne Gespräch, Werbung oder praktisch kein Gespräch. UNCERTAIN: nicht eindeutig. Antworte ausschließlich mit JSON: {"relevance":"RELEVANT","confidence":0.0} oder {"relevance":"IRRELEVANT","confidence":0.0} oder {"relevance":"UNCERTAIN","confidence":0.0} """ items = [] for line in SCREEN.read_text(encoding="utf-8").splitlines(): r = json.loads(line) if r["relevance"] == "UNCERTAIN": items.append(r["file"]) print(f"UNCERTAIN: {len(items)}") def run(filename): text = (SRC / filename).read_text( encoding="utf-8", errors="replace" ).strip() payload = { "model": MODEL, "messages": [ {"role": "system", "content": SYSTEM}, {"role": "user", "content": text[:5000]} ], "stream": False, "think": False, "format": "json", "options": {"temperature": 0} } start = time.time() r = requests.post(URL, json=payload, timeout=120) r.raise_for_status() result = json.loads(r.json()["message"]["content"]) return { "file": filename, "relevance": result.get("relevance", "UNCERTAIN"), "confidence": result.get("confidence", 0), "seconds": round(time.time() - start, 1) } with OUT.open("w", encoding="utf-8") as f: with ThreadPoolExecutor(max_workers=WORKERS) as pool: futures = [pool.submit(run, x) for x in items] for i, future in enumerate(as_completed(futures), 1): try: r = future.result() f.write(json.dumps(r, ensure_ascii=False) + "\n") f.flush() print( f"[{i}/{len(items)}] {r['file']} " f"{r['seconds']}s → {r['relevance']} " f"{r['confidence']}", flush=True ) except Exception as e: print("ERROR:", e, flush=True) print("\nFertig:", OUT)