AIPaperPilot / app.py
sunbal7's picture
Update app.py
59f49c7 verified
raw
history blame
5.67 kB
import streamlit as st
import torch
import numpy as np
import faiss
from transformers import AutoModelForCausalLM, AutoTokenizer
from sentence_transformers import SentenceTransformer
import fitz # PyMuPDF for better PDF extraction
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Configuration
MODEL_NAME = "ibm-granite/granite-3.1-1b-a400m-instruct"
EMBED_MODEL = "sentence-transformers/all-mpnet-base-v2"
CHUNK_SIZE = 512
CHUNK_OVERLAP = 64
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# Initialize session state
if "docs" not in st.session_state:
st.session_state.docs = []
if "index" not in st.session_state:
st.session_state.index = None
# Model loading with better error handling
@st.cache_resource
def load_models():
try:
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
device_map="auto" if DEVICE == "cuda" else None,
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
low_cpu_mem_usage=True
).eval()
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()
# Improved text processing
def process_text(text):
splitter = RecursiveCharacterTextSplitter(
chunk_size=CHUNK_SIZE,
chunk_overlap=CHUNK_OVERLAP,
length_function=len
)
return splitter.split_text(text)
# Enhanced 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 ""
# Multi-step summarization
def generate_summary(text):
chunks = process_text(text)[:10] # Use first 10 chunks for summary
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))
# Combine summaries
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()
# Enhanced retrieval system
def build_faiss_index(texts):
embeddings = embedder.encode(texts, show_progress_bar=True)
dimension = embeddings.shape[1]
index = faiss.IndexFlatIP(dimension)
faiss.normalize_L2(embeddings)
index.add(embeddings)
return index
# Context-aware 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.set_page_config(page_title="πŸ“š Smart Book Analyst", layout="wide")
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 st.session_state.index:
query = st.text_input("Ask about the book:")
if query:
with st.spinner("πŸ” Searching for answers..."):
try:
# Retrieve top 3 relevant chunks
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)}")