From 043722624d2cfcfb8d4c2474f0b8637d926f2676 Mon Sep 17 00:00:00 2001 From: kosarevskiydp Date: Thu, 2 Mar 2023 17:32:39 +0300 Subject: [PATCH] init project --- .gitignore | 1 + .streamlit/config.toml | 6 ++++ chat.py | 81 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++ run.sh | 1 + src/styles/.css | 32 +++++++++++++++++ src/utils/helpers.py | 7 ++++ 7 files changed, 131 insertions(+) create mode 100644 .streamlit/config.toml create mode 100644 chat.py create mode 100644 requirements.txt create mode 100644 run.sh create mode 100644 src/styles/.css create mode 100644 src/utils/helpers.py diff --git a/.gitignore b/.gitignore index 872c981..3ee87fc 100644 --- a/.gitignore +++ b/.gitignore @@ -129,3 +129,4 @@ dmypy.json .pyre/ .idea +*secrets.toml diff --git a/.streamlit/config.toml b/.streamlit/config.toml new file mode 100644 index 0000000..5c460d9 --- /dev/null +++ b/.streamlit/config.toml @@ -0,0 +1,6 @@ +[theme] +primaryColor = "#4ECCA3" +backgroundColor = "#232931" +secondaryBackgroundColor = "#393E46" +textColor = "#EEEEEE" +font = "monospace" diff --git a/chat.py b/chat.py new file mode 100644 index 0000000..a454daa --- /dev/null +++ b/chat.py @@ -0,0 +1,81 @@ +from pathlib import Path +from gtts import gTTS, lang +from io import BytesIO + +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" +assets_dir = current_dir / "src/assets" +icons_dir = assets_dir / "icons" + +# --- GENERAL SETTINGS --- +PAGE_TITLE = "AI Talks" +PAGE_ICON = ":robot:" + +st.set_page_config(page_title=PAGE_TITLE, page_icon=PAGE_ICON) + +# --- LOAD CSS --- +with open(css_file) as f: + st.markdown("".format(f.read()), unsafe_allow_html=True) + +st.markdown(f"

{PAGE_TITLE}

", unsafe_allow_html=True) +st.markdown("---") + +api_key = st.text_input(label="Input OpenAI API key:") +if api_key == "ZVER": + api_key = st.secrets.api_credentials.api_key + +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) + + 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: + 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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..16d3529 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +streamlit==1.19.0 +openai==0.27.0 +gtts==2.3.1 diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..26918a6 --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +streamlit run chat.py diff --git a/src/styles/.css b/src/styles/.css new file mode 100644 index 0000000..8941533 --- /dev/null +++ b/src/styles/.css @@ -0,0 +1,32 @@ +a { + text-decoration: none; + font-weight: 500; +} + +.block-container { + padding: 32px 16px 96px 16px; +} + +a:hover { + text-decoration: none; + color: #d33682 !important; +} + +ul { + list-style-type: none; +} + +hr { + margin-top: 0; + margin-bottom: 5%; +} + +#MainMenu { + visibility: hidden; +} +footer { + visibility: hidden; +} +header { + visibility: hidden; +} diff --git a/src/utils/helpers.py b/src/utils/helpers.py new file mode 100644 index 0000000..199337b --- /dev/null +++ b/src/utils/helpers.py @@ -0,0 +1,7 @@ +from typing import Any, Dict, List, Optional + + +def get_dict_key(dictionary: Dict, value: Any) -> Optional[Any]: + for key, val in dictionary.items(): + if val == value: + return key