Spaces:
Paused
Paused
Inialize Colivara Demo
Browse files- .gitignore +1 -0
- app.py +136 -0
- requirements.txt +3 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
venv/
|
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import libraries
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from colivara_py import Colivara
|
| 4 |
+
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
|
| 11 |
+
try:
|
| 12 |
+
client = Colivara(base_url="https://api.colivara.com", api_key=api_key)
|
| 13 |
+
client.list_collections()
|
| 14 |
+
return True
|
| 15 |
+
except Exception:
|
| 16 |
+
return False
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# ask for api key from user in professional way
|
| 20 |
+
st.title("Colivara Demo")
|
| 21 |
+
st.write("Welcome to the Colivara Demo.")
|
| 22 |
+
st.subheader("API Key")
|
| 23 |
+
api_key = st.text_input("API Key", type="password")
|
| 24 |
+
if not api_key or not validate_api_key(api_key):
|
| 25 |
+
st.write("Please enter your API key to proceed.")
|
| 26 |
+
st.markdown("If you don't have one, get yours [here](https://colivara.com).")
|
| 27 |
+
st.stop()
|
| 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",
|
| 34 |
+
help="Enter the name of the collection to create, or leave empty if not creating one.",
|
| 35 |
+
)
|
| 36 |
+
if st.button("Create Collection"):
|
| 37 |
+
if not collection_name:
|
| 38 |
+
st.error("Please enter a collection name to create the collection.")
|
| 39 |
+
else:
|
| 40 |
+
with st.spinner("Creating 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.text_area(
|
| 51 |
+
"Existing Collections:",
|
| 52 |
+
value=collections_text,
|
| 53 |
+
height=200,
|
| 54 |
+
help="List of existing collections.",
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
st.subheader("Upsert Documents")
|
| 58 |
+
uploaded_files = st.file_uploader(
|
| 59 |
+
"Upload Docoments",
|
| 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 |
+
new_files = [
|
| 74 |
+
file for file in uploaded_files if file.name not in uploaded_file_names
|
| 75 |
+
]
|
| 76 |
+
if not new_files:
|
| 77 |
+
st.info("No new files to upsert. Please upload new files to proceed.")
|
| 78 |
+
else:
|
| 79 |
+
with st.spinner("Upserting Files..."):
|
| 80 |
+
progress_bar = st.progress(0)
|
| 81 |
+
total_files = len(new_files)
|
| 82 |
+
for idx, file in enumerate(new_files):
|
| 83 |
+
uploaded_file_names.add(file.name)
|
| 84 |
+
encoded_file = base64.b64encode(file.read()).decode("utf-8")
|
| 85 |
+
client.upsert_document(
|
| 86 |
+
name=file.name,
|
| 87 |
+
document_base64=encoded_file,
|
| 88 |
+
collection_name=selected_collection,
|
| 89 |
+
wait=True,
|
| 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."
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
top_k = st.slider(
|
| 102 |
+
"Number of Top Documents to Retrieve",
|
| 103 |
+
min_value=1,
|
| 104 |
+
max_value=10,
|
| 105 |
+
value=3,
|
| 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.")
|
| 113 |
+
else:
|
| 114 |
+
with st.spinner("Searching..."):
|
| 115 |
+
results = client.search(
|
| 116 |
+
query=query, collection_name=selected_collection, top_k=top_k
|
| 117 |
+
)
|
| 118 |
+
results = results.results
|
| 119 |
+
|
| 120 |
+
if not results:
|
| 121 |
+
st.info("No documents found for the given query.")
|
| 122 |
+
|
| 123 |
+
else:
|
| 124 |
+
for idx, result in enumerate(results):
|
| 125 |
+
img_base64 = result.img_base64
|
| 126 |
+
page_number = result.page_number
|
| 127 |
+
document_name = result.document_name
|
| 128 |
+
img = Image.open(BytesIO(base64.b64decode(img_base64)))
|
| 129 |
+
st.image(
|
| 130 |
+
img, caption=f"Document: {document_name}, Page: {page_number}"
|
| 131 |
+
)
|
| 132 |
+
st.success("Search completed.")
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
st.markdown("----")
|
| 136 |
+
st.markdown("Developed by Abdulhaleem from TJM Labs")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
colivara-py
|
| 3 |
+
Pillow
|