Anne31415 commited on
Commit
10a642d
·
1 Parent(s): 59878ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -121
app.py CHANGED
@@ -1,114 +1,55 @@
1
  import streamlit as st
2
- import os
3
  import pickle
4
- from streamlit_extras.add_vertical_space import add_vertical_space
5
  from huggingface_hub import Repository
6
  from PyPDF2 import PdfReader
 
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  from langchain.embeddings.openai import OpenAIEmbeddings
9
  from langchain.vectorstores import FAISS
10
  from langchain.llms import OpenAI
11
  from langchain.chains.question_answering import load_qa_chain
12
  from langchain.callbacks import get_openai_callback
 
13
 
14
  # Step 1: Clone the Dataset Repository
15
  repo = Repository(
16
  local_dir="Private_Book", # Local directory to clone the repository
17
  repo_type="dataset", # Specify that this is a dataset repository
 
18
  clone_from="Anne31415/Private_Book", # Replace with your repository URL
19
- token=os.getenv("HUB_TOKEN") # Use the secret token to authenticate
 
20
  )
21
  repo.git_pull() # Pull the latest changes (if any)
22
 
23
  # Step 2: Load the PDF File
24
  pdf_file_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
25
 
26
- def cloud_button(label, key=None):
27
- button_id = f"cloud-button-{key or label}"
28
- cloud_button_html = f"""
29
- <div class="cloud" id="{button_id}">
30
- <div class="circle small"></div>
31
- <div class="circle medium"></div>
32
- <div class="circle large"></div>
33
- <div class="rectangle">{label}</div>
34
- <div class="circle medium"></div>
35
- <div class="circle small"></div>
36
- </div>
37
- <style>
38
- .cloud {{
39
- position: relative;
40
- display: inline-block;
41
- cursor: pointer;
42
- user-select: none;
43
- }}
44
- .rectangle {{
45
- width: 120px;
46
- height: 60px;
47
- background-color: white;
48
- border-radius: 30px;
49
- position: absolute;
50
- top: 20px;
51
- left: 10px;
52
- display: flex;
53
- align-items: center;
54
- justify-content: center;
55
- font-size: 16px;
56
- font-weight: bold;
57
- transition: background-color 0.4s;
58
- }}
59
- .circle {{
60
- position: absolute;
61
- background-color: white;
62
- }}
63
- .circle.small {{
64
- width: 40px;
65
- height: 40px;
66
- border-radius: 20px;
67
- top: 10px;
68
- left: 50px;
69
- }}
70
- .circle.medium {{
71
- width: 60px;
72
- height: 60px;
73
- border-radius: 30px;
74
- }}
75
- .circle.large {{
76
- width: 80px;
77
- height: 80px;
78
- border-radius: 40px;
79
- top: -10px;
80
- left: 40px;
81
- }}
82
- .cloud:hover .rectangle {{
83
- background-color: #008CBA;
84
- color: white;
85
- }}
86
- </style>
87
- <script>
88
- document.getElementById("{button_id}").onclick = function() {{
89
- google.script.run.withSuccessHandler(function(e) {{
90
- document.getElementById("{button_id}").innerText = e;
91
- }}).getCloudButtonValue("{label}");
92
- }};
93
- </script>
94
- """
95
- return cloud_button_html
96
-
97
- def spaced_cloud_button(label):
98
- button_id = f"cloud-button-{label}"
99
- cloud_button_html = f"""
100
- <div style="padding: 10px 10px 10px 0;">
101
- """
102
- cloud_button_html += cloud_button(label)
103
- cloud_button_html += """
104
- </div>
105
- """
106
- st.markdown(cloud_button_html, unsafe_allow_html=True)
107
- if st.session_state.get(button_id, False):
108
- return True
109
- return False
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
 
 
 
 
 
 
112
 
113
 
114
  def load_pdf(file_path):
@@ -137,34 +78,20 @@ def load_pdf(file_path):
137
 
138
  return VectorStore
139
 
 
 
 
 
 
 
140
  def load_chatbot():
141
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
142
 
143
- def display_chat_history(chat_history):
144
- for chat in chat_history:
145
- background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
146
- 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)
147
 
148
  def main():
