"""Opt-in applied ASR policy; provider routing remains in the engine gateway.""" import base64 from email.parser import BytesParser from email.policy import default import json import httpx from app.modules.voice.local_s2s import RESPONSIVE_ASR_PROMPT def install_stt_policy(module): native=module.OpenAICompatibleSTTHandler.setup def setup(self,*args,**kwargs): kwargs['gen_kwargs']={**kwargs.get('gen_kwargs',{}),'temperature':0,'prompt':RESPONSIVE_ASR_PROMPT} return native(self,*args,**kwargs) module.OpenAICompatibleSTTHandler.setup=setup def transcription_request(request): """Map pinned multipart to documented JSON provider.options; never change audio.""" if not request.headers.get('content-type','').startswith('multipart/form-data;'): raise ValueError('LOCAL_ASR_MULTIPART_REQUIRED') message=BytesParser(policy=default).parsebytes(('Content-Type: '+request.headers['content-type']+'\r\nMIME-Version: 1.0\r\n\r\n').encode()+request.content) fields={} for part in message.iter_parts(): name=part.get_param('name',header='content-disposition') if name in fields:raise ValueError('LOCAL_ASR_DUPLICATE_FIELD') fields[name]=part.get_payload(decode=True) if (set(fields)!={'file','model','language','response_format','temperature','prompt'} or fields['language']!=b'fa' or fields['response_format']!=b'json' or fields['temperature'] not in (b'0',b'0.0') or fields['prompt'].decode()!=RESPONSIVE_ASR_PROMPT): raise ValueError('LOCAL_ASR_PROFILE_MISMATCH') data=dict(model=fields['model'].decode(),language='fa',response_format='json',temperature=0, input_audio=dict(data=base64.b64encode(fields['file']).decode('ascii'),format='wav'), provider=dict(options={'groq':{'prompt':RESPONSIVE_ASR_PROMPT}})) return httpx.Request(request.method,request.url, headers={k:v for k,v in request.headers.items() if k not in ('content-type','content-length')},json=data)