awacke1 commited on
Commit
b0d34ed
β€’
1 Parent(s): be00e53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -96
app.py CHANGED
@@ -1,8 +1,5 @@
1
  import streamlit as st
2
  import streamlit.components.v1 as components
3
- #import huggingface_hub
4
- #import gradio_client as gc
5
-
6
  import os
7
  import json
8
  import random
@@ -34,11 +31,23 @@ from PIL import Image
34
  from urllib.parse import quote # Ensure this import is included
35
 
36
 
37
- ## Show examples
38
- sample_outputs = {
39
- 'output_placeholder': 'The LLM will provide an answer to your question here...',
40
- 'search_placeholder': '1. What is MoE, Multi Agent Systems, Self Rewarding AI, Semantic and Episodic memory, What is AutoGen, ChatDev, Omniverse, Lumiere, SORA?'
41
- }
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  def save_file(content, file_type):
44
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
@@ -110,10 +119,13 @@ def search_arxiv(query):
110
  api_name="/update_with_rag_md"
111
  )
112
  result = parse_to_markdown(result)
113
- st.markdown(result)
114
- arxiv_results = st.text_area("ArXiv Results: ", value=result, height=700)
 
 
115
  result = str(result) # cast as string for these - check content length and format if encoding changes..
116
  result=result.replace('\\n', ' ')
 
117
  SpeechSynthesis(result) # Search History Reader / Writer IO Memory - Audio at Same time as Reading.
118
 
119
  filename=generate_filename(query, "md")
@@ -130,18 +142,6 @@ def search_arxiv(query):
130
 
131
  return result
132
 
133
- # Set page configuration with a title and favicon
134
- st.set_page_config(
135
- page_title="πŸš€πŸŒŒScholarly Article Document Search Memory",
136
- page_icon="πŸ”πŸš€πŸŒŒπŸ“–",
137
- layout="wide",
138
- initial_sidebar_state="expanded",
139
- menu_items={
140
- 'Get Help': 'https://huggingface.co/awacke1',
141
- 'Report a bug': "https://huggingface.co/spaces/awacke1",
142
- 'About': "# πŸš€πŸŒŒWorld-Ship-Design"
143
- }
144
- )
145
 
146
  # Prompts for App, for App Product, and App Product Code
147
  PromptPrefix = 'Create a specification with streamlit functions creating markdown outlines and tables rich with appropriate emojis for methodical step by step rules defining the concepts at play. Use story structure architect rules to plan, structure and write three dramatic situations to include in the rules and how to play by matching the theme for topic of '
@@ -387,6 +387,35 @@ def get_zip_download_link(zip_file):
387
  b64 = base64.b64encode(data).decode()
388
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
389
  return href # ----------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
 
391
  def FileSidebar():
392
  # ----------------------------------------------------- File Sidebar for Jump Gates ------------------------------------------
@@ -394,55 +423,105 @@ def FileSidebar():
394
  all_files = glob.glob("*.md")
395
  all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
396
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
397
- if st.sidebar.button("πŸ—‘ Delete All Text"):
398
- for file in all_files:
399
- os.remove(file)
400
- st.experimental_rerun()
401
- if st.sidebar.button("⬇️ Download All"):
402
- zip_file = create_zip_of_files(all_files)
403
- st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
404
  file_contents=''
 
405
  next_action=''
 
 
406
  for file in all_files:
407
  col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
408
  with col1:
409
  if st.button("🌐", key="md_"+file): # md emoji button
410
- #with open(file, 'r') as f:
411
- #file_contents = f.read()
412
  file_contents = load_file(file)
 
413
  next_action='md'
414
  with col2:
415
  st.markdown(get_table_download_link(file), unsafe_allow_html=True)
416
  with col3:
417
  if st.button("πŸ“‚", key="open_"+file): # open emoji button
418
- #with open(file, 'r') as f:
419
- #file_contents = f.read()
420
  file_contents = load_file(file)
 
 
 
 
 
 
