Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,47 @@
|
|
1 |
import streamlit as st
|
2 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
4 |
from langchain_community.vectorstores import FAISS
|
5 |
-
from langchain.chains import
|
6 |
-
from
|
|
|
7 |
import os
|
8 |
|
9 |
-
st.set_page_config(page_title="
|
10 |
|
11 |
-
if "messages" not in st.session_state:
|
12 |
-
st.session_state.messages = []
|
13 |
if "vector_store" not in st.session_state:
|
14 |
st.session_state.vector_store = None
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
chunks =
|
19 |
-
embeddings = HuggingFaceEmbeddings()
|
20 |
return FAISS.from_documents(chunks, embeddings)
|
21 |
|
22 |
-
def
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
)
|
29 |
-
|
30 |
-
st.title("
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
if st.
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
st.
|
48 |
-
|
49 |
-
for message in st.session_state.messages:
|
50 |
-
st.chat_message("user").write(message[0])
|
51 |
-
st.chat_message("assistant").write(message[1])
|
52 |
-
|
53 |
-
elif not api_key:
|
54 |
-
st.warning("⚠️ Please enter your OpenAI API key")
|
55 |
-
elif not st.session_state.vector_store:
|
56 |
-
st.info("👈 Please input your text and process it first")
|
|
|
1 |
import streamlit as st
|
2 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
3 |
+
from langchain_community.document_loaders import PyPDFLoader
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
from langchain_community.vectorstores import FAISS
|
6 |
+
from langchain.chains import RetrievalQA
|
7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
8 |
+
import tempfile
|
9 |
import os
|
10 |
|
11 |
+
st.set_page_config(page_title="Document QA Bot")
|
12 |
|
|
|
|
|
13 |
if "vector_store" not in st.session_state:
|
14 |
st.session_state.vector_store = None
|
15 |
|
16 |
+
def process_text(text):
|
17 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
18 |
+
chunks = splitter.create_documents([text])
|
19 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
20 |
return FAISS.from_documents(chunks, embeddings)
|
21 |
|
22 |
+
def process_pdf(file):
|
23 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
|
24 |
+
tmp_file.write(file.getvalue())
|
25 |
+
loader = PyPDFLoader(tmp_file.name)
|
26 |
+
pages = loader.load()
|
27 |
+
os.unlink(tmp_file.name)
|
28 |
+
return process_text('\n'.join(page.page_content for page in pages))
|
29 |
+
|
30 |
+
st.title("Document QA Bot")
|
31 |
+
|
32 |
+
uploaded_file = st.file_uploader("Upload Document", type=["txt", "pdf"])
|
33 |
+
if uploaded_file:
|
34 |
+
with st.spinner("Processing document..."):
|
35 |
+
if uploaded_file.type == "text/plain":
|
36 |
+
text = uploaded_file.getvalue().decode()
|
37 |
+
st.session_state.vector_store = process_text(text)
|
38 |
+
else:
|
39 |
+
st.session_state.vector_store = process_pdf(uploaded_file)
|
40 |
+
st.success("Document processed!")
|
41 |
+
|
42 |
+
if st.session_state.vector_store:
|
43 |
+
if question := st.chat_input("Ask a question about the document:"):
|
44 |
+
results = st.session_state.vector_store.similarity_search(question)
|
45 |
+
context = "\n".join(doc.page_content for doc in results)
|
46 |
+
st.chat_message("user").write(question)
|
47 |
+
st.chat_message("assistant").write(context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|