Spaces:
Paused
Paused
persistent outputs
Browse files
app.py
CHANGED
|
@@ -5,6 +5,14 @@ import base64
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def validate_api_key(api_key):
|
| 10 |
# check if the api key is valid
|
|
@@ -16,7 +24,7 @@ def validate_api_key(api_key):
|
|
| 16 |
return False
|
| 17 |
|
| 18 |
|
| 19 |
-
#
|
| 20 |
st.title("Colivara Demo")
|
| 21 |
st.write("Welcome to the Colivara Demo.")
|
| 22 |
st.subheader("API Key")
|
|
@@ -28,6 +36,7 @@ if not api_key or not validate_api_key(api_key):
|
|
| 28 |
|
| 29 |
client = Colivara(base_url="https://api.colivara.com", api_key=api_key)
|
| 30 |
|
|
|
|
| 31 |
st.subheader("Create Collection (Optional)")
|
| 32 |
collection_name = st.text_input(
|
| 33 |
"Collection Name",
|
|
@@ -41,58 +50,67 @@ if st.button("Create Collection"):
|
|
| 41 |
client.create_collection(collection_name)
|
| 42 |
st.success(f"Collection '{collection_name}' successfully created.")
|
| 43 |
|
| 44 |
-
|
| 45 |
st.subheader("List Collections")
|
| 46 |
if st.button("List Collections"):
|
| 47 |
with st.spinner("Fetching collections..."):
|
| 48 |
collections = client.list_collections()
|
| 49 |
collections_text = "\n".join([collection.name for collection in collections])
|
| 50 |
-
st.
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
st.subheader("Upsert Documents")
|
| 58 |
uploaded_files = st.file_uploader(
|
| 59 |
-
"Upload
|
| 60 |
accept_multiple_files=True,
|
| 61 |
help="Upload your documents here, most extensions are supported (pdf, docx, png, jpg, xlsx, etc.)",
|
| 62 |
)
|
| 63 |
selected_collection = st.text_input(
|
| 64 |
"Collection Name", help="Enter the collection name to store the documents in."
|
| 65 |
)
|
| 66 |
-
if not uploaded_files or not selected_collection:
|
| 67 |
-
st.write("Please upload your documents and enter the collection name to proceed.")
|
| 68 |
-
st.stop()
|
| 69 |
-
|
| 70 |
-
uploaded_file_names = set()
|
| 71 |
|
| 72 |
if st.button("Upsert Files"):
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
st.info("No new files to upsert. Please upload new files to proceed.")
|
| 78 |
else:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
for
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
)
|
| 91 |
-
progress_bar.progress((idx + 1) / total_files)
|
| 92 |
-
st.success(
|
| 93 |
-
f"{len(new_files)} new files successfully upserted to the collection '{selected_collection}'."
|
| 94 |
-
)
|
| 95 |
|
|
|
|
| 96 |
st.subheader("Search and Retrieve Documents")
|
| 97 |
query = st.text_input(
|
| 98 |
"Search Query", help="Enter the search query to retrieve documents."
|
|
@@ -106,7 +124,6 @@ top_k = st.slider(
|
|
| 106 |
help="Select the number of top documents to retrieve.",
|
| 107 |
)
|
| 108 |
|
| 109 |
-
|
| 110 |
if st.button("Search"):
|
| 111 |
if not query:
|
| 112 |
st.error("Please enter a search query to retrieve documents.")
|
|
@@ -115,22 +132,24 @@ if st.button("Search"):
|
|
| 115 |
results = client.search(
|
| 116 |
query=query, collection_name=selected_collection, top_k=top_k
|
| 117 |
)
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
st.markdown("----")
|
| 136 |
st.markdown("Developed by Abdulhaleem from TJM Labs")
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
+
# Initialize session state variables
|
| 9 |
+
if "uploaded_file_names" not in st.session_state:
|
| 10 |
+
st.session_state["uploaded_file_names"] = set()
|
| 11 |
+
if "search_results" not in st.session_state:
|
| 12 |
+
st.session_state["search_results"] = []
|
| 13 |
+
if "collections_list" not in st.session_state:
|
| 14 |
+
st.session_state["collections_list"] = ""
|
| 15 |
+
|
| 16 |
|
| 17 |
def validate_api_key(api_key):
|
| 18 |
# check if the api key is valid
|
|
|
|
| 24 |
return False
|
| 25 |
|
| 26 |
|
| 27 |
+
# Ask for API key from user
|
| 28 |
st.title("Colivara Demo")
|
| 29 |
st.write("Welcome to the Colivara Demo.")
|
| 30 |
st.subheader("API Key")
|
|
|
|
| 36 |
|
| 37 |
client = Colivara(base_url="https://api.colivara.com", api_key=api_key)
|
| 38 |
|
| 39 |
+
# Create Collection Section
|
| 40 |
st.subheader("Create Collection (Optional)")
|
| 41 |
collection_name = st.text_input(
|
| 42 |
"Collection Name",
|
|
|
|
| 50 |
client.create_collection(collection_name)
|
| 51 |
st.success(f"Collection '{collection_name}' successfully created.")
|
| 52 |
|
| 53 |
+
# List Collections Section
|
| 54 |
st.subheader("List Collections")
|
| 55 |
if st.button("List Collections"):
|
| 56 |
with st.spinner("Fetching collections..."):
|
| 57 |
collections = client.list_collections()
|
| 58 |
collections_text = "\n".join([collection.name for collection in collections])
|
| 59 |
+
st.session_state["collections_list"] = collections_text
|
| 60 |
+
|
| 61 |
+
if st.session_state["collections_list"]:
|
| 62 |
+
st.text_area(
|
| 63 |
+
"Existing Collections:",
|
| 64 |
+
value=st.session_state["collections_list"],
|
| 65 |
+
height=200,
|
| 66 |
+
help="List of existing collections.",
|
| 67 |
+
key="collections_text_area",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Upsert Documents Section
|
| 71 |
st.subheader("Upsert Documents")
|
| 72 |
uploaded_files = st.file_uploader(
|
| 73 |
+
"Upload Documents",
|
| 74 |
accept_multiple_files=True,
|
| 75 |
help="Upload your documents here, most extensions are supported (pdf, docx, png, jpg, xlsx, etc.)",
|
| 76 |
)
|
| 77 |
selected_collection = st.text_input(
|
| 78 |
"Collection Name", help="Enter the collection name to store the documents in."
|
| 79 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
if st.button("Upsert Files"):
|
| 82 |
+
if not uploaded_files or not selected_collection:
|
| 83 |
+
st.error(
|
| 84 |
+
"Please upload your documents and enter the collection name to proceed."
|
| 85 |
+
)
|
|
|
|
| 86 |
else:
|
| 87 |
+
# Filter new files only
|
| 88 |
+
new_files = [
|
| 89 |
+
file
|
| 90 |
+
for file in uploaded_files
|
| 91 |
+
if file.name not in st.session_state["uploaded_file_names"]
|
| 92 |
+
]
|
| 93 |
+
if not new_files:
|
| 94 |
+
st.info("No new files to upsert. Please upload new files to proceed.")
|
| 95 |
+
else:
|
| 96 |
+
with st.spinner("Upserting Files..."):
|
| 97 |
+
progress_bar = st.progress(0)
|
| 98 |
+
total_files = len(new_files)
|
| 99 |
+
for idx, file in enumerate(new_files):
|
| 100 |
+
st.session_state["uploaded_file_names"].add(file.name)
|
| 101 |
+
encoded_file = base64.b64encode(file.read()).decode("utf-8")
|
| 102 |
+
client.upsert_document(
|
| 103 |
+
name=file.name,
|
| 104 |
+
document_base64=encoded_file,
|
| 105 |
+
collection_name=selected_collection,
|
| 106 |
+
wait=True,
|
| 107 |
+
)
|
| 108 |
+
progress_bar.progress((idx + 1) / total_files)
|
| 109 |
+
st.success(
|
| 110 |
+
f"{len(new_files)} new files successfully upserted to the collection '{selected_collection}'."
|
| 111 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
# Search and Retrieve Documents Section
|
| 114 |
st.subheader("Search and Retrieve Documents")
|
| 115 |
query = st.text_input(
|
| 116 |
"Search Query", help="Enter the search query to retrieve documents."
|
|
|
|
| 124 |
help="Select the number of top documents to retrieve.",
|
| 125 |
)
|
| 126 |
|
|
|
|
| 127 |
if st.button("Search"):
|
| 128 |
if not query:
|
| 129 |
st.error("Please enter a search query to retrieve documents.")
|
|
|
|
| 132 |
results = client.search(
|
| 133 |
query=query, collection_name=selected_collection, top_k=top_k
|
| 134 |
)
|
| 135 |
+
st.session_state["search_results"] = results.results
|
| 136 |
+
|
| 137 |
+
# Display Search Results
|
| 138 |
+
if st.session_state["search_results"]:
|
| 139 |
+
st.write("Search Results:")
|
| 140 |
+
cols = st.columns(2)
|
| 141 |
+
for idx, result in enumerate(st.session_state["search_results"]):
|
| 142 |
+
img_base64 = result.img_base64
|
| 143 |
+
page_number = result.page_number
|
| 144 |
+
document_name = result.document_name
|
| 145 |
+
img = Image.open(BytesIO(base64.b64decode(img_base64)))
|
| 146 |
+
cols[idx % 2].image(
|
| 147 |
+
img,
|
| 148 |
+
caption=f"Document: {document_name}, Page: {page_number}",
|
| 149 |
+
use_container_width=False,
|
| 150 |
+
width=300,
|
| 151 |
+
)
|
| 152 |
+
st.success("Search completed.")
|
| 153 |
|
| 154 |
st.markdown("----")
|
| 155 |
st.markdown("Developed by Abdulhaleem from TJM Labs")
|