Spaces:
Sleeping
Sleeping
File size: 6,425 Bytes
90f9b02 46b544f 90f9b02 e620b8b 90f9b02 | 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 | #Hugging Face Spaces deployment file for a Gradio-based Policy & Claims Agent.
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
import os
from pathlib import Path
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint, ChatHuggingFace
from langchain_community.vectorstores import Chroma
from langchain_classic.chains import create_retrieval_chain
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
import getpass
import gradio as gr
import os
PDF_PATH = Path("./Bank.pdf")
HF_KEY = os.getenv("HF_TOKEN", "")
os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_KEY
def load_pdf():
# drive.mount('/content/drive')
#base_path = Path('/content/drive/MyDrive/GenerativeAI')
#file_path = base_path /'Bank.pdf'
file_path = str(PDF_PATH)
loader = PyPDFLoader(file_path)
pages = loader.load()
print(f"PDF loaed and returing page number {len(pages)} \n")
return pages
def split_pages(pages):
text_splitter = RecursiveCharacterTextSplitter(chunk_size=700,
chunk_overlap=50
)
chunks = text_splitter.split_documents(pages)
print(f"Created {len(chunks)} chunks")
return chunks
def create_embedding():
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'}
)
return embeddings
def create_vector(chunks, embeddings):
vector_db = Chroma.from_documents(documents=chunks,
embedding= embeddings,
persist_directory="./hf_cloud_db"
)
print(f"Created vectore DB using huggig face\n")
return vector_db
def get_retriver(vector_db):
retriever = vector_db.as_retriever(search_type="similarity",
search_kwargs={"k": 5}
)
print(f"Retreiver created \n")
return retriever
def create_model():
llm = HuggingFaceEndpoint(
repo_id="deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
task="text-generation",
max_new_tokens=500,
temperature=0.2,
do_sample=False,
)
chat_model = ChatHuggingFace(llm=llm)
print(f"Model created")
return chat_model
def build_policy_prompt():
prompt = ChatPromptTemplate.from_template("""
You are a Policy & Claims agent.
Answer the user's question using ONLY the retrieved policy context.
Rules:
1. Do not guess.
2. Do not use outside knowledge.
3. If the answer is not found in the context, say:
"Answer not found in the provided policy document."
4. Keep the answer short and clear.
5. Mention source page number when available.
Question:
{input}
Context:
{context}
""")
return prompt
def build_claim_prompt():
prompt = ChatPromptTemplate.from_template("""
You are a Policy & Claims Copilot.
You are doing a claim pre-check, not final claim approval.
Use ONLY the retrieved policy context.
Rules:
1. Do not guess.
2. Do not use outside knowledge.
3. If evidence is missing, say:
"Unclear based on provided policy context."
4. Give output in this format:
Pre-check Result:
- Likely Covered / Likely Not Covered / Unclear
Reason:
- Short explanation from the policy context
Waiting Period / Limits:
- Mention only if found
Documents Needed:
- Mention only if found
Disclaimer:
- This is only a pre-check, not final claim approval.
User Claim Scenario:
{input}
Context:
{context}
""")
return prompt
def rag_chaining(retriever, chat_model, prompt):
document_chain = create_stuff_documents_chain(chat_model, prompt)
rag_chain = create_retrieval_chain(retriever, document_chain)
return rag_chain
def chat_function(message, history, mode):
if mode in ["Q&A", "Policy Q&A"]:
return ask_policy_question(message, APP["policy_chain"])
else:
return claim_precheck(message, APP["precheck_chain"])
def format_sources(docs):
if not docs:
return "No sources found."
lines = []
seen = set()
for doc in docs:
page = doc.metadata.get("page", "N/A")
source = doc.metadata.get("source", "N/A")
key = (source, page)
if key not in seen:
seen.add(key)
lines.append(f"- File: {source}, Page: {page}")
return "\n".join(lines)
def ask_policy_question(message, policy_chain):
result = policy_chain.invoke({"input": message})
answer = result.get("answer", "")
docs = result.get("context", [])
sources = format_sources(docs)
return f"""{answer}
Sources:
{sources}
"""
def claim_precheck(message, precheck_chain):
result = precheck_chain.invoke({"input": message})
answer = result.get("answer", "")
docs = result.get("context", [])
sources = format_sources(docs)
return f"""{answer}
Sources:
{sources}
"""
def launch_ui():
mode_input = gr.Radio(
choices=["Q&A", "Claim Pre-check"],
value="Q&A",
label="Mode"
)
demo = gr.ChatInterface(
fn=chat_function,
additional_inputs=[mode_input],
title="Policy & Claims Agent",
description=(
"Ask policy questions or run a basic claim pre-check using the uploaded PDF. "
"Responses are grounded in retrieved document chunks."
),
examples=[
["What is covered under hospitalization?", "Q&A"],
["What documents are needed to submit a claim?", "Q&A"],
["My policy started 4 months ago and I want to claim for a surgery. Is it likely covered?", "Claim Pre-check"],
],
cache_examples=False
)
demo.launch(debug=False, share=True)
def initialize_app():
pages = load_pdf()
chunks = split_pages(pages)
embeddings = create_embedding()
vector_db = create_vector(chunks, embeddings)
retriever = get_retriver(vector_db)
chat_model = create_model()
policy_prompt = build_policy_prompt()
precheck_prompt = build_claim_prompt()
policy_chain = rag_chaining(retriever, chat_model, policy_prompt)
precheck_chain = rag_chaining(retriever, chat_model, precheck_prompt)
return {
"policy_chain": policy_chain,
"precheck_chain": precheck_chain
}
APP = initialize_app()
demo = launch_ui()
if __name__ == "__main__":
demo.launch() |