add save chat button

This commit is contained in:
if 2023-03-25 02:09:45 +03:00
parent ff0aad9f77
commit 5c51939126
2 changed files with 18 additions and 9 deletions

View File

@ -18,11 +18,17 @@ def get_user_input():
def show_chat_buttons() -> None:
b0, b1, b2 = st.columns(3)
b0, b1, b2, b4 = st.columns(4)
with b0, b1, b2:
b0.button(st.session_state.locale.chat_run_btn)
b1.button(st.session_state.locale.chat_rerun_btn, on_click=st.cache_data.clear)
b2.button(st.session_state.locale.chat_clear_btn, on_click=clear_chat)
b0.button(label=st.session_state.locale.chat_run_btn)
b1.button(label=st.session_state.locale.chat_rerun_btn, on_click=st.cache_data.clear)
b2.button(label=st.session_state.locale.chat_clear_btn, on_click=clear_chat)
b4.download_button(
label=st.session_state.locale.chat_save_btn,
data="\n".join([str(d) for d in st.session_state.messages[1:]]),
file_name="ai-talks-chat.json",
mime="application/json",
)
def show_chat(ai_content: str, user_text: str) -> None:
@ -38,15 +44,15 @@ def show_chat(ai_content: str, user_text: str) -> None:
def show_conversation(user_content: str, model: str, role: str) -> None:
if st.session_state["messages"]:
st.session_state["messages"].append({"role": "user", "content": user_content})
if st.session_state.messages:
st.session_state.messages.append({"role": "user", "content": user_content})
else:
st.session_state["messages"] = [
{"role": "system", "content": f"{st.session_state.locale.ai_role_prefix} {role}."},
{"role": "user", "content": user_content},
]
try:
completion = send_ai_request(model, st.session_state["messages"])
completion = send_ai_request(model, st.session_state.messages)
ai_content = completion.get("choices")[0].get("message").get("content")
st.session_state["messages"].append({"role": "assistant", "content": ai_content})
if ai_content:

View File

@ -17,6 +17,7 @@ class Locale:
chat_run_btn: str
chat_rerun_btn: str
chat_clear_btn: str
chat_save_btn: str
select_placeholder1: str
select_placeholder2: str
stt_placeholder: str
@ -40,7 +41,8 @@ class EnLocale(Locale):
chat_placeholder: str = "Start Your Conversation With AI:"
chat_run_btn: str = "Run"
chat_rerun_btn: str = "Rerun"
chat_clear_btn: str = "Clear Conversation"
chat_clear_btn: str = "Clear Chat"
chat_save_btn: str = "Save Chat"
select_placeholder1: str = "Select AI Model"
select_placeholder2: str = "Select AI Role"
stt_placeholder: str = "To Hear The Voice Of AI, Press Play"
@ -64,7 +66,8 @@ class RuLocale(Locale):
chat_placeholder: str = "Начните Вашу Беседу с ИИ:"
chat_run_btn: str = "Запустить"
chat_rerun_btn: str = "Перезапустить"
chat_clear_btn: str = "Очистить Беседу"
chat_clear_btn: str = "Очистить Чат"
chat_save_btn: str = "Сохранить Чат"
select_placeholder1: str = "Выберите Модель ИИ"
select_placeholder2: str = "Выберите Роль ИИ"
stt_placeholder: str = "Чтобы Услышать ИИ Нажми Кнопку Проигрывателя"