add error handler for API request

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

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,6 +35,7 @@ user_text = st.text_area(label="Start your conversation with AI:")
if api_key and user_text:
openai.api_key = api_key
try:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
@ -79,3 +81,5 @@ if api_key and user_text:
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)