harshinde commited on
Commit
dd107cc
·
verified ·
1 Parent(s): e7147d8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.embeddings import HuggingFaceEmbeddings
3
+ from langchain.vectorstores import FAISS
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain.llms import HuggingFacePipeline
6
+ from langchain.schema import Document
7
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
8
+ from sentence_transformers import SentenceTransformer
9
+ import torch
10
+ from io import BytesIO
11
+ import fitz # PyMuPDF
12
+
13
+ # Set device based on GPU availability
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+
16
+ # Load embeddings with a smaller model and run on CPU
17
+ embedding_model = "all-MiniLM-L6-v2"
18
+ embeddings = HuggingFaceEmbeddings(model_name=embedding_model, model_kwargs={'device': 'cpu'})
19
+
20
+ # Set up text generation model with PyTorch-compatible pipeline
21
+ model_name = "google/flan-t5-small" # Or use a smaller model if needed
22
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
23
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
24
+
25
+ # Create a text generation pipeline
26
+ generator = pipeline(
27
+ "text2text-generation",
28
+ model=model,
29
+ tokenizer=tokenizer,
30
+ device=0 if device == "cuda" else -1,
31
+ model_kwargs={"max_length": 256, "temperature": 0.7}
32
+ )
33
+
34
+ llm = HuggingFacePipeline(pipeline=generator)
35
+
36
+ # Streamlit interface
37
+ def main():
38
+ st.title("Chat with Multiple PDFs")
39
+ st.write("Upload PDF files and chat with them.")
40
+
41
+ # File uploader
42
+ uploaded_files = st.file_uploader("Upload PDF Files", accept_multiple_files=True, type=["pdf"])
43
+
44
+ if uploaded_files:
45
+ # Load PDF documents
46
+ documents = []
47
+ for uploaded_file in uploaded_files:
48
+ pdf_content = BytesIO(uploaded_file.read())
49
+ doc = fitz.open(stream=pdf_content, filetype="pdf") # Open PDF with PyMuPDF
50
+ text = ""
51
+ for page in doc:
52
+ text += page.get_text() # Extract text from each page
53
+ doc.close()
54
+
55
+ # Create Document instance with page content
56
+ documents.append(Document(page_content=text, metadata={"file_name": uploaded_file.name}))
57
+
58
+ # Split documents into manageable chunks
59
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
60
+ chunks = text_splitter.split_documents(documents)
61
+
62
+ # Embed document chunks into vector store
63
+ vector_store = FAISS.from_documents(chunks, embeddings)
64
+
65
+ # User query input
66
+ st.write("You can now start chatting with your PDFs!")
67
+ user_input = st.text_input("Ask a question:")
68
+
69
+ if user_input:
70
+ # Perform similarity search on the vector store
71
+ docs = vector_store.similarity_search(user_input, k=3)
72
+
73
+ # Concatenate retrieved docs into a single prompt
74
+ prompt = "\n".join([doc.page_content for doc in docs]) + "\n\n" + user_input
75
+
76
+ # Generate response
77
+ try:
78
+ response = generator(prompt, max_new_tokens=50, num_return_sequences=1)[0]["generated_text"]
79
+ st.write(response)
80
+ except torch.cuda.OutOfMemoryError:
81
+ st.error("Out of memory. Try using a smaller model or fewer documents.")
82
+
83
+ if __name__ == "__main__":
84
+ main()