Spaces:
Sleeping
Sleeping
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import random
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
#decorator
|
| 8 |
+
def enable_chat_history(func):
|
| 9 |
+
if os.environ.get("OPENAI_API_KEY"):
|
| 10 |
+
|
| 11 |
+
# to clear chat history after swtching chatbot
|
| 12 |
+
current_page = func.__qualname__
|
| 13 |
+
if "current_page" not in st.session_state:
|
| 14 |
+
st.session_state["current_page"] = current_page
|
| 15 |
+
if st.session_state["current_page"] != current_page:
|
| 16 |
+
try:
|
| 17 |
+
st.cache_resource.clear()
|
| 18 |
+
del st.session_state["current_page"]
|
| 19 |
+
del st.session_state["messages"]
|
| 20 |
+
except:
|
| 21 |
+
pass
|
| 22 |
+
|
| 23 |
+
# to show chat history on ui
|
| 24 |
+
if "messages" not in st.session_state:
|
| 25 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
|
| 26 |
+
for msg in st.session_state["messages"]:
|
| 27 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 28 |
+
|
| 29 |
+
def execute(*args, **kwargs):
|
| 30 |
+
func(*args, **kwargs)
|
| 31 |
+
return execute
|
| 32 |
+
|
| 33 |
+
def display_msg(msg, author):
|
| 34 |
+
"""Method to display message on the UI
|
| 35 |
+
Args:
|
| 36 |
+
msg (str): message to display
|
| 37 |
+
author (str): author of the message -user/assistant
|
| 38 |
+
"""
|
| 39 |
+
st.session_state.messages.append({"role": author, "content": msg})
|
| 40 |
+
st.chat_message(author).write(msg)
|
| 41 |
+
|
| 42 |
+
def configure_openai():
|
| 43 |
+
openai_api_key = st.sidebar.text_input(
|
| 44 |
+
label="OpenAI API Key",
|
| 45 |
+
type="password",
|
| 46 |
+
value=st.session_state['OPENAI_API_KEY'] if 'OPENAI_API_KEY' in st.session_state else '',
|
| 47 |
+
placeholder="sk-..."
|
| 48 |
+
)
|
| 49 |
+
if openai_api_key:
|
| 50 |
+
st.session_state['OPENAI_API_KEY'] = openai_api_key
|
| 51 |
+
os.environ['OPENAI_API_KEY'] = openai_api_key
|
| 52 |
+
else:
|
| 53 |
+
st.error("Please add your OpenAI API key to continue.")
|
| 54 |
+
st.info("Obtain your key from this link: https://platform.openai.com/account/api-keys")
|
| 55 |
+
st.stop()
|
| 56 |
+
|
| 57 |
+
model = "gpt-3.5-turbo"
|
| 58 |
+
try:
|
| 59 |
+
client = openai.OpenAI()
|
| 60 |
+
available_models = [{"id": i.id, "created":datetime.fromtimestamp(i.created)} for i in client.models.list() if str(i.id).startswith("gpt")]
|
| 61 |
+
available_models = sorted(available_models, key=lambda x: x["created"])
|
| 62 |
+
available_models = [i["id"] for i in available_models]
|
| 63 |
+
|
| 64 |
+
model = st.sidebar.selectbox(
|
| 65 |
+
label="Model",
|
| 66 |
+
options=available_models,
|
| 67 |
+
index=available_models.index(st.session_state['OPENAI_MODEL']) if 'OPENAI_MODEL' in st.session_state else 0
|
| 68 |
+
)
|
| 69 |
+
st.session_state['OPENAI_MODEL'] = model
|
| 70 |
+
except openai.AuthenticationError as e:
|
| 71 |
+
st.error(e.body["message"])
|
| 72 |
+
st.stop()
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(e)
|
| 75 |
+
st.error("Something went wrong. Please try again later.")
|
| 76 |
+
st.stop()
|
| 77 |
+
return model
|