-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFInancial_Helper.py
More file actions
176 lines (151 loc) · 5.99 KB
/
Copy pathFInancial_Helper.py
File metadata and controls
176 lines (151 loc) · 5.99 KB
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
import os
from pathlib import Path
import logging
import streamlit as st
from io import BytesIO
from dotenv import load_dotenv
import google.generativeai as genai
import chromadb
from chromadb import Documents, EmbeddingFunction, Embeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from PyPDF2 import PdfReader
import re
from deep_translator import GoogleTranslator# Setup logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger(__name__)
# Load env and configure Gemini
load_dotenv()
API_KEY = os.getenv('GEMINI_API_KEY')
if not API_KEY:
st.error("Missing GEMINI_API_KEY in environment variables")
st.stop()
genai.configure(api_key=API_KEY)
# Constants
db_path = Path("./db")
collection_name = "sme_db"
# Embedding function with batching
class GeminiEmbeddingFunction(EmbeddingFunction):
def __init__(self, model: str = 'models/embedding-001', batch_size: int = 16):
self.model = model
self.batch_size = batch_size
def __call__(self, input: Documents) -> Embeddings:
texts = list(input)
embeddings = []
for i in range(0, len(texts), self.batch_size):
batch = texts[i:i + self.batch_size]
resp = genai.embed_content(
model=self.model,
content=batch,
task_type="retrieval_document",
title="RAG Batch"
)
embeddings.extend(resp['embedding'])
return embeddings
# Utilities
def extract_text_from_pdf(file_stream: BytesIO) -> str:
reader = PdfReader(file_stream)
text = []
# skip first page if desired
for page in reader.pages[1:]:
page_text = page.extract_text() or ""
text.append(page_text)
return "\n".join(text)
# Keep Latin and Arabic chars
def clean_text(text: str) -> str:
text = re.sub(r"(\w+)-\n(\w+)", r"\1\2", text)
lines = text.splitlines()
allowed = re.compile(r"[0-9A-Za-z\u0600-\u06FF\.,\$%\-\s]")
cleaned = []
for line in lines:
raw = line.strip()
if len(raw) < 20 or raw.isdigit():
continue
if re.match(r"^Page \d+ of", raw):
continue
filtered = ''.join(ch for ch in raw if allowed.match(ch))
cleaned.append(filtered)
return "\n\n".join(cleaned)
def translate_query_to_doc_lang(query: str, target_lang='en') -> str:
return GoogleTranslator(source='auto', target=target_lang).translate(query)
def split_text(text: str, chunk_size: int = 1000, overlap: int = 100) -> list:
splitter = RecursiveCharacterTextSplitter(
separators=["\\n\\n", "\\n", "。", ".", "!", "؟", " "],
chunk_size=1000,
chunk_overlap=100,
add_start_index=True
)
docs = splitter.create_documents([text])
return [{"content": d.page_content, "start": d.metadata["start_index"]} for d in docs]
def get_collection() -> chromadb.api.models.Collection:
client = chromadb.PersistentClient(path=str(db_path))
return client.get_or_create_collection(
name=collection_name,
embedding_function=GeminiEmbeddingFunction()
)
def check_hallucination(answer: str, context: list) -> bool:
for sent in answer.split('.'):
for w in [w.strip(' ,') for w in sent.split()]:
if w.isdigit():
continue
if w.isalpha() and all(w not in chunk.lower() for chunk in context):
return False
return True
@st.cache_resource
def ingest(pdf_stream: BytesIO):
raw = extract_text_from_pdf(pdf_stream)
cleaned = clean_text(raw)
chunks = split_text(cleaned)
col = get_collection()
if col.count() == 0:
docs, metas, ids = [], [], []
for i, c in enumerate(chunks):
docs.append(c['content'])
metas.append({"source": "uploaded_pdf", "chunk_start": c['start']})
ids.append(str(i))
col.add(documents=docs, ids=ids, metadatas=metas)
return col
# Streamlit UI
st.title("RAG Financial Assistant")
uploaded_file = st.file_uploader("Upload a PDF file (it can be in Arabic or English)", type=["pdf"])
lang = st.selectbox("Select language you want the answer to be in:", ["English", "Arabic"])
if uploaded_file:
query = st.text_input("Enter your question:")
if st.button("Ask") and query:
pdf_bytes = uploaded_file.read()
pdf_stream = BytesIO(pdf_bytes)
col = ingest(pdf_stream)
query =translate_query_to_doc_lang(query)
results = col.query(query_texts=[query], n_results=5)
passages = results['documents'][0]
if not check_hallucination(query, passages):
st.warning("⚠️ Potential hallucination detected. Please refine your query.")
context = "\n\n".join(f"Doc {i + 1}: {p}" for i, p in enumerate(passages))
if lang == "Arabic":
question_prefix = "سؤال:"
answer_suffix = "الإجابة معتمدًا على السياق فقط."
else:
question_prefix = "Question:"
answer_suffix = "Answer using only context."
prompt = (
f"You are a financial assistant tasked with answering user queries using reliable context.\n\n"
f"Context:\n{context}\n\n"
f"{question_prefix}\n{query}\n\n"
"Instructions:\n"
f"- Context might be in Arabic or English translate accordingly. answer in {lang} using only the provided context.\n"
"- Use only the information in the context to answer.\n"
"- Be precise and avoid speculation.\n"
"- If information is missing, state that clearly.\n"
"- Format numeric data with units.\n"
f"{answer_suffix}\n\n"
"Answer:"
)
with st.spinner("Generating answer..."):
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
resp = model.generate_content(prompt)
st.markdown("**Answer:**")
st.write(resp.text)
else:
st.info("Please upload a PDF to get started.")