"""Cancel in Android after real Whisper weights finish while Silero is delayed.
Usage: silero-cancel.py SESSION OUTPUT WEIGHTS_VERIFIED_FILE PROXY_LOG
Start download-proxy.py first; move Silero out of the isolated server's store
while it is stopped, then launch that server with Node's trusted HTTPS proxy.
Open Transcription models and search for Whisper Tiny English before running.
"""
import datetime,subprocess,sys,time
from pathlib import Path
session,output,weights,proxylog=sys.argv[1:]
run=Path(output)/datetime.datetime.now(datetime.UTC).strftime('%Y%m%dT%H%M%SZ');run.mkdir(parents=True)
def device(*args):
 r=subprocess.run(['agent-device',*args,'--session',session],capture_output=True,text=True)
 if r.returncode: raise RuntimeError(r.stdout+r.stderr)
 return r.stdout
print(run,flush=True)
device('record','start',str(run/'cancellation.mp4'),'--hide-touches')
device('press','role=button label="Whisper Tiny English"','--settle')
device('press','text="Download"','--settle')
for _ in range(300):
 if Path(weights).exists() and 'SILERO_WAITING' in Path(proxylog).read_text():break
 time.sleep(.2)
else:raise RuntimeError('Real weights did not finish with Silero held')
print('Real weights verified; Silero upstream request held',flush=True)
print(device('snapshot','-i'),flush=True)
device('screenshot',str(run/'pending.png'))
device('press','role=button label="Whisper Tiny English"','--settle')
device('wait','absent','text="Tap to cancel"')
device('screenshot',str(run/'models.png'))
device('record','stop')
device('press','label="Close transcription models"','--settle')
device('screenshot',str(run/'voice-a.png'));time.sleep(.5)
device('screenshot',str(run/'voice-b.png'))
print(device('snapshot','-i'),flush=True)
