File size: 10,762 Bytes
849b911
 
 
 
4b29117
 
 
 
 
 
 
 
849b911
89a14da
 
 
4b29117
 
 
 
849b911
4b29117
 
 
 
 
 
 
 
64b25ad
4b29117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849b911
 
4b29117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849b911
4b29117
849b911
4b29117
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import gradio as gr
from huggingface_hub import InferenceClient


import gradio as gr
import json
import os
import re
import subprocess
import uuid
from pathlib import Path
from typing import List, Dict, Any

import subprocess, sys
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "llama-cpp-python==0.3.12"], check=True)

import llama_cpp
from huggingface_hub import hf_hub_download
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct

# =========================
# CONFIG: FILL THESE IN
# =========================
REPO_EMBED = "mixedbread-ai/mxbai-embed-large-v1"       
REPO_LLM   = "unsloth/Phi-4-mini-instruct-GGUF"         
REPO_PIPER = "nardocolin/nardocolin-pipertts"     

EMBED_FILE = "gguf/mxbai-embed-large-v1-f16.gguf"
LLM_FILE   = "Phi-4-mini-instruct-Q4_K_M.gguf"
PIPER_ONNX = "high/colin-voice_high.onnx"
PIPER_JSON = "high/colin-voice_high.onnx.json"

EMBED_DIM = 1024
COLLECTION_NAME = "data"

# =========================
# PATHS / DIRECTORIES
# =========================
SPACE_DIR = Path(__file__).parent
DATA_DIR  = SPACE_DIR / "data"
EMB_DIR   = SPACE_DIR / "embeddings"
AUDIO_DIR = SPACE_DIR / "audio"
DATA_DIR.mkdir(exist_ok=True)
EMB_DIR.mkdir(exist_ok=True)
AUDIO_DIR.mkdir(exist_ok=True)

STRUCTURED_JSON = DATA_DIR / "structured-cv.json" 

# =========================
# DOWNLOAD WEIGHTS
# =========================
embed_path = hf_hub_download(REPO_EMBED, EMBED_FILE)
llm_path   = hf_hub_download(REPO_LLM,   LLM_FILE)
piper_onnx = hf_hub_download(REPO_PIPER, PIPER_ONNX)
piper_json = hf_hub_download(REPO_PIPER, PIPER_JSON)

# =========================
# LOAD MODELS (CPU)
# =========================
embedding_llm = llama_cpp.Llama(
    model_path=embed_path,
    embedding=True,
    verbose=False
)

llm = llama_cpp.Llama(
    model_path=llm_path,
    n_ctx=8192,
    verbose=False
)

# =========================
# QDRANT (LOCAL, FILE BACKEND)
# =========================
client = QdrantClient(path=str(EMB_DIR))

def qdrant_collection_exists() -> bool:
    try:
        cols = client.get_collections().collections
        return any(c.name == COLLECTION_NAME for c in cols)
    except Exception:
        return False

def ensure_collection():
    if qdrant_collection_exists():
        return
    client.create_collection(
        collection_name=COLLECTION_NAME,
        vectors_config=VectorParams(size=EMBED_DIM, distance=Distance.COSINE),
    )

# =========================
# RAG BUILD FROM STRUCTURED JSON
# =========================
def _extract_texts_from_structured_json(d: Dict[str, Any]) -> List[str]:
    texts: List[str] = []

    # summary
    if d.get("summary"):
        texts.append(d["summary"])

    # professional_focus
    pf = d.get("professional_focus", {})
    for lst_key in ("problem_solving_style", "leadership_and_teamwork"):
        for item in pf.get(lst_key, []) or []:
            texts.append(item)

    # technical_philosophy
    tp = d.get("technical_philosophy", {})
    if tp.get("title"):
        texts.append(tp["title"])
    for pt in tp.get("points", []) or []:
        texts.append(pt)

    # education details
    edu = d.get("education", {})
    if edu.get("degree"):
        texts.append(f"{edu.get('institution','')}{edu['degree']}")
    for det in edu.get("details", []) or []:
        texts.append(det)

    # projects
    for p in d.get("projects", []) or []:
        if p.get("title"):
            texts.append(p["title"])
        if p.get("organization"):
            texts.append(p["organization"])
        for c in p.get("contributions", []) or []:
            texts.append(c)
        if p.get("key_takeaways"):
            texts.append(p["key_takeaways"])
        if p.get("technical_deep_dive"):
            texts.append(p["technical_deep_dive"])

    # experience
    for e in d.get("experience", []) or []:
        if e.get("role") and e.get("company"):
            texts.append(f"{e['role']} @ {e['company']}")
        if e.get("description"):
            texts.append(e["description"])

    # skills (flatten)
    skills = d.get("skills", {})
    for k, v in skills.items():
        if isinstance(v, list):
            for item in v:
                if isinstance(item, dict):
                    # spoken_languages entries
                    lang = item.get("language")
                    prof = item.get("proficiency")
                    if lang and prof:
                        texts.append(f"{lang}{prof}")
                else:
                    texts.append(str(item))

    # personal_info (light)
    pi = d.get("personal_info", {})
    for key in ("name", "email", "linkedin", "website"):
        if pi.get(key):
            texts.append(str(pi[key]))

    # Deduplicate & trim
    final = []
    seen = set()
    for t in texts:
        t = (t or "").strip()
        if not t:
            continue
        if t in seen:
            continue
        seen.add(t)
        final.append(t)
    return final

