RKP64 commited on
Commit
403c7a1
·
1 Parent(s): f79202f

Upload brain.py

Browse files
Files changed (1) hide show
  1. brain.py +39 -0
brain.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+
4
+ def brain(supabase):
5
+ ## List all documents
6
+ response = supabase.table("documents").select("name:metadata->>file_name, size:metadata->>file_size", count="exact").execute()
7
+
8
+ documents = response.data # Access the data from the response
9
+
10
+ # Convert each dictionary to a tuple of items, then to a set to remove duplicates, and then back to a dictionary
11
+ unique_data = [dict(t) for t in set(tuple(d.items()) for d in documents)]
12
+
13
+ # Sort the list of documents by size in decreasing order
14
+ unique_data.sort(key=lambda x: int(x['size']), reverse=True)
15
+
16
+ # Display some metrics at the top of the page
17
+ col1, col2 = st.columns(2)
18
+ col1.metric(label="Total Documents", value=len(unique_data))
19
+ col2.metric(label="Total Size (bytes)", value=sum(int(doc['size']) for doc in unique_data))
20
+
21
+ for document in unique_data:
22
+ # Create a unique key for each button by using the document name
23
+ button_key = f"delete_{document['name']}"
24
+
25
+ # Display the document name, size and the delete button on the same line
26
+ col1, col2, col3 = st.columns([3, 1, 1])
27
+ col1.markdown(f"**{document['name']}** ({document['size']} bytes)")
28
+
29
+ if col2.button('❌', key=button_key):
30
+ delete_document(supabase, document['name'])
31
+
32
+ def delete_document(supabase, document_name):
33
+ # Delete the document from the database
34
+ response = supabase.table("documents").delete().match({"metadata->>file_name": document_name}).execute()
35
+ # Check if the deletion was successful
36
+ if len(response.data) > 0:
37
+ st.write(f"✂️ {document_name} was deleted.")
38
+ else:
39
+ st.write(f"❌ {document_name} was not deleted.")