421
  next_action='open'
422
  with col4:
423
  if st.button("πŸ”", key="read_"+file): # search emoji button
424
- #with open(file, 'r') as f:
425
- #file_contents = f.read()
426
  file_contents = load_file(file)
 
427
  next_action='search'
428
  with col5:
429
  if st.button("πŸ—‘", key="delete_"+file):
430
  os.remove(file)
 
431
  st.experimental_rerun()
432
 
433
 
434
  if len(file_contents) > 0:
435
  if next_action=='open':
436
- file_content_area = st.text_area("File Contents:", file_contents, height=500)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
437
  #try:
438
- if st.button("πŸ”", key="filecontentssearch"):
439
  #search_glossary(file_content_area)
440
  filesearch = PromptPrefix + file_content_area
441
  st.markdown(filesearch)
442
  if st.button(key=rerun, label='πŸ”Re-Spec' ):
443
  search_glossary(filesearch)
444
  #except:
445
- st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
446
 
447
  if next_action=='md':
448
  st.markdown(file_contents)
@@ -452,7 +531,7 @@ def FileSidebar():
452
  #try:
453
  search_glossary(file_contents)
454
  #except:
455
- st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
456
 
457
  if next_action=='search':
458
  file_content_area = st.text_area("File Contents:", file_contents, height=500)
@@ -465,11 +544,13 @@ def FileSidebar():
465
  search_glossary(filesearch)
466
 
467
  #except:
468
- st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
469
  # ----------------------------------------------------- File Sidebar for Jump Gates ------------------------------------------
470
- FileSidebar()
471
 
472
 
 
 
 
473
 
474
  # ---- Art Card Sidebar with Random Selection of image:
475
  def get_image_as_base64(url):
@@ -647,30 +728,6 @@ def display_videos_and_links():
647
  display_glossary_entity(k)
648
  col_index += 1 # Increment column index to place the next video in the next column
649
 
650
- @st.cache_resource
651
- def display_images_and_wikipedia_summariesold():
652
- image_files = [f for f in os.listdir('.') if f.endswith('.png')]
653
- if not image_files:
654
- st.write("No PNG images found in the current directory.")
655
- return
656
- image_files_sorted = sorted(image_files, key=lambda x: len(x.split('.')[0]))
657
- num_columns=4
658
-
659
- grid_sizes = [len(f.split('.')[0]) for f in image_files_sorted]
660
- col_sizes = ['small' if size <= 4 else 'medium' if size <= 8 else 'large' for size in grid_sizes]
661
- num_columns_map = {"small": 4, "medium": 3, "large": 2}
662
- current_grid_size = 0
663
- for image_file, col_size in zip(image_files_sorted, col_sizes):
664
- if current_grid_size != num_columns_map[col_size]:
665
- cols = st.columns(num_columns_map[col_size])
666
- current_grid_size = num_columns_map[col_size]
667
- col_index = 0
668
- with cols[col_index % current_grid_size]:
669
- image = Image.open(image_file)
670
- st.image(image, caption=image_file, use_column_width=True)
671
- k = image_file.split('.')[0] # Assumes keyword is the file name without extension
672
- display_glossary_entity(k)
673
-
674
  @st.cache_resource
675
  def display_images_and_wikipedia_summaries(num_columns=4):
676
  image_files = [f for f in os.listdir('.') if f.endswith('.png')]
@@ -692,8 +749,6 @@ def display_images_and_wikipedia_summaries(num_columns=4):
692
  col_index += 1 # Increment to move to the next column in the next iteration
693
 
694
 
695
-
696
-
697
  def get_all_query_params(key):
698
  return st.query_params().get(key, [])
699
 
@@ -1189,7 +1244,12 @@ def get_audio_download_link(file_path):
1189
  base64_file = get_base64_encoded_file(file_path)
1190
  return f'<a href="data:file/wav;base64,{base64_file}" download="{os.path.basename(file_path)}">⬇️ Download Audio</a>'
