Spaces:
Runtime error
Runtime error
File size: 12,760 Bytes
6e0b1c1 31bc246 6e0b1c1 31bc246 6e0b1c1 31bc246 6e0b1c1 482ac25 31bc246 6e0b1c1 31bc246 6e0b1c1 31bc246 6e0b1c1 31bc246 482ac25 |
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# import re
# import PyPDF2
# from langchain_community.embeddings import OllamaEmbeddings
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain_community.vectorstores import Chroma
# from langchain.chains import ConversationalRetrievalChain
# from langchain_community.chat_models import ChatOllama
# from langchain_groq import ChatGroq
# from langchain.memory import ChatMessageHistory, ConversationBufferMemory
# import chainlit as cl
# from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
# import logging
# import pypandoc
# import pdfkit
# from paddleocr import PaddleOCR
# import fitz
# import asyncio
# from langchain_nomic.embeddings import NomicEmbeddings
# llm_groq = ChatGroq(
# model_name='llama3-70b-8192'
# )
# # Initialize anonymizer
# anonymizer = PresidioReversibleAnonymizer(analyzed_fields=['PERSON', 'EMAIL_ADDRESS', 'PHONE_NUMBER', 'IBAN_CODE', 'CREDIT_CARD', 'CRYPTO', 'IP_ADDRESS', 'LOCATION', 'DATE_TIME', 'NRP', 'MEDICAL_LICENSE', 'URL'], faker_seed=18)
# def extract_text_from_pdf(file_path):
# pdf = PyPDF2.PdfReader(file_path)
# pdf_text = ""
# for page in pdf.pages:
# pdf_text += page.extract_text()
# return pdf_text
# def has_sufficient_selectable_text(page, threshold=50):
# text = page.extract_text()
# if len(text.strip()) > threshold:
# return True
# return False
# async def get_text(file_path):
# text = ""
# try:
# logging.info("Starting OCR process for file: %s", file_path)
# extension = file_path.split(".")[-1].lower()
# allowed_extension = ["jpg", "jpeg", "png", "pdf", "docx"]
# if extension not in allowed_extension:
# error = "Not a valid File. Allowed Format are jpg, jpeg, png, pdf, docx"
# logging.error(error)
# return {"error": error}
# if extension == "docx":
# file_path = convert_docx_to_pdf(file_path)
# ocr = PaddleOCR(use_angle_cls=True, lang='en')
# result = ocr.ocr(file_path, cls=True)
# for idx in range(len(result)):
# res = result[idx]
# for line in res:
# text += line[1][0] + " "
# logging.info("OCR process completed successfully for file: %s", file_path)
# except Exception as e:
# logging.error("Error occurred during OCR process for file %s: %s", file_path, e)
# text = "Error occurred during OCR process."
# logging.info("Extracted text: %s", text)
# return text
# def convert_docx_to_pdf(input_path):
# html_path = input_path.replace('.docx', '.html')
# output_path = ".".join(input_path.split(".")[:-1]) + ".pdf"
# pypandoc.convert_file(input_path, 'html', outputfile=html_path)
# pdfkit.from_file(html_path, output_path)
# logging.info("DOCX Format Handled")
# return output_path
# async def extract_text_from_mixed_pdf(file_path):
# pdf = PyPDF2.PdfReader(file_path)
# ocr = PaddleOCR(use_angle_cls=True, lang='en')
# pdf_text = ""
# for i, page in enumerate(pdf.pages):
# text = page.extract_text()
# if not has_sufficient_selectable_text(page):
# logging.info(f"Page {i+1} has insufficient selectable text, performing OCR.")
# pdf_document = fitz.open(file_path)
# pdf_page = pdf_document.load_page(i)
# pix = pdf_page.get_pixmap()
# image_path = f"page_{i+1}.png"
# pix.save(image_path)
# result = ocr.ocr(image_path, cls=True)
# for idx in range(len(result)):
# res = result[idx]
# for line in res:
# text += line[1][0] + " "
# pdf_text += text
# return pdf_text
# @cl.on_chat_start
# async def on_chat_start():
# files = None # Initialize variable to store uploaded files
# # Wait for the user to upload a file
# while files is None:
# files = await cl.AskFileMessage(
# content="Please upload a pdf file to begin!",
# # accept=["application/pdf"],
# accept=["application/pdf", "image/jpeg", "image/png", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
# max_size_mb=100,
# timeout=180,
# ).send()
# file = files[0] # Get the first uploaded file
# # Inform the user that processing has started
# msg = cl.Message(content=f"Processing `{file.name}`...")
# await msg.send()
# # Extract text from PDF, checking for selectable and handwritten text
# if file.name.endswith('.pdf'):
# pdf_text = await extract_text_from_mixed_pdf(file.path)
# else:
# pdf_text = await get_text(file.path)
# # Anonymize the text
# anonymized_text = anonymizer.anonymize(
# pdf_text
# )
# embeddings = NomicEmbeddings(model="nomic-embed-text-v1.5")
# docsearch = await cl.make_async(Chroma.from_texts)(
# [anonymized_text], embeddings, metadatas=[{"source": "0-pl"}]
# )
# # }
# # Initialize message history for conversation
# message_history = ChatMessageHistory()
# # Memory for conversational context
# memory = ConversationBufferMemory(
# memory_key="chat_history",
# output_key="answer",
# chat_memory=message_history,
# return_messages=True,
# )
# # Create a chain that uses the Chroma vector store
# chain = ConversationalRetrievalChain.from_llm(
# llm = llm_groq,
# chain_type="stuff",
# retriever=docsearch.as_retriever(),
# memory=memory,
# return_source_documents=True,
# )
# # Let the user know that the system is ready
# msg.content = f"Processing `{file.name}` done. You can now ask questions!"
# await msg.update()
# # Store the chain in user session
# cl.user_session.set("chain", chain)
# @cl.on_message
# async def main(message: cl.Message):
# # Retrieve the chain from user session
# chain = cl.user_session.get("chain")
# # Callbacks happen asynchronously/parallel
# cb = cl.AsyncLangchainCallbackHandler()
# # Call the chain with user's message content
# res = await chain.ainvoke(message.content, callbacks=[cb])
# answer = anonymizer.deanonymize(
# res["answer"]
# )
# text_elements = []
# # Return results
# await cl.Message(content=answer, elements=text_elements).send()
# v2:
import re
import PyPDF2
from langchain_community.embeddings import OllamaEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain.chains import ConversationalRetrievalChain
from langchain_community.chat_models import ChatOllama
from langchain_groq import ChatGroq
from langchain.memory import ChatMessageHistory, ConversationBufferMemory
import chainlit as cl
from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer
import logging
import pypandoc
import pdfkit
from paddleocr import PaddleOCR
import fitz
import asyncio
from langchain_nomic.embeddings import NomicEmbeddings
llm_groq = ChatGroq(
model_name='llama3-70b-8192'
)
# Initialize anonymizer
anonymizer = PresidioReversibleAnonymizer(
analyzed_fields=['PERSON', 'EMAIL_ADDRESS', 'PHONE_NUMBER', 'IBAN_CODE', 'CREDIT_CARD', 'CRYPTO', 'IP_ADDRESS', 'LOCATION', 'DATE_TIME', 'NRP', 'MEDICAL_LICENSE', 'URL'],
faker_seed=18
)
def extract_text_from_pdf(file_path):
pdf = PyPDF2.PdfReader(file_path)
pdf_text = ""
for page in pdf.pages:
pdf_text += page.extract_text()
return pdf_text
def has_sufficient_selectable_text(page, threshold=50):
text = page.extract_text()
if len(text.strip()) > threshold:
return True
return False
async def get_text(file_path):
text = ""
try:
logging.info("Starting OCR process for file: %s", file_path)
extension = file_path.split(".")[-1].lower()
allowed_extension = ["jpg", "jpeg", "png", "pdf", "docx"]
if extension not in allowed_extension:
error = "Not a valid File. Allowed Format are jpg, jpeg, png, pdf, docx"
logging.error(error)
return {"error": error}
if extension == "docx":
file_path = convert_docx_to_pdf(file_path)
ocr = PaddleOCR(use_angle_cls=True, lang='en')
result = ocr.ocr(file_path, cls=True)
for idx in range(len(result)):
res = result[idx]
for line in res:
text += line[1][0] + " "
logging.info("OCR process completed successfully for file: %s", file_path)
except Exception as e:
logging.error("Error occurred during OCR process for file %s: %s", file_path, e)
text = "Error occurred during OCR process."
logging.info("Extracted text: %s", text)
return text
def convert_docx_to_pdf(input_path):
html_path = input_path.replace('.docx', '.html')
output_path = ".".join(input_path.split(".")[:-1]) + ".pdf"
pypandoc.convert_file(input_path, 'html', outputfile=html_path)
pdfkit.from_file(html_path, output_path)
logging.info("DOCX Format Handled")
return output_path
async def extract_text_from_mixed_pdf(file_path):
pdf = PyPDF2.PdfReader(file_path)
ocr = PaddleOCR(use_angle_cls=True, lang='en')
pdf_text = ""
for i, page in enumerate(pdf.pages):
text = page.extract_text()
if not has_sufficient_selectable_text(page):
logging.info(f"Page {i+1} has insufficient selectable text, performing OCR.")
pdf_document = fitz.open(file_path)
pdf_page = pdf_document.load_page(i)
pix = pdf_page.get_pixmap()
image_path = f"page_{i+1}.png"
pix.save(image_path)
result = ocr.ocr(image_path, cls=True)
for idx in range(len(result)):
res = result[idx]
for line in res:
text += line[1][0] + " "
pdf_text += text
return pdf_text
@cl.on_chat_start
async def on_chat_start():
files = None # Initialize variable to store uploaded files
# Wait for the user to upload a file
while files is None:
files = await cl.AskFileMessage(
content="Please upload a pdf file to begin!",
accept=["application/pdf", "image/jpeg", "image/png", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
max_size_mb=100,
timeout=180,
).send()
file = files[0] # Get the first uploaded file
# Inform the user that processing has started
msg = cl.Message(content=f"Processing `{file.name}`...")
await msg.send()
# Extract text from PDF, checking for selectable and handwritten text
if file.name.endswith('.pdf'):
pdf_text = await extract_text_from_mixed_pdf(file.path)
else:
pdf_text = await get_text(file.path)
# Anonymize the text
anonymized_text = anonymizer.anonymize(
pdf_text
)
embeddings = NomicEmbeddings(model="nomic-embed-text-v1.5")
docsearch = await cl.make_async(Chroma.from_texts)(
[anonymized_text], embeddings, metadatas=[{"source": "0-pl"}]
)
# Initialize message history for conversation
message_history = ChatMessageHistory()
# Memory for conversational context
memory = ConversationBufferMemory(
memory_key="chat_history",
output_key="answer",
chat_memory=message_history,
return_messages=True,
)
# Create a chain that uses the Chroma vector store
chain = ConversationalRetrievalChain.from_llm(
llm = llm_groq,
chain_type="stuff",
retriever=docsearch.as_retriever(),
memory=memory,
return_source_documents=True,
)
# Let the user know that the system is ready
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
await msg.update()
# Store the chain in user session
cl.user_session.set("chain", chain)
@cl.on_message
async def main(message: cl.Message):
# Retrieve the chain from user session
chain = cl.user_session.get("chain")
# Callbacks happen asynchronously/parallel
cb = cl.AsyncLangchainCallbackHandler()
# Call the chain with user's message content
res = await chain.ainvoke(message.content, callbacks=[cb])
answer = anonymizer.deanonymize(
res["answer"]
)
text_elements = []
# Return results
await cl.Message(content=answer, elements=text_elements).send()
|