Spaces:
Build error
Build error
Create rag_pipeline.py
Browse files- rag_pipeline.py +72 -0
rag_pipeline.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
import faiss
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# Load models
|
| 9 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 10 |
+
qa_model = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
| 11 |
+
|
| 12 |
+
def extract_text_from_pdf(pdf_file):
|
| 13 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 14 |
+
text = ""
|
| 15 |
+
for page in doc:
|
| 16 |
+
text += page.get_text()
|
| 17 |
+
return text
|
| 18 |
+
|
| 19 |
+
def chunk_text(text, chunk_size=500, overlap=100):
|
| 20 |
+
chunks = []
|
| 21 |
+
start = 0
|
| 22 |
+
while start < len(text):
|
| 23 |
+
end = start + chunk_size
|
| 24 |
+
chunks.append(text[start:end])
|
| 25 |
+
start += chunk_size - overlap
|
| 26 |
+
return chunks
|
| 27 |
+
|
| 28 |
+
def embed_chunks(chunks):
|
| 29 |
+
return embedder.encode(chunks)
|
| 30 |
+
|
| 31 |
+
def build_faiss_index(embeddings):
|
| 32 |
+
dim = embeddings[0].shape[0]
|
| 33 |
+
index = faiss.IndexFlatL2(dim)
|
| 34 |
+
index.add(embeddings)
|
| 35 |
+
return index
|
| 36 |
+
|
| 37 |
+
def retrieve_relevant_chunks(query, chunks, embeddings, index, top_k=3):
|
| 38 |
+
query_vec = embedder.encode([query])
|
| 39 |
+
distances, indices = index.search(query_vec, top_k)
|
| 40 |
+
return [chunks[i] for i in indices[0]]
|
| 41 |
+
|
| 42 |
+
def generate_prompt(context):
|
| 43 |
+
return f"""
|
| 44 |
+
Given the following medical prescription text:
|
| 45 |
+
|
| 46 |
+
\"\"\"{context}\"\"\"
|
| 47 |
+
|
| 48 |
+
Extract the following details in JSON format:
|
| 49 |
+
- Doctor Name
|
| 50 |
+
- Patient Name
|
| 51 |
+
- Date
|
| 52 |
+
- Medications (list of name + dosage if available)
|
| 53 |
+
|
| 54 |
+
Only return JSON. Do not explain.
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
def extract_structured_data(pdf_file):
|
| 58 |
+
text = extract_text_from_pdf(pdf_file)
|
| 59 |
+
chunks = chunk_text(text)
|
| 60 |
+
embeddings = embed_chunks(chunks)
|
| 61 |
+
index = build_faiss_index(embeddings)
|
| 62 |
+
|
| 63 |
+
query = "Extract doctor name, patient name, date, and medications from prescription"
|
| 64 |
+
top_chunks = retrieve_relevant_chunks(query, chunks, embeddings, index)
|
| 65 |
+
|
| 66 |
+
prompt = generate_prompt("\n\n".join(top_chunks))
|
| 67 |
+
result = qa_model(prompt, max_new_tokens=300)[0]['generated_text']
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
return eval(result)
|
| 71 |
+
except:
|
| 72 |
+
return {"error": "Failed to parse model response", "raw_output": result}
|