1191
 
1192
- # Sidebar of past encounters
 
 
 
 
 
1193
  all_files = glob.glob("*.wav")
1194
  all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
1195
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
@@ -1262,27 +1322,11 @@ if 'action' in st.query_params:
1262
  clear_query_params()
1263
  st.experimental_rerun()
1264
 
1265
- # Handling repeated keys
1266
- #if 'multi' in st.query_params:
1267
- # multi_values = get_all_query_params('multi')
1268
- # st.write("Values for 'multi':", multi_values)
1269
-
1270
- # Manual entry for demonstration
1271
- #st.write("Enter query parameters in the URL like this: ?action=show_message&multi=1&multi=2")
1272
-
1273
  if 'query' in st.query_params:
1274
  query = st.query_params['query'][0] # Get the query parameter
1275
  # Display content or image based on the query
1276
  display_content_or_image(query)
1277
 
1278
- # Add a clear query parameters button for convenience
1279
- #if st.button("Clear Query Parameters", key='ClearQueryParams'):
1280
- # This will clear the browser URL's query parameters
1281
- # st.experimental_set_query_params
1282
- # st.experimental_rerun()
1283
-
1284
-
1285
- st.markdown("### πŸŽ²πŸ—ΊοΈ Scholarly Article Document Search Memory")
1286
 
1287
  filename = save_and_play_audio(audio_recorder)
1288
  if filename is not None:
@@ -1400,18 +1444,7 @@ if len(document_sections) > 0:
1400
  create_file(filename, user_prompt, response, should_save)
1401
  st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
1402
 
1403
- #if st.button('πŸ’¬ Chat'):
1404
- # st.write('Reasoning with your inputs...')
1405
- # user_prompt_sections = divide_prompt(user_prompt, max_length)
1406
- # full_response = ''
1407
- # for prompt_section in user_prompt_sections:
1408
- # response = chat_with_model(prompt_section, ''.join(list(document_sections)), model_choice)
1409
- # full_response += response + '\n' # Combine the responses
1410
- # response = full_response
1411
- # st.write('Response:')
1412
- # st.write(response)
1413
- # filename = generate_filename(user_prompt, choice)
1414
- # create_file(filename, user_prompt, response, should_save)
1415
 
1416
  display_images_and_wikipedia_summaries() # Image Jump Grid
1417
  display_videos_and_links() # Video Jump Grid
 
1
  import streamlit as st
2
  import streamlit.components.v1 as components
 
 
 
3
  import os
4
  import json
5
  import random
 
31
  from urllib.parse import quote # Ensure this import is included
32
 
33
 
34
+ # 1. Configuration
35
+ title="πŸš€πŸŒŒArXiv Article Document Search Memory"
36
+ helpURL='https://huggingface.co/awacke1'
37
+ bugURL='https://huggingface.co/spaces/awacke1'
38
+ icons='πŸ”πŸš€πŸŒŒπŸ“–'
39
+ st.set_page_config(
40
+ page_title=title,
41
+ page_icon=icons,
42
+ layout="wide",
43
+ initial_sidebar_state="expanded",
44
+ menu_items={
45
+ 'Get Help': helpURL,
46
+ 'Report a bug': bugURL,
47
+ 'About': title
48
+ }
49
+ )
50
+
51
 
52
  def save_file(content, file_type):
53
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
 
119
  api_name="/update_with_rag_md"
120
  )
121
  result = parse_to_markdown(result)
122
+
123
+ #st.markdown(result)
124
+ #arxiv_results = st.text_area("ArXiv Results: ", value=result, height=700)
125
+
126
  result = str(result) # cast as string for these - check content length and format if encoding changes..
127
  result=result.replace('\\n', ' ')
128
+
129
  SpeechSynthesis(result) # Search History Reader / Writer IO Memory - Audio at Same time as Reading.
130
 
131
  filename=generate_filename(query, "md")
 
