Spaces:
Running
Running
File size: 3,682 Bytes
8e018ae e04dd70 8e018ae c5a0a6e 6e74ef2 8e018ae e04dd70 8e018ae e04dd70 8e018ae e04dd70 611f226 8e018ae 611f226 e04dd70 611f226 e04dd70 8e018ae 611f226 6e74ef2 8e018ae 611f226 aadb9c7 611f226 6e74ef2 611f226 8e018ae 611f226 6e74ef2 611f226 8e018ae 611f226 8e018ae 611f226 |
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 |
"""Entry point of streamòit app"""
import streamlit as st
import hmac
import os
import requests
from dotenv import load_dotenv
import streamlit.components.v1 as components
from utils import get_chroma_client, get_embedding_function
from retrieve_kb import get_current_knowledge_bases
__import__("pysqlite3")
import sys
# settings
sys.modules["sqlite3"] = sys.modules.pop("pysqlite3")
st.set_page_config(page_title="Hello", page_icon="👋", layout="wide")
load_dotenv()
brian_api_key = os.getenv("BRIAN_API_KEY")
openai_key = os.getenv("OPENAI_API_KEY")
def askbrian_request(prompt, kb, api_key):
url = " https://api.brianknows.org/api/v0/agent/knowledge"
data = {"prompt": prompt, "kb": kb}
headers = {
"Content-Type": "application/json",
"X-Brian-Api-Key": api_key, # Include the API key in the headers
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.json() # Returns the JSON response if successful
else:
return (
response.status_code,
response.text,
) # Returns the status code and error if not successful
def show_sidebar():
"""Shows sidebar with Biran info"""
# Sidebar
st.sidebar.header(("About"))
st.sidebar.markdown(
(
"[Brian](https://www.brianknows.org/) Built on top of Brian API, Brian App offers an user interface for performing transactions in a non-custodial way, researching web3 info, and deploying smart contracts by prompt."
)
)
st.sidebar.header(("Resources"))
st.sidebar.markdown(
(
"""
- [Brian Documentation](https://docs.brianknows.org/)
- [X (Twitter)](https://x.com/BrianknowsAI?mx=2)
- [Linkedin](https://www.linkedin.com/company/brianknowsai/)
- [Medium](https://medium.com/@BrianknowsAI)
"""
)
)
def check_password():
"""Returns `True` if the user had the correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
if hmac.compare_digest(st.session_state["password"], st.secrets["password"]):
st.session_state["password_correct"] = True
del st.session_state["password"] # Don't store the password.
else:
st.session_state["password_correct"] = False
# Return True if the password is validated.
if st.session_state.get("password_correct", False):
return True
# Show input for password.
st.text_input(
"Password", type="password", on_change=password_entered, key="password"
)
if "password_correct" in st.session_state:
st.error("😕 Password incorrect")
return False
if not check_password():
st.stop() # Do not continue if check_password is not True.
# Main Streamlit app starts here
client = get_chroma_client()
default_embedding_function = get_embedding_function(openai_key=openai_key)
show_sidebar()
col1, col2, col3 = st.columns((1, 4, 1))
st.write("# Brian Knowledge Base System! 👋")
tab1, tab2 = st.tabs(["AskBrian", "BrianApp"])
# Ask Brian Tab
with tab1:
st.markdown("## Ask Brian Anything")
kb_name = "public-knowledge-box"
# Example usage:
kbs = get_current_knowledge_bases(client=client)
kbs = (kb.name for kb in kbs)
kb_name = st.selectbox("Select knowledge box", kbs)
query = st.text_input(label="query")
if st.button("askbrian"):
result = askbrian_request(query, kb_name, brian_api_key)
st.json(result)
# Brian App embedded Tab
with tab2:
components.iframe("https://www.brianknows.org/", height=650, scrolling=True)
|