Spaces:
Runtime error
Runtime error
File size: 9,919 Bytes
44cbe88 bb07d5b 44cbe88 |
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 |
#Import Library
from unstructured.partition.pdf import partition_pdf
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough,RunnableLambda
from langchain_postgres.vectorstores import PGVector
from database import COLLECTION_NAME, CONNECTION_STRING
from langchain_community.storage import RedisStore
from langchain.schema.document import Document
from langchain_openai import OpenAIEmbeddings
from langchain.retrievers.multi_vector import MultiVectorRetriever
from pathlib import Path
from IPython.display import display, HTML
from base64 import b64decode
import os, hashlib, shutil, uuid, json, time
import torch, redis, streamlit as st
import logging
import openai
# from dotenv import load_dotenv
# load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
# Ensure PyTorch module path is correctly set
torch.classes.__path__ = [os.path.join(torch.__path__[0], torch.classes.__file__)]
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize Redis client
# client = redis.Redis(host="localhost", port=6379, db=0)
redis_host = os.getenv("REDIS_HOST", "redis-stack")
redis_port = int(os.getenv("REDIS_PORT", 6379))
client = redis.Redis(host=redis_host, port=redis_port, db=0)
#Data Loading
def load_pdf_data(file_path):
logging.info(f"Data ready to be partitioned and loaded ")
raw_pdf_elements = partition_pdf(
filename=file_path,
infer_table_structure=True,
strategy = "hi_res",
extract_image_block_types = ["Image"],
extract_image_block_to_payload = True,
chunking_strategy="by_title",
mode='elements',
max_characters=10000,
new_after_n_chars=5000,
combine_text_under_n_chars=2000,
image_output_dir_path="data/",
)
logging.info(f"Pdf data finish loading, chunks now available!")
return raw_pdf_elements
# Generate a unique hash for a PDF file
def get_pdf_hash(pdf_path):
"""Generate a SHA-256 hash of the PDF file content."""
with open(pdf_path, "rb") as f:
pdf_bytes = f.read()
return hashlib.sha256(pdf_bytes).hexdigest()
# Summarize extracted text and tables using LLM
def summarize_text_and_tables(text, tables):
logging.info("Ready to summarize data with LLM")
prompt_text = """You are an assistant tasked with summarizing text and tables. \
You are to give a concise summary of the table or text and do nothing else.
Table or text chunk: {element} """
prompt = ChatPromptTemplate.from_template(prompt_text)
model = ChatOpenAI(temperature=0.6, model="gpt-4o-mini", openai_api_key=openai_api_key)
summarize_chain = {"element": RunnablePassthrough()}| prompt | model | StrOutputParser()
logging.info(f"{model} done with summarization")
return {
"text": summarize_chain.batch(text, {"max_concurrency": 5}),
"table": summarize_chain.batch(tables, {"max_concurrency": 5})
}
#Initialize a pgvector and retriever for storing and searching documents
def initialize_retriever():
store = RedisStore(client=client)
id_key = "doc_id"
vectorstore = PGVector(
embeddings=OpenAIEmbeddings(),
collection_name=COLLECTION_NAME,
connection=CONNECTION_STRING,
use_jsonb=True,
)
retrieval_loader = MultiVectorRetriever(vectorstore=vectorstore, docstore=store, id_key="doc_id")
return retrieval_loader
# Store text, tables, and their summaries in the retriever
def store_docs_in_retriever(text, text_summary, table, table_summary, retriever):
"""Store text and table documents along with their summaries in the retriever."""
def add_documents_to_retriever(documents, summaries, retriever, id_key = "doc_id"):
"""Helper function to add documents and their summaries to the retriever."""
if not summaries:
return None, []
doc_ids = [str(uuid.uuid4()) for _ in documents]
summary_docs = [
Document(page_content=summary, metadata={id_key: doc_ids[i]})
for i, summary in enumerate(summaries)
]
retriever.vectorstore.add_documents(summary_docs, ids=doc_ids)
retriever.docstore.mset(list(zip(doc_ids, documents)))
# Add text, table, and image summaries to the retriever
add_documents_to_retriever(text, text_summary, retriever)
add_documents_to_retriever(table, table_summary, retriever)
return retriever
# Parse the retriever output
def parse_retriver_output(data):
parsed_elements = []
for element in data:
# Decode bytes to string if necessary
if isinstance(element, bytes):
element = element.decode("utf-8")
parsed_elements.append(element)
return parsed_elements
# Chat with the LLM using retrieved context
def chat_with_llm(retriever):
logging.info(f"Context ready to send to LLM ")
prompt_text = """
You are an AI Assistant tasked with understanding detailed
information from text and tables. You are to answer the question based on the
context provided to you. You must not go beyond the context given to you.
Context:
{context}
Question:
{question}
"""
prompt = ChatPromptTemplate.from_template(prompt_text)
model = ChatOpenAI(temperature=0.6, model="gpt-4o-mini", openai_api_key=openai_api_key)
rag_chain = ({
"context": retriever | RunnableLambda(parse_retriver_output), "question": RunnablePassthrough(),
}
| prompt
| model
| StrOutputParser()
)
logging.info(f"Completed! ")
return rag_chain
# Generate temporary file path of uploaded docs
def _get_file_path(file_upload):
temp_dir = "temp"
os.makedirs(temp_dir, exist_ok=True) # Ensure the directory exists
if isinstance(file_upload, str):
file_path = file_upload # Already a string path
else:
file_path = os.path.join(temp_dir, file_upload.name)
with open(file_path, "wb") as f:
f.write(file_upload.getbuffer())
return file_path
# Process uploaded PDF file
def process_pdf(file_upload):
print('Processing PDF hash info...')
file_path = _get_file_path(file_upload)
pdf_hash = get_pdf_hash(file_path)
load_retriever = initialize_retriever()
existing = client.exists(f"pdf:{pdf_hash}")
print(f"Checking Redis for hash {pdf_hash}: {'Exists' if existing else 'Not found'}")
if existing:
print(f"PDF already exists with hash {pdf_hash}. Skipping upload.")
return load_retriever
print(f"New PDF detected. Processing... {pdf_hash}")
pdf_elements = load_pdf_data(file_path)
tables = [element.metadata.text_as_html for element in
pdf_elements if 'Table' in str(type(element))]
text = [element.text for element in pdf_elements if
'CompositeElement' in str(type(element))]
summaries = summarize_text_and_tables(text, tables)
retriever = store_docs_in_retriever(text, summaries['text'], tables, summaries['table'], load_retriever)
# Store the PDF hash in Redis
client.set(f"pdf:{pdf_hash}", json.dumps({"text": "PDF processed"}))
# Debug: Check if Redis stored the key
stored = client.exists(f"pdf:{pdf_hash}")
# #remove temp directory
# shutil.rmtree("dir")
print(f"Stored PDF hash in Redis: {'Success' if stored else 'Failed'}")
return retriever
#Invoke chat with LLM based on uploaded PDF and user query
def invoke_chat(file_upload, message):
retriever =process_pdf(file_upload)
rag_chain = chat_with_llm(retriever)
response = rag_chain.invoke(message)
response_placeholder = st.empty()
response_placeholder.write(response)
return response
# Main application interface using Streamlit
def main():
st.title("PDF Chat Assistant ")
logging.info("App started")
if 'messages' not in st.session_state:
st.session_state.messages = []
file_upload = st.sidebar.file_uploader(
label="Upload", type=["pdf"],
accept_multiple_files=False,
key="pdf_uploader"
)
if file_upload:
st.success("File uploaded successfully! You can now ask your question.")
# Prompt for user input
if prompt := st.chat_input("Your question"):
st.session_state.messages.append({"role": "user", "content": prompt})
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
# Generate response if last message is not from assistant
if st.session_state.messages and st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
start_time = time.time()
logging.info("Generating response...")
with st.spinner("Processing..."):
user_message = " ".join([msg["content"] for msg in st.session_state.messages if msg])
response_message = invoke_chat(file_upload, user_message)
duration = time.time() - start_time
response_msg_with_duration = f"{response_message}\n\nDuration: {duration:.2f} seconds"
st.session_state.messages.append({"role": "assistant", "content": response_msg_with_duration})
st.write(f"Duration: {duration:.2f} seconds")
logging.info(f"Response: {response_message}, Duration: {duration:.2f} s")
if __name__ == "__main__":
main() |