code: import streamlit as st
  import pandas as pd
  import json
  import requests
  from datetime import datetime
  from pathlib import Path
  import lancedb
  from services.lancedb_index import IndexDocuments
  from services.lancedb_notes import IndexDocumentsNotes
  import yaml
  
  ### For multipage note taking, save to a json and then load the json in a state
  ### Delete the file
  
  
  ### Create second page for using notes and searching those
  ### ### Sends it to a notes embedding api and index
  ### Create setup page to add templates, research buckets, etc
  ### ### Store initially as json, then as sqlite
  ### Create a drag and drop file upload with schema to add data
  ### ### Low priority
  ### Create a page where selected results are sent to a model for summaries for review/notes/etc
  ### ### Tag with the query, datetime, maybe a name from a modal pop out?
  ### Create a notes template
  
  st.set_page_config(layout='wide',
                    page_title='Search')
  
  notes_folder = Path('data/notes')
  collections_folder = Path('data/collections')
  tmp_folder = Path('data/tmp')
  
  st.title("Welcome to streamSearchable\n**Your local reSearch engine**")
  
  
  st.header("Query your data here:")
  index_folder = Path('indexes')
  
  index = lancedb.connect(index_folder)
  available_indexes = index.table_names()
  index_to_search = st.selectbox(label='Available Indexes', options=available_indexes)
  
  query = st.text_input(label="What do you want to search?", value='')
  result_cutoff = st.number_input(label='Result cutoff', value=50)
  keyword_importance = st.slider(label='Importance of keyword matches',
                                 min_value=0.0, max_value=1.0, step=0.05, value=0.5)
  st.session_state['query'] = query
  
  @st.cache_data
  def remote_search(query, collection_name):
      results = requests.post('http://localhost:8000/hybrid',
                             json={'query':query, 'collection_name':collection_name,
                                   'top_k':50, 'fts_weight':keyword_importance, 'vec_weight':1-keyword_importance})
  
      result_data, available_fields = results.json()
      available_fields = set(available_fields)
      new_fields = set()
      for result in result_data:
          if 'metadata' in result and len(result['metadata']) > 0:
              metadata = json.loads(result['metadata'])
              result.update(metadata)
              new_fields.update(metadata.keys())
              del result['metadata']
  
      return result_data, sorted(list(new_fields))
  
  if 'results_to_save' not in st.session_state:
      st.session_state['results_to_save'] = dict()
  def add_result_to_save(result):
      note_hash = hash(str(result))
      st.write(st.session_state['results_to_save'].keys())
      if note_hash not in st.session_state['results_to_save']:
          st.session_state['results_to_save'][note_hash] = result
      else:
          del st.session_state['results_to_save'][note_hash]
  
  with st.sidebar:
      current_collections = index.table_names()
      selected_collections = st.selectbox('Existing Collection Destination',
                                        options=current_collections)
  
      new_index = st.text_input(label='Create new index', value='')
  
      note_quick_view = [x['title'] for _hash, x in st.session_state['results_to_save'].items()]
      st.markdown("Selected Notes")
      st.json(note_quick_view, expanded=False)
      if st.button('Save selected results'):
          if new_index != '':
              notes_save_name = new_index
          else:
              notes_save_name = selected_collections
          notes_save_name = notes_save_name + '_notes'
          notes_save_path = collections_folder.joinpath(notes_save_name)
          notes_save_path.mkdir(parents=True, exist_ok=True)
          notes_save_file = notes_save_path.joinpath('notes.json')
          save_data = list()
          for session_key, _note in st.session_state['results_to_save'].items():
              save_data.append(_note)
          with open(notes_save_file, 'w') as f:
              json.dump(save_data, f)
  
          indexer = IndexDocumentsNotes(field_mapping={'text':'text', 'tags':'tags','title':'title','date':'date'},
                                   source_file=notes_save_file,
                                   index_name=notes_save_name,
                                   overwrite=False)
          indexer.open_json()
          indexer.create_documents()
          indexer.ingest()
          st.write('done')
  
  if query:
      query_results, available_fields = remote_search(query, index_to_search)
      show_fields = st.multiselect("Show Fields", available_fields, default=available_fields)
      st.write(len(query_results))
      for index, result in enumerate(query_results):
          st.markdown(f"**:blue[{result['title']}]**")
          st.markdown(f"*:blue[Score: {round(result['score'], 3)}]*")
          with st.container():
              st.write(f"{' '.join(result['text'].split(' ')[:100])}.....")
              with st.expander('See Full Text and Details'):
                  full_text, quick_annotate = st.columns([4,1])
                  with full_text:
                      if 'date' in result:
                          st.markdown(f"""**Date:** {result['date']}""")
                      if 'tags' in result:
                          st.markdown(f"""**Tags:** {', '.join(result['tags'])}""")
                      st.markdown('**Text:**')
                      st.markdown(result['text'])
                      for _field in show_fields:
                          st.markdown(f"**{_field}:** {result[_field]}")
              save_to_collection = st.toggle('Save to collection',key=f'toggle_{index}',
                                             on_change=add_result_to_save, args=(result, ))
          st.divider()
  
  
