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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
from transformers import AutoTokenizer
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
import faiss
|
| 9 |
+
import numpy as np
|
| 10 |
+
import PyPDF2
|
| 11 |
+
from pathlib import Path
|
| 12 |
|
| 13 |
+
# Ensure all required packages are installed
|
| 14 |
+
# This is generally handled by requirements.txt on Hugging Face Spaces,
|
| 15 |
+
# but this is a fallback for local execution.
|
| 16 |
+
try:
|
| 17 |
+
import faiss
|
| 18 |
+
except ImportError:
|
| 19 |
+
print("Installing faiss-cpu...")
|
| 20 |
+
os.system("pip install --quiet faiss-cpu")
|
| 21 |
+
import faiss
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
import PyPDF2
|
| 25 |
+
except ImportError:
|
| 26 |
+
print("Installing PyPDF2...")
|
| 27 |
+
os.system("pip install --quiet PyPDF2")
|
| 28 |
+
import PyPDF2
|
| 29 |
+
|
| 30 |
+
# --- Model Architecture (Copied from your provided code) ---
|
| 31 |
+
class EfficientMultiHeadAttention(nn.Module):
|
| 32 |
+
def __init__(self, d_model, n_heads, dropout=0.1):
|
| 33 |
+
super().__init__()
|
| 34 |
+
self.d_model = d_model
|
| 35 |
+
self.n_heads = n_heads
|
| 36 |
+
self.head_dim = d_model // n_heads
|
| 37 |
+
assert d_model % n_heads == 0, "d_model must be divisible by n_heads"
|
| 38 |
+
self.qkv_proj = nn.Linear(d_model, d_model * 3, bias=False)
|
| 39 |
+
self.out_proj = nn.Linear(d_model, d_model)
|
| 40 |
+
self.dropout = nn.Dropout(dropout)
|
| 41 |
+
self.scale = 1.0 / (self.head_dim ** 0.5)
|
| 42 |
+
def forward(self, x, mask=None):
|
| 43 |
+
B, T, C = x.shape
|
| 44 |
+
qkv = self.qkv_proj(x).reshape(B, T, 3, self.n_heads, self.head_dim)
|
| 45 |
+
qkv = qkv.permute(2, 0, 3, 1, 4)
|
| 46 |
+
q, k, v = qkv[0], qkv[1], qkv[2]
|
| 47 |
+
att = (q @ k.transpose(-2, -1)) * self.scale
|
| 48 |
+
if mask is not None:
|
| 49 |
+
mask = mask.view(B, 1, 1, T)
|
| 50 |
+
att = att.masked_fill(mask == 0, float('-inf'))
|
| 51 |
+
att = F.softmax(att, dim=-1)
|
| 52 |
+
att = self.dropout(att)
|
| 53 |
+
y = att @ v
|
| 54 |
+
y = y.transpose(1, 2).contiguous().view(B, T, C)
|
| 55 |
+
y = self.out_proj(y)
|
| 56 |
+
return y
|
| 57 |
+
|
| 58 |
+
class CompactFeedForward(nn.Module):
|
| 59 |
+
def __init__(self, d_model, d_ff, dropout=0.1):
|
| 60 |
+
super().__init__()
|
| 61 |
+
self.w1 = nn.Linear(d_model, d_ff)
|
| 62 |
+
self.w2 = nn.Linear(d_ff, d_model)
|
| 63 |
+
self.dropout = nn.Dropout(dropout)
|
| 64 |
+
def forward(self, x):
|
| 65 |
+
return self.w2(self.dropout(F.gelu(self.w1(x))))
|
| 66 |
+
|
| 67 |
+
class TransformerBlock(nn.Module):
|
| 68 |
+
def __init__(self, d_model, n_heads, d_ff, dropout=0.1):
|
| 69 |
+
super().__init__()
|
| 70 |
+
self.ln1 = nn.LayerNorm(d_model)
|
| 71 |
+
self.attn = EfficientMultiHeadAttention(d_model, n_heads, dropout)
|
| 72 |
+
self.ln2 = nn.LayerNorm(d_model)
|
| 73 |
+
self.mlp = CompactFeedForward(d_model, d_ff, dropout)
|
| 74 |
+
self.dropout = nn.Dropout(dropout)
|
| 75 |
+
def forward(self, x, mask=None):
|
| 76 |
+
x = x + self.dropout(self.attn(self.ln1(x), mask))
|
| 77 |
+
x = x + self.dropout(self.mlp(self.ln2(x)))
|
| 78 |
+
return x
|
| 79 |
+
|
| 80 |
+
class EdgeOptimizedSLM(nn.Module):
|
| 81 |
+
def __init__(self, vocab_size, d_model=320, n_heads=8, n_layers=4, d_ff=1280, max_length=512, dropout=0.1):
|
| 82 |
+
super().__init__()
|
| 83 |
+
self.tok_emb = nn.Embedding(vocab_size, d_model)
|
| 84 |
+
self.pos_emb = nn.Embedding(max_length, d_model)
|
| 85 |
+
self.drop = nn.Dropout(dropout)
|
| 86 |
+
self.blocks = nn.ModuleList([TransformerBlock(d_model, n_heads, d_ff, dropout) for _ in range(n_layers)])
|
| 87 |
+
self.ln_f = nn.LayerNorm(d_model)
|
| 88 |
+
self.qa_proj = nn.Linear(d_model, d_model // 2)
|
| 89 |
+
self.qa_start = nn.Linear(d_model // 2, 1)
|
| 90 |
+
self.qa_end = nn.Linear(d_model // 2, 1)
|
| 91 |
+
def forward(self, input_ids, attention_mask=None):
|
| 92 |
+
device = input_ids.device
|
| 93 |
+
B, T = input_ids.size()
|
| 94 |
+
pos = torch.arange(0, T, dtype=torch.long, device=device).unsqueeze(0)
|
| 95 |
+
tok_emb = self.tok_emb(input_ids)
|
| 96 |
+
pos_emb = self.pos_emb(pos)
|
| 97 |
+
x = self.drop(tok_emb + pos_emb)
|
| 98 |
+
for block in self.blocks:
|
| 99 |
+
x = block(x, attention_mask)
|
| 100 |
+
x = self.ln_f(x)
|
| 101 |
+
qa_hidden = F.gelu(self.qa_proj(x))
|
| 102 |
+
start_logits = self.qa_start(qa_hidden).squeeze(-1)
|
| 103 |
+
end_logits = self.qa_end(qa_hidden).squeeze(-1)
|
| 104 |
+
return {"start_logits": start_logits, "end_logits": end_logits}
|
| 105 |
+
|
| 106 |
+
# --- Global Variables and Model Loading ---
|
| 107 |
+
MODEL_PATH = "edge_deployment_package/models/model_dynamic_quantized_int8.pt"
|
| 108 |
+
TOKENIZER_NAME = "bert-base-uncased"
|
| 109 |
+
EMBEDDING_MODEL_NAME = 'all-MiniLM-L6-v2'
|
| 110 |
+
DEVICE = torch.device('cpu')
|
| 111 |
+
|
| 112 |
+
# Create a dummy model if the actual model is not found
|
| 113 |
+
if not os.path.exists(MODEL_PATH):
|
| 114 |
+
print(f"Warning: Model not found at {MODEL_PATH}. Creating a dummy model for demonstration.")
|
| 115 |
+
os.makedirs(os.path.dirname(MODEL_PATH), exist_ok=True)
|
| 116 |
+
dummy_tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME)
|
| 117 |
+
dummy_config = {
|
| 118 |
+
'vocab_size': dummy_tokenizer.vocab_size, 'd_model': 320, 'n_heads': 8, 'n_layers': 4, 'd_ff': 1280, 'max_length': 512
|
| 119 |
+
}
|
| 120 |
+
dummy_model = EdgeOptimizedSLM(**dummy_config)
|
| 121 |
+
torch.save({
|
| 122 |
+
'config': dummy_config, 'model_state_dict': dummy_model.state_dict(), 'quantization': 'dynamic_int8'
|
| 123 |
+
}, MODEL_PATH)
|
| 124 |
+
|
| 125 |
+
def load_custom_model(model_path):
|
| 126 |
+
checkpoint = torch.load(model_path, map_location=DEVICE)
|
| 127 |
+
config = checkpoint['config']
|
| 128 |
+
model = EdgeOptimizedSLM(**config)
|
| 129 |
+
model.load_state_dict(checkpoint['model_state_dict'])
|
| 130 |
+
model.to(DEVICE)
|
| 131 |
+
model.eval()
|
| 132 |
+
return model, config
|
| 133 |
+
|
| 134 |
+
print("Loading models...")
|
| 135 |
+
inference_model, model_config = load_custom_model(MODEL_PATH)
|
| 136 |
+
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME)
|
| 137 |
+
embedding_model = SentenceTransformer(EMBEDDING_MODEL_NAME, device=DEVICE)
|
| 138 |
+
print("Models loaded successfully.")
|
| 139 |
+
|
| 140 |
+
# --- RAG and PDF Processing Logic ---
|
| 141 |
+
class RAGPipeline:
|
| 142 |
+
def __init__(self, embedding_model):
|
| 143 |
+
self.text_chunks = []
|
| 144 |
+
self.vector_store = None
|
| 145 |
+
self.embedding_model = embedding_model
|
| 146 |
+
self.raw_embeddings_path = "document_embeddings.raw"
|
| 147 |
+
|
| 148 |
+
def process_pdf(self, pdf_file_obj):
|
| 149 |
+
if pdf_file_obj is None:
|
| 150 |
+
return "Please upload a PDF file first.", None
|
| 151 |
+
|
| 152 |
+
print(f"Processing PDF: {pdf_file_obj.name}")
|
| 153 |
+
self.text_chunks = []
|
| 154 |
+
|
| 155 |
+
try:
|
| 156 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file_obj.name)
|
| 157 |
+
text = "".join(page.extract_text() for page in pdf_reader.pages if page.extract_text())
|
| 158 |
+
|
| 159 |
+
if not text:
|
| 160 |
+
return "Could not extract text from the PDF.", None
|
| 161 |
+
|
| 162 |
+
words = text.split()
|
| 163 |
+
chunk_size, overlap = 200, 30
|
| 164 |
+
for i in range(0, len(words), chunk_size - overlap):
|
| 165 |
+
self.text_chunks.append(" ".join(words[i:i + chunk_size]))
|
| 166 |
+
|
| 167 |
+
if not self.text_chunks:
|
| 168 |
+
return "Text extracted but could not be split into chunks.", None
|
| 169 |
+
|
| 170 |
+
print(f"Generating embeddings for {len(self.text_chunks)} chunks...")
|
| 171 |
+
embeddings = self.embedding_model.encode(self.text_chunks, convert_to_tensor=False, show_progress_bar=True)
|
| 172 |
+
|
| 173 |
+
with open(self.raw_embeddings_path, 'wb') as f:
|
| 174 |
+
f.write(embeddings.tobytes())
|
| 175 |
+
|
| 176 |
+
self.vector_store = faiss.IndexFlatL2(embeddings.shape[1])
|
| 177 |
+
self.vector_store.add(embeddings)
|
| 178 |
+
|
| 179 |
+
status_message = f"Successfully processed '{Path(pdf_file_obj.name).name}'. Ready for questions."
|
| 180 |
+
print("PDF processing complete.")
|
| 181 |
+
return status_message, self.raw_embeddings_path
|
| 182 |
+
except Exception as e:
|
| 183 |
+
print(f"Error processing PDF: {e}")
|
| 184 |
+
return f"Error processing PDF: {e}", None
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def retrieve_context(self, query, top_k=3):
|
| 188 |
+
if self.vector_store is None: return ""
|
| 189 |
+
query_embedding = self.embedding_model.encode([query])
|
| 190 |
+
_, indices = self.vector_store.search(query_embedding, top_k)
|
| 191 |
+
return " ".join([self.text_chunks[i] for i in indices[0]])
|
| 192 |
+
|
| 193 |
+
rag_pipeline = RAGPipeline(embedding_model)
|
| 194 |
+
|
| 195 |
+
# --- Chatbot Inference Logic ---
|
| 196 |
+
def get_answer(question, context):
|
| 197 |
+
if not context:
|
| 198 |
+
return "I could not find relevant information in the document to answer that question."
|
| 199 |
+
|
| 200 |
+
inputs = tokenizer(question, context, return_tensors='pt', max_length=model_config.get('max_length', 512), truncation=True, padding='max_length')
|
| 201 |
+
input_ids, attention_mask = inputs['input_ids'].to(DEVICE), inputs['attention_mask'].to(DEVICE)
|
| 202 |
+
|
| 203 |
+
with torch.no_grad():
|
| 204 |
+
outputs = inference_model(input_ids, attention_mask)
|
| 205 |
+
start_index = torch.argmax(outputs['start_logits'], dim=1).item()
|
| 206 |
+
end_index = torch.argmax(outputs['end_logits'], dim=1).item()
|
| 207 |
+
|
| 208 |
+
if start_index <= end_index:
|
| 209 |
+
answer_ids = input_ids[0][start_index:end_index+1]
|
| 210 |
+
answer = tokenizer.decode(answer_ids, skip_special_tokens=True)
|
| 211 |
+
return answer if answer.strip() else "I found a relevant section, but could not extract a precise answer."
|
| 212 |
+
else:
|
| 213 |
+
return "I found relevant information, but I'm having trouble formulating a clear answer."
|
| 214 |
+
|
| 215 |
+
# --- Gradio Interface ---
|
| 216 |
+
def add_text(history, text):
|
| 217 |
+
history = history + [(text, None)]
|
| 218 |
+
return history, ""
|
| 219 |
+
|
| 220 |
+
def bot(history):
|
| 221 |
+
question = history[-1][0]
|
| 222 |
+
context = rag_pipeline.retrieve_context(question)
|
| 223 |
+
answer = get_answer(question, context)
|
| 224 |
+
history[-1][1] = answer
|
| 225 |
+
return history
|
| 226 |
+
|
| 227 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 228 |
+
gr.Markdown("# Chat with your PDF using a Custom Edge SLM")
|
| 229 |
+
gr.Markdown("1. Upload a PDF. 2. Wait for it to be processed. 3. Ask questions about its content.")
|
| 230 |
+
|
| 231 |
+
with gr.Row():
|
| 232 |
+
with gr.Column(scale=1):
|
| 233 |
+
pdf_upload = gr.File(label="Upload PDF")
|
| 234 |
+
upload_status = gr.Textbox(label="PDF Status", interactive=False)
|
| 235 |
+
download_embeddings = gr.File(label="Download Raw Embeddings", interactive=False)
|
| 236 |
+
|
| 237 |
+
with gr.Column(scale=2):
|
| 238 |
+
chatbot = gr.Chatbot(label="Chat History", height=500)
|
| 239 |
+
question_box = gr.Textbox(label="Your Question", placeholder="Ask something about the document...")
|
| 240 |
+
|
| 241 |
+
# Event Handlers
|
| 242 |
+
question_box.submit(add_text, [chatbot, question_box], [chatbot, question_box]).then(
|
| 243 |
+
bot, chatbot, chatbot
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
pdf_upload.upload(
|
| 247 |
+
fn=rag_pipeline.process_pdf,
|
| 248 |
+
inputs=[pdf_upload],
|
| 249 |
+
outputs=[upload_status, download_embeddings]
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
# To this:
|
| 253 |
+
if __name__ == "__main__":
|
| 254 |
+
demo.launch(server_name="0.0.0.0", server_port=7830) # Or another port if 7860 is taken
|
edge_deployment_package/models/model_dynamic_quantized_int8.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5a24ece9801cb01363bf5ba63954e505aa2c67c6f33a95d947f3a85b64a28f7a
|
| 3 |
+
size 59665069
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
sentence-transformers
|
| 5 |
+
faiss-cpu
|
| 6 |
+
PyPDF2
|
| 7 |
+
numpy
|