code: import lancedb
  import matplotlib.pyplot as plt
  import rasterio as rio
  import streamlit as st
  from rasterio.plot import show
  
  st.set_page_config(layout="wide")
  
  
  # Get preferrred chips
  def get_unique_chips(tbl):
      chips = [
          {"tile": "17MNP", "idx": "0271", "year": 2023},
          {"tile": "19HGU", "idx": "0033", "year": 2018},
          {"tile": "33NVB", "idx": "0393", "year": 2020},
          {"tile": "21JVJ", "idx": "0100", "year": 2020},
          {"tile": "34KHD", "idx": "0080", "year": 2018},
          {"tile": "19JCF", "idx": "0215", "year": 2023},
          {"tile": "20HMK", "idx": "0100", "year": 2020},
          {"tile": "37MFT", "idx": "0313", "year": 2023},
          {"tile": "49KHR", "idx": "0020", "year": 2017},
          {"tile": "55LBC", "idx": "0075", "year": 2022},
      ]
  
      tile_filter = " OR ".join(
          [
              f"(tile == '{chip['tile']}' "
              f"AND idx == '{chip['idx']}') "
              f"AND year == {chip['year']}"
              for chip in chips
          ]
      )
      result = tbl.search().where(tile_filter, prefilter=True).to_pandas()
      return result
  
  
  # Load embeddings
  @st.cache_resource()
  def connect_to_database():
      db = lancedb.connect("nbs/embeddings")
      tbl = db.open_table("clay-v001")
      return tbl
  
  
  @st.cache_resource()
  def show_samples(_tbl):
      df = get_unique_chips(_tbl)
      # df = _tbl.head(10).to_pandas()
      # sample 100 random rows
      # samples = df.sample(100).to_dict("records")
      samples = df.to_dict("records")
  
      cols = st.columns(10)
      options = {}
      for idx, sample in enumerate(samples):
          path = sample["path"]
          rgb_chip = rio.open(path).read(indexes=[3, 2, 1])
          rgb_chip = (rgb_chip - rgb_chip.min()) / (rgb_chip.max() - rgb_chip.min())
          with cols[idx % 10]:
              st.caption(f"{sample['tile']}-{sample['date']}-{sample['idx']}")
              show(rgb_chip)
              plt.axis("off")
              st.pyplot(plt)
  
          options[f"{sample['tile']}-{sample['idx']}"] = {
              "vector": sample["vector"],
              "tile": sample["tile"],
              "year": sample["year"],
          }
  
      return options
  
  
  # Function to find similar vectors
  @st.cache_data()
  def find_similar_vectors(_tbl, query):
      # tile, year = query["tile"], query["year"]
      # filter = f"tile != '{tile}'"
      result = (
          _tbl.search(query=query["vector"], vector_column_name="vector")
          .metric("cosine")
          # .where(filter, prefilter=True)
          .limit(10)
          .to_pandas()
      )
      # st.dataframe(result)
      cols = st.columns(10)
      for idx, row in result.iterrows():
          path = row["path"]
          rgb_chip = rio.open(path).read(indexes=[3, 2, 1])
          rgb_chip = (rgb_chip - rgb_chip.min()) / (rgb_chip.max() - rgb_chip.min())
          with cols[idx % 10]:
              st.caption(f"{row['tile']}-{row['date']}-{row['idx']}")
              show(rgb_chip)
              plt.axis("off")
              st.pyplot(plt)
  
  
  # Main app
  def main():
      st.title("Clayground")
  
      tbl = connect_to_database()
      options = show_samples(tbl)
  
      # UI to select an embedding
      with st.sidebar:
          selection = st.selectbox("Select a chip", options=options.keys())
  
          arithmetic = st.toggle("Arithmetic", False)
          if arithmetic:
              multiselect = st.multiselect(
                  "Select multiple chips", options=options.keys(), default=[]
              )
  
          submit = st.button("Submit")
  
      if submit and not arithmetic:
          query = options[selection]
          find_similar_vectors(tbl, query)
  
      if submit and arithmetic and len(multiselect) > 1:
          st.write("Selected:", multiselect)
          v1 = options[multiselect[0]]
          v2 = options[multiselect[1]]
          v3 = (v1["vector"] + v2["vector"]) / 2
  
          find_similar_vectors(tbl, {"vector": v3})
  
  
  if __name__ == "__main__":
      main()
  
