Skip to main content

The official Protean Streamlit library

Project description

Protean SDK Streamlit

The CoGrow Protean Streamlit library provides functionality that simplifies integration of Streamlit applications with Protean platform.

Installation

# install from PyPI
pip install cogrow-protean-streamlit

Protean Authenticator

ProteanAuthenticator allows Streamlit applications to leverage Protean platform authentication using OAuth2 Resource Server pattern.

Usage

Using ProteanAuthenticator is as simple as importing the module, initializing an instance with the OAuth2 issuer URI and calling authenticate to verify logged-in user's credentials:

import streamlit as st

from protean_streamlit import ProteanAuthenticator

protean_authenticator = ProteanAuthenticator("https://keycloak.cogrow.tech/realms/cogrow")
protean_authenticator.authenticate()
if protean_authenticator.is_logged_in:
    st.title("Protean Streamlit App")

You could use Protean bearer token to authenticate and access all Protean platform APIs:

import streamlit as st
from openai import OpenAI

from protean import Protean
from protean_streamlit import ProteanAuthenticator

protean_authenticator = ProteanAuthenticator("https://keycloak.cogrow.tech/realms/cogrow")
protean_authenticator.authenticate()

if protean_authenticator.is_logged_in:
    st.title("💬 Chatbot")

    # Use Protean APIs to fetch the user profile details
    protean_client = Protean(base_url=st.context.headers.get('Origin'), api_key=protean_authenticator.bearer_token)
    user_profile = protean_client.user.get_user_profile()
    with st.sidebar:
        st.markdown(f"### 👤 Logged in as: [{user_profile.fullname}](mailto:{user_profile.email})")

    if "messages" not in st.session_state:
        st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]

    for msg in st.session_state.messages:
        st.chat_message(msg["role"]).write(msg["content"])

    if prompt := st.chat_input():
        client = OpenAI(api_key=protean_authenticator.bearer_token, base_url=f"{st.context.headers.get('Origin')}/api/inference/text")
        st.session_state.messages.append({"role": "user", "content": prompt})
        st.chat_message("user").write(prompt)
        response = client.chat.completions.create(model="llama-3.2-1b-instruct", messages=st.session_state.messages)
        msg = response.choices[0].message.content
        st.session_state.messages.append({"role": "assistant", "content": msg})
        st.chat_message("assistant").write(msg)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cogrow_protean_streamlit-0.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file cogrow_protean_streamlit-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cogrow_protean_streamlit-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 22f93261d0dc582b95295f3bbbcde2978450f0df9e19a28b758ae3487e3e1b82
MD5 0b5715a523aefc08a017589e8c17444b
BLAKE2b-256 a01df157f8247fcd3432e56f9e005d7b0f0c96bfdb9d574b32952324dc44d170

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page