awacke1 commited on
Commit
148c8fc
Β·
verified Β·
1 Parent(s): 0f601cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -102
app.py CHANGED
@@ -1180,116 +1180,115 @@ def main():
1180
  create_file(filename, user_prompt, response, should_save)
1181
 
1182
 
1183
- # Function to encode file to base64
1184
- def get_base64_encoded_file(file_path):
1185
- with open(file_path, "rb") as file:
1186
- return base64.b64encode(file.read()).decode()
1187
-
1188
- # Function to create a download link
1189
- def get_audio_download_link(file_path):
1190
- base64_file = get_base64_encoded_file(file_path)
1191
- return f'<a href="data:file/wav;base64,{base64_file}" download="{os.path.basename(file_path)}">⬇️ Download Audio</a>'
1192
-
1193
- # Compose a file sidebar of past encounters
1194
- all_files = glob.glob("*.wav")
1195
- all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
1196
- all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
1197
-
1198
- filekey = 'delall'
1199
- if st.sidebar.button("πŸ—‘ Delete All Audio", key=filekey):
1200
- for file in all_files:
1201
- os.remove(file)
1202
- st.experimental_rerun()
1203
-
1204
  for file in all_files:
1205
- col1, col2 = st.sidebar.columns([6, 1]) # adjust the ratio as needed
1206
- with col1:
1207
- st.markdown(file)
1208
- if st.button("🎡", key="play_" + file): # play emoji button
1209
- audio_file = open(file, 'rb')
1210
- audio_bytes = audio_file.read()
1211
- st.audio(audio_bytes, format='audio/wav')
1212
- #st.markdown(get_audio_download_link(file), unsafe_allow_html=True)
1213
- #st.text_input(label="", value=file)
1214
- with col2:
1215
- if st.button("πŸ—‘", key="delete_" + file):
1216
- os.remove(file)
1217
- st.experimental_rerun()
1218
-
 
 
 
1219
 
1220
 
1221
- GiveFeedback=False
1222
- if GiveFeedback:
1223
- with st.expander("Give your feedback πŸ‘", expanded=False):
1224
- feedback = st.radio("Step 8: Give your feedback", ("πŸ‘ Upvote", "πŸ‘Ž Downvote"))
1225
- if feedback == "πŸ‘ Upvote":
1226
- st.write("You upvoted πŸ‘. Thank you for your feedback!")
1227
- else:
1228
- st.write("You downvoted πŸ‘Ž. Thank you for your feedback!")
1229
- load_dotenv()
1230
- st.write(css, unsafe_allow_html=True)
1231
- st.header("Chat with documents :books:")
1232
- user_question = st.text_input("Ask a question about your documents:")
1233
- if user_question:
1234
- process_user_input(user_question)
1235
- with st.sidebar:
1236
- st.subheader("Your documents")
1237
- docs = st.file_uploader("import documents", accept_multiple_files=True)
1238
- with st.spinner("Processing"):
1239
- raw = pdf2txt(docs)
1240
- if len(raw) > 0:
1241
- length = str(len(raw))
1242
- text_chunks = txt2chunks(raw)
1243
- vectorstore = vector_store(text_chunks)
1244
- st.session_state.conversation = get_chain(vectorstore)
1245
- st.markdown('# AI Search Index of Length:' + length + ' Created.') # add timing
1246
- filename = generate_filename(raw, 'txt')
1247
- create_file(filename, raw, '', should_save)
1248
-
1249
 
1250
- try:
1251
- query_params = st.query_params
1252
- query = (query_params.get('q') or query_params.get('query') or [''])
1253
- if query: search_glossary(query)
1254
- except:
1255
- st.markdown(' ')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1256
 
1257
- # Display the glossary grid
1258
- st.markdown("### πŸŽ²πŸ—ΊοΈ Word Game Gallery")
 
 
1259
 
1260
- display_glossary_grid(roleplaying_glossary) # Word Glossary Jump Grid
1261
- display_buttons_with_scores() # Feedback Jump Grid
1262
 
1263
- display_videos_and_links() # Video Jump Grid
1264
- display_images_and_wikipedia_summaries() # Image Jump Grid
 
 
1265
 
1266
-
1267
- if 'action' in st.query_params:
1268
- action = st.query_params()['action'][0] # Get the first (or only) 'action' parameter
1269
- if action == 'show_message':
1270
- st.success("Showing a message because 'action=show_message' was found in the URL.")
1271
- elif action == 'clear':
1272
- clear_query_params()
1273
- st.experimental_rerun()
1274
-
1275
- # Handling repeated keys
1276
- if 'multi' in st.query_params:
1277
- multi_values = get_all_query_params('multi')
1278
- st.write("Values for 'multi':", multi_values)
1279
-
1280
- # Manual entry for demonstration
1281
- st.write("Enter query parameters in the URL like this: ?action=show_message&multi=1&multi=2")
1282
-
1283
- if 'query' in st.query_params:
1284
- query = st.query_params['query'][0] # Get the query parameter
1285
- # Display content or image based on the query
1286
- display_content_or_image(query)
1287
-
1288
- # Add a clear query parameters button for convenience
1289
- if st.button("Clear Query Parameters", key='ClearQueryParams'):
1290
- # This will clear the browser URL's query parameters
1291
- st.experimental_set_query_params
1292
- st.experimental_rerun()
1293
 
1294
  # 18. Run AI Pipeline
1295
  if __name__ == "__main__":
 