149
- with st.sidebar:
150
- st.title('BinDoc GmbH')
151
- st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
152
-
153
- add_vertical_space(1) # Adjust as per the desired spacing
154
-
155
- st.markdown("""
156
- Hello! I’m here to assist you with:<br><br>
157
- 📘 **Glossary Inquiries:**<br>
158
- I can clarify terms like "DiGA", "AOP", or "BfArM", providing clear and concise explanations to help you understand our content better.<br><br>
159
- 🆘 **Help Page Navigation:**<br>
160
- Ask me if you forgot your password or want to know more about topics related to the platform.<br><br>
161
- 📰 **Latest Whitepapers Insights:**<br>
162
- Curious about our recent publications? Feel free to ask about our latest whitepapers!<br><br>
163
- """, unsafe_allow_html=True)
164
-
165
- add_vertical_space(1) # Adjust as per the desired spacing
166
- st.write('Made with ❤️ by BinDoc GmbH')
167
- api_key = os.getenv("OPENAI_API_KEY")
168
 
169
  hide_streamlit_style = """
170
  <style>
@@ -174,8 +101,11 @@ def main():
174
  """
175
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
176
 
 
177
  # Main content
178
  st.title("Welcome to BinDocs ChatBot! 🤖")
 
 
179
  pdf_path = pdf_file_path
180
  if not os.path.exists(pdf_path):
181
  st.error("File not found. Please check the file path.")
@@ -195,33 +125,63 @@ def main():
195
  if pdf_path is not None:
196
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
197
 
198
-
199
- if spaced_cloud_button("Was genau ist ein Belegarzt?"):
200
  query = "Was genau ist ein Belegarzt?"
201
-
202
- if spaced_cloud_button("Test, test test ?"):
203
- query = "Was genau ist ein Test test test ?"
204
-
205
-
206
-
 
 
 
 
 
 
207
  if st.button("Ask") or (not st.session_state['chat_history'] and query) or (st.session_state['chat_history'] and query != st.session_state['chat_history'][-1][1]):
208
  st.session_state['chat_history'].append(("User", query, "new"))
 
209
  loading_message = st.empty()
210
  loading_message.text('Bot is thinking...')
 
 
 
 
 
 
 
211
  VectorStore = load_pdf(pdf_path)
212
  chain = load_chatbot()
213
  docs = VectorStore.similarity_search(query=query, k=3)
214
- response = chain.run(input_documents=docs, question=query)
 
 
215
  st.session_state['chat_history'].append(("Bot", response, "new"))
 
 
216
  new_messages = st.session_state['chat_history'][-2:]
217
  for chat in new_messages:
218
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
219
  new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
 
 
220
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
 
221
  loading_message.empty()
 
 
222
  query = ""
 
 
223
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
224
 
225
- if __name__ == "__main__":
226
- main()
227
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from dotenv import load_dotenv
3
  import pickle
 
4
  from huggingface_hub import Repository
5
  from PyPDF2 import PdfReader
6
+ from streamlit_extras.add_vertical_space import add_vertical_space
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  from langchain.embeddings.openai import OpenAIEmbeddings
9
  from langchain.vectorstores import FAISS
10
  from langchain.llms import OpenAI
11
  from langchain.chains.question_answering import load_qa_chain
12
  from langchain.callbacks import get_openai_callback
13
+ import os
14
 
15
  # Step 1: Clone the Dataset Repository
16
  repo = Repository(
17
  local_dir="Private_Book", # Local directory to clone the repository
18
  repo_type="dataset", # Specify that this is a dataset repository
19
+
20
  clone_from="Anne31415/Private_Book", # Replace with your repository URL
21
+
22
+ token=os.environ["HUB_TOKEN"] # Use the secret token to authenticate
23
  )
24
  repo.git_pull() # Pull the latest changes (if any)
25
 
26
  # Step 2: Load the PDF File
27
  pdf_file_path = "Private_Book/KOMBI_all2.pdf" # Replace with your PDF file path
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+
31
+ with st.sidebar:
32
+ st.title('BinDoc GmbH')
33
+ st.markdown("Experience revolutionary interaction with BinDocs Chat App, leveraging state-of-the-art AI technology.")
34
+
35
+ add_vertical_space(1) # Adjust as per the desired spacing
36
+
37
+ st.markdown("""
38
+ Hello! I’m here to assist you with:<br><br>
39
+ 📘 **Glossary Inquiries:**<br>
40
+ I can clarify terms like "DiGA", "AOP", or "BfArM", providing clear and concise explanations to help you understand our content better.<br><br>
41
+ 🆘 **Help Page Navigation:**<br>
42
+ Ask me if you forgot your password or want to know more about topics related to the platform.<br><br>
43
+ 📰 **Latest Whitepapers Insights:**<br>
44
+ Curious about our recent publications? Feel free to ask about our latest whitepapers!<br><br>
45
+ """, unsafe_allow_html=True)
46
 
