Anne31415 commited on
Commit
2efa07b
·
1 Parent(s): 2006ce0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -74
app.py CHANGED
@@ -12,50 +12,6 @@ from langchain.chains.question_answering import load_qa_chain
12
  from langchain.callbacks import get_openai_callback
13
  import os
14
 
15
- def cloud_button(label, key):
16
- button_id = f"button_{key}"
17
- button_html = f"""
18
- <style>
19
- #{button_id} {{
20
- display: inline-flex;
21
- align-items: center;
22
- justify-content: center;
23
- padding: 8px 15px;
24
- font-size: 14px;
25
- cursor: pointer;
26
- text-align: center;
27
- text-decoration: none;
28
- outline: none;
29
- color: #fff;
30
- background-color: #add8e6;
31
- border: none;
32
- border-radius: 20px;
33
- box-shadow: 0 5px 10px rgba(0,0,0,0.1);
34
- margin: 5px;
35
- }}
36
- #{button_id}:hover {{
37
- background-color: #9fced1;
38
- }}
39
- #{button_id}:active {{
40
- background-color: #87b6a7;
41
- box-shadow: 0 2px 5px rgba(0,0,0,0.1);
42
- transform: translateY(2px);
43
- }}
44
- </style>
45
- <button id="{button_id}" class="cloud-button">{label}</button>
46
- <script>
47
- document.getElementById("{button_id}").onclick = function() {{
48
- window.parent.postMessage({{
49
- 'setSessionState': {{ '{key}': true }}
50
- }}, "*");
51
- }};
52
- </script>
53
- """
54
-
55
- st.markdown(button_html, unsafe_allow_html=True)
56
- return st.session_state.get(key, False)
57
-
58
-
59
  # Step 1: Clone the Dataset Repository
60
  repo = Repository(
61
  local_dir="Private_Book", # Local directory to clone the repository
@@ -76,9 +32,7 @@ with st.sidebar:
76
 
77
  add_vertical_space(1) # Adjust as per the desired spacing
78
 
79
- st.markdown(""")
80
-
81
-
82
  Hello! I’m here to assist you with:<br><br>
83
  📘 **Glossary Inquiries:**<br>
84
  I can clarify terms like "DiGA", "AOP", or "BfArM", providing clear and concise explanations to help you understand our content better.<br><br>
@@ -128,57 +82,70 @@ def load_chatbot():
128
  return load_qa_chain(llm=OpenAI(), chain_type="stuff")
129
 
130
  def main():
131
- # Hide Streamlit's menu and footer
132
  hide_streamlit_style = """
133
- <style>
134
- #MainMenu {visibility: hidden;}
135
- footer {visibility: hidden;}
136
- </style>
137
- """
138
  st.markdown(hide_streamlit_style, unsafe_allow_html=True)
139
 
 
140
  # Main content
141
  st.title("Welcome to BinDocs ChatBot! 🤖")
142
-
143
- # Check if PDF file exists
144
  pdf_path = pdf_file_path
145
  if not os.path.exists(pdf_path):
146
  st.error("File not found. Please check the file path.")
147
  return
148
 
149
- # Initialize chat history in session state
150
  if "chat_history" not in st.session_state:
151
  st.session_state['chat_history'] = []
152
 
153
- # Display previous chat messages
154
  display_chat_history(st.session_state['chat_history'])
155
 
156
- # Spacer
157
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
158
  st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
159
  st.write("<!-- End Spacer -->", unsafe_allow_html=True)
160
 
161
  new_messages_placeholder = st.empty()
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  if pdf_path is not None:
164
- # User input for questions
165
  query = st.text_input("Ask questions about your PDF file (in any preferred language):")
166
 
167
- # Buttons for predefined queries
168
- if st.button("Was genau ist ein Belegarzt?", key="button1"):
169
- st.session_state['belegarzt_button'] = True
170
- if st.button("Wofür wird die Alpha-ID verwendet?", key="button2"):
171
- query = "Wofür wird die Alpha-ID verwendet?"
172
- if st.button("Was sind die Vorteile des ambulanten operierens?", key="button3"):
173
- query = "Was sind die Vorteile des ambulanten operierens?"
174
-
175
- if st.session_state.get('belegarzt_button'):
176
  query = "Was genau ist ein Belegarzt?"
177
- st.session_state['belegarzt_button'] = False # Reset the button state
178
-
 
 
 
 
179
 
180
- # Processing the query
181
- if query:
182
  loading_message = st.empty()
183
  loading_message.text('Bot is thinking...')
184
 
@@ -204,8 +171,9 @@ def main():
204
  # Clear the input field by setting the query variable to an empty string
205
  query = ""
206
 
207
- # Mark all messages as old after displaying
208
- st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
 
209
 
210
 
211
  def display_chat_history(chat_history):
 
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
 
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>
 
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;}
89
+ footer {visibility: hidden;}
90
+ </style>
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.")
102
  return
103
 
 
104
  if "chat_history" not in st.session_state:
105
  st.session_state['chat_history'] = []
106
 
 
107
  display_chat_history(st.session_state['chat_history'])
108
 
 
109
  st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
110
  st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
111
  st.write("<!-- End Spacer -->", unsafe_allow_html=True)
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
 
 
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):