Spaces:
Build error
Build error
Update rag_pipeline.py
Browse files- rag_pipeline.py +19 -14
rag_pipeline.py
CHANGED
|
@@ -3,10 +3,10 @@ import faiss
|
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
-
from io import BytesIO
|
| 7 |
import json
|
|
|
|
| 8 |
|
| 9 |
-
# Load models
|
| 10 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 11 |
qa_model = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
| 12 |
|
|
@@ -17,7 +17,8 @@ def extract_text_from_pdf(pdf_file):
|
|
| 17 |
text += page.extract_text() or ""
|
| 18 |
return text
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
chunks = []
|
| 22 |
start = 0
|
| 23 |
while start < len(text):
|
|
@@ -50,13 +51,13 @@ Prescription text:
|
|
| 50 |
Return only a valid JSON object in the following format:
|
| 51 |
|
| 52 |
{{
|
| 53 |
-
"Doctor Name": "Dr. John Smith",
|
| 54 |
-
"Patient Name": "Jane Doe",
|
| 55 |
-
"Date": "YYYY-MM-DD",
|
| 56 |
-
"Medications": [
|
| 57 |
{{
|
| 58 |
-
"name": "Medication Name",
|
| 59 |
-
"dosage": "Dosage Info"
|
| 60 |
}}
|
| 61 |
]
|
| 62 |
}}
|
|
@@ -67,10 +68,11 @@ Strictly output only valid JSON. Do not include any quotes, markdown, or extra t
|
|
| 67 |
def extract_structured_data(pdf_file):
|
| 68 |
text = extract_text_from_pdf(pdf_file)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
if len(text) < 2000:
|
| 72 |
prompt = generate_prompt(text)
|
| 73 |
else:
|
|
|
|
| 74 |
chunks = chunk_text(text)
|
| 75 |
embeddings = embed_chunks(chunks)
|
| 76 |
index = build_faiss_index(embeddings)
|
|
@@ -82,9 +84,12 @@ def extract_structured_data(pdf_file):
|
|
| 82 |
result = qa_model(prompt, max_new_tokens=300)[0]['generated_text']
|
| 83 |
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
except Exception as e:
|
| 90 |
return {"error": "Failed to parse model response", "raw_output": result, "exception": str(e)}
|
|
|
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
| 5 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 6 |
import json
|
| 7 |
+
import re
|
| 8 |
|
| 9 |
+
# Load models (switch to distilflan-t5-small for speed improvement)
|
| 10 |
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 11 |
qa_model = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
| 12 |
|
|
|
|
| 17 |
text += page.extract_text() or ""
|
| 18 |
return text
|
| 19 |
|
| 20 |
+
# Optimization: Increase chunk size to reduce the number of chunks
|
| 21 |
+
def chunk_text(text, chunk_size=2000, overlap=200):
|
| 22 |
chunks = []
|
| 23 |
start = 0
|
| 24 |
while start < len(text):
|
|
|
|
| 51 |
Return only a valid JSON object in the following format:
|
| 52 |
|
| 53 |
{{
|
| 54 |
+
\"Doctor Name\": \"Dr. John Smith\",
|
| 55 |
+
\"Patient Name\": \"Jane Doe\",
|
| 56 |
+
\"Date\": \"YYYY-MM-DD\",
|
| 57 |
+
\"Medications\": [
|
| 58 |
{{
|
| 59 |
+
\"name\": \"Medication Name\",
|
| 60 |
+
\"dosage\": \"Dosage Info\"
|
| 61 |
}}
|
| 62 |
]
|
| 63 |
}}
|
|
|
|
| 68 |
def extract_structured_data(pdf_file):
|
| 69 |
text = extract_text_from_pdf(pdf_file)
|
| 70 |
|
| 71 |
+
# Skip chunking/embedding if the text is small
|
| 72 |
if len(text) < 2000:
|
| 73 |
prompt = generate_prompt(text)
|
| 74 |
else:
|
| 75 |
+
# Skip FAISS for small documents, do direct extraction
|
| 76 |
chunks = chunk_text(text)
|
| 77 |
embeddings = embed_chunks(chunks)
|
| 78 |
index = build_faiss_index(embeddings)
|
|
|
|
| 84 |
result = qa_model(prompt, max_new_tokens=300)[0]['generated_text']
|
| 85 |
|
| 86 |
try:
|
| 87 |
+
# Extract valid JSON
|
| 88 |
+
json_match = re.search(r'\\{.*\\}', result, re.DOTALL)
|
| 89 |
+
if json_match:
|
| 90 |
+
cleaned_json = json_match.group(0)
|
| 91 |
+
return json.loads(cleaned_json)
|
| 92 |
+
else:
|
| 93 |
+
raise ValueError("No valid JSON found.")
|
| 94 |
except Exception as e:
|
| 95 |
return {"error": "Failed to parse model response", "raw_output": result, "exception": str(e)}
|