Spaces:
Sleeping
Sleeping
File size: 19,045 Bytes
2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 4d4a229 766a4a2 4d4a229 766a4a2 2e4aae4 e05da15 766a4a2 2e4aae4 4d4a229 2e4aae4 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 4d4a229 766a4a2 4d4a229 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 4d4a229 766a4a2 4d4a229 766a4a2 4d4a229 2e4aae4 766a4a2 2e4aae4 4d4a229 766a4a2 2e4aae4 4d4a229 2e4aae4 4d4a229 2e4aae4 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 4d4a229 2e4aae4 4d4a229 2e4aae4 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 4d4a229 2e4aae4 4d4a229 766a4a2 2e4aae4 766a4a2 2e4aae4 766a4a2 |
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# # import streamlit as st
# # import torch
# # from transformers import GPTNeoXForCausalLM, AutoTokenizer
# # from sentence_transformers import SentenceTransformer
# # import faiss
# # import fitz # PyMuPDF
# # from langchain_text_splitters import RecursiveCharacterTextSplitter
# # # 1. Set page config FIRST
# # st.set_page_config(page_title="π Smart Book Analyst", layout="wide")
# # # Configuration
# # MODEL_NAME = "ibm-granite/granite-3.1-1b-a400m-instruct"
# # EMBED_MODEL = "sentence-transformers/all-mpnet-base-v2"
# # DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# # CHUNK_SIZE = 512
# # CHUNK_OVERLAP = 50
# # @st.cache_resource
# # def load_models():
# # try:
# # # Load Granite model
# # tokenizer = AutoTokenizer.from_pretrained(
# # MODEL_NAME,
# # trust_remote_code=True
# # )
# # model = GPTNeoXForCausalLM.from_pretrained(
# # MODEL_NAME,
# # device_map="auto" if DEVICE == "cuda" else None,
# # torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
# # trust_remote_code=True
# # ).eval()
# # # Load sentence transformer for embeddings
# # embedder = SentenceTransformer(EMBED_MODEL, device=DEVICE)
# # return tokenizer, model, embedder
# # except Exception as e:
# # st.error(f"Model loading failed: {str(e)}")
# # st.stop()
# # tokenizer, model, embedder = load_models()
# # # Text processing
# # def process_text(text):
# # splitter = RecursiveCharacterTextSplitter(
# # chunk_size=CHUNK_SIZE,
# # chunk_overlap=CHUNK_OVERLAP,
# # length_function=len
# # )
# # return splitter.split_text(text)
# # # PDF extraction
# # def extract_pdf_text(uploaded_file):
# # try:
# # doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
# # return "\n".join([page.get_text() for page in doc])
# # except Exception as e:
# # st.error(f"PDF extraction error: {str(e)}")
# # return ""
# # # Summarization function
# # def generate_summary(text):
# # chunks = process_text(text)[:10]
# # summaries = []
# # for chunk in chunks:
# # prompt = f"""<|user|>
# # Summarize this text section focusing on key themes, characters, and plot points:
# # {chunk[:2000]}
# # <|assistant|>
# # """
# # inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
# # outputs = model.generate(**inputs, max_new_tokens=300, temperature=0.3)
# # summaries.append(tokenizer.decode(outputs[0], skip_special_tokens=True))
# # combined = "\n".join(summaries)
# # final_prompt = f"""<|user|>
# # Combine these section summaries into a coherent book summary:
# # {combined}
# # <|assistant|>
# # The comprehensive summary is:"""
# # inputs = tokenizer(final_prompt, return_tensors="pt").to(DEVICE)
# # outputs = model.generate(**inputs, max_new_tokens=500, temperature=0.5)
# # return tokenizer.decode(outputs[0], skip_special_tokens=True).split(":")[-1].strip()
# # # FAISS index creation
# # def build_faiss_index(texts):
# # embeddings = embedder.encode(texts, show_progress_bar=False)
# # dimension = embeddings.shape[1]
# # index = faiss.IndexFlatIP(dimension)
# # faiss.normalize_L2(embeddings)
# # index.add(embeddings)
# # return index
# # # Answer generation
# # def generate_answer(query, context):
# # prompt = f"""<|user|>
# # Using this context: {context}
# # Answer the question precisely and truthfully. If unsure, say "I don't know".
# # Question: {query}
# # <|assistant|>
# # """
# # inputs = tokenizer(prompt, return_tensors="pt", max_length=1024, truncation=True).to(DEVICE)
# # outputs = model.generate(
# # **inputs,
# # max_new_tokens=300,
# # temperature=0.4,
# # top_p=0.9,
# # repetition_penalty=1.2,
# # do_sample=True
# # )
# # return tokenizer.decode(outputs[0], skip_special_tokens=True).split("<|assistant|>")[-1].strip()
# # # Streamlit UI
# # st.title("π AI-Powered Book Analysis System")
# # uploaded_file = st.file_uploader("Upload book (PDF or TXT)", type=["pdf", "txt"])
# # if uploaded_file:
# # with st.spinner("π Analyzing book content..."):
# # try:
# # if uploaded_file.type == "application/pdf":
# # text = extract_pdf_text(uploaded_file)
# # else:
# # text = uploaded_file.read().decode()
# # chunks = process_text(text)
# # st.session_state.docs = chunks
# # st.session_state.index = build_faiss_index(chunks)
# # with st.expander("π Book Summary", expanded=True):
# # summary = generate_summary(text)
# # st.write(summary)
# # except Exception as e:
# # st.error(f"Processing failed: {str(e)}")
# # if 'index' in st.session_state and st.session_state.index:
# # query = st.text_input("Ask about the book:")
# # if query:
# # with st.spinner("π Searching for answers..."):
# # try:
# # query_embed = embedder.encode([query])
# # faiss.normalize_L2(query_embed)
# # distances, indices = st.session_state.index.search(query_embed, k=3)
# # context = "\n".join([st.session_state.docs[i] for i in indices[0]])
# # answer = generate_answer(query, context)
# # st.subheader("Answer")
# # st.markdown(f"```\n{answer}\n```")
# # st.caption("Retrieved context confidence: {:.2f}".format(distances[0][0]))
# # except Exception as e:
# # st.error(f"Query failed: {str(e)}")
# import streamlit as st
# import torch
# from transformers import GPTNeoXForCausalLM, AutoTokenizer
# from sentence_transformers import SentenceTransformer
# import faiss
# import fitz
# from langchain_text_splitters import RecursiveCharacterTextSplitter
# # Set page config FIRST
# st.set_page_config(page_title="π Smart Book Analyst", layout="wide")
# # Configuration
# MODEL_NAME = "ibm-granite/granite-3.1-1b-a400m-instruct"
# EMBED_MODEL = "sentence-transformers/all-mpnet-base-v2"
# DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# CHUNK_SIZE = 1024 # Increased chunk size for better performance
# CHUNK_OVERLAP = 100
# MAX_SUMMARY_CHUNKS = 5 # Reduced from 10 to 5 for faster processing
# @st.cache_resource
# def load_models():
# try:
# # Load model with optimized settings
# tokenizer = AutoTokenizer.from_pretrained(
# MODEL_NAME,
# trust_remote_code=True
# )
# model = GPTNeoXForCausalLM.from_pretrained(
# MODEL_NAME,
# device_map="auto",
# torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
# trust_remote_code=True,
# low_cpu_mem_usage=True
# ).eval()
# # Load embedder with faster model
# embedder = SentenceTransformer(EMBED_MODEL, device=DEVICE)
# embedder.max_seq_length = 256 # Reduce embedding dimension
# return tokenizer, model, embedder
# except Exception as e:
# st.error(f"Model loading failed: {str(e)}")
# st.stop()
# tokenizer, model, embedder = load_models()
# def process_text(text):
# splitter = RecursiveCharacterTextSplitter(
# chunk_size=CHUNK_SIZE,
# chunk_overlap=CHUNK_OVERLAP,
# length_function=len
# )
# return splitter.split_text(text)
# def extract_pdf_text(uploaded_file):
# try:
# doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
# return "\n".join(page.get_text() for page in doc)
# except Exception as e:
# st.error(f"PDF extraction error: {str(e)}")
# return ""
# def generate_summary(text):
# chunks = process_text(text)[:MAX_SUMMARY_CHUNKS]
# if not chunks:
# return "No meaningful content found."
# progress_bar = st.progress(0)
# summaries = []
# for i, chunk in enumerate(chunks):
# progress_bar.progress((i+1)/len(chunks), text=f"Processing chunk {i+1}/{len(chunks)}...")
# prompt = f"""<|user|>
# Summarize key points in 2 sentences:
# {chunk[:1500]}
# <|assistant|>
# """
# inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
# outputs = model.generate(
# **inputs,
# max_new_tokens=150,
# temperature=0.2,
# do_sample=False # Disable sampling for faster generation
# )
# summaries.append(tokenizer.decode(outputs[0], skip_special_tokens=True))
# combined = "\n".join(summaries)
# final_prompt = f"""<|user|>
# Combine these into a concise summary (3-5 paragraphs):
# {combined}
# <|assistant|>
# Summary:"""
# inputs = tokenizer(final_prompt, return_tensors="pt").to(DEVICE)
# outputs = model.generate(
# **inputs,
# max_new_tokens=300,
# temperature=0.3,
# do_sample=False
# )
# return tokenizer.decode(outputs[0], skip_special_tokens=True).split("Summary:")[-1].strip()
# def build_faiss_index(texts):
# embeddings = embedder.encode(texts, show_progress_bar=False, batch_size=32)
# dimension = embeddings.shape[1]
# index = faiss.IndexFlatIP(dimension)
# faiss.normalize_L2(embeddings)
# index.add(embeddings)
# return index
# def generate_answer(query, context):
# prompt = f"""<|user|>
# Context: {context[:2000]}
# Q: {query}
# A:"""
# inputs = tokenizer(prompt, return_tensors="pt", max_length=1024, truncation=True).to(DEVICE)
# outputs = model.generate(
# **inputs,
# max_new_tokens=200,
# temperature=0.3,
# top_p=0.85,
# repetition_penalty=1.1,
# do_sample=True
# )
# return tokenizer.decode(outputs[0], skip_special_tokens=True).split("A:")[-1].strip()
# # Streamlit UI
# st.title("π AI-Powered Book Analysis System")
# uploaded_file = st.file_uploader("Upload book (PDF or TXT)", type=["pdf", "txt"])
# if uploaded_file:
# with st.spinner("π Analyzing book content..."):
# try:
# if uploaded_file.type == "application/pdf":
# text = extract_pdf_text(uploaded_file)
# else:
# text = uploaded_file.read().decode()
# if not text.strip():
# st.error("Uploaded file appears to be empty")
# st.stop()
# chunks = process_text(text)
# st.session_state.docs = chunks
# st.session_state.index = build_faiss_index(chunks)
# with st.expander("π Book Summary", expanded=True):
# summary = generate_summary(text)
# st.write(summary)
# except Exception as e:
# st.error(f"Processing failed: {str(e)}")
# if 'index' in st.session_state and st.session_state.index:
# query = st.text_input("Ask about the book:")
# if query:
# with st.spinner("π Searching for answers..."):
# try:
# query_embed = embedder.encode([query])
# faiss.normalize_L2(query_embed)
# distances, indices = st.session_state.index.search(query_embed, k=2)
# context = "\n".join([st.session_state.docs[i] for i in indices[0]])
# answer = generate_answer(query, context)
# st.subheader("Answer")
# st.markdown(f"```\n{answer}\n```")
# st.caption(f"Confidence: {distances[0][0]:.2f}")
# except Exception as e:
# st.error(f"Query failed: {str(e)}")
import streamlit as st
import torch
from transformers import GPTNeoXForCausalLM, AutoTokenizer
from sentence_transformers import SentenceTransformer
import faiss
import fitz
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Set page config first
st.set_page_config(page_title="π Smart Book Analyst", layout="wide")
# Configuration
MODEL_NAME = "ibm-granite/granite-3.1-1b-a400m-instruct"
EMBED_MODEL = "sentence-transformers/all-mpnet-base-v2"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
CHUNK_SIZE = 1024
CHUNK_OVERLAP = 100
MAX_SUMMARY_CHUNKS = 5
@st.cache_resource
def load_models():
try:
# Load model with correct tokenizer mapping
tokenizer = AutoTokenizer.from_pretrained(
MODEL_NAME,
trust_remote_code=True,
padding_side="left" # Crucial for generation quality
)
tokenizer.pad_token = tokenizer.eos_token
model = GPTNeoXForCausalLM.from_pretrained(
MODEL_NAME,
device_map="auto",
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
trust_remote_code=True,
low_cpu_mem_usage=True
).eval()
# Configure embedder properly
embedder = SentenceTransformer(EMBED_MODEL, device=DEVICE)
embedder.max_seq_length = 512
return tokenizer, model, embedder
except Exception as e:
st.error(f"Model loading failed: {str(e)}")
st.stop()
tokenizer, model, embedder = load_models()
def process_text(text):
splitter = RecursiveCharacterTextSplitter(
chunk_size=CHUNK_SIZE,
chunk_overlap=CHUNK_OVERLAP,
length_function=len
)
return splitter.split_text(text)
def extract_pdf_text(uploaded_file):
try:
doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
return "\n".join(page.get_text() for page in doc)
except Exception as e:
st.error(f"PDF extraction error: {str(e)}")
return ""
def generate_summary(text):
chunks = process_text(text)[:MAX_SUMMARY_CHUNKS]
if not chunks:
return "No meaningful content found."
progress_bar = st.progress(0)
summaries = []
for i, chunk in enumerate(chunks):
# Proper progress text formatting
progress_bar.progress((i+1)/len(chunks),
text=f"Processing section {i+1}/{len(chunks)}...")
prompt = f"""<|user|>
Summarize the key points from this text section in 3 bullet points:
{chunk[:1500]}
<|assistant|>
"""
inputs = tokenizer(
prompt,
return_tensors="pt",
max_length=1024,
truncation=True
).to(DEVICE)
outputs = model.generate(
**inputs,
max_new_tokens=200,
temperature=0.3,
top_p=0.9,
repetition_penalty=1.1,
do_sample=True,
pad_token_id=tokenizer.eos_token_id # Critical fix
)
decoded = tokenizer.decode(
outputs[0],
skip_special_tokens=True
).split("<|assistant|>")[-1].strip()
summaries.append(decoded)
combined = "\n\n".join(summaries)
final_prompt = f"""<|user|>
Combine these bullet points into a coherent 3-paragraph summary:
{combined}
<|assistant|>
Here is the comprehensive summary:"""
inputs = tokenizer(final_prompt, return_tensors="pt").to(DEVICE)
outputs = model.generate(
**inputs,
max_new_tokens=400,
temperature=0.5,
top_p=0.9,
repetition_penalty=1.1,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
return tokenizer.decode(
outputs[0],
skip_special_tokens=True
).split("Here is the comprehensive summary:")[-1].strip()
def build_faiss_index(texts):
embeddings = embedder.encode(
texts,
show_progress_bar=False,
batch_size=16,
convert_to_tensor=True
).cpu().numpy()
dimension = embeddings.shape[1]
index = faiss.IndexFlatIP(dimension)
faiss.normalize_L2(embeddings)
index.add(embeddings)
return index
def generate_answer(query, context):
prompt = f"""<|user|>
Based on this context:
{context[:2000]}
Answer this question concisely: {query}
<|assistant|>
"""
inputs = tokenizer(
prompt,
return_tensors="pt",
max_length=1024,
truncation=True
).to(DEVICE)
outputs = model.generate(
**inputs,
max_new_tokens=300,
temperature=0.4,
top_p=0.95,
repetition_penalty=1.15,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=3 # Prevent repetition
)
return tokenizer.decode(
outputs[0],
skip_special_tokens=True
).split("<|assistant|>")[-1].strip()
# Streamlit UI
st.title("π AI-Powered Book Analysis System")
uploaded_file = st.file_uploader("Upload book (PDF or TXT)", type=["pdf", "txt"])
if uploaded_file:
with st.spinner("π Analyzing book content..."):
try:
if uploaded_file.type == "application/pdf":
text = extract_pdf_text(uploaded_file)
else:
text = uploaded_file.read().decode()
if not text.strip():
st.error("Uploaded file is empty")
st.stop()
chunks = process_text(text)
st.session_state.docs = chunks
st.session_state.index = build_faiss_index(chunks)
with st.expander("π Book Summary", expanded=True):
summary = generate_summary(text)
st.write(summary)
except Exception as e:
st.error(f"Processing failed: {str(e)}")
if 'index' in st.session_state and st.session_state.index:
query = st.text_input("Ask about the book:")
if query:
with st.spinner("π Searching for answers..."):
try:
query_embed = embedder.encode([query])
faiss.normalize_L2(query_embed)
distances, indices = st.session_state.index.search(query_embed, k=3)
context = "\n".join([st.session_state.docs[i] for i in indices[0]])
answer = generate_answer(query, context)
st.subheader("Answer")
st.markdown(f"```\n{answer}\n```")
st.caption(f"Confidence score: {distances[0][0]:.2f}")
except Exception as e:
st.error(f"Query failed: {str(e)}") |