Spaces:
Sleeping
Sleeping
Update backend.py
Browse files- backend.py +71 -0
backend.py
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import fitz # PyMuPDF
|
| 3 |
+
import faiss
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
|
| 8 |
+
# Configure Gemini API
|
| 9 |
+
genai.configure(api_key=os.getenv("AIzaSyBaCZ19qDVQoGglZaPASytcZQerrDeYz6c")) # use Hugging Face Secrets
|
| 10 |
+
|
| 11 |
+
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
+
generation_model = genai.GenerativeModel("gemini-1.5-flash-002")
|
| 13 |
+
|
| 14 |
+
# Initialize globals
|
| 15 |
+
index = None
|
| 16 |
+
document_chunks = []
|
| 17 |
+
user_data = ""
|
| 18 |
+
logged_in = True # toggle for simulation
|
| 19 |
+
|
| 20 |
+
def extract_text_from_pdf(file):
|
| 21 |
+
doc = fitz.open(stream=file.read(), filetype="pdf")
|
| 22 |
+
text = ""
|
| 23 |
+
for page in doc:
|
| 24 |
+
text += page.get_text()
|
| 25 |
+
return text
|
| 26 |
+
|
| 27 |
+
def split_into_chunks(text, chunk_size=500):
|
| 28 |
+
import textwrap
|
| 29 |
+
return textwrap.wrap(text, width=chunk_size)
|
| 30 |
+
|
| 31 |
+
def build_faiss_index(chunks):
|
| 32 |
+
global index, document_chunks
|
| 33 |
+
document_chunks = chunks
|
| 34 |
+
embeddings = embedding_model.encode(chunks, convert_to_numpy=True)
|
| 35 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 36 |
+
index.add(embeddings)
|
| 37 |
+
|
| 38 |
+
def retrieve_context(query, top_k=5):
|
| 39 |
+
if index is None:
|
| 40 |
+
return ["No documents indexed."]
|
| 41 |
+
query_embedding = embedding_model.encode([query], convert_to_numpy=True)
|
| 42 |
+
_, indices = index.search(query_embedding, top_k)
|
| 43 |
+
return [document_chunks[i] for i in indices[0]]
|
| 44 |
+
|
| 45 |
+
def generate_answer(query):
|
| 46 |
+
retrieved_docs = retrieve_context(query)
|
| 47 |
+
context = "\n".join(retrieved_docs)
|
| 48 |
+
|
| 49 |
+
if logged_in and user_data:
|
| 50 |
+
context += "\nUser Data:\n" + user_data
|
| 51 |
+
|
| 52 |
+
prompt = f"""
|
| 53 |
+
You are FinBot, a knowledgeable assistant trained on financial banking queries.
|
| 54 |
+
|
| 55 |
+
Your job is to read the context carefully and explain the answer to the user's question using only the provided information.
|
| 56 |
+
Do not make estimations or include any knowledge that is not in the context. Be clear and concise.
|
| 57 |
+
|
| 58 |
+
<context>
|
| 59 |
+
{context}
|
| 60 |
+
</context>
|
| 61 |
+
|
| 62 |
+
<question>
|
| 63 |
+
{query}
|
| 64 |
+
</question>
|
| 65 |
+
"""
|
| 66 |
+
response = generation_model.generate_content(prompt)
|
| 67 |
+
return response.text.strip()
|
| 68 |
+
|
| 69 |
+
def set_user_data(file):
|
| 70 |
+
global user_data
|
| 71 |
+
user_data = extract_text_from_pdf(file)
|