Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,56 @@
|
|
1 |
import streamlit as st
|
2 |
-
from langchain_community.document_loaders import PyPDFLoader
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
from langchain_community.vectorstores import FAISS
|
6 |
from langchain.chains import ConversationalRetrievalChain
|
7 |
from langchain_openai import ChatOpenAI
|
8 |
-
import tempfile
|
9 |
import os
|
10 |
|
11 |
-
|
12 |
-
st.set_page_config(page_title="Document Q&A Bot")
|
13 |
|
14 |
-
# Initialize session state
|
15 |
if "messages" not in st.session_state:
|
16 |
st.session_state.messages = []
|
17 |
-
if "
|
18 |
-
st.session_state.
|
19 |
|
20 |
-
|
21 |
-
api_key = st.sidebar.text_input("Enter OpenAI API Key", type="password")
|
22 |
-
os.environ["OPENAI_API_KEY"] = api_key
|
23 |
-
|
24 |
-
# File upload
|
25 |
-
uploaded_file = st.sidebar.file_uploader("Upload your PDF", type="pdf")
|
26 |
-
|
27 |
-
@st.cache_resource
|
28 |
-
def process_document(file):
|
29 |
-
# Save uploaded file temporarily
|
30 |
-
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
31 |
-
tmp_file.write(file.getvalue())
|
32 |
-
tmp_path = tmp_file.name
|
33 |
-
|
34 |
-
# Load and process the document
|
35 |
-
loader = PyPDFLoader(tmp_path)
|
36 |
-
documents = loader.load()
|
37 |
-
|
38 |
-
# Split text into chunks
|
39 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
40 |
-
chunks = text_splitter.
|
41 |
-
|
42 |
-
# Create embeddings and vector store
|
43 |
embeddings = HuggingFaceEmbeddings()
|
44 |
-
|
45 |
-
|
46 |
-
# Clean up temp file
|
47 |
-
os.unlink(tmp_path)
|
48 |
-
|
49 |
-
return vector_store
|
50 |
|
51 |
-
def
|
52 |
llm = ChatOpenAI(temperature=0)
|
53 |
-
|
54 |
llm=llm,
|
55 |
retriever=vector_store.as_retriever(),
|
56 |
return_source_documents=True
|
57 |
)
|
58 |
-
return chain
|
59 |
|
60 |
-
|
61 |
-
st.title("Document Q&A Bot")
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
80 |
|
81 |
elif not api_key:
|
82 |
-
st.warning("Please enter your OpenAI API key
|
83 |
-
elif not
|
84 |
-
st.
|
|
|
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 ConversationalRetrievalChain
|
6 |
from langchain_openai import ChatOpenAI
|
|
|
7 |
import os
|
8 |
|
9 |
+
st.set_page_config(page_title="Chat with Text", layout="wide")
|
|
|
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 create_vector_store(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
18 |
+
chunks = text_splitter.create_documents([text])
|
|
|
|
|
19 |
embeddings = HuggingFaceEmbeddings()
|
20 |
+
return FAISS.from_documents(chunks, embeddings)
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
def get_qa_chain(vector_store):
|
23 |
llm = ChatOpenAI(temperature=0)
|
24 |
+
return ConversationalRetrievalChain.from_llm(
|
25 |
llm=llm,
|
26 |
retriever=vector_store.as_retriever(),
|
27 |
return_source_documents=True
|
28 |
)
|
|
|
29 |
|
30 |
+
st.title("💬 Chat with Your Text")
|
|
|
31 |
|
32 |
+
with st.sidebar:
|
33 |
+
api_key = st.text_input("OpenAI API Key", type="password")
|
34 |
+
if api_key:
|
35 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
36 |
|
37 |
+
text_input = st.text_area("Your Text Here", height=300)
|
38 |
+
if st.button("Process Text") and text_input and api_key:
|
39 |
+
with st.spinner("Processing text..."):
|
40 |
+
st.session_state.vector_store = create_vector_store(text_input)
|
41 |
+
st.success("Ready to chat!")
|
42 |
+
|
43 |
+
if st.session_state.vector_store and api_key:
|
44 |
+
if question := st.chat_input("Ask your question about the text"):
|
45 |
+
chain = get_qa_chain(st.session_state.vector_store)
|
46 |
+
response = chain({"question": question, "chat_history": st.session_state.messages})
|
47 |
+
st.session_state.messages.append((question, response["answer"]))
|
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")
|