Spaces:
Sleeping
Sleeping
File size: 7,914 Bytes
c25f92a a5c102a c25f92a 2ba4984 c25f92a a5c102a 2ba4984 a5c102a c25f92a 2ba4984 c25f92a a5c102a c25f92a 2ba4984 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a e5abbe4 c25f92a 2ba4984 c25f92a 2ba4984 c25f92a a5c102a |
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 |
import os
import shutil
import json
import pandas as pd
import chainlit as cl
from dotenv import load_dotenv
from langchain_core.documents import Document
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_experimental.text_splitter import SemanticChunker
from langchain_community.vectorstores import Qdrant
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain.tools import tool
from langchain.schema import HumanMessage
from typing_extensions import List, TypedDict
from operator import itemgetter
# Load environment variables
load_dotenv()
# Define paths
UPLOAD_PATH = "upload/"
OUTPUT_PATH = "output/"
os.makedirs(UPLOAD_PATH, exist_ok=True)
os.makedirs(OUTPUT_PATH, exist_ok=True)
model_id = "Snowflake/snowflake-arctic-embed-m"
embedding_model = HuggingFaceEmbeddings(model_name=model_id)
semantic_splitter = SemanticChunker(embedding_model, add_start_index=True, buffer_size=30)
llm = ChatOpenAI(model="gpt-4o-mini")
# Export comparison prompt
export_prompt = export_prompt = """
CONTEXT:
{context}
QUERY:
{question}
You are a helpful assistant. Use the available context to answer the question.
Between these two files containing protocols, identify and match **entire assessment sections** based on conceptual similarity. Do NOT match individual questions.
### **Output Format:**
Return the response in **valid JSON format** structured as a list of dictionaries, where each dictionary contains:
[
{{
"Derived Description": "A short name for the matched concept",
"Protocol_1": "Protocol 1 - Matching Element",
"Protocol_2": "Protocol 2 - Matching Element"
}},
...
]
### **Example Output:**
[
{{
"Derived Description": "Pain Coping Strategies",
"Protocol_1": "Pain Coping Strategy Scale (PCSS-9)",
"Protocol_2": "Chronic Pain Adjustment Index (CPAI-10)"
}},
{{
"Derived Description": "Work Stress and Fatigue",
"Protocol_1": "Work-Related Stress Scale (WRSS-8)",
"Protocol_2": "Occupational Fatigue Index (OFI-7)"
}},
...
]
### Rules:
1. Only output **valid JSON** with no explanations, summaries, or markdown formatting.
2. Ensure each entry in the JSON list represents a single matched data element from the two protocols.
3. If no matching element is found in a protocol, leave it empty ("").
4. **Do NOT include headers, explanations, or additional formatting**—only return the raw JSON list.
5. It should include all the elements in the two protocols.
6. If it cannot match the element, create the row and include the protocol it did find and put "could not match" in the other protocol column.
7. protocol should be the between
"""
compare_export_prompt = ChatPromptTemplate.from_template(export_prompt)
QUERY_PROMPT = """
You are a helpful assistant. Use the available context to answer the question concisely and informatively.
CONTEXT:
{context}
QUERY:
{question}
Provide a natural-language response using the given information. If you do not know the answer, say so.
"""
query_prompt = ChatPromptTemplate.from_template(QUERY_PROMPT)
## tool configurations
@tool
def document_query_tool(question: str) -> str:
"""Retrieves relevant document sections and answers questions based on the uploaded documents."""
retriever = cl.user_session.get("qdrant_retriever")
if not retriever:
return "Error: No documents available for retrieval. Please upload two PDF files first."
retriever = retriever.with_config({"k": 10})
# Use a RAG chain similar to the comparison tool
rag_chain = (
{"context": itemgetter("question") | retriever, "question": itemgetter("question")}
| query_prompt | llm | StrOutputParser()
)
response_text = rag_chain.invoke({"question": question})
# Get the retrieved docs for context
retrieved_docs = retriever.invoke(question)
return {
"messages": [HumanMessage(content=response_text)],
"context": retrieved_docs
}
@tool
def document_comparison_tool(question: str) -> str:
"""Compares documents, identifies matched elements, exports them as JSON, formats into CSV, and provides a download link."""
# Retrieve the vector database retriever
retriever = cl.user_session.get("qdrant_retriever")
if not retriever:
return "Error: No documents available for retrieval. Please upload two PDF files first."
retriever = retriever.with_config({"k": 10})
# Process query using RAG
rag_chain = (
{"context": itemgetter("question") | retriever, "question": itemgetter("question")}
| compare_export_prompt | llm | StrOutputParser()
)
response_text = rag_chain.invoke({"question": question})
# Parse response and save as CSV
try:
structured_data = json.loads(response_text)
if not structured_data:
return "Error: No matched elements found."
# Define output file path
file_path = os.path.join(OUTPUT_PATH, "comparison_results.csv")
# Save to CSV
df = pd.DataFrame(structured_data, columns=["Derived Description", "Protocol_1", "Protocol_2"])
df.to_csv(file_path, index=False)
return file_path # Return path to the CSV file
except json.JSONDecodeError:
return "Error: Response is not valid JSON."
async def process_files(files: list[cl.File]):
documents_with_metadata = []
for file in files:
file_path = os.path.join(UPLOAD_PATH, file.name)
shutil.copyfile(file.path, file_path)
loader = PyMuPDFLoader(file_path)
documents = loader.load()
for doc in documents:
source_name = file.name
chunks = semantic_splitter.split_text(doc.page_content)
for chunk in chunks:
doc_chunk = Document(page_content=chunk, metadata={"source": source_name})
documents_with_metadata.append(doc_chunk)
if documents_with_metadata:
qdrant_vectorstore = Qdrant.from_documents(
documents_with_metadata,
embedding_model,
location=":memory:",
collection_name="document_comparison",
)
return qdrant_vectorstore.as_retriever()
return None
@cl.on_chat_start
async def start():
cl.user_session.set("qdrant_retriever", None)
files = await cl.AskFileMessage(
content="Please upload **two PDF files** for comparison:",
accept=["application/pdf"],
max_files=2
).send()
if len(files) != 2:
await cl.Message("Error: You must upload exactly two PDF files.").send()
return
retriever = await process_files(files)
if retriever:
cl.user_session.set("qdrant_retriever", retriever)
await cl.Message("Files uploaded and processed successfully! You can now enter your query.").send()
else:
await cl.Message("Error: Unable to process files. Please try again.").send()
@cl.on_message
async def handle_message(message: cl.Message):
user_input = message.content.lower()
# If the user asks for a comparison, run the document_comparison_tool
if "compare" in user_input or "export" in user_input:
file_path = document_comparison_tool.invoke(user_input)
if file_path and file_path.endswith(".csv"):
await cl.Message(
content="Comparison complete! Download the CSV below:",
elements=[cl.File(name="comparison_results.csv", path=file_path, display="inline")],
).send()
else:
await cl.Message(file_path).send()
else:
response_text = document_query_tool.invoke(user_input)
await cl.Message(response_text["messages"][0].content).send() |