vikaso001 commited on
Commit
f030b55
·
verified ·
1 Parent(s): 4811950

Update rag_pipeline.py

Browse files
Files changed (1) hide show
  1. rag_pipeline.py +13 -8
rag_pipeline.py CHANGED
@@ -8,7 +8,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-large", device=-1)
12
 
13
  def extract_text_from_pdf(pdf_file):
14
  text = ""
@@ -17,7 +17,7 @@ def extract_text_from_pdf(pdf_file):
17
  text += page.extract_text() or ""
18
  return text
19
 
20
- def chunk_text(text, chunk_size=500, overlap=100):
21
  chunks = []
22
  start = 0
23
  while start < len(text):
@@ -66,14 +66,19 @@ Only output valid JSON. Do not include any other text.
66
 
67
  def extract_structured_data(pdf_file):
68
  text = extract_text_from_pdf(pdf_file)
69
- chunks = chunk_text(text)
70
- embeddings = embed_chunks(chunks)
71
- index = build_faiss_index(embeddings)
72
 
73
- query = "Extract doctor name, patient name, date, and medications from prescription"
74
- top_chunks = retrieve_relevant_chunks(query, chunks, embeddings, index)
 
 
 
 
 
 
 
 
 
75
 
76
- prompt = generate_prompt("\n\n".join(top_chunks))
77
  result = qa_model(prompt, max_new_tokens=300)[0]['generated_text']
78
 
79
  try:
 
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
 
13
  def extract_text_from_pdf(pdf_file):
14
  text = ""
 
17
  text += page.extract_text() or ""
18
  return text
19
 
20
+ def chunk_text(text, chunk_size=1000, overlap=100):
21
  chunks = []
22
  start = 0
23
  while start < len(text):
 
66
 
67
  def extract_structured_data(pdf_file):
68
  text = extract_text_from_pdf(pdf_file)
 
 
 
69
 
70
+ # Optimization: Skip chunking/embedding if the text is short
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)
77
+
78
+ query = "Extract doctor name, patient name, date, and medications from prescription"
79
+ top_chunks = retrieve_relevant_chunks(query, chunks, embeddings, index)
80
+ prompt = generate_prompt("\n\n".join(top_chunks))
81
 
 
82
  result = qa_model(prompt, max_new_tokens=300)[0]['generated_text']
83
 
84
  try: