htafer commited on
Commit
892f4c0
1 Parent(s): fa5b409

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from langchain.chains import RetrievalQA, ConversationalRetrievalChain
4
+ from langchain.chat_models import ChatOpenAI
5
+ from langchain.document_loaders import TextLoader
6
+ from langchain.text_splitter import CharacterTextSplitter
7
+ from langchain.embeddings import OpenAIEmbeddings
8
+ from langchain.vectorstores import FAISS
9
+ from langchain.memory import ConversationBufferMemory
10
+ import re
11
+
12
+ # Initialize the Streamlit app
13
+ st.title('Document-Based Q&A System')
14
+
15
+ # API Key input securely
16
+ api_key = st.text_input("Enter your OpenAI API key:", type="password")
17
+ if api_key:
18
+ os.environ["OPENAI_API_KEY"] = api_key
19
+ st.success("API Key has been set!")
20
+
21
+ # File uploader
22
+ uploaded_file = st.file_uploader("Upload your document", type=['txt'])
23
+ if uploaded_file is not None:
24
+ # Read and process the document
25
+ text_data = uploaded_file.getvalue().decode("utf-8")
26
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
27
+ data = text_splitter.split_documents(text_data)
28
+
29
+ # Create vector store
30
+ embeddings = OpenAIEmbeddings()
31
+ vectorstore = FAISS.from_documents(data, embedding=embeddings)
32
+
33
+ # Create conversation chain
34
+ llm = ChatOpenAI(temperature=0.3, model_name="gpt-4-turbo")
35
+ memory = ConversationBufferMemory(
36
+ memory_key='chat_history', return_messages=True, output_key='answer')
37
+ conversation_chain = ConversationalRetrievalChain.from_llm(
38
+ llm=llm,
39
+ chain_type="stuff",
40
+ retriever=vectorstore.as_retriever(),
41
+ memory=memory,
42
+ return_source_documents=True
43
+ )
44
+
45
+ # Question input
46
+ query = st.text_input("Ask a question about the document:")
47
+ if query:
48
+ result = conversation_chain({"question": query})
49
+ answer = result["answer"]
50
+ st.write("Answer:", answer)
51
+
52
+ # Optionally display source text snippets
53
+ if st.checkbox("Show source text snippets"):
54
+ st.write("Source documents:")
55
+ for i in result["source_documents"]:
56
+ res = re.search(r'^[^\n]*', i.page_content)
57
+ st.write(i.page_content[res.span()[0]:res.span()[1]])