import json import requests import random ARCHETYPE_DESC = ( "An abandoned clause, false start, or spoken restart. The speaker begins saying one thought or syntactic structure, " "cuts themselves off, and starts their sentence over. Patterns include aborted clauses, interrupted questions, " "abandoned conditionals, or sentence-initial/mid-sentence restarts. MUST be linguistically varied and natural." ) DIVERSE_FS_EXAMPLES = [ ("Let's deploy the update tonight.", "If we could— but actually Let's deploy the update tonight.", ["If we could— but actually "]), ("We need to upgrade the database version.", "Regarding our plan to— well actually We need to upgrade the database version.", ["Regarding our plan to— well actually "]), ("The transaction failed due to network timeout.", "When they submitted the— so what happened was The transaction failed due to network timeout.", ["When they submitted the— so what happened was "]), ("You can find the documentation on the intranet.", "Where did we put— okay You can find the documentation on the intranet.", ["Where did we put— okay "]), ("The report will be ready by Thursday morning.", "As far as completing the— well The report will be ready by Thursday morning.", ["As far as completing the— well "]), ("We should consider alternative vendors.", "It might be better to— or rather We should consider alternative vendors.", ["It might be better to— or rather "]), ("Please send the tracking number to the customer.", "Could you check if— actually Please send the tracking number to the customer.", ["Could you check if— actually "]), ("Our team finished the sprint ahead of schedule.", "In terms of progress on the— so Our team finished the sprint ahead of schedule.", ["In terms of progress on the— so "]), ("There are several flights available tomorrow.", "Looking at the morning— well There are several flights available tomorrow.", ["Looking at the morning— well "]), ("The conference was postponed until next month.", "Did they announce when— anyway The conference was postponed until next month.", ["Did they announce when— anyway "]), ] PROMPT_TEMPLATE = """ Corrupt the input clean sentence into ONE natural spoken-disfluency example using the requested SPEECH ARCHETYPE: Archetype: FALSE_START Archetype Description: {archetype_desc} CRITICAL RULES: 1. Every word, character, and casing outside the disfluent span MUST remain 100% VERBATIM IDENTICAL to the input sentence. - If the input starts with a capitalized word like 'The', keep 'The' capitalized: 'When they started— The...'. Do NOT lowercase it. 2. Excising the disfluent span(s) MUST recreate the input clean sentence exactly. Do NOT delete or alter any clean words or punctuation. 3. Return ONLY two fields: and . 4. MUST contain the exact verbatim substrings to excise, matching the exact casing in . 5. FORBIDDEN PATTERNS: - Do NOT use generic conversational filler preambles like 'I mean', 'I was wondering', 'I think', 'Basically', or 'So I mean'. - A false start MUST contain an aborted thought, interrupted clause, or abandoned sentence fragment (e.g. 'If we could—', 'When they—', 'Regarding our—', 'Could you check—'). 6. DO NOT delete, shorten, or excise valid sentence-initial subject pronouns ('I', 'We', 'It', 'You', 'They'). 7. VARY your false starts widely! Adapt the aborted thought to the specific topic/context of the sentence. {examples_block} Input: {sentence} Respond with JSON only: {{"corrupted_text": "...", "disfluent_spans": ["..."]}} """ test_sentences = [ "The project deadline has been moved to next Friday.", "You should restart the web application after modifying the config.", "We noticed an unusual spike in memory consumption on node three.", "Customers reported issues when checking out with international cards.", "The algorithm converges much faster with a smaller learning rate." ] rng = random.Random(42) for sent in test_sentences: sampled_ex = rng.sample(DIVERSE_FS_EXAMPLES, 3) ex_lines = [] for clean_in, corrupted_out, spans in sampled_ex: ex_lines.append(f"Input: {clean_in} Response: " + json.dumps({ "corrupted_text": corrupted_out, "disfluent_spans": spans }, indent=2)) examples_block = " ".join(ex_lines) prompt = PROMPT_TEMPLATE.format( archetype_desc=ARCHETYPE_DESC, examples_block=examples_block, sentence=sent ) payload = { "model": "gemma4:12b", "messages": [{"role": "user", "content": prompt}], "stream": False, "think": False, "format": "json", "options": {"temperature": 0.70, "top_p": 0.95, "num_predict": 256} } r = requests.post("http://127.0.0.1:11434/api/chat", json=payload, timeout=25) res = r.json() content = res.get("message", {}).get("content", "") try: parsed = json.loads(content) raw = parsed.get("corrupted_text", "") spans = parsed.get("disfluent_spans", []) print(f"CLEAN: {sent}") print(f"RAW: {raw}") print(f"SPANS: {spans}") test_clean = raw for s in spans: test_clean = test_clean.replace(s, "") print(f"ROUNDTRIP OK: {test_clean.strip() == sent.strip()}") print("-" * 60) except Exception as e: print(f"Error: {e}")