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