Spaces:
Running
Running
File size: 6,410 Bytes
1eda6bd 36ef5b4 1eda6bd 36ef5b4 01bfc89 56ec698 32aff05 36ef5b4 01bfc89 36ef5b4 b840334 36ef5b4 32aff05 36ef5b4 32aff05 36ef5b4 32aff05 01bfc89 36ef5b4 1eda6bd 01bfc89 1eda6bd ceda798 32aff05 ceda798 32aff05 1eda6bd 32aff05 ceda798 1eda6bd 32aff05 ceda798 56ec698 32aff05 ceda798 32aff05 56ec698 ceda798 32aff05 56ec698 32aff05 ceda798 32aff05 1eda6bd ceda798 1eda6bd ceda798 1eda6bd ceda798 32aff05 1eda6bd 01bfc89 1eda6bd ceda798 32aff05 c573d72 ceda798 c573d72 01bfc89 c573d72 1eda6bd c573d72 32aff05 c573d72 32aff05 01bfc89 1eda6bd 01bfc89 ceda798 c573d72 ceda798 c573d72 ceda798 01bfc89 32aff05 ceda798 c573d72 ceda798 1eda6bd 9997982 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
import os
import requests
import pickle
import sentence_transformers
import faiss
import gradio as gr
from transformers import pipeline
import numpy as np
from sentence_transformers import CrossEncoder
# ------------------------------
# Configuration
# ------------------------------
INDEX_URL = "https://huggingface.co/LoneWolfgang/abalone-index/resolve/main/index.faiss"
DOCSTORE_URL = "https://huggingface.co/LoneWolfgang/abalone-index/resolve/main/docstore.pkl"
INDEX_DIR = "data/index"
SBERT = "all-MiniLM-L12-v2"
# ------------------------------
# Ensure data folder exists
# ------------------------------
os.makedirs(INDEX_DIR, exist_ok=True)
# ------------------------------
# Download helper
# ------------------------------
def download_file(url, dest_path):
print(f"Downloading {url} ...")
r = requests.get(url)
r.raise_for_status()
with open(dest_path, "wb") as f:
f.write(r.content)
print(f"Saved to {dest_path}")
# Download index + docstore
download_file(INDEX_URL, os.path.join(INDEX_DIR, "index.faiss"))
download_file(DOCSTORE_URL, os.path.join(INDEX_DIR, "docstore.pkl"))
# ------------------------------
# Retriever
# ------------------------------
class Retriever:
def __init__(self, index_dir, cross_encoder_model="cross-encoder/ms-marco-MiniLM-L-6-v2"):
index, segments = self._load_index(index_dir)
self.index = index
self.segments = segments
# bi-encoder
self.sbert = sentence_transformers.SentenceTransformer(SBERT)
# cross-encoder
self.cross = CrossEncoder(cross_encoder_model)
def _load_index(self, index_dir):
index = faiss.read_index(os.path.join(index_dir, "index.faiss"))
with open(os.path.join(index_dir, "docstore.pkl"), "rb") as f:
segments = pickle.load(f)
return index, segments
def preprocess_query(self, query):
embedding = self.sbert.encode([query]).astype("float32")
faiss.normalize_L2(embedding)
return embedding
def retrieve(self, query, k=50):
# ---------- Stage 1: Bi-Encoder ----------
embedding = self.preprocess_query(query)
D, I = self.index.search(embedding, k)
candidates = []
ce_pairs_segments = []
for idx in I[0]:
seg = self.segments[idx]
candidates.append(seg)
ce_pairs_segments.append([query, seg["text"]])
# ---------- Stage 2: Cross-Encoder Re-Rank ----------
segment_scores = self.cross.predict(ce_pairs_segments)
best_seg_idx = int(np.argmax(segment_scores))
best_segment = candidates[best_seg_idx]
# ---------- Stage 3: Cross-Encoder Sentence Ranking ----------
sentences = best_segment["sentences"]
ce_pairs_sentences = [[query, s] for s in sentences]
sentence_scores = self.cross.predict(ce_pairs_sentences)
best_sent_idx = int(np.argmax(sentence_scores))
best_sentence = sentences[best_sent_idx].strip()
highlighted_text = (
best_segment["text"]
.replace(best_sentence, f"**{best_sentence}**")
.replace("\n", " ")
)
return {
"text": highlighted_text,
"url": best_segment.get("url"),
"document_id": best_segment.get("document_id"),
"segment_score": float(segment_scores[best_seg_idx]),
"sentence_score": float(sentence_scores[best_sent_idx]),
}
# ------------------------------
# Generators (loaded once)
# ------------------------------
generators = {
"TinyLlama": pipeline(
"text-generation",
model="LoneWolfgang/tinyllama-for-abalone-RAG",
max_new_tokens=150,
temperature=0.1,
),
"FLAN-T5": pipeline(
"text2text-generation",
model="google/flan-t5-base",
max_length=200,
)
}
retriever = Retriever(INDEX_DIR)
# ------------------------------
# Combined function: retrieve β generate
# ------------------------------
def answer_query(query, model_choice):
doc = retriever.retrieve(query)
url = doc["url"]
context = doc["text"].replace("\n", " ")
if model_choice == "No Generation":
# Just return context, no model generation
return (
f"#### Response\n\n"
f"{context}\n\n"
f"---\n"
f"[Source]({url})"
)
else:
prompt = f"""
You answer questions strictly using the provided context.
Context: {context}
Question: {query}
"""
# Choose generator
gen = generators[model_choice]
if model_choice == "TinyLlama":
out = gen(f"<|system|>{prompt}<|assistant|>")[0]["generated_text"]
result = out.split("<|assistant|>")[-1].strip()
else:
# FLAN-T5 returns text in "generated_text"
result = gen(prompt)[0]["generated_text"]
return (
f"#### Response\n\n"
f"{result}\n\n"
f"---\n"
f"#### Context\n\n"
f"{context}\n\n"
f"---\n"
f"[Source]({url})"
)
# ------------------------------
# Gradio UI
# ------------------------------
demo = gr.Interface(
fn=answer_query,
inputs=[
gr.Textbox(label="Enter your question"),
gr.Radio(
["TinyLlama", "FLAN-T5", "No Generation"],
label="Choose Model",
value="No Generation"
)
],
outputs=gr.Markdown(label="Answer"),
title="Abalone RAG Demo",
description="""This RAG system uses [SBERT](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) for initial retrieval and a [Cross Encoder](https://huggingface.co/cross-encoder/ms-marco-MiniLM-L6-v2) for re-ranking and highlighting.
Sentence embeddings are computed and [indexed](https://huggingface.co/LoneWolfgang/abalone-index) using FAISS.
For generation, you can choose between:
- [FLAN-T5](https://huggingface.co/google/flan-t5-base) β Fast and reliable, the baseline experience.
- [Finetuned TinyLlama](https://huggingface.co/LoneWolfgang/tinyllama-for-abalone-RAG) β Slower, but more expressive.
- **No Generation** β Only retrieve and highlight relevant context without generating a response. Explore the retrieval quality.
"""
)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|