htafer commited on
Commit
79ab819
1 Parent(s): 13977d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -4
app.py CHANGED
@@ -9,12 +9,53 @@ from langchain.vectorstores import FAISS
9
  from langchain.memory import ConversationBufferMemory
10
  import re
11
  def main():
12
- st.title('Simple Streamlit App')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- st.write("Welcome to our simple static Streamlit application!")
 
 
 
 
 
15
 
16
- if st.button('Click me!'):
17
- st.write('Thanks for clicking!')
18
 
19
  if __name__ == "__main__":
20
  main()
 
9
  from langchain.memory import ConversationBufferMemory
10
  import re
11
  def main():
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]])
58
 
 
 
59
 
60
  if __name__ == "__main__":
61
  main()