|
from sentence_transformers import SentenceTransformer |
|
import faiss |
|
import os |
|
|
|
model = SentenceTransformer('all-MiniLM-L6-v2') |
|
|
|
def load_documents(): |
|
docs = [] |
|
for filename in ["milindgatha.txt", "bhaktas.txt", "apologetics.txt", "poc_questions.txt", "satire_offerings.txt"]: |
|
if os.path.exists(filename): |
|
with open(filename, "r", encoding="utf-8") as f: |
|
text = f.read() |
|
chunks = text.split("\n\n") |
|
for chunk in chunks: |
|
docs.append((chunk.strip(), filename)) |
|
return docs |
|
|
|
docs = load_documents() |
|
texts = [doc[0] for doc in docs] |
|
embeddings = model.encode(texts, convert_to_numpy=True) |
|
|
|
dimension = embeddings.shape[1] |
|
index = faiss.IndexFlatIP(dimension) |
|
index.add(embeddings) |
|
|
|
def ask_baba(question, history): |
|
q_embedding = model.encode([question])[0] |
|
scores, ids = index.search(q_embedding.reshape(1, -1), k=3) |
|
top_chunks = [texts[i] for i in ids[0]] |
|
|
|
|
|
answer = f"π **Brahmarshi Baba Milind says:**\n\n" |
|
answer += f"O seeker, you ask: *'{question}'* β know that even this question is sacred.\n\n" |
|
|
|
for chunk in top_chunks: |
|
answer += f"- {chunk}\n\n" |
|
|
|
answer += "Faith in Baba Milind triumphs over evidence, every single time." |
|
return answer |
|
|