File size: 5,581 Bytes
07e9e93
 
 
 
 
 
2ec294e
 
 
07e9e93
7e3ceba
 
07e9e93
dc33aef
2ec294e
 
 
 
 
dc33aef
2ec294e
 
 
 
 
 
 
 
 
 
 
 
 
 
07e9e93
 
7e3ceba
07e9e93
 
2ec294e
 
 
dc33aef
 
 
 
c591b46
2ec294e
 
07e9e93
2ec294e
 
dc33aef
 
 
 
 
 
 
 
 
 
 
 
2ec294e
7e3ceba
2ec294e
 
07e9e93
2ec294e
 
 
 
 
dc33aef
 
2ec294e
dc33aef
 
 
 
 
2ec294e
 
 
 
07e9e93
2ec294e
07e9e93
c591b46
2ec294e
07e9e93
1cdc954
 
07e9e93
 
 
 
 
 
2ec294e
07e9e93
 
dc33aef
07e9e93
2ec294e
 
07e9e93
 
2ec294e
 
 
 
 
 
 
7e3ceba
2ec294e
dc33aef
2ec294e
 
dc33aef
7e3ceba
 
 
 
 
dc33aef
 
 
 
 
 
 
 
 
 
 
 
2ec294e
07e9e93
 
22b2241
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# import libraries
import streamlit as st
from colivara_py import Colivara
import base64
from PIL import Image
from io import BytesIO
import os

API_KEY = os.getenv("COLIVARA_API_KEY")

if "search_results" not in st.session_state:
    st.session_state["search_results"] = []

client = Colivara(api_key=API_KEY)
# Add badges at the top of the page
st.markdown(
    """
    <div style="display: flex; gap: 10px; margin-bottom: 20px;">
        <a href="https://discord.gg/DtGRxWuj8y" target="_blank">
            <img src="https://img.shields.io/badge/Join%20Discord-7289DA?style=for-the-badge&logo=discord&logoColor=white" alt="Discord">
        </a>
        <a href="https://www.colivara.com" target="_blank">
            <img src="https://img.shields.io/badge/Website-0078D7?style=for-the-badge&logo=internetexplorer&logoColor=white" alt="Website">
        </a>
        <a href="https://docs.colivara.com" target="_blank">
            <img src="https://img.shields.io/badge/Docs-217346?style=for-the-badge&logo=readthedocs&logoColor=white" alt="Docs">
        </a>
        <a href="https://github.com/tjmlabs/ColiVara" target="_blank">
            <img src="https://img.shields.io/badge/GitHub-181717?style=for-the-badge&logo=github&logoColor=white" alt="GitHub">
        </a>
    </div>
    """,
    unsafe_allow_html=True,
)


# Ask for API key from user
st.title("Colivara Demo")

st.markdown(
    """
    ### Welcome to the Colivara Search Demo
    This demo allows you to search through the Competitive Programmer's Handbook by Antti Laaksonen.
    We included 12 chapters from the handbook in this demo to help you understand how Colivara works.
    **Chapters Covered:**
    `Introduction` `Time Complexity` `Sorting` `Data Structures` `Complete Search` `Greedy Algorithms` `Dynamic Programming` `Amortized Analysis` `Range Queries` `Bit Manipulation` `Basics of Graphs` `Graph Traversal`

    - Enter a search query in the input box below.
    - Retrieve the most relevant pages from the document.

    **Example Queries:**
    ```
    1. What is set theory?
    2. How to analyze complexity using Big O notation?
    3. Explain Binary Search Algorithm and provide implementation of lower bound.
    4. How to use Stack, Queue and Deque?
    5. Explain generating permutations and subsets in Complete Search.
    6. What makes an algorithm greedy?
    7. How to solve the coin change problem using Dynamic Programming?
    8. What is amortized analysis?
    9. How to implement prefix sum array?
    10. Explain basic bitwise operations.
    11. Show graph representation using matrix and adjacency list.
    12. How to implement DFS and BFS traversal?
    ```

    Try it out and see how the AI retrieves the information you need efficiently.
    """
)

# Display placeholder for document details with a badge for the PDF
st.markdown(
    """
    #### Document Overview:
    **Title:** Competitive Programmer's Handbook  
    **Author:** Antti Laaksonen  
    **Type:** PDF  
    **Included Pages:** 100+  
    **Included Chapters Count:** 12  
    **Full Book Chapters Count:** 30

    <a href="https://cses.fi/book/book.pdf" target="_blank">
        <img src="https://img.shields.io/badge/View%20PDF-EF8D21?style=for-the-badge&logo=adobeacrobatreader&logoColor=white" alt="View PDF">
    </a>
    """,
    unsafe_allow_html=True,
)
# Search and retrieve documents
st.subheader("Search and Retrieve Documents")

# User inputs for search
query = st.text_input(
    "Enter your search query:",
    help="Type a query to search through 12 chapters of the Competitive Programmer's Handbook.",
)
top_k = st.slider(
    "Number of Top Documents to Retrieve",
    min_value=1,
    max_value=10,
    value=3,
    help="Select the number of top pages to retrieve.",
)

collection_name = "Algorithm"
if st.button("Search"):
    if not query:
        st.error("Please enter a search query to retrieve results.")
    else:
        with st.spinner("Searching..."):
            try:
                results = client.search(
                    query=query, collection_name=collection_name, top_k=top_k
                )
                st.session_state["search_results"] = results.results
            except Exception as e:
                st.error(f"Error during search: {str(e)}")

# Display search results

if "search_results" in st.session_state and st.session_state["search_results"]:
    st.write("### Search Results")
    # Remove the columns and display images one by one
    for idx, result in enumerate(st.session_state["search_results"]):
        img_base64 = result.img_base64
        page_number = result.page_number
        document_name = result.document_name
        img = Image.open(BytesIO(base64.b64decode(img_base64)))

        # Create a container for each result
        with st.container():
            # Display the image with increased width
            st.image(
                img,
                caption=f"Document: {document_name}, Page: {page_number}",
                use_container_width=True,  # This makes the image use full column width
            )
            # Add some spacing between images
            st.markdown("---")

    st.success("Search completed successfully.")

st.markdown("----")
st.markdown(
    """
    <div style="text-align: center; padding: 10px;">
        Developed by <a href="mailto:abdulhaleem@tjmlabs.com" style="color: #ff4b4b; text-decoration: none;">Abdulhaleem</a> @ 
        <a href="https://tjmlabs.com" style="color: #ff4b4b; text-decoration: none;">TJM Labs</a>
    </div>
    """,
    unsafe_allow_html=True,
)