Anne31415 commited on
Commit
2b04423
·
1 Parent(s): faa0b97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -18
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  from datasets import load_dataset
 
3
  import pickle
4
  from nltk.tokenize import sent_tokenize
5
  import nltk
@@ -83,6 +84,23 @@ def remove_redundant_information(text):
83
  # Define a maximum token limit to avoid infinite loops
84
  MAX_TOKEN_LIMIT = 400
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  def main():
87
  st.title("BinDocs Chat App")
88
 
@@ -102,17 +120,11 @@ def main():
102
  </style>
103
  """, unsafe_allow_html=True)
104
 
105
-
106
-
107
  if "chat_history" not in st.session_state:
108
  st.session_state['chat_history'] = []
109
 
110
  display_chat_history(st.session_state['chat_history'])
111
 
112
- st.write("<!-- Start Spacer -->", unsafe_allow_html=True)
113
- st.write("<div style='flex: 1;'></div>", unsafe_allow_html=True)
114
- st.write("<!-- End Spacer -->", unsafe_allow_html=True)
115
-
116
  new_messages_placeholder = st.empty()
117
 
118
  pdf = st.file_uploader("Upload your PDF", type="pdf")
@@ -134,37 +146,51 @@ def main():
134
 
135
  with get_openai_callback() as cb:
136
  response = chain.run(input_documents=docs, question=query)
137
-
138
  # Post-processing to remove incomplete sentences and redundant information
139
  filtered_response = remove_incomplete_sentences(response)
140
  filtered_response = remove_redundant_information(filtered_response)
141
 
142
- # Check if the response ends with a sentence-ending punctuation
143
- while not filtered_response.strip().endswith(('.', '!', '?')) and max_tokens < MAX_TOKEN_LIMIT:
144
- max_tokens += 100 # Increase the max_tokens limit
145
- chain = load_chatbot(max_tokens=max_tokens)
146
- additional_response = chain.run(input_documents=docs, question=query)
147
- filtered_response += additional_response # Append the additional response to the filtered_response
148
 
149
  st.session_state['chat_history'].append(("Bot", filtered_response, "new"))
150
 
151
- # Display new messages at the bottom
152
  new_messages = st.session_state['chat_history'][-2:]
153
  for chat in new_messages:
154
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
155
  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)
156
 
157
- # Scroll to the latest response using JavaScript
158
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
159
 
160
  loading_message.empty()
161
 
162
- # Clear the input field by setting the query variable to an empty string
163
  query = ""
164
 
165
- # Mark all messages as old after displaying
166
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  if __name__ == "__main__":
169
  main()
170
-
 
1
  import os
2
  from datasets import load_dataset
3
+ import random
4
  import pickle
5
  from nltk.tokenize import sent_tokenize
6
  import nltk
 
84
  # Define a maximum token limit to avoid infinite loops
85
  MAX_TOKEN_LIMIT = 400
86
 
87
+ import random
88
+
89
+ def generate_dynamic_question(text_chunks):
90
+ # Randomly pick a sentence from the text chunks and frame a question
91
+ random_sentence = random.choice(text_chunks)
92
+ return f"What is the significance of: '{random_sentence}'?"
93
+
94
+ def display_example_questions(dynamic_question):
95
+ if not st.session_state['chat_history']:
96
+ st.markdown("""
97
+ <div class="question-box" id="question1">Was genau ist ein Belegarzt?</div>
98
+ <div class="question-box" id="question2">Wofür wird die Alpha-ID verwendet?</div>
99
+ <br>
100
+ <div class="question-box" id="question3">Was sind die Vorteile des ambulanten operierens?</div>
101
+ <div class="question-box" id="question4">AI Generated Question: {0}</div>
102
+ """.format(dynamic_question), unsafe_allow_html=True)
103
+
104
  def main():
105
  st.title("BinDocs Chat App")
106
 
 
120
  </style>
121
  """, unsafe_allow_html=True)
122
 
 
 
123
  if "chat_history" not in st.session_state:
124
  st.session_state['chat_history'] = []
125
 
126
  display_chat_history(st.session_state['chat_history'])
127
 
 
 
 
 
128
  new_messages_placeholder = st.empty()
129
 
130
  pdf = st.file_uploader("Upload your PDF", type="pdf")
 
146
 
147
  with get_openai_callback() as cb:
148
  response = chain.run(input_documents=docs, question=query)
149
+
150
  # Post-processing to remove incomplete sentences and redundant information
151
  filtered_response = remove_incomplete_sentences(response)
152
  filtered_response = remove_redundant_information(filtered_response)
153
 
154
+ # Dynamic question generation and display example questions
155
+ dynamic_question = generate_dynamic_question(chunks)
156
+ display_example_questions(dynamic_question)
 
 
 
157
 
158
  st.session_state['chat_history'].append(("Bot", filtered_response, "new"))
159
 
 
160
  new_messages = st.session_state['chat_history'][-2:]
161
  for chat in new_messages:
162
  background_color = "#FFA07A" if chat[2] == "new" else "#acf" if chat[0] == "User" else "#caf"
163
  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)
164
 
 
165
  st.write("<script>document.getElementById('response').scrollIntoView();</script>", unsafe_allow_html=True)
166
 
167
  loading_message.empty()
168
 
 
169
  query = ""
170
 
 
171
  st.session_state['chat_history'] = [(sender, msg, "old") for sender, msg, _ in st.session_state['chat_history']]
172
+
173
+ # JavaScript for handling question box clicks
174
+ st.markdown("""
175
+ <script>
176
+ document.getElementById('question1').onclick = function() {
177
+ document.querySelector('input').value = 'Was genau ist ein Belegarzt?';
178
+ document.querySelector('input').dispatchEvent(new Event('change'));
179
+ };
180
+ document.getElementById('question2').onclick = function() {
181
+ document.querySelector('input').value = 'Wofür wird die Alpha-ID verwendet?';
182
+ document.querySelector('input').dispatchEvent(new Event('change'));
183
+ };
184
+ document.getElementById('question3').onclick = function() {
185
+ document.querySelector('input').value = 'Was sind die Vorteile des ambulanten operierens?';
186
+ document.querySelector('input').dispatchEvent(new Event('change'));
187
+ };
188
+ document.getElementById('question4').onclick = function() {
189
+ document.querySelector('input').value = 'AI Generated Question: {0}';
190
+ document.querySelector('input').dispatchEvent(new Event('change'));
191
+ };
192
+ </script>
193
+ """.format(dynamic_question), unsafe_allow_html=True)
194
 
195
  if __name__ == "__main__":
196
  main()