File size: 12,030 Bytes
bc853b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc19f4
 
bc853b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc19f4
 
bc853b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc19f4
bc853b8
 
 
 
 
cbc19f4
 
 
 
bc853b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc19f4
 
 
 
 
 
 
 
 
 
 
bc853b8
 
 
 
 
 
 
 
 
 
 
 
cbc19f4
bc853b8
 
cbc19f4
 
bc853b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc19f4
bc853b8
cbc19f4
bc853b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbc19f4
bc853b8
cbc19f4
bc853b8
 
 
 
 
 
 
 
 
 
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
import os
import tempfile
import pickle
from typing import List, Dict, Any

import streamlit as st
import numpy as np

from sentence_transformers import SentenceTransformer
import faiss
import PyPDF2
import docx
from groq import Groq

DEFAULT_EMBED_MODEL = "all-MiniLM-L6-v2"
DEFAULT_CHUNK_SIZE = 200
DEFAULT_CHUNK_OVERLAP = 50
DEFAULT_TOP_K = 5
INDEX_PATH = "faiss_index.bin"
METADATA_PATH = "metadata.pkl"

@st.cache_resource
def load_embedding_model(model_name: str = DEFAULT_EMBED_MODEL):
    return SentenceTransformer(model_name)


def save_uploaded_file(uploaded_file) -> str:
    tmpdir = tempfile.gettempdir()
    safe_name = os.path.basename(uploaded_file.name)
    temp_path = os.path.join(tmpdir, safe_name)
    with open(temp_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
    return temp_path


def extract_text_from_pdf(path: str) -> str:
    text_pages = []
    try:
        reader = PyPDF2.PdfReader(path)
        for page in reader.pages:
            page_text = page.extract_text() or ""
            text_pages.append(page_text)
    except Exception:
        return ""
    return "\n\n".join(text_pages)


def extract_text_from_docx(path: str) -> str:
    try:
        doc = docx.Document(path)
        paragraphs = [p.text for p in doc.paragraphs]
        return "\n\n".join(paragraphs)
    except Exception:
        return ""


def extract_text_from_txt(path: str) -> str:
    try:
        with open(path, "r", encoding="utf-8", errors="ignore") as f:
            return f.read()
    except Exception:
        return ""


def chunk_text(text: str, chunk_size: int = DEFAULT_CHUNK_SIZE, overlap: int = DEFAULT_CHUNK_OVERLAP) -> List[str]:
    words = text.split()
    chunks: List[str] = []
    start = 0
    n = len(words)
    while start < n:
        end = min(start + chunk_size, n)
        chunk = " ".join(words[start:end])
        chunks.append(chunk)
        if end == n:
            break
        start = max(0, end - overlap)
    return chunks


def build_faiss_index(embeddings: np.ndarray) -> faiss.IndexFlat:
    embeddings = np.ascontiguousarray(embeddings.astype('float32'))
    d = embeddings.shape[1]
    index = faiss.IndexFlatIP(d)
    index.add(embeddings)
    return index


def normalize_embeddings(vecs: np.ndarray) -> np.ndarray:
    vecs = np.asarray(vecs, dtype=np.float32)
    norms = np.linalg.norm(vecs, axis=1, keepdims=True)
    norms[norms == 0] = 1.0
    return vecs / norms


def save_index_and_metadata(index: faiss.Index, metadata: List[Dict[str, Any]], index_path: str = INDEX_PATH, meta_path: str = METADATA_PATH):
    try:
        faiss.write_index(index, index_path)
        with open(meta_path, "wb") as f:
            pickle.dump(metadata, f)
    except Exception:
        pass


def load_index_and_metadata(index_path: str = INDEX_PATH, meta_path: str = METADATA_PATH):
    try:
        if os.path.exists(index_path) and os.path.exists(meta_path):
            index = faiss.read_index(index_path)
            with open(meta_path, "rb") as f:
                metadata = pickle.load(f)
            return index, metadata
    except Exception:
        return None, None
    return None, None


def get_groq_client_from_env() -> Groq | None:
    key = os.environ.get("GROQ_API_KEY") or os.environ.get("HF_GROQ_API_KEY") or os.environ.get("HF_API_TOKEN")
    if not key:
        return None
    return Groq(api_key=key)


def groq_chat_answer(client: Groq, messages: List[Dict[str, str]], model: str = "llama-3.3-70b-versatile", temperature: float = 0.0, max_tokens: int = 512) -> str:
    try:
        chat_completion = client.chat.completions.create(
            messages=messages,
            model=model,
            temperature=temperature,
            max_tokens=max_tokens,
        )
        return getattr(chat_completion.choices[0].message, 'content', '')
    except Exception:
        return ""


def main():
    st.set_page_config(page_title="RAG with Groq + Sentence-Transformers", layout="wide")
    st.title("RAG (Retrieval Augmented Generation) — Groq + sentence-transformers + FAISS")

    st.sidebar.header("Index / Retrieval settings")
    chunk_size = int(st.sidebar.number_input("Chunk size (words)", min_value=50, max_value=2000, value=DEFAULT_CHUNK_SIZE, step=50))
    chunk_overlap = int(st.sidebar.number_input("Chunk overlap (words)", min_value=0, max_value=500, value=DEFAULT_CHUNK_OVERLAP, step=10))
    top_k = int(st.sidebar.number_input("Top-k results to retrieve", min_value=1, max_value=50, value=DEFAULT_TOP_K))

    st.sidebar.markdown("---")
    st.sidebar.write("Groq API key is loaded from Hugging Face secrets or environment variables. Do not paste keys in the UI.")

    with st.spinner("Loading embedding model..."):
        embed_model = load_embedding_model()

    index, metadata = load_index_and_metadata()
    if index is None and "index" in st.session_state:
        index = st.session_state["index"]
        metadata = st.session_state.get("metadata")

    if index is None:
        st.info("No existing FAISS index found. Upload documents and click 'Ingest documents' to build index.")
    else:
        try:
            nt = index.ntotal
        except Exception:
            nt = 0
        st.success(f"Loaded FAISS index with {nt} vectors.")

    st.header("1) Upload documents")
    uploaded_files = st.file_uploader("Upload PDF / DOCX / TXT files", type=["pdf", "docx", "txt"], accept_multiple_files=True)

    if uploaded_files:
        st.session_state["uploaded_list"] = [f.name for f in uploaded_files]
        st.write("Files ready to ingest:", st.session_state["uploaded_list"])

    if st.button("Ingest documents"):
        if not uploaded_files:
            st.warning("Please upload at least one file before ingesting.")
        else:
            all_chunks: List[str] = []
            metadata = []
            for uploaded in uploaded_files:
                tmp_path = save_uploaded_file(uploaded)
                name = uploaded.name
                text = ""
                if name.lower().endswith(".pdf"):
                    text = extract_text_from_pdf(tmp_path)
                elif name.lower().endswith(".docx"):
                    text = extract_text_from_docx(tmp_path)
                elif name.lower().endswith(".txt"):
                    text = extract_text_from_txt(tmp_path)
                else:
                    continue

                if not text or not text.strip():
                    continue

                chunks = chunk_text(text, chunk_size=chunk_size, overlap=chunk_overlap)
                for i, c in enumerate(chunks):
                    all_chunks.append(c)
                    metadata.append({"source": name, "chunk_id": i, "text": c})

            if not all_chunks:
                st.error("No chunks were generated. Aborting.")
            else:
                with st.spinner("Computing embeddings..."):
                    embeddings = embed_model.encode(all_chunks, show_progress_bar=True, convert_to_numpy=True)
                    embeddings = normalize_embeddings(embeddings)

                index = build_faiss_index(embeddings)
                st.session_state["index"] = index
                st.session_state["metadata"] = metadata
                try:
                    save_index_and_metadata(index, metadata)
                    st.success(f"Index built and saved. {len(all_chunks)} chunks indexed.")
                except Exception:
                    st.success(f"Index built in memory. {len(all_chunks)} chunks indexed.")
                st.info(f"Ingested {len(all_chunks)} chunks from {len(set(m['source'] for m in metadata))} file(s).")
                if len(all_chunks) > 0:
                    st.markdown("### Example chunk")
                    st.write(all_chunks[0][:1000])

    st.markdown("---")

    st.header("2) Ask questions (uses retrieved context + Groq generation)")
    query = st.text_area("Enter your question here", height=120)

    col1, col2 = st.columns([1, 3])
    with col1:
        if st.button("Ask"):
            if not query or not query.strip():
                st.warning("Please enter a question.")
            else:
                if "index" not in st.session_state or "metadata" not in st.session_state:
                    st.warning("No index available. Please ingest documents first.")
                else:
                    index = st.session_state["index"]
                    metadata = st.session_state["metadata"]
                    q_emb = embed_model.encode([query], convert_to_numpy=True)
                    q_emb = normalize_embeddings(q_emb)
                    q_emb = np.ascontiguousarray(q_emb.astype('float32'))
                    k = max(1, min(top_k, int(index.ntotal)))
                    if k == 0:
                        st.warning("Index is empty. Ingest documents first.")
                    else:
                        D, I = index.search(q_emb, k)
                        hits = I[0].tolist() if I is not None else []

                        contexts = []
                        for idx in hits:
                            if idx < 0 or idx >= len(metadata):
                                continue
                            meta = metadata[idx]
                            contexts.append(meta)

                        client = get_groq_client_from_env()
                        if client is None:
                            st.error("Groq client could not be created. Set GROQ_API_KEY in your Hugging Face secrets or environment.")
                        else:
                            system_msg = {
                                "role": "system",
                                "content": "You are an assistant that answers user questions using ONLY the context passages provided. If the answer is not contained in the context, say you don't know. Provide short answers and include the source(s) for any factual claims."
                            }

                            context_blocks = []
                            for c in contexts:
                                block = f"[Source: {c['source']} | chunk_id: {c['chunk_id']}]\n{c['text']}"
                                context_blocks.append(block)

                            context_text = "\n\n---\n\n".join(context_blocks)

                            user_msg_text = (
                                f"Context passages:\n\n{context_text}\n\n"
                                f"Question: {query}\n\n"
                                "Instructions: Use only the context passages above to answer. If the context does not contain enough information, respond that you don't know. Keep the answer concise and cite the source tags in square brackets."
                            )

                            user_msg = {"role": "user", "content": user_msg_text}

                            with st.spinner("Generating answer from Groq model..."):
                                answer = groq_chat_answer(client, messages=[system_msg, user_msg])

                            if answer:
                                st.subheader("Answer")
                                st.write(answer)

                                st.subheader("Retrieved passages (for transparency)")
                                for c in contexts:
                                    st.markdown(f"**Source:** {c['source']} — chunk {c['chunk_id']}")
                                    st.write(c['text'][:500])
                                    st.markdown("---")

    st.sidebar.markdown("---")
    st.sidebar.header("Index status")
    if "index" in st.session_state:
        try:
            st.sidebar.write(f"Vectors in index: {st.session_state['index'].ntotal}")
        except Exception:
            st.sidebar.write("Vectors in index: unknown")
    else:
        st.sidebar.write("No index built yet")

    st.sidebar.markdown("\nMade for development in Colab + Streamlit.\nUse responsibly and avoid publishing API keys.")


if __name__ == '__main__':
    main()