Lesson 166 — Advanced

Voice or Multimodal Build

Voice aur vision dono mein ek hi asli challenge hai: latency budget. Is lesson mein do paths hain — realtime voice agent (sunna → samajhna → bolna) aur vision pipeline (image → description/decision) — dono ka architecture aur trade-offs.

⏱ 30 min• Advanced• Monitored Production API

Project kya hai

WHAT

Path A: ek voice assistant jo ruk kar bole, beech mein interrupt ho sake, seconds mein reply de. Path B: image upload par analysis pipeline (description, extraction, decision) — dono mein streaming + budget management.

WHEN

Voice: hands-free help, IVR replacement, accessibility. Vision: invoices/photos scan karna, visual QA, catalogue tagging. Ek time par EK path chuno — dono ek project mein alag-alag honge.

WHERE

Browser (WebRTC/MediaRecorder) ya mobile se audio; vision = upload endpoint. STT/LLM/TTS APIs + ek thin Python/JS backend.

Project ka demo point hai feel: voice agent kitna responsive lagta hai, vision pipeline kitna reliable. Do numbers hain jinse judge honge — turn latency (kitne seconds mein bolna shuru) aur task success rate.

Scope rule: v1 = ek wake/ek session, ek language, single-turn clarity. Multi-party conversation, noise robustness, ek bade vision use-case baad mein.

Voice loop — sunna, bolna, rukna

Loop ke steps: VAD (bolna shuru/band) → STT streaming → LLM streaming → TTS streaming. Chaaron streaming honi chahiye — batch pipeline realtime nahi lagti.

async def voice_loop(mic, tts, stt, llm):
    speaking = False
    async for audio in mic.stream():          # chhote frames
        if vad.ends_utterance(audio):         # user ne bolna band kiya
            text = await stt.finalize()
            if not text:
                continue
            if speaking:
                tts.stop()                     # barge-in: user bole toh interrupt
            first_chunk = False
            async for token in llm.stream(text):
                if not first_chunk:            # pehla word turant bolo,
                    tts.start(token)           # poora response mat wait karo
                    first_chunk = True
                else:
                    tts.feed(token)
            speaking = True
Barge-in zaroori: User beech mein bole toh TTS turant roko — warna agent bolta rahega aur user feel karega "yeh sun nahi raha". Demo mein yahi cheez "smart" feel karati hai.

Vision path — image kaise aati hai

Image ko model tak pahunchana simple hai (upload → resize → encode → request), asli decision hai: OCR/VLM kab:

import base64

def image_part(path, max_side=1024):
    img = Image.open(path)
    img.thumbnail((max_side, max_side))          # budget control
    buf = io.BytesIO(); img.save(buf, format="JPEG", quality=85)
    b64 = base64.b64encode(buf.getvalue()).decode()
    return {"type": "input_image", "image_url": f"data:image/jpeg;base64,{b64}"}

resp = client.responses.create(
    model=VLM,
    input=[{"role": "user", "content": [
        {"type": "input_text", "text": "Is receipt ka total aur vendor batao."},
        image_part("receipt.jpg"),
    ]},
],)
PII check pehle: Receipts/photos mein personal data hota hai — blur/redaction pre-check karo aur README mein retention policy likho (L8.6). Images kabhi third-party bhejne se pehle disclosure do.

Latency budget

Total turn latency = VAD lag + STT finalize + LLM first token + TTS first audio. Ek example budget banao (apne numbers, apne targets) aur har stage par time measure karo:

Measure, guess nahi: Har stage ka timestamp span mein log karo (L14.5 tracing isi liye tha) — budget ke against actual numbers dekho.

Testing — devices aur networks

Demo honesty: README mein latency numbers daalo jo tumhe apne test se mile hain (p50/p95) — "real-time" ka matlab define karo, assume mat karo.

Exercise

Question: Mic se text banane wala step — STT ka full form? (space se, English)

Question: Speech mein bolna/rukna detect karne wali technique — voice activity ___? (ek shabd)

Question: Image ko text-format mein bhejne ka common encoding? (ek shabd)

Common mistakes

Desi example: Receptionist vs teleprompter

Achha receptionist sunta hai, beech mein bol sakta hai aur turant react karta hai — wahi voice agent ka standard hai. Poori line padh kar bolne wala teleprompter wala agent boring lagta hai:

text
while True:
    audio = mic.read()                 # sun rahe ho
    if vad.ends_utterance(audio):      # user ne bolna band kiya
        if speaking: tts.stop()        # barge-in
        reply = llm.stream(stt(audio)) # turant stream
        tts.start_first(reply)         # pehla word bolo, poora wait nahi
Interview angle — yahan se sawaal aate hain
  • Q: Ek voice agent ka architecture batao. (VAD -> streaming STT -> LLM streaming -> TTS + barge-in handling)
  • Q: Latency budget kaise todoge? (stage-wise spans, STT finalize, small model, prompt cache, sentence-level TTS)
  • Q: OCR aur VLM mein kab kya chunoge? (extraction vs understanding, cost/latency trade-off)
  • Q: Multimodal system mein safety kaise? (PII pre-check, size caps, disclosure, content policy on inputs — L8.6)
Lesson complete?

Ek path chuno (voice ya vision), budget banao aur stage-wise measure karo. Demo mein barge-in/latency numbers hi tumhara strong point honge.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course