Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,18 @@ import PyPDF2
|
|
| 9 |
# Model Setup
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
model_path = "ibm-granite/granite-3.1-1b-a400m-instruct"
|
| 12 |
-
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
model.eval()
|
| 16 |
|
| 17 |
# Embedding Model for FAISS
|
|
@@ -23,14 +32,12 @@ index = faiss.IndexFlatL2(dimension)
|
|
| 23 |
docs = [] # Store document texts
|
| 24 |
summary = "" # Store book summary
|
| 25 |
|
| 26 |
-
|
| 27 |
# Function to extract text from PDF
|
| 28 |
def extract_text_from_pdf(uploaded_file):
|
| 29 |
reader = PyPDF2.PdfReader(uploaded_file)
|
| 30 |
text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
| 31 |
return text
|
| 32 |
|
| 33 |
-
|
| 34 |
# Function to process uploaded documents and generate summary
|
| 35 |
def process_documents(files):
|
| 36 |
global docs, index, summary
|
|
@@ -41,7 +48,6 @@ def process_documents(files):
|
|
| 41 |
text = extract_text_from_pdf(file)
|
| 42 |
else:
|
| 43 |
text = file.getvalue().decode("utf-8")
|
| 44 |
-
|
| 45 |
docs.append(text)
|
| 46 |
|
| 47 |
embeddings = embedding_model.encode(docs)
|
|
@@ -50,22 +56,18 @@ def process_documents(files):
|
|
| 50 |
# Generate summary after processing documents
|
| 51 |
summary = generate_summary("\n".join(docs))
|
| 52 |
|
| 53 |
-
|
| 54 |
# Function to generate a book summary
|
| 55 |
def generate_summary(text):
|
| 56 |
chat = [
|
| 57 |
{"role": "system", "content": "You are a helpful AI that summarizes books."},
|
| 58 |
-
{"role": "user", "content": f"Summarize this book in a short paragraph:\n{text[:4000]}"} # Limiting input size
|
| 59 |
]
|
| 60 |
chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
| 61 |
-
|
| 62 |
input_tokens = tokenizer(chat, return_tensors="pt").to(device)
|
| 63 |
output = model.generate(**input_tokens, max_new_tokens=300)
|
| 64 |
-
|
| 65 |
return tokenizer.batch_decode(output, skip_special_tokens=True)[0]
|
| 66 |
|
| 67 |
-
|
| 68 |
-
# Function to retrieve relevant context
|
| 69 |
def retrieve_context(query):
|
| 70 |
if index.ntotal == 0:
|
| 71 |
return "No documents available. Please upload files first."
|
|
@@ -75,36 +77,30 @@ def retrieve_context(query):
|
|
| 75 |
|
| 76 |
if len(indices) == 0 or indices[0][0] >= len(docs):
|
| 77 |
return "No relevant context found."
|
| 78 |
-
|
| 79 |
return docs[indices[0][0]]
|
| 80 |
|
| 81 |
-
|
| 82 |
-
# Function to generate response using IBM Granite
|
| 83 |
def generate_response(query, context):
|
| 84 |
chat = [
|
| 85 |
{"role": "system", "content": "You are a helpful assistant using retrieved knowledge."},
|
| 86 |
{"role": "user", "content": f"Context: {context}\nQuestion: {query}\nAnswer based on context:"},
|
| 87 |
]
|
| 88 |
chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
| 89 |
-
|
| 90 |
input_tokens = tokenizer(chat, return_tensors="pt").to(device)
|
| 91 |
output = model.generate(**input_tokens, max_new_tokens=200)
|
| 92 |
-
|
| 93 |
return tokenizer.batch_decode(output, skip_special_tokens=True)[0]
|
| 94 |
|
| 95 |
-
|
| 96 |
# Streamlit UI
|
| 97 |
st.set_page_config(page_title="π AI Book Assistant", page_icon="π")
|
| 98 |
st.title("π AI-Powered Book Assistant")
|
| 99 |
st.subheader("Upload a book and get its summary or ask questions!")
|
| 100 |
|
| 101 |
-
|
| 102 |
|
| 103 |
-
if
|
| 104 |
with st.spinner("Processing book and generating summary..."):
|
| 105 |
-
process_documents([
|
| 106 |
st.success("Book uploaded and processed!")
|
| 107 |
-
|
| 108 |
st.markdown("### π Book Summary:")
|
| 109 |
st.write(summary)
|
| 110 |
|
|
|
|
| 9 |
# Model Setup
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
model_path = "ibm-granite/granite-3.1-1b-a400m-instruct"
|
|
|
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 13 |
+
|
| 14 |
+
# Load the model with a conditional to avoid meta tensor issues on CPU vs GPU
|
| 15 |
+
if device == "cpu":
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 17 |
+
else:
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
model_path,
|
| 20 |
+
device_map="auto",
|
| 21 |
+
low_cpu_mem_usage=True,
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
)
|
| 24 |
model.eval()
|
| 25 |
|
| 26 |
# Embedding Model for FAISS
|
|
|
|
| 32 |
docs = [] # Store document texts
|
| 33 |
summary = "" # Store book summary
|
| 34 |
|
|
|
|
| 35 |
# Function to extract text from PDF
|
| 36 |
def extract_text_from_pdf(uploaded_file):
|
| 37 |
reader = PyPDF2.PdfReader(uploaded_file)
|
| 38 |
text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
| 39 |
return text
|
| 40 |
|
|
|
|
| 41 |
# Function to process uploaded documents and generate summary
|
| 42 |
def process_documents(files):
|
| 43 |
global docs, index, summary
|
|
|
|
| 48 |
text = extract_text_from_pdf(file)
|
| 49 |
else:
|
| 50 |
text = file.getvalue().decode("utf-8")
|
|
|
|
| 51 |
docs.append(text)
|
| 52 |
|
| 53 |
embeddings = embedding_model.encode(docs)
|
|
|
|
| 56 |
# Generate summary after processing documents
|
| 57 |
summary = generate_summary("\n".join(docs))
|
| 58 |
|
|
|
|
| 59 |
# Function to generate a book summary
|
| 60 |
def generate_summary(text):
|
| 61 |
chat = [
|
| 62 |
{"role": "system", "content": "You are a helpful AI that summarizes books."},
|
| 63 |
+
{"role": "user", "content": f"Summarize this book in a short paragraph:\n{text[:4000]}"} # Limiting input size for summarization
|
| 64 |
]
|
| 65 |
chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
|
|
|
| 66 |
input_tokens = tokenizer(chat, return_tensors="pt").to(device)
|
| 67 |
output = model.generate(**input_tokens, max_new_tokens=300)
|
|
|
|
| 68 |
return tokenizer.batch_decode(output, skip_special_tokens=True)[0]
|
| 69 |
|
| 70 |
+
# Function to retrieve relevant context using FAISS
|
|
|
|
| 71 |
def retrieve_context(query):
|
| 72 |
if index.ntotal == 0:
|
| 73 |
return "No documents available. Please upload files first."
|
|
|
|
| 77 |
|
| 78 |
if len(indices) == 0 or indices[0][0] >= len(docs):
|
| 79 |
return "No relevant context found."
|
|
|
|
| 80 |
return docs[indices[0][0]]
|
| 81 |
|
| 82 |
+
# Function to generate response using IBM Granite model
|
|
|
|
| 83 |
def generate_response(query, context):
|
| 84 |
chat = [
|
| 85 |
{"role": "system", "content": "You are a helpful assistant using retrieved knowledge."},
|
| 86 |
{"role": "user", "content": f"Context: {context}\nQuestion: {query}\nAnswer based on context:"},
|
| 87 |
]
|
| 88 |
chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
|
|
|
| 89 |
input_tokens = tokenizer(chat, return_tensors="pt").to(device)
|
| 90 |
output = model.generate(**input_tokens, max_new_tokens=200)
|
|
|
|
| 91 |
return tokenizer.batch_decode(output, skip_special_tokens=True)[0]
|
| 92 |
|
|
|
|
| 93 |
# Streamlit UI
|
| 94 |
st.set_page_config(page_title="π AI Book Assistant", page_icon="π")
|
| 95 |
st.title("π AI-Powered Book Assistant")
|
| 96 |
st.subheader("Upload a book and get its summary or ask questions!")
|
| 97 |
|
| 98 |
+
uploaded_file = st.file_uploader("Upload a book (PDF or TXT)", accept_multiple_files=False)
|
| 99 |
|
| 100 |
+
if uploaded_file:
|
| 101 |
with st.spinner("Processing book and generating summary..."):
|
| 102 |
+
process_documents([uploaded_file])
|
| 103 |
st.success("Book uploaded and processed!")
|
|
|
|
| 104 |
st.markdown("### π Book Summary:")
|
| 105 |
st.write(summary)
|
| 106 |
|