Spaces:
Sleeping
Sleeping
| 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) |