AMUseBot/chat.py

101 lines
3.1 KiB
Python
Raw Normal View History

2023-03-22 17:13:02 +01:00
from streamlit_option_menu import option_menu
2023-03-02 15:32:39 +01:00
from pathlib import Path
2023-03-23 22:58:02 +01:00
from src.styles.menu_styles import HEADER_STYLES, FOOTER_STYLES
2023-03-22 17:13:02 +01:00
from src.utils.lang import en, ru
2023-03-23 22:58:02 +01:00
from src.utils.footer import show_donates, show_info
2023-03-23 23:48:33 +01:00
from src.utils.helpers import get_random_img, get_files_in_dir
2023-03-22 17:13:02 +01:00
from src.utils.conversation import get_user_input, show_chat_buttons, show_conversation
2023-03-02 18:39:03 +01:00
2023-03-02 15:32:39 +01:00
import streamlit as st
# --- PATH SETTINGS ---
current_dir = Path(__file__).parent if "__file__" in locals() else Path.cwd()
css_file = current_dir / "src/styles/.css"
2023-03-23 22:58:02 +01:00
assets_dir = current_dir / "assets"
2023-03-02 15:32:39 +01:00
icons_dir = assets_dir / "icons"
2023-03-23 22:58:02 +01:00
tg_svg = icons_dir / "tg.svg"
2023-03-02 15:32:39 +01:00
# --- GENERAL SETTINGS ---
PAGE_TITLE = "AI Talks"
2023-03-02 18:34:01 +01:00
PAGE_ICON = "🤖"
2023-03-22 17:13:02 +01:00
AI_MODEL_OPTIONS = [
"gpt-3.5-turbo",
"gpt-4",
"gpt-4-32k",
]
2023-03-02 15:32:39 +01:00
st.set_page_config(page_title=PAGE_TITLE, page_icon=PAGE_ICON)
# --- LOAD CSS ---
with open(css_file) as f:
st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)
2023-03-22 17:13:02 +01:00
selected_lang = option_menu(
menu_title=None,
options=["En", "Ru", ],
2023-03-23 22:58:02 +01:00
icons=["globe2", "globe"],
2023-03-22 17:13:02 +01:00
menu_icon="cast",
default_index=0,
2023-03-22 17:13:02 +01:00
orientation="horizontal",
2023-03-23 22:58:02 +01:00
styles=HEADER_STYLES
2023-03-22 17:13:02 +01:00
)
2023-03-02 15:32:39 +01:00
2023-03-11 15:39:23 +01:00
# Storing The Context
if "generated" not in st.session_state:
st.session_state["generated"] = []
if "past" not in st.session_state:
st.session_state["past"] = []
if "messages" not in st.session_state:
st.session_state["messages"] = []
if "user_text" not in st.session_state:
st.session_state["user_text"] = ""
2023-03-02 19:32:09 +01:00
def main() -> None:
if st.session_state.user_text:
show_conversation(st.session_state.user_text, st.session_state.model, st.session_state.role)
st.session_state.user_text = ""
2023-03-02 19:32:09 +01:00
2023-03-22 17:13:02 +01:00
c1, c2 = st.columns(2)
with c1, c2:
c1.selectbox(label=st.session_state.locale.select_placeholder1, key="model", options=AI_MODEL_OPTIONS)
c2.selectbox(label=st.session_state.locale.select_placeholder2, key="role",
options=st.session_state.locale.ai_role_options)
2023-03-06 14:27:43 +01:00
get_user_input()
show_chat_buttons()
2023-03-02 19:32:09 +01:00
if __name__ == "__main__":
2023-03-22 17:13:02 +01:00
match selected_lang:
case "En":
st.session_state.locale = en
case "Ru":
st.session_state.locale = ru
case _:
locale = en
st.markdown(f"<h1 style='text-align: center;'>{st.session_state.locale.title}</h1>", unsafe_allow_html=True)
st.markdown("---")
2023-03-02 19:32:09 +01:00
main()
2023-03-20 12:50:21 +01:00
st.markdown("---")
2023-03-23 23:48:33 +01:00
st.image(f"assets/{get_random_img(get_files_in_dir(assets_dir))}")
2023-03-23 22:58:02 +01:00
st.markdown("---")
selected_footer = option_menu(
menu_title=None,
2023-03-23 23:04:10 +01:00
options=[st.session_state.locale.footer_option1, st.session_state.locale.footer_option2],
2023-03-23 22:58:02 +01:00
icons=["info-circle", "piggy-bank"],
menu_icon="cast",
default_index=0,
orientation="horizontal",
styles=FOOTER_STYLES
)
st.markdown("---")
match selected_footer:
2023-03-23 23:14:19 +01:00
case st.session_state.locale.footer_option1:
2023-03-23 22:58:02 +01:00
show_info(tg_svg)
2023-03-23 23:14:19 +01:00
case st.session_state.locale.footer_option2:
2023-03-23 22:58:02 +01:00
show_donates()
case _:
show_info(tg_svg)