Anne31415 commited on
Commit
6755ee0
·
1 Parent(s): 2efa07b

Update app.py

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