Spaces:
Runtime error
Runtime error
Commit ·
eb09214
1
Parent(s): d2894a4
fix: corrected import paths
Browse files- app.py +1 -1
- config.py +5 -0
- retriever.py +38 -0
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from qa_pipeline import answer_question
|
| 3 |
|
| 4 |
def qa_interface(question):
|
| 5 |
return answer_question(question)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from qa_pipeline import get_answer as answer_question
|
| 3 |
|
| 4 |
def qa_interface(question):
|
| 5 |
return answer_question(question)
|
config.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
DATA_FOLDER = os.getenv("DATA_FOLDER", "data")
|
| 4 |
+
EMBEDDING_MODEL = "all-MiniLM-L6-v2"
|
| 5 |
+
LLM_MODEL = "google/flan-t5-base" # Lightweight, fast, free on HF
|
retriever.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
| 4 |
+
from config import EMBEDDING_MODEL
|
| 5 |
+
|
| 6 |
+
model = SentenceTransformer(EMBEDDING_MODEL)
|
| 7 |
+
index = faiss.IndexFlatL2(384)
|
| 8 |
+
docs = []
|
| 9 |
+
|
| 10 |
+
def chunk_text(text, max_length=500):
|
| 11 |
+
sentences = text.split('. ')
|
| 12 |
+
chunks = []
|
| 13 |
+
current_chunk = ''
|
| 14 |
+
for sentence in sentences:
|
| 15 |
+
if len(current_chunk) + len(sentence) <= max_length:
|
| 16 |
+
current_chunk += sentence + '. '
|
| 17 |
+
else:
|
| 18 |
+
chunks.append(current_chunk.strip())
|
| 19 |
+
current_chunk = sentence + '. '
|
| 20 |
+
if current_chunk:
|
| 21 |
+
chunks.append(current_chunk.strip())
|
| 22 |
+
return chunks
|
| 23 |
+
|
| 24 |
+
def build_index(doc_texts):
|
| 25 |
+
global docs
|
| 26 |
+
docs = []
|
| 27 |
+
all_chunks = []
|
| 28 |
+
for doc in doc_texts:
|
| 29 |
+
chunks = chunk_text(doc)
|
| 30 |
+
docs.extend(chunks)
|
| 31 |
+
all_chunks.extend(chunks)
|
| 32 |
+
embeddings = model.encode(all_chunks)
|
| 33 |
+
index.add(np.array(embeddings))
|
| 34 |
+
|
| 35 |
+
def get_relevant_chunks(query, top_k=1):
|
| 36 |
+
query_vec = model.encode([query])
|
| 37 |
+
D, I = index.search(np.array(query_vec), top_k)
|
| 38 |
+
return "\n\n".join([docs[i] for i in I[0]])
|