47
+ add_vertical_space(1) # Adjust as per the desired spacing
48
+
49
+ st.write('Made with ❤️ by BinDoc GmbH')
50
+
51
+ api_key = os.getenv("OPENAI_API_KEY")
52
+ # Retrieve the API key from st.secrets
53
 
54
 
55
  def load_pdf(file_path):
 
78
 
79
  return VectorStore
80
 
81
+ # Load the PDF file when the app starts
82
+ if "pdf_data" not in st.session_state:
83
+ st.session_state.pdf_data = load_pdf(pdf_file_path)
84
+
85
+
86
+
87
  def load_chatbot():
88
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
89
 
90
+ # Load the chatbot when the app starts
91
+ if "chatbot_instance" not in st.session_state:
92
+ st.session_state.chatbot_instance = load_chatbot()
 
93
 
94
  def main():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  hide_streamlit_style = """
97
  <style>
 
101
  """
102
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
103
 
104
+
105
  # Main content
106
  st.title("Welcome to BinDocs ChatBot! 🤖")
107
+
108
+ # Directly specifying the path to the PDF file
109
  pdf_path = pdf_file_path
110
  if not os.path.exists(pdf_path):
111
  st.error("File not found. Please check the file path.")
 
125
  if pdf_path is not None:
126
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
127
 
128
+ if st.button("Was genau ist ein Belegarzt?"):
 
129
  query = "Was genau ist ein Belegarzt?"
130
+ if st.button("Wofür wird die Alpha-ID verwendet?"):
131
+ query = "Wofür wird die Alpha-ID verwendet?"
132
+ if st.button("Was sind die Vorteile des ambulanten operierens?"):
133
+ query = "Was sind die Vorteile des ambulanten operierens?"
134
+ if st.button("Was kann ich mit dem Prognose-Analyse Toll machen?"):
135
+ query = "Was kann ich mit dem Prognose-Analyse Toll machen?"
136
+ if st.button("Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"):
137
+ query = "Was sagt mir die Farbe der Balken der Bevölkerungsentwicklung?"
138
+ if st.button("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?"):
139
+ query = ("Ich habe mein Meta Password vergessen, wie kann ich es zurücksetzen?")
140
+
141
+
142
  if st.button("Ask") or (not st.session_state['chat_history'] and query) or (st.session_state['chat_history'] and query != st.session_state['chat_history'][-1][1]):
143
  st.session_state['chat_history'].append(("User", query, "new"))
144
+
145
  loading_message = st.empty()
146
  loading_message.text('Bot is thinking...')
147
+
148
+ # Use the chatbot and PDF data from the session state
149
+ docs = st.session_state.pdf_data.similarity_search(query=query, k=3)
150
+ with get_openai_callback() as cb:
151
+ response = st.session_state.chatbot_instance.run(input_documents=docs, question=query)
152
+
153
+
154
  VectorStore = load_pdf(pdf_path)
155
  chain = load_chatbot()
156
  docs = VectorStore.similarity_search(query=query, k=3)
157
+ with get_openai_callback() as cb:
158
+ response = chain.run(input_documents=docs, question=query)
159
+
160
  st.session_state['chat_history'].append(("Bot", response, "new"))
161
+
162
+ # Display new messages at the bottom
163
  new_messages = st.session_state['chat_history'][-2:]
164
  for chat in new_messages:
165
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
166
  new_messages_placeholder.markdown(f"<div style='background-color: {background_color}; padding: 10px; border-radius: 10px; margin: 10px;'>{chat[0]}: {chat[1]}</div>", unsafe_allow_html=True)
167
+
168
+ # Scroll to the latest response using JavaScript
169
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
170
+
171
  loading_message.empty()
172
+
173
+ # Clear the input field by setting the query variable to an empty string
174
  query = ""
175
+
176
+ # Mark all messages as old after displaying
177
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
178
 
 
 
179
 
180
+
181
+ def display_chat_history(chat_history):
182
+ for chat in chat_history:
183
+ background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
184
+ 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)
185
+
186
+ if __name__ == "__main__":
187
+ main()