apis: lancedb.connect
extract_api: [((125, 158), 'streamlit.set_page_config', 'st.set_page_config', ([], {'layout': '"""wide"""'}), "(layout='wide')\n", (143, 158), True, 'import streamlit as st\n'), ((1119, 1138), 'streamlit.cache_resource', 'st.cache_resource', ([], {}), '()\n', (1136, 1138), True, 'import streamlit as st\n'), ((1264, 1283), 'streamlit.cache_resource', 'st.cache_resource', ([], {}), '()\n', (1281, 1283), True, 'import streamlit as st\n'), ((2176, 2191), 'streamlit.cache_data', 'st.cache_data', ([], {}), '()\n', (2189, 2191), True, 'import streamlit as st\n'), ((1175, 1208), 'lancedb.connect', 'lancedb.connect', (['"""nbs/embeddings"""'], {}), "('nbs/embeddings')\n", (1190, 1208), False, 'import lancedb\n'), ((1504, 1518), 'streamlit.columns', 'st.columns', (['(10)'], {}), '(10)\n', (1514, 1518), True, 'import streamlit as st\n'), ((2552, 2566), 'streamlit.columns', 'st.columns', (['(10)'], {}), '(10)\n', (2562, 2566), True, 'import streamlit as st\n'), ((2982, 3004), 'streamlit.title', 'st.title', (['"""Clayground"""'], {}), "('Clayground')\n", (2990, 3004), True, 'import streamlit as st\n'), ((3220, 3250), 'streamlit.toggle', 'st.toggle', (['"""Arithmetic"""', '(False)'], {}), "('Arithmetic', False)\n", (3229, 3250), True, 'import streamlit as st\n'), ((3424, 3443), 'streamlit.button', 'st.button', (['"""Submit"""'], {}), "('Submit')\n", (3433, 3443), True, 'import streamlit as st\n'), ((3619, 3653), 'streamlit.write', 'st.write', (['"""Selected:"""', 'multiselect'], {}), "('Selected:', multiselect)\n", (3627, 3653), True, 'import streamlit as st\n'), ((1791, 1855), 'streamlit.caption', 'st.caption', (['f"""{sample[\'tile\']}-{sample[\'date\']}-{sample[\'idx\']}"""'], {}), '(f"{sample[\'tile\']}-{sample[\'date\']}-{sample[\'idx\']}")\n', (1801, 1855), True, 'import streamlit as st\n'), ((1868, 1882), 'rasterio.plot.show', 'show', (['rgb_chip'], {}), '(rgb_chip)\n', (1872, 1882), False, 'from rasterio.plot import show\n'), ((1895, 1910), 'matplotlib.pyplot.axis', 'plt.axis', (['"""off"""'], {}), "('off')\n", (1903, 1910), True, 'import matplotlib.pyplot as plt\n'), ((1923, 1937), 'streamlit.pyplot', 'st.pyplot', (['plt'], {}), '(plt)\n', (1932, 1937), True, 'import streamlit as st\n'), ((2815, 2870), 'streamlit.caption', 'st.caption', (['f"""{row[\'tile\']}-{row[\'date\']}-{row[\'idx\']}"""'], {}), '(f"{row[\'tile\']}-{row[\'date\']}-{row[\'idx\']}")\n', (2825, 2870), True, 'import streamlit as st\n'), ((2883, 2897), 'rasterio.plot.show', 'show', (['rgb_chip'], {}), '(rgb_chip)\n', (2887, 2897), False, 'from rasterio.plot import show\n'), ((2910, 2925), 'matplotlib.pyplot.axis', 'plt.axis', (['"""off"""'], {}), "('off')\n", (2918, 2925), True, 'import matplotlib.pyplot as plt\n'), ((2938, 2952), 'streamlit.pyplot', 'st.pyplot', (['plt'], {}), '(plt)\n', (2947, 2952), True, 'import streamlit as st\n'), ((1628, 1642), 'rasterio.open', 'rio.open', (['path'], {}), '(path)\n', (1636, 1642), True, 'import rasterio as rio\n'), ((2652, 2666), 'rasterio.open', 'rio.open', (['path'], {}), '(path)\n', (2660, 2666), True, 'import rasterio as rio\n')]