chatwithfiles / gui /sidebar.py
Unterwexi's picture
move to gui
59f37ba
import os
import streamlit as st
from chatbot import Chatbot
from embedding import Embedder
class Sidebar:
MODEL_OPTIONS = ["gpt-3.5-turbo", "gpt-4"]
TEMPERATURE_MIN_VALUE = 0.0
TEMPERATURE_MAX_VALUE = 1.0
TEMPERATURE_DEFAULT_VALUE = 0.0
TEMPERATURE_STEP = 0.01
@staticmethod
def about():
about = st.sidebar.expander("About πŸ€–")
sections = [
"This is for Demo of the 506 CompanyGPT Functions only. Please visit https://www.506.ai for more Details"
"⚑",
]
for section in sections:
about.write(section)
def model_selector(self):
model = st.selectbox(label="Model", options=self.MODEL_OPTIONS)
st.session_state["model"] = model
@staticmethod
def reset_chat_button():
if st.button("Reset chat"):
st.session_state["reset_chat"] = True
st.session_state.setdefault("reset_chat", False)
def temperature_slider(self):
temperature = st.slider(
label="Temperature",
min_value=self.TEMPERATURE_MIN_VALUE,
max_value=self.TEMPERATURE_MAX_VALUE,
value=self.TEMPERATURE_DEFAULT_VALUE,
step=self.TEMPERATURE_STEP,
)
st.session_state["temperature"] = temperature
def show_options(self):
with st.sidebar.expander("πŸ› οΈ Tools", expanded=True):
self.reset_chat_button()
self.model_selector()
self.temperature_slider()
st.session_state.setdefault("model", self.MODEL_OPTIONS[0])
st.session_state.setdefault("temperature", self.TEMPERATURE_DEFAULT_VALUE)
class Utilities:
@staticmethod
def load_api_key():
"""
Loads the OpenAI API key from the .env file or from the user's input
and returns it
"""
if os.path.exists(".env") and os.environ.get("OPENAI_API_KEY") is not None:
user_api_key = os.environ["OPENAI_API_KEY"]
st.sidebar.success("API key loaded from .env", icon="πŸš€")
else:
user_api_key = st.sidebar.text_input(
label="#### Your OpenAI API key πŸ‘‡", placeholder="Paste your openAI API key, sk-", type="password"
)
if user_api_key:
st.sidebar.success("API key loaded", icon="πŸš€")
return user_api_key
@staticmethod
def handle_upload():
"""
Handles the file upload and displays the uploaded file
"""
uploaded_file = st.sidebar.file_uploader("upload", type="pdf", label_visibility="collapsed")
if uploaded_file is not None:
pass
else:
st.sidebar.info(
"Upload your PDF file to get started", icon="πŸ‘†"
)
st.session_state["reset_chat"] = True
return uploaded_file
@staticmethod
def setup_chatbot(uploaded_file, model, temperature):
"""
Sets up the chatbot with the uploaded file, model, and temperature
"""
embeds = Embedder()
with st.spinner("Processing..."):
uploaded_file.seek(0)
file = uploaded_file.read()
vectors = embeds.getDocEmbeds(file, uploaded_file.name)
chatbot = Chatbot(model, temperature, vectors)
st.session_state["ready"] = True
return chatbot