Spaces:
Running
Running
"""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) | |