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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Avid Runner Chatbot — RAG pipeline class
|
| 3 |
+
==========================================
|
| 4 |
+
|
| 5 |
+
Class-based version of the pipeline built in Final_Project_Check-in_4.ipynb, structured
|
| 6 |
+
for deployment (e.g. a HuggingFace Space with ZeroGPU) rather than notebook-style
|
| 7 |
+
top-to-bottom execution.
|
| 8 |
+
|
| 9 |
+
Two-step usage:
|
| 10 |
+
|
| 11 |
+
1. Build the index once (from your PubMed dataframe) and save it to disk:
|
| 12 |
+
|
| 13 |
+
df = ... # your collected PubMed dataframe with PMID, Title, Abstract columns
|
| 14 |
+
AvidRunnerRAGPipeline.build_and_save_index(df, "index/")
|
| 15 |
+
|
| 16 |
+
2. At inference time (e.g. in your Space's app.py), load the pipeline from that saved index:
|
| 17 |
+
|
| 18 |
+
rag = AvidRunnerRAGPipeline(
|
| 19 |
+
model_name="Qwen/Qwen2.5-7B-Instruct",
|
| 20 |
+
embedding_model_name="BAAI/bge-small-en-v1.5",
|
| 21 |
+
vector_db_path="index/",
|
| 22 |
+
)
|
| 23 |
+
print(rag.generate("What advice can you give a runner regarding stress fractures?"))
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
import os
|
| 27 |
+
|
| 28 |
+
import torch
|
| 29 |
+
from langchain_community.vectorstores import FAISS
|
| 30 |
+
from langchain_community.vectorstores.utils import DistanceStrategy
|
| 31 |
+
from langchain_core.documents import Document as LangchainDocument
|
| 32 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 33 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 34 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 35 |
+
|
| 36 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class AvidRunnerRAGPipeline:
|
| 40 |
+
def __init__(
|
| 41 |
+
self,
|
| 42 |
+
model_name: str,
|
| 43 |
+
embedding_model_name: str,
|
| 44 |
+
vector_db_path: str,
|
| 45 |
+
max_new_tokens: int = 250,
|
| 46 |
+
do_sample: bool = False,
|
| 47 |
+
):
|
| 48 |
+
self.embedding_model_name = embedding_model_name
|
| 49 |
+
self.max_new_tokens = max_new_tokens
|
| 50 |
+
self.do_sample = do_sample
|
| 51 |
+
|
| 52 |
+
print(f"Loading Model: {model_name}...")
|
| 53 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN)
|
| 54 |
+
|
| 55 |
+
# --- CRITICAL: Load to CPU first ---
|
| 56 |
+
# ZeroGPU does not have a GPU available during global startup.
|
| 57 |
+
# We load the weights into System RAM now, and move them to GPU later
|
| 58 |
+
# (Spaces' @spaces.GPU decorator handles the move at call time).
|
| 59 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 60 |
+
model_name,
|
| 61 |
+
device_map="cpu", # Force CPU loading
|
| 62 |
+
torch_dtype=torch.bfloat16,
|
| 63 |
+
token=HF_TOKEN,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
|
| 67 |
+
self.tokenizer.padding_side = "left"
|
| 68 |
+
|
| 69 |
+
print("Loading Embeddings...")
|
| 70 |
+
self.embedding_model = HuggingFaceEmbeddings(
|
| 71 |
+
model_name=self.embedding_model_name,
|
| 72 |
+
model_kwargs={"device": "cpu"}, # Keep embeddings on CPU
|
| 73 |
+
encode_kwargs={"normalize_embeddings": True}, # matches cosine similarity, the winning retriever setup
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
print(f"Loading Vector DB from {vector_db_path}...")
|
| 77 |
+
if not os.path.exists(vector_db_path):
|
| 78 |
+
raise FileNotFoundError(
|
| 79 |
+
f"Could not find vector DB at {vector_db_path}. "
|
| 80 |
+
f"Run AvidRunnerRAGPipeline.build_and_save_index(df, '{vector_db_path}') first."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
self.vector_db = FAISS.load_local(vector_db_path, self.embedding_model, allow_dangerous_deserialization=True)
|
| 84 |
+
print("RAG Pipeline Initialized (CPU Mode)")
|
| 85 |
+
|
| 86 |
+
# ------------------------------------------------------------------
|
| 87 |
+
# Index construction — builds the same 256-token-chunk FAISS index
|
| 88 |
+
# used in the notebook, and saves it so __init__ can load it later.
|
| 89 |
+
# ------------------------------------------------------------------
|
| 90 |
+
@staticmethod
|
| 91 |
+
def build_and_save_index(
|
| 92 |
+
df,
|
| 93 |
+
save_path: str,
|
| 94 |
+
embedding_model_name: str = "BAAI/bge-small-en-v1.5",
|
| 95 |
+
splitter_tokenizer_name: str = "Qwen/Qwen2.5-7B-Instruct",
|
| 96 |
+
chunk_size: int = 256,
|
| 97 |
+
):
|
| 98 |
+
"""Builds the retrieval index from a PubMed dataframe (columns: PMID, Title,
|
| 99 |
+
Abstract) and saves it to disk so it can be loaded via FAISS.load_local later.
|
| 100 |
+
"""
|
| 101 |
+
knowledge_base = [
|
| 102 |
+
LangchainDocument(
|
| 103 |
+
page_content=row["Abstract"] if isinstance(row["Abstract"], str) else "",
|
| 104 |
+
metadata={"PMID": row["PMID"], "Title": row["Title"]},
|
| 105 |
+
)
|
| 106 |
+
for _, row in df.iterrows()
|
| 107 |
+
if isinstance(row["Abstract"], str) and len(row["Abstract"]) > 0
|
| 108 |
+
]
|
| 109 |
+
|
| 110 |
+
splitter_tokenizer = AutoTokenizer.from_pretrained(splitter_tokenizer_name)
|
| 111 |
+
splitter = RecursiveCharacterTextSplitter.from_huggingface_tokenizer(
|
| 112 |
+
splitter_tokenizer,
|
| 113 |
+
chunk_size=chunk_size,
|
| 114 |
+
chunk_overlap=int(chunk_size / 10),
|
| 115 |
+
add_start_index=True,
|
| 116 |
+
strip_whitespace=True,
|
| 117 |
+
separators=["\n\n", "\n", ". ", " ", ""],
|
| 118 |
+
)
|
| 119 |
+
processed = []
|
| 120 |
+
for doc in knowledge_base:
|
| 121 |
+
processed += splitter.split_documents([doc])
|
| 122 |
+
seen, docs_processed = {}, []
|
| 123 |
+
for d in processed:
|
| 124 |
+
if d.page_content not in seen:
|
| 125 |
+
seen[d.page_content] = True
|
| 126 |
+
docs_processed.append(d)
|
| 127 |
+
print(f"Chunks after splitting: {len(docs_processed)}")
|
| 128 |
+
|
| 129 |
+
embedding_model = HuggingFaceEmbeddings(
|
| 130 |
+
model_name=embedding_model_name,
|
| 131 |
+
model_kwargs={"device": "cuda" if torch.cuda.is_available() else "cpu"},
|
| 132 |
+
encode_kwargs={"normalize_embeddings": True},
|
| 133 |
+
)
|
| 134 |
+
vector_db = FAISS.from_documents(docs_processed, embedding_model, distance_strategy=DistanceStrategy.COSINE)
|
| 135 |
+
vector_db.save_local(save_path)
|
| 136 |
+
print(f"Saved index to {save_path}")
|
| 137 |
+
|
| 138 |
+
# ------------------------------------------------------------------
|
| 139 |
+
def retrieve(self, query, num_docs=3):
|
| 140 |
+
return self.vector_db.similarity_search(query, k=num_docs)
|
| 141 |
+
|
| 142 |
+
def _format_prompt(self, query, retrieved_docs):
|
| 143 |
+
# 1. Build context, citing PMID/Title so the model can ground its answer to a source
|
| 144 |
+
context = "\n\n".join(
|
| 145 |
+
f"[PMID {doc.metadata.get('PMID', 'N/A')}] {doc.metadata.get('Title', 'Untitled')}:\n{doc.page_content}"
|
| 146 |
+
for doc in retrieved_docs
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# 2. Universal chat template (works for Qwen, Llama, Mistral, etc.)
|
| 150 |
+
messages = [
|
| 151 |
+
{
|
| 152 |
+
"role": "system",
|
| 153 |
+
"content": (
|
| 154 |
+
"You are a sports medicine assistant helping recreational runners. "
|
| 155 |
+
"Using only the context provided below, answer the question accurately and concisely.\n\n"
|
| 156 |
+
f"Context:\n{context}"
|
| 157 |
+
),
|
| 158 |
+
},
|
| 159 |
+
{"role": "user", "content": query},
|
| 160 |
+
]
|
| 161 |
+
|
| 162 |
+
prompt = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 163 |
+
return prompt
|
| 164 |
+
|
| 165 |
+
def generate(self, query, num_docs=3, return_sources=False):
|
| 166 |
+
# 1. Retrieve
|
| 167 |
+
retrieved_docs = self.retrieve(query, num_docs)
|
| 168 |
+
|
| 169 |
+
# 2. Format prompt
|
| 170 |
+
prompt_str = self._format_prompt(query, retrieved_docs)
|
| 171 |
+
|
| 172 |
+
# 3. Tokenize
|
| 173 |
+
inputs = self.tokenizer(prompt_str, return_tensors="pt").to(self.model.device)
|
| 174 |
+
|
| 175 |
+
# 4. Generate — matches the notebook's deterministic (do_sample=False) setup by default;
|
| 176 |
+
# pass do_sample=True at construction if you want sampling instead
|
| 177 |
+
with torch.no_grad():
|
| 178 |
+
gen_kwargs = {"max_new_tokens": self.max_new_tokens, "do_sample": self.do_sample, "pad_token_id": self.tokenizer.eos_token_id}
|
| 179 |
+
if self.do_sample:
|
| 180 |
+
gen_kwargs["temperature"] = 0.7
|
| 181 |
+
outputs = self.model.generate(**inputs, **gen_kwargs)
|
| 182 |
+
|
| 183 |
+
# 5. Decode — slicing [input_len:] returns only the newly generated text, not the prompt
|
| 184 |
+
input_len = inputs.input_ids.shape[1]
|
| 185 |
+
generated_text = self.tokenizer.decode(outputs[0][input_len:], skip_special_tokens=True).strip()
|
| 186 |
+
|
| 187 |
+
if return_sources:
|
| 188 |
+
sources = [{"PMID": d.metadata.get("PMID"), "Title": d.metadata.get("Title")} for d in retrieved_docs]
|
| 189 |
+
return generated_text, sources
|
| 190 |
+
return generated_text
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
# --- Example usage ---
|
| 194 |
+
if __name__ == "__main__":
|
| 195 |
+
MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"
|
| 196 |
+
EMBEDDING_NAME = "BAAI/bge-small-en-v1.5"
|
| 197 |
+
VECDB_PATH = "index/"
|
| 198 |
+
|
| 199 |
+
# One-time build step, if you haven't saved an index yet:
|
| 200 |
+
# import pandas as pd
|
| 201 |
+
# df = pd.read_csv("results/pubmed_corpus.csv") # your collected PubMed dataframe
|
| 202 |
+
# AvidRunnerRAGPipeline.build_and_save_index(df, VECDB_PATH)
|
| 203 |
+
|
| 204 |
+
rag = AvidRunnerRAGPipeline(MODEL_NAME, EMBEDDING_NAME, VECDB_PATH)
|
| 205 |
+
|
| 206 |
+
prompt = "What advice can you give a runner regarding stress fracture prevention during marathon training?"
|
| 207 |
+
print(rag.generate(prompt))
|