1
0
AMUseBot/ai_talks/chat.py

132 lines
4.4 KiB
Python
Raw Normal View History

2023-03-02 15:32:39 +01:00
from pathlib import Path
2023-04-23 21:33:19 +02:00
from random import randrange
2023-03-02 18:39:03 +01:00
2023-03-02 15:32:39 +01:00
import streamlit as st
2023-04-23 21:33:19 +02:00
from src.styles.menu_styles import FOOTER_STYLES, HEADER_STYLES
from src.utils.conversation import get_user_input, show_chat_buttons, show_conversation
from src.utils.footer import show_donates, show_info
from src.utils.helpers import get_files_in_dir, get_random_img
from src.utils.lang import en, ru
from streamlit_option_menu import option_menu
2023-03-02 15:32:39 +01:00
# --- PATH SETTINGS ---
2023-04-07 22:10:58 +02:00
current_dir: Path = Path(__file__).parent if "__file__" in locals() else Path.cwd()
css_file: Path = current_dir / "src/styles/.css"
assets_dir: Path = current_dir / "assets"
icons_dir: Path = assets_dir / "icons"
img_dir: Path = assets_dir / "img"
tg_svg: Path = icons_dir / "tg.svg"
2023-03-02 15:32:39 +01:00
# --- GENERAL SETTINGS ---
2023-04-07 22:10:58 +02:00
PAGE_TITLE: str = "AI Talks"
PAGE_ICON: str = "🤖"
LANG_EN: str = "En"
LANG_RU: str = "Ru"
AI_MODEL_OPTIONS: list[str] = [
2023-03-22 17:13:02 +01:00
"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:
2023-04-23 21:33:19 +02:00
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
2023-03-02 15:32:39 +01:00
2023-03-22 17:13:02 +01:00
selected_lang = option_menu(
menu_title=None,
2023-04-07 22:10:58 +02:00
options=[LANG_EN, LANG_RU, ],
2023-04-08 13:46:06 +02:00
icons=["globe2", "translate"],
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
2023-04-07 22:10:58 +02:00
if "locale" not in st.session_state:
st.session_state.locale = en
2023-03-11 15:39:23 +01:00
if "generated" not in st.session_state:
2023-04-07 22:10:58 +02:00
st.session_state.generated = []
2023-03-11 15:39:23 +01:00
if "past" not in st.session_state:
2023-04-07 22:10:58 +02:00
st.session_state.past = []
2023-03-11 15:39:23 +01:00
if "messages" not in st.session_state:
2023-04-07 22:10:58 +02:00
st.session_state.messages = []
2023-03-11 15:39:23 +01:00
if "user_text" not in st.session_state:
2023-04-07 22:10:58 +02:00
st.session_state.user_text = ""
if "input_kind" not in st.session_state:
st.session_state.input_kind = st.session_state.locale.input_kind_1
2023-04-23 21:33:19 +02:00
if "seed" not in st.session_state:
st.session_state.seed = randrange(10**3) # noqa: S311
2023-04-26 10:02:34 +02:00
if "costs" not in st.session_state:
st.session_state.costs = []
if "total_tokens" not in st.session_state:
st.session_state.total_tokens = []
2023-03-11 15:39:23 +01:00
2023-03-02 19:32:09 +01:00
def main() -> None:
2023-04-07 22:10:58 +02:00
c1, c2 = st.columns(2)
2023-03-22 17:13:02 +01:00
with c1, c2:
c1.selectbox(label=st.session_state.locale.select_placeholder1, key="model", options=AI_MODEL_OPTIONS)
2023-04-07 22:10:58 +02:00
st.session_state.input_kind = c2.radio(
label=st.session_state.locale.input_kind,
options=(st.session_state.locale.input_kind_1, st.session_state.locale.input_kind_2),
horizontal=True,
)
role_kind = c1.radio(
2023-04-01 23:37:20 +02:00
label=st.session_state.locale.radio_placeholder,
options=(st.session_state.locale.radio_text1, st.session_state.locale.radio_text2),
horizontal=True,
)
2023-03-28 02:22:21 +02:00
match role_kind:
2023-04-01 23:37:20 +02:00
case st.session_state.locale.radio_text1:
2023-04-07 22:10:58 +02:00
c2.selectbox(label=st.session_state.locale.select_placeholder2, key="role",
2023-03-28 02:22:21 +02:00
options=st.session_state.locale.ai_role_options)
2023-04-01 23:37:20 +02:00
case st.session_state.locale.radio_text2:
2023-04-07 22:10:58 +02:00
c2.text_input(label=st.session_state.locale.select_placeholder3, key="role")
2023-03-06 14:27:43 +01:00
2023-04-07 22:10:58 +02:00
if st.session_state.user_text:
show_conversation()
st.session_state.user_text = ""
get_user_input()
show_chat_buttons()
2023-03-02 19:32:09 +01:00
2023-04-16 02:01:02 +02:00
def run_agi():
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 _:
2023-04-07 22:10:58 +02:00
st.session_state.locale = en
2023-03-22 17:13:02 +01:00
st.markdown(f"<h1 style='text-align: center;'>{st.session_state.locale.title}</h1>", unsafe_allow_html=True)
2023-03-23 22:58:02 +01:00
selected_footer = option_menu(
menu_title=None,
2023-04-08 12:03:08 +02:00
options=[
st.session_state.locale.footer_option1,
st.session_state.locale.footer_option0,
st.session_state.locale.footer_option2,
],
2023-04-08 13:46:06 +02:00
icons=["info-circle", "chat-square-text", "piggy-bank"], # https://icons.getbootstrap.com/
2023-03-23 22:58:02 +01:00
menu_icon="cast",
default_index=0,
orientation="horizontal",
styles=FOOTER_STYLES
)
match selected_footer:
2023-04-08 12:03:08 +02:00
case st.session_state.locale.footer_option0:
main()
2023-03-23 23:14:19 +01:00
case st.session_state.locale.footer_option1:
2023-04-08 12:03:08 +02:00
st.image(f"{img_dir}/{get_random_img(get_files_in_dir(img_dir))}")
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)
2023-04-16 02:01:02 +02:00
if __name__ == "__main__":
run_agi()