File size: 6,333 Bytes
bca1dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import gradio as gr
import pinecone
import time
import os
from pymongo.mongo_client import MongoClient

pinecone_key = os.environ['PINECONE_KEY']


def getBrains():
    pinecone.init(api_key=pinecone_key,
                  environment="us-west4-gcp")
    active_indexes = pinecone.list_indexes()
    #print(active_indexes)
    return gr.update(choices=active_indexes)

def isBrainFound(brainName):
    pinecone.init(api_key=pinecone_key,
                  environment="us-west4-gcp")
    active_indexes = pinecone.list_indexes()
    print(active_indexes)
    brainName = brainName.lower()
    if brainName in active_indexes:
        return True
    else:
        return False

def createBrain(brainName):
    
    pinecone.init(api_key=pinecone_key,
                  environment="us-west4-gcp")
    pinecone.create_index(
        brainName,
        dimension=1536,
        metric="cosine",
        pod_type="p1"
    )
    return brainName+" Brain Created!"

def delete_all_files(username):
    client = MongoClient(os.environ["MONGO_KEY"])
    db = client['nbrain']
    collection = db['files']
    query = {"brain": username}
    collection.delete_many(query)

def deleteBrain(brainName):
    pinecone.init(api_key=pinecone_key,
                  environment="us-west4-gcp")
    pinecone.delete_index(brainName)
    delete_all_files(brainName)
    return brainName+" Brain Deleted!"


def onChange(optionVal):
    if optionVal == "Create New Brain":
        return [gr.update(visible=True), gr.update(visible=False)]
    else:
        return [gr.update(visible=False), gr.update(visible=True)]


def is_valid_string(s):
    """
    Checks if a string contains only lowercase letters, numbers, and hyphens.
    """
    # Define allowed characters
    allowed_chars = set("abcdefghijklmnopqrstuvwxyz0123456789-")

    # Check each character in the string
    for char in s:
        if char not in allowed_chars:
            return False

    # All characters are allowed
    return True

get_window_url_params = """
    function(url_params) {
        console.log(url_params);
        const params = new URLSearchParams(window.location.search);
        url_params = Object.fromEntries(params);
        console.log(url_params)
        return url_params;
        }
    """
def checkAuth(params):
    print (params)
    if ("password" in params):
        if (params["password"] == os.environ['PASSWORD']):
            return ["""# Build Brain!""", gr.update(visible=True)]

    else:
        return ["""# Authorization Failed!""", gr.update(visible=False)]    

def handleSubmit(option, newBrainName, prevBrainName):
    if option == "Create New Brain":
        if (newBrainName == ""):
            return "Please Enter Brain Name!"
        if (isBrainFound(newBrainName) == False):
            if (is_valid_string(newBrainName) == False):
                return "Brain Name can only contain lowercase letters, numbers, and hyphens!"
            return createBrain(newBrainName)
        return newBrainName+" Brain is already created.."
    if option == "Delete Brain":
        print(prevBrainName)
        if (prevBrainName == ""):
            return "Please Select Any Brain!"
        return deleteBrain(prevBrainName)
        
        


bg_color = "#c5dde0"
s_color = "#1d2230"
mycss = """
    .gradio-container {{background-color: {bgcolor}}}
    #title {{margin-top:6%;margin-bottom:16px;display:flex;justify-content:center;align-items:center}}
    #title h1 {{font-weight:900;color:{scolor}}}
    #secondrow {{padding:0 6%;gap:30px;display:flex;justify-content:center;align-items:center;padding-left:30%;padding-right:30%}}
    #choose {{background-color: {bgcolor};border-style:none;border-width:0;box-shadow:none;padding-left:0;padding-right:0}}
    #choose .svelte-1gfkn6j {{background-color:{bgcolor};color:{scolor};font-size:17px}}
    #namedr {{background-color: {bgcolor};border-style:none;border-width:0;box-shadow:none;padding-left:0;padding-right:0}}
    #namedr .svelte-1gfkn6j {{background-color:{bgcolor};color:{scolor};font-size:17px}}
    #name {{background-color: {bgcolor};border-style:none;box-shadow:none;padding-left:0;padding-right:0}}
    #name span {{background-color:{bgcolor};color:{scolor};font-size:17px}}
    #file .svelte-1frtwj3 {{background-color:#ffffff;color:{scolor};font-size:17px}}
    #file .svelte-xwlu1w {{color:{scolor};min-height:fit-content}}
    #file .svelte-116rqfv {{height:15vh}}
    #file .file-preview-holder {{overflow-y:scroll;max-height:17vh}}
    #status {{display:flex;justify-content:center;align-items:center;margin-top:20px;font-size:20px;font-weight:700;color:{scolor}}}
    #output {{background-color: {bgcolor};border-style:none;border-width:0;box-shadow:none}}
    #output span {{background-color:{bgcolor};color:{scolor};font-size:18px}}
    #button {{background-color:{scolor};color:#ffffff;margin-top:14px}}
"""
formatted_css = mycss.format(bgcolor=bg_color, scolor=s_color)


with gr.Blocks(theme=gr.themes.Soft(), css=formatted_css) as block_demo:
    with gr.Row(elem_id="first"):

        with gr.Column():
            title=gr.Markdown(
                """
            # Admin!
            """, elem_id="title")

    with gr.Row(elem_id="secondrow"):

        with gr.Column(scale=1,  elem_id="inputsCol") as myrow:
            choose = gr.Dropdown(
                label="Select Operation", value="Create New Brain", choices=["Create New Brain", "Delete Brain"], elem_id="choose", multiselect=False, interactive=True)

            brain_name = gr.Textbox(
                label="Brain Name", elem_id="name")

            brain_name_dr = gr.Dropdown(
                label="Select Brain", choices=None, elem_id="namedr", multiselect=False, interactive=True, visible=False)

            choose.change(onChange, choose, [
                brain_name, brain_name_dr])


            submit_button = gr.Button(value="Submit", elem_id="button")

            status = gr.Markdown(
                """
            """, elem_id="status")

    #
    submit_button.click(
        handleSubmit, [choose, brain_name, brain_name_dr,], status,api_name="up")

    block_demo.load(checkAuth, inputs=title, outputs=[
                    title, myrow], _js=get_window_url_params)
    block_demo.load(getBrains, inputs=None, outputs=brain_name_dr)

block_demo.launch(show_api=False)