Spaces:
Sleeping
Sleeping
File size: 10,444 Bytes
44669ca ce96f36 44669ca ce96f36 44669ca ce96f36 44669ca ce96f36 44669ca |
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# =============================================================
# π USTP Student Handbook Assistant (2023 Edition)
# =============================================================
# Enhanced: dynamic model selection + real (printed) page numbering
import os
import glob
import json
import time
from typing import List, Dict, Any
import numpy as np
import streamlit as st
import PyPDF2
import requests
from dotenv import load_dotenv
from huggingface_hub import InferenceClient, login
from streamlit_chat import message as st_message
# Optional: FAISS for fast vector search
try:
import faiss
except ImportError:
faiss = None
# =============================================================
# π Startup Fix for PermissionError
# =============================================================
os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit"
os.makedirs("/tmp/.streamlit", exist_ok=True)
# =============================================================
# βοΈ Streamlit Page Setup
# =============================================================
st.set_page_config(page_title="π Handbook Assistant", page_icon="π", layout="wide")
st.title("π USTP Student Handbook Assistant (2023 Edition)")
st.caption("Answers sourced only from the official *USTP Student Handbook 2023 Edition.pdf*.")
load_dotenv()
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
st.warning("β οΈ No Hugging Face API token found in .env file. Online models will be unavailable.")
else:
try:
login(HF_TOKEN)
except Exception:
pass
hf_client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else None
# =============================================================
# βοΈ Sidebar Configuration
# =============================================================
with st.sidebar:
st.header("βοΈ Settings")
model_options = {
"Qwen 2.5 14B Instruct": "Qwen/Qwen2.5-14B-Instruct",
"Mistral 7B Instruct": "mistralai/Mistral-7B-Instruct-v0.3",
"Llama 3 8B Instruct": "meta-llama/Meta-Llama-3-8B-Instruct",
"Mixtral 8x7B Instruct": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"Falcon 7B Instruct": "tiiuae/falcon-7b-instruct",
}
model_choice = st.selectbox("Select reasoning model", list(model_options.keys()), index=0)
DEFAULT_MODEL = model_options[model_choice]
st.markdown("---")
similarity_threshold = st.slider("Similarity threshold", 0.3, 1.0, 0.6, 0.01)
top_k = st.slider("Top K retrieved chunks", 1, 10, 4)
chunk_size_chars = st.number_input("Chunk size (chars)", 400, 2500, 1200, 100)
chunk_overlap = st.number_input("Chunk overlap (chars)", 20, 600, 150, 10)
front_matter_pages = st.number_input(
"Pages before main content (e.g. table of contents, cover)", min_value=0, max_value=50, value=12
)
regenerate_index = st.button("π Rebuild handbook index")
# =============================================================
# π File Config
# =============================================================
INDEX_FILE = "handbook_faiss.index"
META_FILE = "handbook_metadata.json"
EMB_DIM_FILE = "handbook_emb_dim.json"
EMBED_MODEL = "sentence-transformers/all-mpnet-base-v2"
# =============================================================
# π§© Utility Functions
# =============================================================
def find_handbook() -> List[str]:
preferred = "USTP Student Handbook 2023 Edition.pdf"
pdfs = glob.glob("*.pdf")
for f in pdfs:
if preferred.lower() in f.lower():
st.success(f"π Found handbook: {f}")
return [f]
if pdfs:
st.warning(f"β οΈ Preferred handbook not found. Using {os.path.basename(pdfs[0])}.")
return [pdfs[0]]
st.error("β No PDF found in current folder.")
return []
def load_pdf_texts(pdf_paths: List[str]) -> List[Dict[str, Any]]:
"""Extract page text while adjusting page numbering to printed handbook numbers."""
pages = []
for path in pdf_paths:
with open(path, "rb") as f:
reader = PyPDF2.PdfReader(f)
for i, page in enumerate(reader.pages):
text = page.extract_text() or ""
if text.strip():
# Adjust logical page number to printed numbering
logical_page = i + 1
printed_page = logical_page - front_matter_pages
if printed_page < 1:
printed_page = 1
pages.append({
"filename": os.path.basename(path),
"page": printed_page,
"text": text.strip()
})
return pages
def chunk_text(pages: List[Dict[str, Any]], size: int, overlap: int) -> List[Dict[str, Any]]:
chunks = []
for p in pages:
text = p["text"]
start = 0
while start < len(text):
end = start + size
chunk = text[start:end]
chunks.append({
"filename": p["filename"],
"page": p["page"],
"content": chunk.strip()
})
start += size - overlap
return chunks
def embed_texts(texts: List[str]) -> np.ndarray:
"""Generate embeddings using Hugging Face feature extraction."""
if not HF_TOKEN or not hf_client:
st.error("β Missing Hugging Face token or client.")
return np.zeros((len(texts), 768))
try:
embeddings = hf_client.feature_extraction(texts, model=EMBED_MODEL)
if isinstance(embeddings[0][0], list):
embeddings = [np.mean(np.array(e), axis=0) for e in embeddings]
return np.array(embeddings)
except Exception as e1:
st.warning(f"β οΈ feature_extraction failed, using REST API fallback: {e1}")
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
resp = requests.post(
f"https://api-inference.huggingface.co/models/{EMBED_MODEL}",
headers=headers,
json={"inputs": texts}
)
data = resp.json()
if isinstance(data[0][0], list):
data = [np.mean(np.array(e), axis=0) for e in data]
return np.array(data)
def build_faiss_index(chunks: List[Dict[str, Any]]):
"""Build FAISS index for chunks."""
texts = [c["content"] for c in chunks]
embeddings = embed_texts(texts)
if embeddings.size == 0:
st.error("β Embedding generation failed.")
return
dim = embeddings.shape[1]
index = faiss.IndexFlatL2(dim)
index.add(embeddings.astype("float32"))
faiss.write_index(index, INDEX_FILE)
with open(META_FILE, "w") as f:
json.dump(chunks, f)
with open(EMB_DIM_FILE, "w") as f:
json.dump({"dim": dim}, f)
st.success(f"β
Indexed {len(chunks)} chunks.")
def load_faiss_index():
if not os.path.exists(INDEX_FILE) or not os.path.exists(META_FILE):
return None, None
index = faiss.read_index(INDEX_FILE)
with open(META_FILE) as f:
meta = json.load(f)
return index, meta
def search_index(query: str, index, meta, top_k: int, threshold: float):
query_emb = embed_texts([query])
distances, indices = index.search(query_emb.astype("float32"), top_k)
results = []
for i, dist in zip(indices[0], distances[0]):
if i < len(meta):
r = meta[i]
r["distance"] = float(dist)
results.append(r)
return results
def generate_answer(context: str, query: str) -> str:
"""Generate model-based answer using selected open-source model."""
prompt = f"""
You are a precise academic assistant specialized in university policy.
Use only the *USTP Student Handbook 2023 Edition* below.
If the answer is not in the text, reply:
"The handbook does not specify that."
---
π Context:
{context}
---
π§ Question:
{query}
---
π― Instructions:
- Be factual and concise.
- Cite the correct printed page number.
- Never make assumptions.
"""
try:
response = hf_client.text_generation(
model=DEFAULT_MODEL,
prompt=prompt,
max_new_tokens=400,
temperature=0.25
)
return response if isinstance(response, str) else str(response)
except Exception as e1:
try:
chat_response = hf_client.chat.completions.create(
model=DEFAULT_MODEL,
messages=[{"role": "user", "content": prompt}],
max_tokens=400
)
return chat_response.choices[0].message["content"]
except Exception as e2:
return f"β οΈ Error generating answer: {e2}"
def ensure_index():
"""Ensure FAISS index exists or rebuild."""
if regenerate_index or not os.path.exists(INDEX_FILE):
pdfs = find_handbook()
if not pdfs:
st.stop()
st.info("π Extracting handbook text...")
pages = load_pdf_texts(pdfs)
chunks = chunk_text(pages, chunk_size_chars, chunk_overlap)
build_faiss_index(chunks)
index, meta = load_faiss_index()
if index is None or meta is None:
st.error("β Could not load FAISS index.")
st.stop()
return index, meta
# =============================================================
# π¬ Chat Interface
# =============================================================
st.divider()
st.subheader("π¬ Ask about the Handbook")
if "history" not in st.session_state:
st.session_state.history = []
user_query = st.text_input("Enter your question:")
index, meta = ensure_index()
if st.button("Ask") and user_query.strip():
results = search_index(user_query, index, meta, top_k, similarity_threshold)
if not results:
st.warning("No relevant section found in the handbook.")
else:
context = "\n\n".join(
[f"(π Page {r['page']})\n{r['content']}" for r in results]
)
answer = generate_answer(context, user_query)
st.session_state.history.append({
"user": user_query,
"assistant": answer,
"timestamp": time.time()
})
# β
Ensure unique keys to prevent StreamlitDuplicateElementId
for i, chat in enumerate(st.session_state.history):
st_message(chat["user"], is_user=True, key=f"user_{i}")
st_message(chat["assistant"], key=f"assistant_{i}")
st.caption("β‘ Powered by FAISS + Open Source Models + Accurate Page Referencing")
|