Munir1234 commited on
Commit
1fd9847
·
verified ·
1 Parent(s): f66e4dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -59
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
- # Configure page
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 "chat_history" not in st.session_state:
18
- st.session_state.chat_history = []
19
 
20
- # API key input
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.split_documents(documents)
41
-
42
- # Create embeddings and vector store
43
  embeddings = HuggingFaceEmbeddings()
44
- vector_store = FAISS.from_documents(chunks, embeddings)
45
-
46
- # Clean up temp file
47
- os.unlink(tmp_path)
48
-
49
- return vector_store
50
 
51
- def get_conversation_chain(vector_store):
52
  llm = ChatOpenAI(temperature=0)
53
- chain = ConversationalRetrievalChain.from_llm(
54
  llm=llm,
55
  retriever=vector_store.as_retriever(),
56
  return_source_documents=True
57
  )
58
- return chain
59
 
60
- # Main interface
61
- st.title("Document Q&A Bot")
62
 
63
- if uploaded_file and api_key:
64
- # Process document
65
- vector_store = process_document(uploaded_file)
66
- chain = get_conversation_chain(vector_store)
67
 
68
- # Chat interface
69
- user_question = st.chat_input("Ask a question about your document:")
70
- if user_question:
71
- response = chain({"question": user_question, "chat_history": st.session_state.chat_history})
72
-
73
- # Update chat history
74
- st.session_state.chat_history.extend([(user_question, response["answer"])])
75
-
76
- # Display chat history
77
- for question, answer in st.session_state.chat_history:
78
- st.chat_message("user").write(question)
79
- st.chat_message("assistant").write(answer)
 
 
 
80
 
81
  elif not api_key:
82
- st.warning("Please enter your OpenAI API key in the sidebar.")
83
- elif not uploaded_file:
84
- st.warning("Please upload a PDF document in the sidebar.")
 
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")