add empty_api_handler

This commit is contained in:
if 2023-04-16 01:07:54 +03:00
parent b6febf4ab7
commit 252322b50d
4 changed files with 20 additions and 5 deletions

View File

@ -3,7 +3,7 @@ streamlit-chat==0.0.2.2
streamlit_option_menu==0.3.2 streamlit_option_menu==0.3.2
openai==0.27.4 openai==0.27.4
gtts==2.3.1 gtts==2.3.1
pip==23.0.1 pip==23.1
bokeh==2.4.2 bokeh==2.4.2
streamlit-bokeh-events==0.1.2 streamlit-bokeh-events==0.1.2
watchdog==3.0.0 watchdog==3.0.0

View File

@ -1,4 +1,4 @@
pytest==7.3.0 pytest==7.3.1
ruff==0.0.261 ruff==0.0.261
pre_commit==3.2.2 pre_commit==3.2.2
pytest-cov==4.0.0 pytest-cov==4.0.0

View File

@ -1,5 +1,5 @@
import logging import logging
from typing import List from typing import List # NOQA: UP035
import openai import openai
import streamlit as st import streamlit as st
@ -7,7 +7,10 @@ import streamlit as st
@st.cache_data() @st.cache_data()
def create_gpt_completion(ai_model: str, messages: List[dict]) -> dict: def create_gpt_completion(ai_model: str, messages: List[dict]) -> dict:
try:
openai.api_key = st.secrets.api_credentials.api_key openai.api_key = st.secrets.api_credentials.api_key
except (KeyError, AttributeError):
st.error(st.session_state.locale.empty_api_handler)
logging.info(f"{messages=}") logging.info(f"{messages=}")
completion = openai.ChatCompletion.create( completion = openai.ChatCompletion.create(
model=ai_model, model=ai_model,

View File

@ -1,5 +1,5 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import List from typing import List # NOQA: UP035
@dataclass @dataclass
@ -36,6 +36,7 @@ class Locale:
footer_channel: str footer_channel: str
responsibility_denial: str responsibility_denial: str
donates_info: str donates_info: str
empty_api_handler: str
AI_ROLE_OPTIONS_EN = [ AI_ROLE_OPTIONS_EN = [
@ -71,6 +72,9 @@ AI_ROLE_OPTIONS_RU = [
"переводчик корпоративного жаргона на простой русский", "переводчик корпоративного жаргона на простой русский",
] ]
readme_url = "https://github.com/dKosarevsky/AI-Talks#readme"
ai_talks_url = "https://ai-talks.streamlit.app/"
en = Locale( en = Locale(
ai_role_options=AI_ROLE_OPTIONS_EN, ai_role_options=AI_ROLE_OPTIONS_EN,
ai_role_prefix="You are a female", ai_role_prefix="You are a female",
@ -113,6 +117,10 @@ en = Locale(
This allows you to provide access to communication with AI for all users. This allows you to provide access to communication with AI for all users.
Support us for joint development and interaction with the intelligence of the future! Support us for joint development and interaction with the intelligence of the future!
""", """,
empty_api_handler=f"""
API key not found. Create `.streamlit/secrets.toml` with your API key.
See [README.md]({readme_url}) for instructions or use the original [AI Talks]({ai_talks_url}).
""",
) )
ru = Locale( ru = Locale(
ai_role_options=AI_ROLE_OPTIONS_RU, ai_role_options=AI_ROLE_OPTIONS_RU,
@ -157,4 +165,8 @@ ru = Locale(
Это позволяет обеспечить доступ к общению с ИИ для всех желающих пользователей. Это позволяет обеспечить доступ к общению с ИИ для всех желающих пользователей.
Поддержите нас для совместного развития и взаимодействия с интеллектом будущего! Поддержите нас для совместного развития и взаимодействия с интеллектом будущего!
""", """,
empty_api_handler=f"""
Ключ API не найден. Создайте `.streamlit/secrets.toml` с вашим ключом API.
Инструкции см. в [README.md]({readme_url}) или используйте оригинальный [AI Talks]({ai_talks_url}).
""",
) )