142
 
143
  return result
144
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  # Prompts for App, for App Product, and App Product Code
147
  PromptPrefix = 'Create a specification with streamlit functions creating markdown outlines and tables rich with appropriate emojis for methodical step by step rules defining the concepts at play. Use story structure architect rules to plan, structure and write three dramatic situations to include in the rules and how to play by matching the theme for topic of '
 
387
  b64 = base64.b64encode(data).decode()
388
  href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
389
  return href # ----------------------------------
390
+
391
+ def get_file():
392
+ st.write(st.session_state['file'])
393
+
394
+ def SaveFileTextClicked():
395
+ fileText = st.session_state.file_content_area
396
+ fileName = st.session_state.file_name_input
397
+ with open(fileName, 'w', encoding='utf-8') as file:
398
+ file.write(fileText)
399
+ st.markdown('Saved ' + fileName + '.')
400
+
401
+ def SaveFileNameClicked():
402
+ newFileName = st.session_state.file_name_input
403
+ oldFileName = st.session_state.filename
404
+ if (newFileName!=oldFileName):
405
+ os.rename(oldFileName, newFileName)
406
+ st.markdown('Renamed file ' + oldFileName + ' to ' + newFileName + '.')
407
+ newFileText = st.session_state.file_content_area
408
+ oldFileText = st.session_state.filetext
409
+
410
+
411
+ #if newFileName:
412
+ #os.rename(file_name, newFileName)
413
+ #file_name = newFileName
414
+
415
+
416
+ #if file_name_save_name:
417
+ # with open(file_name, 'w', encoding='utf-8') as file:
418
+ # file.write(file_content_area)
419
 
420
  def FileSidebar():
421
  # ----------------------------------------------------- File Sidebar for Jump Gates ------------------------------------------
 
423
  all_files = glob.glob("*.md")
424
  all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
425
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
426
+
427
+ # Delete and Download:
428
+ Files1, Files2 = st.sidebar.columns(2)
429
+
430
+ with Files1:
431
+ if st.button("πŸ—‘ Delete All"):
432
+ for file in all_files:
433
+ os.remove(file)
434
+ st.experimental_rerun()
435
+
436
+ with Files2:
437
+ if st.button("⬇️ Download"):
438
+ zip_file = create_zip_of_files(all_files)
439
+ st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
440
+
441
+
442
+
443
+
444
+
445
  file_contents=''
446
+ file_name=''
447
  next_action=''
448
+
449
+
450
  for file in all_files:
451
  col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
452
  with col1:
453
  if st.button("🌐", key="md_"+file): # md emoji button
 
 
454
  file_contents = load_file(file)
455
+ file_name=file
456
  next_action='md'
457
  with col2:
458
  st.markdown(get_table_download_link(file), unsafe_allow_html=True)
459
  with col3:
460
  if st.button("πŸ“‚", key="open_"+file): # open emoji button
 
 
461
  file_contents = load_file(file)
462
+ file_name=file
463
+ st.session_state['lastfilename'] = file
464
+ st.session_state['filename'] = file
465
+ st.session_state['filetext'] = file_contents
466
+
467
+
468
  next_action='open'
469
  with col4:
470
  if st.button("πŸ”", key="read_"+file): # search emoji button
 
 
471
  file_contents = load_file(file)
472
+ file_name=file
473
  next_action='search'
474
  with col5:
475
  if st.button("πŸ—‘", key="delete_"+file):
476
  os.remove(file)
477
+ file_name=file
478
  st.experimental_rerun()
479
 
480
 
481
  if len(file_contents) > 0:
482
  if next_action=='open':
