Spaces:
Paused
Paused
mattoofahad
commited on
Commit
·
17c52e7
1
Parent(s):
7789156
discord local variable, reset function update, start app button added, update side bar buttons, update main loop
Browse files- src/app.py +71 -36
src/app.py
CHANGED
|
@@ -13,10 +13,13 @@ load_dotenv()
|
|
| 13 |
|
| 14 |
def discord_hook(message):
|
| 15 |
"""_summary_"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
discord_hook("Simple chat bot initiated")
|
|
@@ -29,9 +32,16 @@ def return_true():
|
|
| 29 |
|
| 30 |
def reset_history():
|
| 31 |
"""_summary_"""
|
|
|
|
| 32 |
st.session_state.messages = []
|
| 33 |
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def check_openai_api_key():
|
| 36 |
"""_summary_"""
|
| 37 |
try:
|
|
@@ -49,6 +59,17 @@ def check_openai_api_key():
|
|
| 49 |
return False
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def main():
|
| 53 |
"""_summary_"""
|
| 54 |
st.set_page_config(
|
|
@@ -71,52 +92,66 @@ def main():
|
|
| 71 |
if "openai_maxtokens" not in st.session_state:
|
| 72 |
st.session_state["openai_maxtokens"] = 50
|
| 73 |
|
| 74 |
-
if st.session_state
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
{"role": "assistant", "content": response}
|
| 100 |
-
)
|
| 101 |
else:
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
with st.sidebar:
|
| 105 |
-
st.
|
| 106 |
label="OpenAI API key",
|
| 107 |
-
value="
|
| 108 |
help="This will not be saved or stored.",
|
| 109 |
type="password",
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
st.selectbox(
|
| 113 |
-
"Select the GPT model",
|
| 114 |
-
("gpt-3.5-turbo", "gpt-4-turbo-preview"),
|
| 115 |
)
|
| 116 |
st.slider(
|
| 117 |
"Max Tokens", min_value=20, max_value=80, step=10, key="openai_maxtokens"
|
| 118 |
)
|
| 119 |
-
st.button(
|
|
|
|
| 120 |
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
|
|
|
| 13 |
|
| 14 |
def discord_hook(message):
|
| 15 |
"""_summary_"""
|
| 16 |
+
if os.environ.get("ENV", "NOT_LOCAL") != "LOCAL":
|
| 17 |
+
url = os.environ.get("DISCORD_HOOK", "NO_HOOK")
|
| 18 |
+
if url != "NO_HOOK":
|
| 19 |
+
webhook = DiscordWebhook(
|
| 20 |
+
url=url, username="simple-chat-bot", content=message
|
| 21 |
+
)
|
| 22 |
+
webhook.execute()
|
| 23 |
|
| 24 |
|
| 25 |
discord_hook("Simple chat bot initiated")
|
|
|
|
| 32 |
|
| 33 |
def reset_history():
|
| 34 |
"""_summary_"""
|
| 35 |
+
st.session_state.openai_api_key = st.session_state.api_key
|
| 36 |
st.session_state.messages = []
|
| 37 |
|
| 38 |
|
| 39 |
+
def start_app():
|
| 40 |
+
"""_summary_"""
|
| 41 |
+
st.session_state.start_app = True
|
| 42 |
+
st.session_state.openai_api_key = st.session_state.api_key
|
| 43 |
+
|
| 44 |
+
|
| 45 |
def check_openai_api_key():
|
| 46 |
"""_summary_"""
|
| 47 |
try:
|
|
|
|
| 59 |
return False
|
| 60 |
|
| 61 |
|
| 62 |
+
def openai_request_cost(prompt_token, completion_token, model_name):
|
| 63 |
+
"""_summary_"""
|
| 64 |
+
if model_name == "gpt-3.5-turbo":
|
| 65 |
+
input_cost = 0.5 / 1e6
|
| 66 |
+
output_cost = 1.5 / 1e6
|
| 67 |
+
elif model_name == "gpt-4-turbo-preview":
|
| 68 |
+
input_cost = 1.5 / 1e6
|
| 69 |
+
output_cost = 2 / 1e6
|
| 70 |
+
return prompt_token * input_cost + completion_token * output_cost
|
| 71 |
+
|
| 72 |
+
|
| 73 |
def main():
|
| 74 |
"""_summary_"""
|
| 75 |
st.set_page_config(
|
|
|
|
| 92 |
if "openai_maxtokens" not in st.session_state:
|
| 93 |
st.session_state["openai_maxtokens"] = 50
|
| 94 |
|
| 95 |
+
if "start_app" not in st.session_state:
|
| 96 |
+
st.session_state["start_app"] = False
|
| 97 |
+
|
| 98 |
+
if st.session_state.start_app:
|
| 99 |
+
print(st.session_state.openai_api_key)
|
| 100 |
+
if (
|
| 101 |
+
st.session_state.openai_api_key is not None
|
| 102 |
+
and st.session_state.openai_api_key != ""
|
| 103 |
+
):
|
| 104 |
+
if check_openai_api_key():
|
| 105 |
+
client = OpenAI(api_key=st.session_state.openai_api_key)
|
| 106 |
+
|
| 107 |
+
for message in st.session_state.messages:
|
| 108 |
+
with st.chat_message(message["role"]):
|
| 109 |
+
st.markdown(message["content"])
|
| 110 |
+
|
| 111 |
+
if prompt := st.chat_input("Type your Query"):
|
| 112 |
+
with st.chat_message("user"):
|
| 113 |
+
st.markdown(prompt)
|
| 114 |
+
st.session_state.messages.append(
|
| 115 |
+
{"role": "user", "content": prompt}
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
with st.chat_message("assistant"):
|
| 119 |
+
stream = client.chat.completions.create(
|
| 120 |
+
model=st.session_state["openai_model"],
|
| 121 |
+
messages=[
|
| 122 |
+
{"role": m["role"], "content": m["content"]}
|
| 123 |
+
for m in st.session_state.messages
|
| 124 |
+
],
|
| 125 |
+
max_tokens=st.session_state["openai_maxtokens"],
|
| 126 |
+
stream=True,
|
| 127 |
+
)
|
| 128 |
+
response = st.write_stream(stream)
|
| 129 |
+
st.session_state.messages.append(
|
| 130 |
+
{"role": "assistant", "content": response}
|
| 131 |
)
|
| 132 |
+
else:
|
| 133 |
+
reset_history()
|
|
|
|
|
|
|
| 134 |
else:
|
| 135 |
+
with st.chat_message("assistant"):
|
| 136 |
+
st.markdown("**'OpenAI API key'** is missing.")
|
| 137 |
|
| 138 |
with st.sidebar:
|
| 139 |
+
st.text_input(
|
| 140 |
label="OpenAI API key",
|
| 141 |
+
value="",
|
| 142 |
help="This will not be saved or stored.",
|
| 143 |
type="password",
|
| 144 |
+
key="api_key",
|
| 145 |
)
|
| 146 |
|
| 147 |
st.selectbox(
|
| 148 |
+
"Select the GPT model", ("gpt-3.5-turbo", "gpt-4-turbo"), key="openai_model"
|
|
|
|
| 149 |
)
|
| 150 |
st.slider(
|
| 151 |
"Max Tokens", min_value=20, max_value=80, step=10, key="openai_maxtokens"
|
| 152 |
)
|
| 153 |
+
st.button("Start Chat", on_click=start_app, use_container_width=True)
|
| 154 |
+
st.button("Reset History", on_click=reset_history, use_container_width=True)
|
| 155 |
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|