|
|
import streamlit as st |
|
|
import json |
|
|
import openai |
|
|
import os |
|
|
|
|
|
|
|
|
api_key = st.secrets["OPENAI_API_KEY"] |
|
|
client = openai.OpenAI(api_key=api_key) |
|
|
|
|
|
|
|
|
USER = os.environ.get("MY_APP_USER") |
|
|
PASSWORD = os.environ.get("MY_APP_PASSWORD") |
|
|
|
|
|
def check_credentials(username, password): |
|
|
return username == USER and password == PASSWORD |
|
|
|
|
|
def login_form(): |
|
|
with st.form("login_form"): |
|
|
username = st.text_input("Username") |
|
|
password = st.text_input("Password", type="password") |
|
|
submitted = st.form_submit_button("Login") |
|
|
if submitted and check_credentials(username, password): |
|
|
return True |
|
|
if submitted: |
|
|
st.error("Incorrect Username or Password") |
|
|
return False |
|
|
|
|
|
def chatbot_interface(): |
|
|
st.title("") |
|
|
|
|
|
model_options = ["gpt-4-turbo", "gpt-3.5-turbo"] |
|
|
model_selection = st.sidebar.selectbox("Choose your model:", model_options) |
|
|
|
|
|
|
|
|
if "openai_model" not in st.session_state: |
|
|
st.session_state.openai_model = model_selection |
|
|
|
|
|
|
|
|
api_key = st.secrets["OPENAI_API_KEY"] |
|
|
client = openai.OpenAI(api_key=api_key) |
|
|
|
|
|
if "messages" not in st.session_state: |
|
|
st.session_state.messages = [] |
|
|
|
|
|
|
|
|
uploaded_file = st.file_uploader("Upload a JSON history file") |
|
|
if uploaded_file is not None: |
|
|
st.session_state.messages = json.load(uploaded_file) |
|
|
|
|
|
|
|
|
for message in st.session_state.messages: |
|
|
with st.chat_message(message["role"]): |
|
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
|
if prompt := st.chat_input("What is up?"): |
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
with st.chat_message("user"): |
|
|
st.markdown(prompt) |
|
|
with st.chat_message("assistant"): |
|
|
stream = client.chat.completions.create( |
|
|
model=st.session_state.openai_model, |
|
|
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages], |
|
|
stream=True, |
|
|
) |
|
|
response = st.write_stream(stream) |
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|
|
|
|
|
|
|
if st.button("Download History"): |
|
|
json_history = json.dumps(st.session_state.messages, indent=4) |
|
|
st.download_button( |
|
|
label="Download History", |
|
|
data=json_history, |
|
|
file_name="chat_history.json", |
|
|
mime="application/json" |
|
|
) |
|
|
|
|
|
|
|
|
def main(): |
|
|
st.sidebar.title("Login") |
|
|
logo_path = "logo.png" |
|
|
st.sidebar.image(logo_path, width=150) |
|
|
|
|
|
if 'logged_in' not in st.session_state: |
|
|
st.session_state['logged_in'] = False |
|
|
|
|
|
if st.session_state['logged_in']: |
|
|
chatbot_interface() |
|
|
else: |
|
|
if login_form(): |
|
|
st.session_state['logged_in'] = True |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|