483
+
484
+ # For "open", prep session state if it hasn't been yet
485
+ if 'lastfilename' not in st.session_state:
486
+ st.session_state['lastfilename'] = ''
487
+ if 'filename' not in st.session_state:
488
+ st.session_state['filename'] = ''
489
+ if 'filetext' not in st.session_state:
490
+ st.session_state['filetext'] = ''
491
+
492
+ open1, open2 = st.columns(spec=[.8,.2])
493
+
494
+ with open1:
495
+
496
+ # File Name Input
497
+ file_name_input = st.text_input(key='file_name_input', on_change=SaveFileNameClicked, label="File Name:",value=file_name )
498
+
499
+ # File Text Input
500
+ file_content_area = st.text_area(key='file_content_area', on_change=SaveFileTextClicked, label="File Contents:", value=file_contents, height=500)
501
+
502
+ if st.button(label='πŸ’Ύ Save File Name'):
503
+ SaveFileNameClicked()
504
+
505
+ if st.button(label='πŸ’Ύ Save File Text'):
506
+ SaveFileTextClicked()
507
+
508
+
509
+ new_file_content_area = st.session_state['file_content_area']
510
+ if new_file_content_area != file_contents:
511
+ st.markdown(new_file_content_area) #changed
512
+ #with open2:
513
+
514
+
515
+
516
  #try:
517
+ if st.button("πŸ” Run AI Meta Strategy", key="filecontentssearch"):
518
  #search_glossary(file_content_area)
519
  filesearch = PromptPrefix + file_content_area
520
  st.markdown(filesearch)
521
  if st.button(key=rerun, label='πŸ”Re-Spec' ):
522
  search_glossary(filesearch)
523
  #except:
524
+ #st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
525
 
526
  if next_action=='md':
527
  st.markdown(file_contents)
 
531
  #try:
532
  search_glossary(file_contents)
533
  #except:
534
+ #st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
535
 
536
  if next_action=='search':
537
  file_content_area = st.text_area("File Contents:", file_contents, height=500)
 
544
  search_glossary(filesearch)
545
 
546
  #except:
547
+ #st.markdown('GPT is sleeping. Restart ETA 30 seconds.')
548
  # ----------------------------------------------------- File Sidebar for Jump Gates ------------------------------------------
 
549
 
550
 
551
+ st.markdown("### πŸŽ²πŸ—ΊοΈ Scholarly Article Document Search Memory")
552
+ FileSidebar()
553
+
554
 
555
  # ---- Art Card Sidebar with Random Selection of image:
556
  def get_image_as_base64(url):
 
728
  display_glossary_entity(k)
729
  col_index += 1 # Increment column index to place the next video in the next column
730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
731
  @st.cache_resource
732
  def display_images_and_wikipedia_summaries(num_columns=4):
733
  image_files = [f for f in os.listdir('.') if f.endswith('.png')]
 
749
  col_index += 1 # Increment to move to the next column in the next iteration
750
 
751
 
 
 
752
  def get_all_query_params(key):
753
  return st.query_params().get(key, [])
754
 
 
1244
  base64_file = get_base64_encoded_file(file_path)
1245
  return f'<a href="data:file/wav;base64,{base64_file}" download="{os.path.basename(file_path)}">⬇️ Download Audio</a>'
1246
 
1247
+
1248
+
1249
+
1250
+
1251
+
1252
+ # Wav audio files - Transcription History in Wav
1253
  all_files = glob.glob("*.wav")
1254
  all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
1255
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
 
1322
  clear_query_params()
1323
  st.experimental_rerun()
1324
 
 
 
 
 
 
 
 
 
1325
  if 'query' in st.query_params:
1326
  query = st.query_params['query'][0] # Get the query parameter
1327
  # Display content or image based on the query
1328
  display_content_or_image(query)
1329
 
 
 
 
 
 
 
 
 
1330
 
1331
  filename = save_and_play_audio(audio_recorder)
1332
  if filename is not None:
 
1444
  create_file(filename, user_prompt, response, should_save)
1445
  st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
1446
 
1447
+
 
 
 
 
 
 
 
 
 
 
 
1448
 
1449
  display_images_and_wikipedia_summaries() # Image Jump Grid
1450
  display_videos_and_links() # Video Jump Grid