1180
  create_file(filename, user_prompt, response, should_save)
1181
 
1182
 
1183
+ # Function to encode file to base64
1184
+ def get_base64_encoded_file(file_path):
1185
+ with open(file_path, "rb") as file:
1186
+ return base64.b64encode(file.read()).decode()
1187
+
1188
+ # Function to create a download link
1189
+ def get_audio_download_link(file_path):
1190
+ base64_file = get_base64_encoded_file(file_path)
1191
+ return f'<a href="data:file/wav;base64,{base64_file}" download="{os.path.basename(file_path)}">⬇️ Download Audio</a>'
1192
+
1193
+ # Compose a file sidebar of past encounters
1194
+ all_files = glob.glob("*.wav")
1195
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
1196
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
1197
+
1198
+ filekey = 'delall'
1199
+ if st.sidebar.button("πŸ—‘ Delete All Audio", key=filekey):
 
 
 
 
1200
  for file in all_files:
1201
+ os.remove(file)
1202
+ st.experimental_rerun()
1203
+
1204
+ for file in all_files:
1205
+ col1, col2 = st.sidebar.columns([6, 1]) # adjust the ratio as needed
1206
+ with col1:
1207
+ st.markdown(file)
1208
+ if st.button("🎡", key="play_" + file): # play emoji button
1209
+ audio_file = open(file, 'rb')
1210
+ audio_bytes = audio_file.read()
1211
+ st.audio(audio_bytes, format='audio/wav')
1212
+ #st.markdown(get_audio_download_link(file), unsafe_allow_html=True)
1213
+ #st.text_input(label="", value=file)
1214
+ with col2:
1215
+ if st.button("πŸ—‘", key="delete_" + file):
1216
+ os.remove(file)
1217
+ st.experimental_rerun()
1218
 
1219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1220
 
1221
+ GiveFeedback=False
1222
+ if GiveFeedback:
1223
+ with st.expander("Give your feedback πŸ‘", expanded=False):
1224
+ feedback = st.radio("Step 8: Give your feedback", ("πŸ‘ Upvote", "πŸ‘Ž Downvote"))
1225
+ if feedback == "πŸ‘ Upvote":
1226
+ st.write("You upvoted πŸ‘. Thank you for your feedback!")
1227
+ else:
1228
+ st.write("You downvoted πŸ‘Ž. Thank you for your feedback!")
1229
+ load_dotenv()
1230
+ st.write(css, unsafe_allow_html=True)
1231
+ st.header("Chat with documents :books:")
1232
+ user_question = st.text_input("Ask a question about your documents:")
1233
+ if user_question:
1234
+ process_user_input(user_question)
1235
+ with st.sidebar:
1236
+ st.subheader("Your documents")
1237
+ docs = st.file_uploader("import documents", accept_multiple_files=True)
1238
+ with st.spinner("Processing"):
1239
+ raw = pdf2txt(docs)
1240
+ if len(raw) > 0:
1241
+ length = str(len(raw))
1242
+ text_chunks = txt2chunks(raw)
1243
+ vectorstore = vector_store(text_chunks)
1244
+ st.session_state.conversation = get_chain(vectorstore)
1245
+ st.markdown('# AI Search Index of Length:' + length + ' Created.') # add timing
1246
+ filename = generate_filename(raw, 'txt')
1247
+ create_file(filename, raw, '', should_save)
1248
+
1249
+
1250
+ try:
1251
+ query_params = st.query_params
1252
+ query = (query_params.get('q') or query_params.get('query') or [''])
1253
+ if query: search_glossary(query)
1254
+ except:
1255
+ st.markdown(' ')
1256
+
1257
+ # Display the glossary grid
1258
+ st.markdown("### πŸŽ²πŸ—ΊοΈ Word Game Gallery")
1259
+
1260
+ display_glossary_grid(roleplaying_glossary) # Word Glossary Jump Grid
1261
+ display_buttons_with_scores() # Feedback Jump Grid
1262
+ display_videos_and_links() # Video Jump Grid
1263
+ display_images_and_wikipedia_summaries() # Image Jump Grid
1264
+
1265
+
1266
+ if 'action' in st.query_params:
1267
+ action = st.query_params()['action'][0] # Get the first (or only) 'action' parameter
1268
+ if action == 'show_message':
1269
+ st.success("Showing a message because 'action=show_message' was found in the URL.")
1270
+ elif action == 'clear':
1271
+ clear_query_params()
1272
+ st.experimental_rerun()
1273
 
1274
+ # Handling repeated keys
1275
+ if 'multi' in st.query_params:
1276
+ multi_values = get_all_query_params('multi')
1277
+ st.write("Values for 'multi':", multi_values)
1278
 
1279
+ # Manual entry for demonstration
1280
+ st.write("Enter query parameters in the URL like this: ?action=show_message&multi=1&multi=2")
1281
 
1282
+ if 'query' in st.query_params:
1283
+ query = st.query_params['query'][0] # Get the query parameter
1284
+ # Display content or image based on the query
1285
+ display_content_or_image(query)
1286
 
1287
+ # Add a clear query parameters button for convenience
1288
+ if st.button("Clear Query Parameters", key='ClearQueryParams'):
1289
+ # This will clear the browser URL's query parameters
1290
+ st.experimental_set_query_params
1291
+ st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1292
 
1293
  # 18. Run AI Pipeline
1294
  if __name__ == "__main__":