Anne31415 commited on
Commit
403a475
·
1 Parent(s): 8855e48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -31
app.py CHANGED
@@ -12,32 +12,36 @@ import os
12
 
13
  # Sidebar contents
14
  with st.sidebar:
15
- st.title(':orange[BinDoc GmbH]')
16
  st.markdown(
17
  "Experience the future of document interaction with the revolutionary"
18
  )
19
 
20
  st.markdown("**BinDocs Chat App**.")
21
 
22
-
23
  st.markdown("Harnessing the power of a Large Language Model and AI technology,")
24
-
25
-
26
 
27
  st.markdown("this innovative platform redefines PDF engagement,")
28
 
29
  st.markdown("enabling dynamic conversations that bridge the gap between")
30
  st.markdown("human and machine intelligence.")
31
 
32
-
33
-
34
  add_vertical_space(3) # Add more vertical space between text blocks
35
  st.write('Made with ❤️ by Anne')
 
 
 
 
 
 
 
 
 
 
36
 
37
- # User input for the OpenAI API key
38
- openai_api_key = st.text_input("Enter your OpenAI API key:")
39
 
40
- def load_pdf(file_path, openai_api_key):
 
41
  pdf_reader = PdfReader(file_path)
42
  text = ""
43
  for page in pdf_reader.pages:
@@ -56,18 +60,15 @@ def load_pdf(file_path, openai_api_key):
56
  with open(f"{store_name}.pkl", "rb") as f:
57
  VectorStore = pickle.load(f)
58
  else:
59
- embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key) # Pass the API key here
60
  VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
61
  with open(f"{store_name}.pkl", "wb") as f:
62
  pickle.dump(VectorStore, f)
63
 
64
  return VectorStore
65
 
66
- def load_chatbot(openai_api_key):
67
- openai_config = {
68
- "api_key": openai_api_key
69
- }
70
- return load_qa_chain(llm=OpenAI(config=openai_config), chain_type="stuff")
71
 
72
  def main():
73
  st.title("BinDocs Chat App")
@@ -80,12 +81,6 @@ def main():
80
  if "current_input" not in st.session_state:
81
  st.session_state['current_input'] = ""
82
 
83
- if "processing_input" not in st.session_state:
84
- st.session_state['processing_input'] = ""
85
-
86
- # Pass the openai_api_key parameter here
87
- VectorStore = load_pdf(pdf, openai_api_key)
88
-
89
  display_chat_history(st.session_state['chat_history'])
90
 
91
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
@@ -95,21 +90,18 @@ def main():
95
  if pdf is not None:
96
  query = st.text_input("Ask questions about your PDF file (in any preferred language):", value=st.session_state['current_input'])
97
 
98
- if query != st.session_state['current_input']:
99
- st.session_state['current_input'] = query
100
-
101
  if st.button("Ask"):
102
- st.session_state['processing_input'] = st.session_state['current_input']
103
- st.session_state['chat_history'].append(("User", st.session_state['processing_input'], "new"))
104
 
105
  loading_message = st.empty()
106
  loading_message.text('Bot is thinking...')
107
 
108
- VectorStore = load_pdf(pdf, openai_api_key) # Pass the API key here
109
- chain = load_chatbot(openai_api_key) # Pass the API key here
110
- docs = VectorStore.similarity_search(query=st.session_state['processing_input'], k=3)
111
  with get_openai_callback() as cb:
112
- response = chain.run(input_documents=docs, question=st.session_state['processing_input'])
113
 
114
  # Display the bot's response immediately using JavaScript
115
  st.write(f"<div id='response' style='background-color: #caf; padding: 10px; border-radius: 10px; margin: 10px;'>Bot: {response}</div>", unsafe_allow_html=True)
@@ -120,5 +112,10 @@ def main():
120
  # Mark all messages as old after displaying
121
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
122
 
 
 
 
 
 
123
  if __name__ == "__main__":
124
- main()
 
12
 
13
  # Sidebar contents
14
  with st.sidebar:
15
+ st.title(':orange_book: BinDoc GmbH')
16
  st.markdown(
17
  "Experience the future of document interaction with the revolutionary"
18
  )
19
 
20
  st.markdown("**BinDocs Chat App**.")
21
 
 
22
  st.markdown("Harnessing the power of a Large Language Model and AI technology,")
 
 
23
 
24
  st.markdown("this innovative platform redefines PDF engagement,")
25
 
26
  st.markdown("enabling dynamic conversations that bridge the gap between")
27
  st.markdown("human and machine intelligence.")
28
 
 
 
29
  add_vertical_space(3) # Add more vertical space between text blocks
30
  st.write('Made with ❤️ by Anne')
31
+
32
+ # API key input (this will not display the entered text)
33
+ api_key = st.text_input('Enter your OpenAI API Key:', type='password')
34
+
35
+ if api_key:
36
+ os.environ['OPENAI_API_KEY'] = api_key
37
+ else:
38
+ st.warning('API key is required to proceed.')
39
+
40
+
41
 
 
 
42
 
43
+
44
+ def load_pdf(file_path):
45
  pdf_reader = PdfReader(file_path)
46
  text = ""
47
  for page in pdf_reader.pages:
 
60
  with open(f"{store_name}.pkl", "rb") as f:
61
  VectorStore = pickle.load(f)
62
  else:
63
+ embeddings = OpenAIEmbeddings()
64
  VectorStore = FAISS.from_texts(chunks, embedding=embeddings)
65
  with open(f"{store_name}.pkl", "wb") as f:
66
  pickle.dump(VectorStore, f)
67
 
68
  return VectorStore
69
 
70
+ def load_chatbot():
71
+ return load_qa_chain(llm=OpenAI(), chain_type="stuff")
 
 
 
72
 
73
  def main():
74
  st.title("BinDocs Chat App")
 
81
  if "current_input" not in st.session_state:
82
  st.session_state['current_input'] = ""
83
 
 
 
 
 
 
 
84
  display_chat_history(st.session_state['chat_history'])
85
 
86
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
 
90
  if pdf is not None:
91
  query = st.text_input("Ask questions about your PDF file (in any preferred language):", value=st.session_state['current_input'])
92
 
 
 
 
93
  if st.button("Ask"):
94
+ st.session_state['current_input'] = query
95
+ st.session_state['chat_history'].append(("User", query, "new"))
96
 
97
  loading_message = st.empty()
98
  loading_message.text('Bot is thinking...')
99
 
100
+ VectorStore = load_pdf(pdf)
101
+ chain = load_chatbot()
102
+ docs = VectorStore.similarity_search(query=query, k=3)
103
  with get_openai_callback() as cb:
104
+ response = chain.run(input_documents=docs, question=query)
105
 
106
  # Display the bot's response immediately using JavaScript
107
  st.write(f"<div id='response' style='background-color: #caf; padding: 10px; border-radius: 10px; margin: 10px;'>Bot: {response}</div>", unsafe_allow_html=True)
 
112
  # Mark all messages as old after displaying
113
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
114
 
115
+ def display_chat_history(chat_history):
116
+ for chat in chat_history:
117
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
118
+ st.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
119
+
120
  if __name__ == "__main__":
121
+ main()