add error handler for API request

This commit is contained in:
kosarevskiydp 2023-03-02 20:39:03 +03:00
parent ecfdccc9ed
commit 17fe0e02e7

92
chat.py
View File

@ -1,12 +1,13 @@
from openai.error import AuthenticationError
from pathlib import Path
from gtts import gTTS, lang
from io import BytesIO
from src.utils.helpers import get_dict_key
import streamlit as st
import openai
from src.utils.helpers import get_dict_key
# --- PATH SETTINGS ---
current_dir = Path(__file__).parent if "__file__" in locals() else Path.cwd()
css_file = current_dir / "src/styles/.css"
@ -34,48 +35,51 @@ user_text = st.text_area(label="Start your conversation with AI:")
if api_key and user_text:
openai.api_key = api_key
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": user_text
}
]
)
if st.checkbox(label="Show Full API Response", value=False):
st.json(completion)
try:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": user_text
}
]
)
if st.checkbox(label="Show Full API Response", value=False):
st.json(completion)
ai_content = completion.get("choices")[0].get("message").get("content")
ai_content = completion.get("choices")[0].get("message").get("content")
if ai_content:
st.markdown(ai_content)
st.markdown("---")
if ai_content:
st.markdown(ai_content)
st.markdown("---")
col1, col2 = st.columns(2)
with col1:
languages = lang.tts_langs()
lang_options = list(lang.tts_langs().values())
default_index = lang_options.index("Russian")
lang_name = st.selectbox(
label="Select speech language",
options=lang_options,
index=default_index
)
lang_code = get_dict_key(languages, lang_name)
with col2:
speed_options = {
"Normal": False,
"Slow": True
}
speed_speech = st.radio(
label="Select speech speed",
options=speed_options.keys(),
)
is_speech_slow = speed_options.get(speed_speech)
if lang_code and is_speech_slow is not None:
sound_file = BytesIO()
tts = gTTS(text=ai_content, lang=lang_code, slow=is_speech_slow)
tts.write_to_fp(sound_file)
st.write("Push play to hear sound of AI:")
st.audio(sound_file)
col1, col2 = st.columns(2)
with col1:
languages = lang.tts_langs()
lang_options = list(lang.tts_langs().values())
default_index = lang_options.index("Russian")
lang_name = st.selectbox(
label="Select speech language",
options=lang_options,
index=default_index
)
lang_code = get_dict_key(languages, lang_name)
with col2:
speed_options = {
"Normal": False,
"Slow": True
}
speed_speech = st.radio(
label="Select speech speed",
options=speed_options.keys(),
)
is_speech_slow = speed_options.get(speed_speech)
if lang_code and is_speech_slow is not None:
sound_file = BytesIO()
tts = gTTS(text=ai_content, lang=lang_code, slow=is_speech_slow)
tts.write_to_fp(sound_file)
st.write("Push play to hear sound of AI:")
st.audio(sound_file)
except AuthenticationError as err:
st.error(err)