apis: lancedb.connect
extract_api: [((864, 918), 'streamlit.set_page_config', 'st.set_page_config', ([], {'layout': '"""wide"""', 'page_title': '"""Search"""'}), "(layout='wide', page_title='Search')\n", (882, 918), True, 'import streamlit as st\n'), ((953, 971), 'pathlib.Path', 'Path', (['"""data/notes"""'], {}), "('data/notes')\n", (957, 971), False, 'from pathlib import Path\n'), ((993, 1017), 'pathlib.Path', 'Path', (['"""data/collections"""'], {}), "('data/collections')\n", (997, 1017), False, 'from pathlib import Path\n'), ((1031, 1047), 'pathlib.Path', 'Path', (['"""data/tmp"""'], {}), "('data/tmp')\n", (1035, 1047), False, 'from pathlib import Path\n'), ((1049, 1123), 'streamlit.title', 'st.title', (['"""Welcome to streamSearchable\n**Your local reSearch engine**"""'], {}), '("""Welcome to streamSearchable\n**Your local reSearch engine**""")\n', (1057, 1123), True, 'import streamlit as st\n'), ((1123, 1157), 'streamlit.header', 'st.header', (['"""Query your data here:"""'], {}), "('Query your data here:')\n", (1132, 1157), True, 'import streamlit as st\n'), ((1173, 1188), 'pathlib.Path', 'Path', (['"""indexes"""'], {}), "('indexes')\n", (1177, 1188), False, 'from pathlib import Path\n'), ((1198, 1227), 'lancedb.connect', 'lancedb.connect', (['index_folder'], {}), '(index_folder)\n', (1213, 1227), False, 'import lancedb\n'), ((1286, 1352), 'streamlit.selectbox', 'st.selectbox', ([], {'label': '"""Available Indexes"""', 'options': 'available_indexes'}), "(label='Available Indexes', options=available_indexes)\n", (1298, 1352), True, 'import streamlit as st\n'), ((1362, 1422), 'streamlit.text_input', 'st.text_input', ([], {'label': '"""What do you want to search?"""', 'value': '""""""'}), "(label='What do you want to search?', value='')\n", (1375, 1422), True, 'import streamlit as st\n'), ((1439, 1487), 'streamlit.number_input', 'st.number_input', ([], {'label': '"""Result cutoff"""', 'value': '(50)'}), "(label='Result cutoff', value=50)\n", (1454, 1487), True, 'import streamlit as st\n'), ((1509, 1614), 'streamlit.slider', 'st.slider', ([], {'label': '"""Importance of keyword matches"""', 'min_value': '(0.0)', 'max_value': '(1.0)', 'step': '(0.05)', 'value': '(0.5)'}), "(label='Importance of keyword matches', min_value=0.0, max_value=\n    1.0, step=0.05, value=0.5)\n", (1518, 1614), True, 'import streamlit as st\n'), ((1748, 1945), 'requests.post', 'requests.post', (['"""http://localhost:8000/hybrid"""'], {'json': "{'query': query, 'collection_name': collection_name, 'top_k': 50,\n    'fts_weight': keyword_importance, 'vec_weight': 1 - keyword_importance}"}), "('http://localhost:8000/hybrid', json={'query': query,\n    'collection_name': collection_name, 'top_k': 50, 'fts_weight':\n    keyword_importance, 'vec_weight': 1 - keyword_importance})\n", (1761, 1945), False, 'import requests\n'), ((2933, 3009), 'streamlit.selectbox', 'st.selectbox', (['"""Existing Collection Destination"""'], {'options': 'current_collections'}), "('Existing Collection Destination', options=current_collections)\n", (2945, 3009), True, 'import streamlit as st\n'), ((3065, 3114), 'streamlit.text_input', 'st.text_input', ([], {'label': '"""Create new index"""', 'value': '""""""'}), "(label='Create new index', value='')\n", (3078, 3114), True, 'import streamlit as st\n'), ((3215, 3244), 'streamlit.markdown', 'st.markdown', (['"""Selected Notes"""'], {}), "('Selected Notes')\n", (3226, 3244), True, 'import streamlit as st\n'), ((3249, 3289), 'streamlit.json', 'st.json', (['note_quick_view'], {'expanded': '(False)'}), '(note_quick_view, expanded=False)\n', (3256, 3289), True, 'import streamlit as st\n'), ((3297, 3331), 'streamlit.button', 'st.button', (['"""Save selected results"""'], {}), "('Save selected results')\n", (3306, 3331), True, 'import streamlit as st\n'), ((4444, 4517), 'streamlit.multiselect', 'st.multiselect', (['"""Show Fields"""', 'available_fields'], {'default': 'available_fields'}), "('Show Fields', available_fields, default=available_fields)\n", (4458, 4517), True, 'import streamlit as st\n'), ((3957, 4141), 'services.lancedb_notes.IndexDocumentsNotes', 'IndexDocumentsNotes', ([], {'field_mapping': "{'text': 'text', 'tags': 'tags', 'title': 'title', 'date': 'date'}", 'source_file': 'notes_save_file', 'index_name': 'notes_save_name', 'overwrite': '(False)'}), "(field_mapping={'text': 'text', 'tags': 'tags', 'title':\n    'title', 'date': 'date'}, source_file=notes_save_file, index_name=\n    notes_save_name, overwrite=False)\n", (3976, 4141), False, 'from services.lancedb_notes import IndexDocumentsNotes\n'), ((4322, 4338), 'streamlit.write', 'st.write', (['"""done"""'], {}), "('done')\n", (4330, 4338), True, 'import streamlit as st\n'), ((4610, 4654), 'streamlit.markdown', 'st.markdown', (['f"""**:blue[{result[\'title\']}]**"""'], {}), '(f"**:blue[{result[\'title\']}]**")\n', (4621, 4654), True, 'import streamlit as st\n'), ((5610, 5622), 'streamlit.divider', 'st.divider', ([], {}), '()\n', (5620, 5622), True, 'import streamlit as st\n'), ((2230, 2260), 'json.loads', 'json.loads', (["result['metadata']"], {}), "(result['metadata'])\n", (2240, 2260), False, 'import json\n'), ((3914, 3937), 'json.dump', 'json.dump', (['save_data', 'f'], {}), '(save_data, f)\n', (3923, 3937), False, 'import json\n'), ((4736, 4750), 'streamlit.container', 'st.container', ([], {}), '()\n', (4748, 4750), True, 'import streamlit as st\n'), ((5458, 5563), 'streamlit.toggle', 'st.toggle', (['"""Save to collection"""'], {'key': 'f"""toggle_{index}"""', 'on_change': 'add_result_to_save', 'args': '(result,)'}), "('Save to collection', key=f'toggle_{index}', on_change=\n    add_result_to_save, args=(result,))\n", (5467, 5563), True, 'import streamlit as st\n'), ((4843, 4883), 'streamlit.expander', 'st.expander', (['"""See Full Text and Details"""'], {}), "('See Full Text and Details')\n", (4854, 4883), True, 'import streamlit as st\n'), ((4929, 4947), 'streamlit.columns', 'st.columns', (['[4, 1]'], {}), '([4, 1])\n', (4939, 4947), True, 'import streamlit as st\n'), ((5234, 5258), 'streamlit.markdown', 'st.markdown', (['"""**Text:**"""'], {}), "('**Text:**')\n", (5245, 5258), True, 'import streamlit as st\n'), ((5279, 5306), 'streamlit.markdown', 'st.markdown', (["result['text']"], {}), "(result['text'])\n", (5290, 5306), True, 'import streamlit as st\n'), ((5044, 5086), 'streamlit.markdown', 'st.markdown', (['f"""**Date:** {result[\'date\']}"""'], {}), '(f"**Date:** {result[\'date\']}")\n', (5055, 5086), True, 'import streamlit as st\n'), ((5378, 5424), 'streamlit.markdown', 'st.markdown', (['f"""**{_field}:** {result[_field]}"""'], {}), "(f'**{_field}:** {result[_field]}')\n", (5389, 5424), True, 'import streamlit as st\n')]