diff --git a/chat.py b/chat.py index 0c83ff7..472fac1 100644 --- a/chat.py +++ b/chat.py @@ -24,23 +24,34 @@ with open(css_file) as f: st.markdown(f"

{PAGE_TITLE}

", unsafe_allow_html=True) st.markdown("---") -api_key = st.text_input(label="Input OpenAI API key:") -api_key = api_key_checker(api_key) -user_text = st.text_area(label="Start your conversation with AI:") +def main() -> None: + api_key = st.text_input(label="Input OpenAI API key:") + api_key = api_key_checker(api_key) -if api_key and user_text: - try: - ai_content = send_ai_request(api_key, user_text) - if ai_content: - st.markdown(ai_content) - st.markdown("---") + user_text = st.text_area(label="Start your conversation with AI:") + if st.button("Rerun"): + st.cache_data.clear() - col1, col2 = st.columns(2) - with col1: - lang_code = lang_selector() - with col2: - is_speech_slow = speech_speed_radio() - show_player(ai_content, lang_code, is_speech_slow) - except AuthenticationError as err: - st.error(err) + if api_key and user_text: + try: + completion = send_ai_request(api_key, 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") + if ai_content: + st.markdown(ai_content) + st.markdown("---") + + col1, col2 = st.columns(2) + with col1: + lang_code = lang_selector() + with col2: + is_speech_slow = speech_speed_radio() + show_player(ai_content, lang_code, is_speech_slow) + except AuthenticationError as err: + st.error(err) + + +if __name__ == "__main__": + main() diff --git a/src/utils/helpers.py b/src/utils/helpers.py index e8cee59..78299d7 100644 --- a/src/utils/helpers.py +++ b/src/utils/helpers.py @@ -46,7 +46,9 @@ def show_player(ai_content: str, lang_code: str, is_speech_slow: bool) -> None: st.audio(sound_file) -def send_ai_request(api_key: str, user_text: str, ) -> str: +@st.cache_data() +# @st.cache_data(suppress_st_warning=True) +def send_ai_request(api_key: str, user_text: str, ) -> Dict: openai.api_key = api_key completion = openai.ChatCompletion.create( model="gpt-3.5-turbo", @@ -57,10 +59,7 @@ def send_ai_request(api_key: str, user_text: str, ) -> str: } ] ) - if st.checkbox(label="Show Full API Response", value=False): - st.json(completion) - - return completion.get("choices")[0].get("message").get("content") + return completion def api_key_checker(api_key: str) -> str: