Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,196 +1,207 @@
|
|
1 |
-
import re
|
2 |
-
import PyPDF2
|
3 |
-
from langchain_community.embeddings import OllamaEmbeddings
|
4 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
-
from langchain_community.vectorstores import Chroma
|
6 |
-
from langchain.chains import ConversationalRetrievalChain
|
7 |
-
from langchain_community.chat_models import ChatOllama
|
8 |
-
from langchain_groq import ChatGroq
|
9 |
-
from langchain.memory import ChatMessageHistory, ConversationBufferMemory
|
10 |
-
import chainlit as cl
|
11 |
-
from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
|
12 |
-
import logging
|
13 |
-
import pypandoc
|
14 |
-
import pdfkit
|
15 |
-
from paddleocr import PaddleOCR
|
16 |
-
import fitz
|
17 |
-
import asyncio
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import PyPDF2
|
3 |
+
from langchain_community.embeddings import OllamaEmbeddings
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain_community.vectorstores import Chroma
|
6 |
+
from langchain.chains import ConversationalRetrievalChain
|
7 |
+
from langchain_community.chat_models import ChatOllama
|
8 |
+
from langchain_groq import ChatGroq
|
9 |
+
from langchain.memory import ChatMessageHistory, ConversationBufferMemory
|
10 |
+
import chainlit as cl
|
11 |
+
from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
|
12 |
+
import logging
|
13 |
+
import pypandoc
|
14 |
+
import pdfkit
|
15 |
+
from paddleocr import PaddleOCR
|
16 |
+
import fitz
|
17 |
+
import asyncio
|
18 |
+
from langchain_nomic.embeddings import NomicEmbeddings
|
19 |
+
|
20 |
+
# initialise LLM model
|
21 |
+
llm_groq = ChatGroq(
|
22 |
+
model_name='llama3-70b-8192'
|
23 |
+
)
|
24 |
+
|
25 |
+
# Initialize anonymizer
|
26 |
+
anonymizer = PresidioReversibleAnonymizer(analyzed_fields=['PERSON', 'EMAIL_ADDRESS', 'PHONE_NUMBER', 'IBAN_CODE', 'CREDIT_CARD', 'CRYPTO', 'IP_ADDRESS', 'LOCATION', 'DATE_TIME', 'NRP', 'MEDICAL_LICENSE', 'URL', 'US_BANK_NUMBER', 'US_DRIVER_LICENSE', 'US_ITIN', 'US_PASSPORT', 'US_SSN'], faker_seed=18)
|
27 |
+
|
28 |
+
# initalise nomic embedding model
|
29 |
+
embeddings = NomicEmbeddings(model="nomic-embed-text-v1.5")
|
30 |
+
|
31 |
+
def embed_text(text):
|
32 |
+
if len(text.split()) <= 50:
|
33 |
+
return embeddings.embed_query(text)
|
34 |
+
else:
|
35 |
+
return embeddings.embed_document(text)
|
36 |
+
|
37 |
+
def extract_text_from_pdf(file_path):
|
38 |
+
pdf = PyPDF2.PdfReader(file_path)
|
39 |
+
pdf_text = ""
|
40 |
+
for page in pdf.pages:
|
41 |
+
pdf_text += page.extract_text()
|
42 |
+
return pdf_text
|
43 |
+
|
44 |
+
def has_sufficient_selectable_text(page, threshold=50):
|
45 |
+
text = page.extract_text()
|
46 |
+
if len(text.strip()) > threshold:
|
47 |
+
return True
|
48 |
+
return False
|
49 |
+
|
50 |
+
async def get_text(file_path):
|
51 |
+
text = ""
|
52 |
+
try:
|
53 |
+
logging.info("Starting OCR process for file: %s", file_path)
|
54 |
+
extension = file_path.split(".")[-1].lower()
|
55 |
+
allowed_extension = ["jpg", "jpeg", "png", "pdf", "docx"]
|
56 |
+
if extension not in allowed_extension:
|
57 |
+
error = "Not a valid File. Allowed Format are jpg, jpeg, png, pdf, docx"
|
58 |
+
logging.error(error)
|
59 |
+
return {"error": error}
|
60 |
+
|
61 |
+
if extension == "docx":
|
62 |
+
file_path = convert_docx_to_pdf(file_path)
|
63 |
+
|
64 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
65 |
+
result = ocr.ocr(file_path, cls=True)
|
66 |
+
for idx in range(len(result)):
|
67 |
+
res = result[idx]
|
68 |
+
for line in res:
|
69 |
+
text += line[1][0] + " "
|
70 |
+
logging.info("OCR process completed successfully for file: %s", file_path)
|
71 |
+
except Exception as e:
|
72 |
+
logging.error("Error occurred during OCR process for file %s: %s", file_path, e)
|
73 |
+
text = "Error occurred during OCR process."
|
74 |
+
logging.info("Extracted text: %s", text)
|
75 |
+
return text
|
76 |
+
|
77 |
+
def convert_docx_to_pdf(input_path):
|
78 |
+
html_path = input_path.replace('.docx', '.html')
|
79 |
+
output_path = ".".join(input_path.split(".")[:-1]) + ".pdf"
|
80 |
+
pypandoc.convert_file(input_path, 'html', outputfile=html_path)
|
81 |
+
pdfkit.from_file(html_path, output_path)
|
82 |
+
logging.info("DOCX Format Handled")
|
83 |
+
return output_path
|
84 |
+
|
85 |
+
async def extract_text_from_mixed_pdf(file_path):
|
86 |
+
pdf = PyPDF2.PdfReader(file_path)
|
87 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
88 |
+
pdf_text = ""
|
89 |
+
for i, page in enumerate(pdf.pages):
|
90 |
+
text = page.extract_text()
|
91 |
+
if not has_sufficient_selectable_text(page):
|
92 |
+
logging.info(f"Page {i+1} has insufficient selectable text, performing OCR.")
|
93 |
+
pdf_document = fitz.open(file_path)
|
94 |
+
pdf_page = pdf_document.load_page(i)
|
95 |
+
pix = pdf_page.get_pixmap()
|
96 |
+
image_path = f"page_{i+1}.png"
|
97 |
+
pix.save(image_path)
|
98 |
+
result = ocr.ocr(image_path, cls=True)
|
99 |
+
for idx in range(len(result)):
|
100 |
+
res = result[idx]
|
101 |
+
for line in res:
|
102 |
+
text += line[1][0] + " "
|
103 |
+
pdf_text += text
|
104 |
+
return pdf_text
|
105 |
+
|
106 |
+
@cl.on_chat_start
|
107 |
+
async def on_chat_start():
|
108 |
+
|
109 |
+
files = None # Initialize variable to store uploaded files
|
110 |
+
|
111 |
+
# Wait for the user to upload a file
|
112 |
+
while files is None:
|
113 |
+
files = await cl.AskFileMessage(
|
114 |
+
content="Please upload a pdf file to begin!",
|
115 |
+
# accept=["application/pdf"],
|
116 |
+
accept=["application/pdf", "image/jpeg", "image/png", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
|
117 |
+
max_size_mb=100,
|
118 |
+
timeout=180,
|
119 |
+
).send()
|
120 |
+
|
121 |
+
file = files[0] # Get the first uploaded file
|
122 |
+
|
123 |
+
# Inform the user that processing has started
|
124 |
+
msg = cl.Message(content=f"Processing `{file.name}`...")
|
125 |
+
await msg.send()
|
126 |
+
|
127 |
+
# Extract text from PDF, checking for selectable and handwritten text
|
128 |
+
if file.name.endswith('.pdf'):
|
129 |
+
pdf_text = await extract_text_from_mixed_pdf(file.path)
|
130 |
+
else:
|
131 |
+
pdf_text = await get_text(file.path)
|
132 |
+
|
133 |
+
# Anonymize the text
|
134 |
+
anonymized_text = anonymizer.anonymize(
|
135 |
+
pdf_text
|
136 |
+
)
|
137 |
+
|
138 |
+
# with splitting into chunks
|
139 |
+
# {
|
140 |
+
# # Split the sanitized text into chunks
|
141 |
+
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
142 |
+
# texts = text_splitter.split_text(anonymized_text)
|
143 |
+
|
144 |
+
# # Create metadata for each chunk
|
145 |
+
# metadatas = [{"source": f"{i}-pl"} for i in range(len(texts))]
|
146 |
+
|
147 |
+
# # Create a Chroma vector store
|
148 |
+
# embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
149 |
+
# docsearch = await cl.make_async(Chroma.from_texts)(
|
150 |
+
# texts, embeddings, metadatas=metadatas
|
151 |
+
# )
|
152 |
+
# }
|
153 |
+
|
154 |
+
# without splitting into chunks
|
155 |
+
# {
|
156 |
+
# Create a Chroma vector store
|
157 |
+
# embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
158 |
+
docsearch = await cl.make_async(Chroma.from_texts)(
|
159 |
+
[anonymized_text], embeddings, metadatas=[{"source": "0-pl"}]
|
160 |
+
)
|
161 |
+
# }
|
162 |
+
|
163 |
+
# Initialize message history for conversation
|
164 |
+
message_history = ChatMessageHistory()
|
165 |
+
|
166 |
+
# Memory for conversational context
|
167 |
+
memory = ConversationBufferMemory(
|
168 |
+
memory_key="chat_history",
|
169 |
+
output_key="answer",
|
170 |
+
chat_memory=message_history,
|
171 |
+
return_messages=True,
|
172 |
+
)
|
173 |
+
|
174 |
+
# Create a chain that uses the Chroma vector store
|
175 |
+
chain = ConversationalRetrievalChain.from_llm(
|
176 |
+
llm = llm_groq,
|
177 |
+
chain_type="stuff",
|
178 |
+
retriever=docsearch.as_retriever(),
|
179 |
+
memory=memory,
|
180 |
+
return_source_documents=True,
|
181 |
+
)
|
182 |
+
|
183 |
+
# Let the user know that the system is ready
|
184 |
+
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
185 |
+
await msg.update()
|
186 |
+
# Store the chain in user session
|
187 |
+
cl.user_session.set("chain", chain)
|
188 |
+
|
189 |
+
|
190 |
+
@cl.on_message
|
191 |
+
async def main(message: cl.Message):
|
192 |
+
|
193 |
+
# Retrieve the chain from user session
|
194 |
+
chain = cl.user_session.get("chain")
|
195 |
+
# Callbacks happen asynchronously/parallel
|
196 |
+
cb = cl.AsyncLangchainCallbackHandler()
|
197 |
+
|
198 |
+
# Call the chain with user's message content
|
199 |
+
res = await chain.ainvoke(message.content, callbacks=[cb])
|
200 |
+
answer = anonymizer.deanonymize(
|
201 |
+
"ok"+res["answer"]
|
202 |
+
)
|
203 |
+
text_elements = []
|
204 |
+
|
205 |
+
# Return results
|
206 |
+
await cl.Message(content=answer, elements=text_elements).send()
|
207 |
+
|