Munir1234 commited on
Commit
c93ffe3
·
verified ·
1 Parent(s): 33451cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -44
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 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")
 
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)