Spaces:
Build error
Build error
Update rag_pipeline.py
Browse files- rag_pipeline.py +10 -11
rag_pipeline.py
CHANGED
|
@@ -6,15 +6,18 @@ from sentence_transformers import SentenceTransformer
|
|
| 6 |
import json
|
| 7 |
import re
|
| 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(
|
| 14 |
text = ""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
return text
|
| 19 |
|
| 20 |
def chunk_text(text, chunk_size=2000, overlap=200):
|
|
@@ -75,10 +78,10 @@ def extract_structured_data(pdf_file):
|
|
| 75 |
top_chunks = retrieve_relevant_chunks(query, chunks, embeddings, index)
|
| 76 |
prompt = generate_prompt("\n\n".join(top_chunks))
|
| 77 |
|
| 78 |
-
print(f"\nGenerated Prompt:\n{prompt[:500]}...\n") #
|
| 79 |
|
| 80 |
result = qa_model(prompt, max_new_tokens=300)[0]['generated_text']
|
| 81 |
-
print(f"Model Output:\n{result[:500]}...\n") #
|
| 82 |
|
| 83 |
try:
|
| 84 |
# Fail-safe: model returned just []
|
|
@@ -103,7 +106,3 @@ def extract_structured_data(pdf_file):
|
|
| 103 |
"raw_output": result,
|
| 104 |
"exception": str(e)
|
| 105 |
}
|
| 106 |
-
|
| 107 |
-
# Example usage:
|
| 108 |
-
extracted_data = extract_structured_data("/mnt/data/251052753219049.pdf")
|
| 109 |
-
print("\nFinal Extracted Data:\n", extracted_data)
|
|
|
|
| 6 |
import json
|
| 7 |
import re
|
| 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_input):
|
| 14 |
text = ""
|
| 15 |
+
try:
|
| 16 |
+
with pdfplumber.open(pdf_input) as pdf:
|
| 17 |
+
for page in pdf.pages:
|
| 18 |
+
text += page.extract_text() or ""
|
| 19 |
+
except Exception as e:
|
| 20 |
+
raise ValueError(f"Failed to open PDF: {e}")
|
| 21 |
return text
|
| 22 |
|
| 23 |
def chunk_text(text, chunk_size=2000, overlap=200):
|
|
|
|
| 78 |
top_chunks = retrieve_relevant_chunks(query, chunks, embeddings, index)
|
| 79 |
prompt = generate_prompt("\n\n".join(top_chunks))
|
| 80 |
|
| 81 |
+
print(f"\nGenerated Prompt:\n{prompt[:500]}...\n") # Debug
|
| 82 |
|
| 83 |
result = qa_model(prompt, max_new_tokens=300)[0]['generated_text']
|
| 84 |
+
print(f"Model Output:\n{result[:500]}...\n") # Debug
|
| 85 |
|
| 86 |
try:
|
| 87 |
# Fail-safe: model returned just []
|
|
|
|
| 106 |
"raw_output": result,
|
| 107 |
"exception": str(e)
|
| 108 |
}
|
|
|
|
|
|
|
|
|
|
|
|