Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def display_msg(msg, author):
|
| 2 |
"""Method to display message on the UI
|
| 3 |
Args:
|
|
@@ -42,4 +74,4 @@ def configure_openai():
|
|
| 42 |
print(e)
|
| 43 |
st.error("Something went wrong. Please try again later.")
|
| 44 |
st.stop()
|
| 45 |
-
return model
|
|
|
|
| 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:
|
|
|
|
| 74 |
print(e)
|
| 75 |
st.error("Something went wrong. Please try again later.")
|
| 76 |
st.stop()
|
| 77 |
+
return model
|