Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,27 @@ import os
|
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 11 |
-
qa_model = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
| 14 |
def extract_text(file):
|
| 15 |
text = ""
|
| 16 |
if file.name.endswith(".pdf"):
|
|
@@ -25,72 +41,79 @@ def extract_text(file):
|
|
| 25 |
text = file.read().decode("utf-8", errors="ignore")
|
| 26 |
return text
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
| 29 |
def build_faiss(text, chunk_size=500, overlap=50):
|
| 30 |
-
# Split text into chunks with overlap
|
| 31 |
chunks = []
|
| 32 |
for i in range(0, len(text), chunk_size - overlap):
|
| 33 |
chunks.append(text[i:i + chunk_size])
|
| 34 |
-
|
| 35 |
-
# Embed chunks
|
| 36 |
embeddings = embedding_model.encode(chunks, convert_to_numpy=True)
|
| 37 |
-
|
| 38 |
-
# Store in FAISS
|
| 39 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 40 |
index.add(embeddings)
|
| 41 |
-
|
| 42 |
return index, chunks
|
| 43 |
|
| 44 |
-
#
|
|
|
|
|
|
|
| 45 |
doc_index = None
|
| 46 |
doc_chunks = None
|
| 47 |
|
| 48 |
-
#
|
|
|
|
|
|
|
| 49 |
def upload_file(file):
|
| 50 |
global doc_index, doc_chunks
|
| 51 |
text = extract_text(file)
|
| 52 |
doc_index, doc_chunks = build_faiss(text)
|
| 53 |
return "β
Document indexed with HyDE! You can now ask questions."
|
| 54 |
|
| 55 |
-
#
|
|
|
|
|
|
|
| 56 |
def answer_query(query):
|
| 57 |
global doc_index, doc_chunks
|
| 58 |
if doc_index is None:
|
| 59 |
return "β οΈ Please upload a document first."
|
| 60 |
-
|
| 61 |
-
# Step 1: Generate hypothetical answer (HyDE step)
|
| 62 |
hyde_prompt = f"Write a detailed, hypothetical answer to the question:\n\nQuestion: {query}\nAnswer:"
|
| 63 |
hypo_answer = qa_model(hyde_prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
|
| 64 |
-
|
| 65 |
-
# Step 2: Embed the hypothetical answer instead of the raw query
|
| 66 |
q_emb = embedding_model.encode([hypo_answer], convert_to_numpy=True)
|
| 67 |
-
|
| 68 |
-
# Step 3: Retrieve top 3 most relevant chunks
|
| 69 |
D, I = doc_index.search(q_emb, k=3)
|
| 70 |
retrieved = [doc_chunks[i] for i in I[0]]
|
| 71 |
-
|
| 72 |
-
# Step 4: Build final prompt with context
|
| 73 |
context = "\n\n".join(retrieved)
|
| 74 |
final_prompt = f"Answer the question based on the context:\n\nContext: {context}\n\nQuestion: {query}\nAnswer:"
|
| 75 |
-
|
| 76 |
-
# Step 5: Generate final response
|
| 77 |
response = qa_model(final_prompt, max_length=200, num_return_sequences=1)[0]["generated_text"]
|
| 78 |
return response
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
with gr.Row():
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
upload_btn.click(upload_file, inputs=file_input, outputs=status)
|
| 94 |
ask_btn.click(answer_query, inputs=query, outputs=answer)
|
| 95 |
|
| 96 |
demo.launch()
|
|
|
|
|
|
| 6 |
from sentence_transformers import SentenceTransformer
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
+
# =============================
|
| 10 |
+
# 1. Hugging Face Authentication
|
| 11 |
+
# =============================
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Make sure to set: export HF_TOKEN="your_token_here"
|
| 13 |
+
if HF_TOKEN is None:
|
| 14 |
+
raise ValueError("β οΈ Please set your HF_TOKEN as an environment variable.")
|
| 15 |
+
|
| 16 |
+
# =============================
|
| 17 |
+
# 2. Load embedding + QA model
|
| 18 |
+
# =============================
|
| 19 |
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 20 |
+
qa_model = pipeline(
|
| 21 |
+
"text-generation",
|
| 22 |
+
model="meta-llama/Llama-3.2-3b-instruct",
|
| 23 |
+
token=HF_TOKEN,
|
| 24 |
+
device_map="auto"
|
| 25 |
+
)
|
| 26 |
|
| 27 |
+
# =============================
|
| 28 |
+
# 3. Helper: extract text from files
|
| 29 |
+
# =============================
|
| 30 |
def extract_text(file):
|
| 31 |
text = ""
|
| 32 |
if file.name.endswith(".pdf"):
|
|
|
|
| 41 |
text = file.read().decode("utf-8", errors="ignore")
|
| 42 |
return text
|
| 43 |
|
| 44 |
+
# =============================
|
| 45 |
+
# 4. Helper: create FAISS index
|
| 46 |
+
# =============================
|
| 47 |
def build_faiss(text, chunk_size=500, overlap=50):
|
|
|
|
| 48 |
chunks = []
|
| 49 |
for i in range(0, len(text), chunk_size - overlap):
|
| 50 |
chunks.append(text[i:i + chunk_size])
|
| 51 |
+
|
|
|
|
| 52 |
embeddings = embedding_model.encode(chunks, convert_to_numpy=True)
|
|
|
|
|
|
|
| 53 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 54 |
index.add(embeddings)
|
| 55 |
+
|
| 56 |
return index, chunks
|
| 57 |
|
| 58 |
+
# =============================
|
| 59 |
+
# 5. Global storage
|
| 60 |
+
# =============================
|
| 61 |
doc_index = None
|
| 62 |
doc_chunks = None
|
| 63 |
|
| 64 |
+
# =============================
|
| 65 |
+
# 6. Process uploaded file
|
| 66 |
+
# =============================
|
| 67 |
def upload_file(file):
|
| 68 |
global doc_index, doc_chunks
|
| 69 |
text = extract_text(file)
|
| 70 |
doc_index, doc_chunks = build_faiss(text)
|
| 71 |
return "β
Document indexed with HyDE! You can now ask questions."
|
| 72 |
|
| 73 |
+
# =============================
|
| 74 |
+
# 7. HyDE RAG answering
|
| 75 |
+
# =============================
|
| 76 |
def answer_query(query):
|
| 77 |
global doc_index, doc_chunks
|
| 78 |
if doc_index is None:
|
| 79 |
return "β οΈ Please upload a document first."
|
| 80 |
+
|
|
|
|
| 81 |
hyde_prompt = f"Write a detailed, hypothetical answer to the question:\n\nQuestion: {query}\nAnswer:"
|
| 82 |
hypo_answer = qa_model(hyde_prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
|
| 83 |
+
|
|
|
|
| 84 |
q_emb = embedding_model.encode([hypo_answer], convert_to_numpy=True)
|
|
|
|
|
|
|
| 85 |
D, I = doc_index.search(q_emb, k=3)
|
| 86 |
retrieved = [doc_chunks[i] for i in I[0]]
|
| 87 |
+
|
|
|
|
| 88 |
context = "\n\n".join(retrieved)
|
| 89 |
final_prompt = f"Answer the question based on the context:\n\nContext: {context}\n\nQuestion: {query}\nAnswer:"
|
| 90 |
+
|
|
|
|
| 91 |
response = qa_model(final_prompt, max_length=200, num_return_sequences=1)[0]["generated_text"]
|
| 92 |
return response
|
| 93 |
|
| 94 |
+
# =============================
|
| 95 |
+
# 8. Gradio UI (Visually Appealing)
|
| 96 |
+
# =============================
|
| 97 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="cyan")) as demo:
|
| 98 |
+
gr.Markdown("""
|
| 99 |
+
# π HyDE RAG Chatbot
|
| 100 |
+
Talk with your documents using **Hypothetical Document Embeddings (HyDE)**.
|
| 101 |
+
Upload a PDF/DOCX/TXT and start asking questions!
|
| 102 |
+
""")
|
| 103 |
+
|
| 104 |
with gr.Row():
|
| 105 |
+
with gr.Column(scale=1):
|
| 106 |
+
file_input = gr.File(label="π Upload Document", type="filepath")
|
| 107 |
+
upload_btn = gr.Button("β‘ Index Document", variant="primary")
|
| 108 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 109 |
+
|
| 110 |
+
with gr.Column(scale=2):
|
| 111 |
+
query = gr.Textbox(label="β Ask a Question", placeholder="Type your question here...")
|
| 112 |
+
ask_btn = gr.Button("π Get Answer", variant="primary")
|
| 113 |
+
answer = gr.Textbox(label="π‘ Answer", lines=6)
|
| 114 |
+
|
| 115 |
upload_btn.click(upload_file, inputs=file_input, outputs=status)
|
| 116 |
ask_btn.click(answer_query, inputs=query, outputs=answer)
|
| 117 |
|
| 118 |
demo.launch()
|
| 119 |
+
|