|
| 1 | +"""AI interviewee — a simulated candidate for both-AI mock interviews. |
| 2 | +
|
| 3 | +It **listens** to the interviewer through speech-to-text (faster-whisper) and |
| 4 | +answers with **Gemini conditioned on a CV**. The "no backdoor" guarantee is |
| 5 | +structural: the candidate only ever receives the interviewer's *audio* via |
| 6 | +``listen()``, which transcribes it; ``respond()`` takes **no text argument** and |
| 7 | +can only answer what was actually heard (stored in ``self.history``). There is |
| 8 | +no code path by which the interviewer's source text reaches the candidate. |
| 9 | +
|
| 10 | +Auth for Gemini is the project's Application Default Credentials (Vertex AI), |
| 11 | +the same as the live barge-in reply — no API key. STT runs locally on CPU. |
| 12 | +
|
| 13 | +Self-check (proves the STT-listen + CV-conditioning loop end to end): |
| 14 | + python -m app.sim.ai_interviewee |
| 15 | +(run from the backend/ directory with ADC configured). |
| 16 | +""" |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import os |
| 20 | +import tempfile |
| 21 | +from typing import List, Optional, Tuple |
| 22 | + |
| 23 | +# Lazily imported heavy deps (STT model, PDF) so importing this module is cheap. |
| 24 | +_stt_model = None |
| 25 | + |
| 26 | + |
| 27 | +def load_cv(path: str) -> str: |
| 28 | + """Extract plain text from a CV PDF (or read a .txt/.md as-is).""" |
| 29 | + if path.lower().endswith((".txt", ".md")): |
| 30 | + with open(path, "r", encoding="utf-8", errors="replace") as fh: |
| 31 | + return fh.read() |
| 32 | + from pypdf import PdfReader |
| 33 | + |
| 34 | + reader = PdfReader(path) |
| 35 | + return "\n".join((page.extract_text() or "") for page in reader.pages).strip() |
| 36 | + |
| 37 | + |
| 38 | +def _get_stt(model_size: str = "base"): |
| 39 | + """Cached multilingual faster-whisper model (CPU, int8). 'base' handles |
| 40 | + English and Hebrew; the model is downloaded/cached once by huggingface.""" |
| 41 | + global _stt_model |
| 42 | + if _stt_model is None: |
| 43 | + from faster_whisper import WhisperModel |
| 44 | + |
| 45 | + _stt_model = WhisperModel(model_size, device="cpu", compute_type="int8") |
| 46 | + return _stt_model |
| 47 | + |
| 48 | + |
| 49 | +class AIInterviewee: |
| 50 | + """A candidate that hears (STT) and answers (Gemini + CV). |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + cv_text : str the candidate's CV, verbatim (their only factual grounding) |
| 55 | + voice : str TTS voice id used by the harness when the candidate speaks |
| 56 | + (kept distinct from the interviewer's voice) |
| 57 | + gender : str 'male' | 'female' — for the harness's gendered TTS |
| 58 | + name : str display name |
| 59 | + """ |
| 60 | + |
| 61 | + def __init__(self, cv_text: str, voice: str = "am_fenrir", |
| 62 | + gender: str = "male", name: str = "Candidate") -> None: |
| 63 | + self.cv_text = cv_text or "" |
| 64 | + self.voice = voice |
| 65 | + self.gender = gender |
| 66 | + self.name = name |
| 67 | + # (speaker, text) where speaker in {'interviewer', 'candidate'}. The |
| 68 | + # 'interviewer' entries are TRANSCRIPTS of what was heard, never source. |
| 69 | + self.history: List[Tuple[str, str]] = [] |
| 70 | + self._provider = None |
| 71 | + |
| 72 | + # --------------------------------------------------------------- listen |
| 73 | + def listen(self, audio_bytes: bytes, language: str = "en") -> str: |
| 74 | + """Transcribe the interviewer's spoken audio. This is the ONLY way the |
| 75 | + interviewer's words enter the candidate — as heard text, not source.""" |
| 76 | + model = _get_stt() |
| 77 | + suffix = ".mp3" |
| 78 | + tmp = tempfile.NamedTemporaryFile(suffix=suffix, delete=False) |
| 79 | + try: |
| 80 | + tmp.write(audio_bytes) |
| 81 | + tmp.close() |
| 82 | + lang = "he" if language.lower().startswith("he") else "en" |
| 83 | + segments, _info = model.transcribe(tmp.name, language=lang, beam_size=1) |
| 84 | + heard = " ".join(seg.text for seg in segments).strip() |
| 85 | + finally: |
| 86 | + try: |
| 87 | + os.unlink(tmp.name) |
| 88 | + except OSError: |
| 89 | + pass |
| 90 | + self.history.append(("interviewer", heard)) |
| 91 | + return heard |
| 92 | + |
| 93 | + # -------------------------------------------------------------- respond |
| 94 | + def respond(self, language: str = "en") -> str: |
| 95 | + """Answer the last thing the candidate HEARD (from self.history), via |
| 96 | + Gemini conditioned on the CV. Takes no text argument by design — the |
| 97 | + candidate cannot answer anything it did not hear.""" |
| 98 | + if not any(s == "interviewer" for s, _ in self.history): |
| 99 | + raise RuntimeError("respond() called before listen(): nothing heard yet") |
| 100 | + |
| 101 | + system = ( |
| 102 | + "You are a job candidate in a live, spoken technical interview. " |
| 103 | + "Answer in the first person as the candidate — concise, technically " |
| 104 | + "correct, and specific. Ground your experience STRICTLY in the CV " |
| 105 | + "below; never invent employers, degrees, or results not in it. If you " |
| 106 | + "are unsure, reason out loud briefly rather than bluffing.\n\nCV:\n" |
| 107 | + + self.cv_text[:6000] |
| 108 | + ) |
| 109 | + convo = "\n".join( |
| 110 | + ("Interviewer: " if s == "interviewer" else "You: ") + t |
| 111 | + for s, t in self.history[-10:] |
| 112 | + ) |
| 113 | + heard = next(t for s, t in reversed(self.history) if s == "interviewer") |
| 114 | + prompt = ( |
| 115 | + convo |
| 116 | + + "\n\nThe interviewer just said (transcribed from their speech): \"" |
| 117 | + + heard |
| 118 | + + "\"\n\nRespond as the candidate in 2-5 sentences." |
| 119 | + + (" Reply in fluent modern Hebrew." if language.lower().startswith("he") else "") |
| 120 | + ) |
| 121 | + answer = self._gemini().complete_text(system, prompt, max_tokens=400, |
| 122 | + timeout=15.0).strip() |
| 123 | + self.history.append(("candidate", answer)) |
| 124 | + return answer |
| 125 | + |
| 126 | + # ---------------------------------------------------------------- gemini |
| 127 | + def _gemini(self): |
| 128 | + if self._provider is None: |
| 129 | + from ..llm.provider import GeminiAPIProvider |
| 130 | + |
| 131 | + self._provider = GeminiAPIProvider() |
| 132 | + return self._provider |
| 133 | + |
| 134 | + |
| 135 | +# --------------------------------------------------------------- self-check |
| 136 | +def _demo() -> None: |
| 137 | + """End-to-end proof: synthesize an interviewer question to AUDIO, have the |
| 138 | + candidate hear it via STT (not read it), and answer from the CV.""" |
| 139 | + from .. import voice_cloud |
| 140 | + import base64 |
| 141 | + |
| 142 | + cv_path = os.environ.get( |
| 143 | + "TI_CV_PATH", |
| 144 | + r"C:\Users\ADMIN\Agentic_Projects\Job_Search\cv\amit-aminov-cv.pdf", |
| 145 | + ) |
| 146 | + cv = load_cv(cv_path) |
| 147 | + assert len(cv) > 200, "CV text did not load" |
| 148 | + |
| 149 | + question = ("Tell me about a machine learning project you built end to end, " |
| 150 | + "and one modelling trade-off you had to make.") |
| 151 | + # Interviewer 'speaks' — the candidate will only ever get this as audio. |
| 152 | + tts = voice_cloud.synthesize(question, "en-US", "female", 1.0) |
| 153 | + audio = base64.b64decode(tts["audio"]) |
| 154 | + assert audio, "interviewer TTS produced no audio" |
| 155 | + |
| 156 | + cand = AIInterviewee(cv, voice="am_fenrir", gender="male", name="Amit") |
| 157 | + heard = cand.listen(audio, language="en") |
| 158 | + print("HEARD (via STT):", heard) |
| 159 | + assert heard, "STT heard nothing" |
| 160 | + # The candidate never saw `question` — only `heard`. |
| 161 | + answer = cand.respond(language="en") |
| 162 | + print("ANSWER (Gemini+CV):", answer) |
| 163 | + assert len(answer.split()) >= 8, "answer implausibly short" |
| 164 | + print("OK: listened via STT and answered from the CV, no source-text backdoor.") |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == "__main__": |
| 168 | + _demo() |
0 commit comments