Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,22 @@ import PyPDF2
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
import numpy as np
|
| 5 |
from transformers import pipeline
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
# Initialize models
|
| 9 |
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Global storage for document chunks
|
| 13 |
document_store = {
|
| 14 |
'chunks': [],
|
| 15 |
-
'embeddings': None
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
def extract_text_from_pdf(file):
|
|
@@ -21,7 +27,7 @@ def extract_text_from_pdf(file):
|
|
| 21 |
pdf_reader = PyPDF2.PdfReader(file)
|
| 22 |
text = ""
|
| 23 |
for page in pdf_reader.pages:
|
| 24 |
-
text += page.extract_text()
|
| 25 |
return text
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error reading PDF: {str(e)}"
|
|
@@ -36,13 +42,14 @@ def extract_text_from_txt(file):
|
|
| 36 |
except Exception as e:
|
| 37 |
return f"Error reading TXT: {str(e)}"
|
| 38 |
|
| 39 |
-
def chunk_text(text, chunk_size=
|
| 40 |
-
"""Split text into chunks"""
|
| 41 |
words = text.split()
|
| 42 |
chunks = []
|
| 43 |
-
for i in range(0, len(words), chunk_size):
|
| 44 |
chunk = ' '.join(words[i:i + chunk_size])
|
| 45 |
-
|
|
|
|
| 46 |
return chunks
|
| 47 |
|
| 48 |
def upload_document(file):
|
|
@@ -61,27 +68,28 @@ def upload_document(file):
|
|
| 61 |
if text.startswith("Error"):
|
| 62 |
return text
|
| 63 |
|
| 64 |
-
# Chunk the text
|
| 65 |
-
chunks = chunk_text(text)
|
| 66 |
|
| 67 |
if not chunks:
|
| 68 |
return "No text extracted from document."
|
| 69 |
|
| 70 |
# Create embeddings
|
| 71 |
-
embeddings = embedder.encode(chunks)
|
| 72 |
|
| 73 |
# Store in global storage
|
| 74 |
document_store['chunks'] = chunks
|
| 75 |
document_store['embeddings'] = embeddings
|
|
|
|
| 76 |
|
| 77 |
-
return f"β Document uploaded successfully! Processed {len(chunks)} chunks."
|
| 78 |
|
| 79 |
-
def find_relevant_chunks(query, top_k=
|
| 80 |
"""Find most relevant chunks for the query"""
|
| 81 |
if not document_store['chunks']:
|
| 82 |
return []
|
| 83 |
|
| 84 |
-
query_embedding = embedder.encode([query])
|
| 85 |
similarities = np.dot(document_store['embeddings'], query_embedding.T).flatten()
|
| 86 |
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 87 |
|
|
@@ -91,52 +99,87 @@ def find_relevant_chunks(query, top_k=3):
|
|
| 91 |
def answer_query(query):
|
| 92 |
"""Answer query based on uploaded document"""
|
| 93 |
if not document_store['chunks']:
|
| 94 |
-
return "Please upload a document first."
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# Find relevant chunks
|
| 97 |
-
relevant_chunks = find_relevant_chunks(query, top_k=
|
| 98 |
context = "\n\n".join(relevant_chunks)
|
| 99 |
|
| 100 |
-
# Create
|
| 101 |
-
prompt = f"""
|
| 102 |
|
| 103 |
-
Context:
|
|
|
|
| 104 |
|
| 105 |
Question: {query}
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# Generate answer
|
| 110 |
-
result = qa_pipeline(prompt, max_length=200, do_sample=False)
|
| 111 |
-
raw_answer = result[0]['generated_text']
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# Create Gradio interface
|
| 126 |
-
with gr.Blocks(title="Simple RAG Application") as demo:
|
| 127 |
gr.Markdown("# π Simple RAG Application")
|
| 128 |
gr.Markdown("Upload a document (PDF or TXT) and ask questions about its content.")
|
| 129 |
|
| 130 |
with gr.Row():
|
| 131 |
-
with gr.Column():
|
| 132 |
-
file_input = gr.File(
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
with gr.Column():
|
| 137 |
-
query_input = gr.Textbox(
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
upload_btn.click(fn=upload_document, inputs=file_input, outputs=upload_status)
|
| 142 |
query_btn.click(fn=answer_query, inputs=query_input, outputs=answer_output)
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
import numpy as np
|
| 5 |
from transformers import pipeline
|
| 6 |
+
import torch
|
| 7 |
|
| 8 |
+
# Initialize models - using a better model for Q&A
|
| 9 |
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
+
# Using a better model for question answering
|
| 11 |
+
qa_pipeline = pipeline(
|
| 12 |
+
"text2text-generation",
|
| 13 |
+
model="google/flan-t5-large",
|
| 14 |
+
device=0 if torch.cuda.is_available() else -1
|
| 15 |
+
)
|
| 16 |
|
| 17 |
# Global storage for document chunks
|
| 18 |
document_store = {
|
| 19 |
'chunks': [],
|
| 20 |
+
'embeddings': None,
|
| 21 |
+
'full_text': ''
|
| 22 |
}
|
| 23 |
|
| 24 |
def extract_text_from_pdf(file):
|
|
|
|
| 27 |
pdf_reader = PyPDF2.PdfReader(file)
|
| 28 |
text = ""
|
| 29 |
for page in pdf_reader.pages:
|
| 30 |
+
text += page.extract_text() + "\n"
|
| 31 |
return text
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error reading PDF: {str(e)}"
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error reading TXT: {str(e)}"
|
| 44 |
|
| 45 |
+
def chunk_text(text, chunk_size=400, overlap=50):
|
| 46 |
+
"""Split text into overlapping chunks"""
|
| 47 |
words = text.split()
|
| 48 |
chunks = []
|
| 49 |
+
for i in range(0, len(words), chunk_size - overlap):
|
| 50 |
chunk = ' '.join(words[i:i + chunk_size])
|
| 51 |
+
if chunk.strip():
|
| 52 |
+
chunks.append(chunk)
|
| 53 |
return chunks
|
| 54 |
|
| 55 |
def upload_document(file):
|
|
|
|
| 68 |
if text.startswith("Error"):
|
| 69 |
return text
|
| 70 |
|
| 71 |
+
# Chunk the text with overlap
|
| 72 |
+
chunks = chunk_text(text, chunk_size=400, overlap=50)
|
| 73 |
|
| 74 |
if not chunks:
|
| 75 |
return "No text extracted from document."
|
| 76 |
|
| 77 |
# Create embeddings
|
| 78 |
+
embeddings = embedder.encode(chunks, show_progress_bar=False)
|
| 79 |
|
| 80 |
# Store in global storage
|
| 81 |
document_store['chunks'] = chunks
|
| 82 |
document_store['embeddings'] = embeddings
|
| 83 |
+
document_store['full_text'] = text
|
| 84 |
|
| 85 |
+
return f"β Document uploaded successfully! Processed {len(chunks)} chunks from {file.name}"
|
| 86 |
|
| 87 |
+
def find_relevant_chunks(query, top_k=5):
|
| 88 |
"""Find most relevant chunks for the query"""
|
| 89 |
if not document_store['chunks']:
|
| 90 |
return []
|
| 91 |
|
| 92 |
+
query_embedding = embedder.encode([query], show_progress_bar=False)
|
| 93 |
similarities = np.dot(document_store['embeddings'], query_embedding.T).flatten()
|
| 94 |
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 95 |
|
|
|
|
| 99 |
def answer_query(query):
|
| 100 |
"""Answer query based on uploaded document"""
|
| 101 |
if not document_store['chunks']:
|
| 102 |
+
return "β οΈ Please upload a document first before asking questions."
|
| 103 |
+
|
| 104 |
+
if not query or query.strip() == "":
|
| 105 |
+
return "β οΈ Please enter a question."
|
| 106 |
|
| 107 |
# Find relevant chunks
|
| 108 |
+
relevant_chunks = find_relevant_chunks(query, top_k=5)
|
| 109 |
context = "\n\n".join(relevant_chunks)
|
| 110 |
|
| 111 |
+
# Create a more specific prompt
|
| 112 |
+
prompt = f"""Answer the question based ONLY on the information provided in the context below. If the answer cannot be found in the context, respond with "I cannot find this information in the document."
|
| 113 |
|
| 114 |
+
Context:
|
| 115 |
+
{context}
|
| 116 |
|
| 117 |
Question: {query}
|
| 118 |
|
| 119 |
+
Provide a clear and concise answer:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
try:
|
| 122 |
+
# Generate answer with better parameters
|
| 123 |
+
result = qa_pipeline(
|
| 124 |
+
prompt,
|
| 125 |
+
max_length=512,
|
| 126 |
+
min_length=20,
|
| 127 |
+
do_sample=False,
|
| 128 |
+
temperature=0.7,
|
| 129 |
+
top_p=0.9
|
| 130 |
+
)
|
| 131 |
+
answer = result[0]['generated_text'].strip()
|
| 132 |
+
|
| 133 |
+
# If answer is too short or seems wrong, try direct extraction
|
| 134 |
+
if len(answer) < 10 or "cannot" in answer.lower():
|
| 135 |
+
# Try a simpler prompt
|
| 136 |
+
simple_prompt = f"""Based on this text, answer: {query}
|
| 137 |
|
| 138 |
+
Text: {context}
|
| 139 |
|
| 140 |
+
Answer:"""
|
| 141 |
+
result = qa_pipeline(simple_prompt, max_length=512, do_sample=False)
|
| 142 |
+
answer = result[0]['generated_text'].strip()
|
| 143 |
+
|
| 144 |
+
return answer if answer else "I cannot find this information in the document."
|
| 145 |
+
|
| 146 |
+
except Exception as e:
|
| 147 |
+
return f"Error generating answer: {str(e)}\n\nRelevant context found:\n{context[:500]}..."
|
| 148 |
|
| 149 |
# Create Gradio interface
|
| 150 |
+
with gr.Blocks(title="Simple RAG Application", theme=gr.themes.Soft()) as demo:
|
| 151 |
gr.Markdown("# π Simple RAG Application")
|
| 152 |
gr.Markdown("Upload a document (PDF or TXT) and ask questions about its content.")
|
| 153 |
|
| 154 |
with gr.Row():
|
| 155 |
+
with gr.Column(scale=1):
|
| 156 |
+
file_input = gr.File(
|
| 157 |
+
label="π Upload Document (PDF or TXT)",
|
| 158 |
+
file_types=[".pdf", ".txt"]
|
| 159 |
+
)
|
| 160 |
+
upload_btn = gr.Button("π€ Upload & Process", variant="primary", size="lg")
|
| 161 |
+
upload_status = gr.Textbox(
|
| 162 |
+
label="Status",
|
| 163 |
+
interactive=False,
|
| 164 |
+
lines=2
|
| 165 |
+
)
|
| 166 |
|
| 167 |
+
with gr.Column(scale=1):
|
| 168 |
+
query_input = gr.Textbox(
|
| 169 |
+
label="β Your Question",
|
| 170 |
+
placeholder="e.g., How many leaves do I get?",
|
| 171 |
+
lines=3
|
| 172 |
+
)
|
| 173 |
+
query_btn = gr.Button("π Get Answer", variant="primary", size="lg")
|
| 174 |
+
|
| 175 |
+
answer_output = gr.Textbox(
|
| 176 |
+
label="π‘ Answer",
|
| 177 |
+
lines=10,
|
| 178 |
+
show_copy_button=True
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
gr.Markdown("### π Example Questions")
|
| 182 |
+
gr.Markdown("- How many privilege leaves do I get?\n- What is the maternity leave policy?\n- Can I carry forward my casual leaves?")
|
| 183 |
|
| 184 |
upload_btn.click(fn=upload_document, inputs=file_input, outputs=upload_status)
|
| 185 |
query_btn.click(fn=answer_query, inputs=query_input, outputs=answer_output)
|