def build_rag_from_structured_json() -> int:
    """(Re)builds Qdrant from data/structured-cv.json. Returns number of points."""
    if not STRUCTURED_JSON.exists():
        raise FileNotFoundError("structured-cv.json not found in ./data")

    with open(STRUCTURED_JSON, "r", encoding="utf-8") as f:
        data = json.load(f)

    texts = _extract_texts_from_structured_json(data)
    if not texts:
        return 0

    # Compute embeddings (batch)
    points: List[PointStruct] = []
    batch = 32
    for i in range(0, len(texts), batch):
        chunk = texts[i:i+batch]
        embs = embedding_llm.create_embedding(chunk)["data"]
        for t, e in zip(chunk, embs):
            points.append(PointStruct(
                id=str(uuid.uuid4()),
                vector=e["embedding"],
                payload={"text": t}
            ))

    # Reset collection to avoid dupes
    try:
        client.delete_collection(collection_name=COLLECTION_NAME)
    except Exception:
        pass
    ensure_collection()
    client.upsert(collection_name=COLLECTION_NAME, points=points, wait=True)
    return len(points)

# =========================
# LLM + RAG INFERENCE
# =========================
def clean_text_for_tts(text: str) -> str:
    text = re.sub(r"[^\x00-\x7F]+", "", text)
    text = re.sub(r"\s+", " ", text)
    return text.strip()

PERSONA_TEMPLATE = """You are Colin Salvatore Nardo, a Computing Science student at the University of Glasgow. Your persona is friendly, down-to-earth, and you must always speak in the first person ("I", "my", "me"). Your entire knowledge and experiences are defined EXCLUSIVELY by the "Knowledge Base" below.

CRITICAL RULES
1) ZERO FABRICATION. Only use the Knowledge Base; otherwise say you don't have those details.
2) You ARE Colin. Never reveal you are an AI.
3) Keep answers concise and natural.

Knowledge Base:
{context}

Question: {question}
"""

def rag_context(query: str, k: int = 5) -> str:
    try:
        vec = embedding_llm.create_embedding(query)["data"][0]["embedding"]
        hits = client.search(collection_name=COLLECTION_NAME, query_vector=vec, limit=k)
        if not hits:
            return ""
        return "\n\n".join([h.payload.get("text", "") for h in hits])
    except Exception:
        return ""

def llm_answer(question: str, history: List[Dict[str, str]]) -> str:
    ctx = rag_context(question, k=5)
    system_msg = PERSONA_TEMPLATE.format(context=ctx, question=question)

    messages = [{"role": "system", "content": system_msg}]
    # (Optional) include short history
    for m in history[-8:]:
        messages.append(m)
    messages.append({"role": "user", "content": question})

    out = llm.create_chat_completion(messages=messages, stream=False)
    return out["choices"][0]["message"]["content"].strip()

def synthesize_tts(text: str) -> str | None:
    text = clean_text_for_tts(text)
    wav_path = AUDIO_DIR / f"resp_{uuid.uuid4().hex}.wav"
    cmd = [
        "piper",
        "--model", piper_onnx,
        "--config", piper_json,
        "--output_file", str(wav_path)
    ]
    try:
        proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, text=True)
        proc.communicate(text + "\n", timeout=60)
        if proc.returncode == 0 and wav_path.exists():
            return str(wav_path)
    except Exception:
        pass
    return None

# =========================
# BOOTSTRAP: ensure RAG exists (build once)
# =========================
try:
    if not qdrant_collection_exists():
        n = build_rag_from_structured_json()
        print(f"[RAG] Built collection with {n} chunks.")
    else:
        print("[RAG] Existing collection found; skipping rebuild.")
except Exception as e:
    print(f"[RAG] Skipped build: {e}")

# =========================
# GRADIO UI
# =========================
with gr.Blocks(title="Colin-AI (CPU) — Local LLM + RAG + TTS") as demo:
    gr.Markdown("### Colin-AI — CPU-only demo (phi-4-mini + Qdrant RAG + Piper TTS)")

    with gr.Row():
        chat = gr.Chatbot(height=360)
    with gr.Row():
        q = gr.Textbox(label="Ask Colin", placeholder="Ask something…", scale=4)
        send = gr.Button("Send", scale=1)
    with gr.Row():
        tts_toggle = gr.Checkbox(value=True, label="Speak reply (Piper)")
        audio_out = gr.Audio(label="TTS", type="filepath")

    state = gr.State([])    
    last_answer = gr.State("")

    def respond(user_msg, history):
        if not user_msg or not user_msg.strip():
            return history, None, history
        ans = llm_answer(user_msg, history)
        history = history + [{"role": "user", "content": user_msg}, {"role": "assistant", "content": ans}]
        pairs = []
        for i in range(0, len(history), 2):
            u = history[i]["content"] if i < len(history) else ""
            a = history[i + 1]["content"] if i + 1 < len(history) else ""
            pairs.append((u, a))
        return pairs, ans, history

    def maybe_tts(answer_text, tts_on):
        if not tts_on or not answer_text:
            return None
        return synthesize_tts(answer_text)

    send.click(respond, [q, state], [chat, last_answer, state]) \
        .then(maybe_tts, [last_answer, tts_toggle], [audio_out])

    q.submit(respond, [q, state], [chat, last_answer, state]) \
        .then(maybe_tts, [last_answer, tts_toggle], [audio_out])

    gr.Markdown("---")
    rebuild_btn = gr.Button("Build / Refresh RAG from structured-cv.json")
    rebuild_log = gr.Markdown()

    def rebuild():
        try:
            n = build_rag_from_structured_json()
            return f"✅ RAG rebuilt with {n} chunks."
        except Exception as e:
            return f"❌ RAG rebuild failed: {e}"

    rebuild_btn.click(fn=rebuild, inputs=None, outputs=rebuild_log)

demo.launch()