#!/usr/bin/env python3 """Mock sr-voice stdio mode for pipeline testing (D-138). Reads JSONL from stdin, writes JSONL to stdout. Simulates inference by uppercasing the base text portion of the prompt as the "voiced" output. Usage: echo '{"prompt":"Re-voice this.","seed":42}' | ./mock-stdio.sh serve --stdio """ import json import sys print("mock-sr-voice: stdio mode (no model loaded)", file=sys.stderr, flush=True) # Use readline() loop — Python's `for line in sys.stdin` has an internal # read-ahead buffer that blocks on piped stdin until 8KB is available. while True: line = sys.stdin.readline() if not line: break line = line.strip() if not line: continue try: req = json.loads(line) except json.JSONDecodeError as e: print(json.dumps({"error": f"invalid JSON: {e}"}), flush=True) continue prompt = req.get("prompt", "") if not prompt: print(json.dumps({"error": "missing prompt field"}), flush=True) continue # Extract the INPUT line from the prompt (last INPUT: before OUTPUT:) input_text = "" for pline in prompt.split("\n"): if pline.startswith("INPUT: "): input_text = pline[7:] if not input_text: input_text = prompt[:80] voiced = f"[VOICED] {input_text.upper()}" result = { "text": voiced, "tokens_generated": len(voiced.split()), "generation_time_ms": 50, "tokens_per_sec": 240.0, "prefill_time_ms": 10, } print(json.dumps(result), flush=True) print("mock-sr-voice: stdin closed, exiting", file=sys.stderr, flush=True)