add cache and Rerun button

This commit is contained in:
kosarevskiydp 2023-03-02 21:32:09 +03:00
parent b496c9a95e
commit c643138384
2 changed files with 32 additions and 22 deletions

45
chat.py
View File

@ -24,23 +24,34 @@ with open(css_file) as f:
st.markdown(f"<h1 style='text-align: center;'>{PAGE_TITLE}</h1>", unsafe_allow_html=True) st.markdown(f"<h1 style='text-align: center;'>{PAGE_TITLE}</h1>", unsafe_allow_html=True)
st.markdown("---") 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: user_text = st.text_area(label="Start your conversation with AI:")
try: if st.button("Rerun"):
ai_content = send_ai_request(api_key, user_text) st.cache_data.clear()
if ai_content:
st.markdown(ai_content)
st.markdown("---")
col1, col2 = st.columns(2) if api_key and user_text:
with col1: try:
lang_code = lang_selector() completion = send_ai_request(api_key, user_text)
with col2: if st.checkbox(label="Show Full API Response", value=False):
is_speech_slow = speech_speed_radio() st.json(completion)
show_player(ai_content, lang_code, is_speech_slow) ai_content = completion.get("choices")[0].get("message").get("content")
except AuthenticationError as err: if ai_content:
st.error(err) 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()

View File

@ -46,7 +46,9 @@ def show_player(ai_content: str, lang_code: str, is_speech_slow: bool) -> None:
st.audio(sound_file) 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 openai.api_key = api_key
completion = openai.ChatCompletion.create( completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo", 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): return completion
st.json(completion)
return completion.get("choices")[0].get("message").get("content")
def api_key_checker(api_key: str) -> str: def api_key_checker(api_key: str) -> str: