Spaces:
Sleeping
Sleeping
ulianast
commited on
Commit
β’
12533cd
1
Parent(s):
5487e51
init commit
Browse files- app.py +121 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import json
|
3 |
+
import hmac
|
4 |
+
import streamlit as st
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
def check_password():
|
8 |
+
"""Returns `True` if the user had a correct password."""
|
9 |
+
|
10 |
+
def login_form():
|
11 |
+
"""Form with widgets to collect user information"""
|
12 |
+
with st.form("Credentials"):
|
13 |
+
st.text_input("Username", key="username")
|
14 |
+
st.text_input("Password", type="password", key="password")
|
15 |
+
st.form_submit_button("Log in", on_click=password_entered)
|
16 |
+
|
17 |
+
def password_entered():
|
18 |
+
"""Checks whether a password entered by the user is correct."""
|
19 |
+
if hmac.compare_digest(st.session_state["password"], st.secrets["PASSWORD"]) \
|
20 |
+
and hmac.compare_digest(st.session_state["username"], st.secrets["USERNAME"]):
|
21 |
+
|
22 |
+
st.session_state["password_correct"] = True
|
23 |
+
#del st.session_state["password"] # Don't store the password.
|
24 |
+
#del st.session_state["username"]
|
25 |
+
else:
|
26 |
+
st.session_state["password_correct"] = False
|
27 |
+
|
28 |
+
if st.session_state.get("password_correct", False):
|
29 |
+
return True
|
30 |
+
|
31 |
+
login_form()
|
32 |
+
|
33 |
+
if "password_correct" in st.session_state:
|
34 |
+
st.error("π User not known or password incorrect")
|
35 |
+
return False
|
36 |
+
|
37 |
+
if not check_password():
|
38 |
+
st.stop()
|
39 |
+
|
40 |
+
def on_suggested_reply_click_handler():
|
41 |
+
response = st.session_state.reply_suggestion
|
42 |
+
st.session_state.messages.append({"role": "Emily", "content": response})
|
43 |
+
|
44 |
+
logging.basicConfig(filename="demo.log", filemode="a+", level=logging.INFO)
|
45 |
+
|
46 |
+
openai_api_key = st.secrets["OPEN_AI_API_KEY"]
|
47 |
+
client = OpenAI(api_key=openai_api_key)
|
48 |
+
|
49 |
+
st.title("Unti debuffing")
|
50 |
+
col1, col2 = st.columns(2)
|
51 |
+
|
52 |
+
instruction = st.secrets["INSTRUCTION"]
|
53 |
+
|
54 |
+
if "messages" not in st.session_state:
|
55 |
+
st.session_state.messages = []
|
56 |
+
|
57 |
+
with col1:
|
58 |
+
st.header("Alison's screen")
|
59 |
+
|
60 |
+
# Accept user input
|
61 |
+
if prompt := st.chat_input("Alison's message", key="elison_prompt"):
|
62 |
+
st.session_state.messages.append({"role": "Alison", "content": prompt})
|
63 |
+
|
64 |
+
if prompt:
|
65 |
+
try:
|
66 |
+
dialog = "".join([f"{message['role']} : {message['content']}\n" for message in st.session_state.messages])
|
67 |
+
|
68 |
+
completion = client.chat.completions.create(
|
69 |
+
model="gpt-4-turbo-preview",
|
70 |
+
messages=[
|
71 |
+
{"role": "system", "content": "You are a good psychologist"},
|
72 |
+
{"role": "user", "content": instruction.format(DIALOG=dialog)}
|
73 |
+
]
|
74 |
+
)
|
75 |
+
reply = completion.choices[0].message.content
|
76 |
+
print(f"Reply: {reply}")
|
77 |
+
reply_suggestion = json.loads(reply)["possible_reply"]
|
78 |
+
manipulations = json.loads(reply)["manipulations"]
|
79 |
+
except Exception as e:
|
80 |
+
logging.error(e, exc_info=True)
|
81 |
+
reply_suggestion = "Hey! I don't understand:)"
|
82 |
+
manipulations = None
|
83 |
+
|
84 |
+
with col2:
|
85 |
+
st.header("Emily's screen")
|
86 |
+
autoreply = st.checkbox('Autoreply', value=True, key='autoreply')
|
87 |
+
|
88 |
+
if not autoreply:
|
89 |
+
if response := st.chat_input("Emily's message", key="emily_prompt"):
|
90 |
+
st.session_state.messages.append({"role": "Emily", "content": response})
|
91 |
+
elif prompt:
|
92 |
+
response = reply_suggestion
|
93 |
+
st.session_state.messages.append({"role": "Emily", "content": response})
|
94 |
+
|
95 |
+
if prompt and manipulations:
|
96 |
+
try:
|
97 |
+
for m in manipulations:
|
98 |
+
m_title = m["manipulation_short_name"]
|
99 |
+
m_desc = m["explanation"]
|
100 |
+
st.warning(f"Detected manipulation: {m_title}\n\n{m_desc}")
|
101 |
+
except Exception as e:
|
102 |
+
logging.error(e, exc_info=True)
|
103 |
+
|
104 |
+
if not autoreply:
|
105 |
+
with st.form(key="use_suggested_reply"):
|
106 |
+
response = reply_suggestion
|
107 |
+
st.session_state.reply_suggestion = response
|
108 |
+
st.info(f"Suggested reply:\n\n{reply_suggestion}")
|
109 |
+
submitted = st.form_submit_button("Use suggested reply", on_click=on_suggested_reply_click_handler)
|
110 |
+
else:
|
111 |
+
st.info(f"Suggested reply:\n\n{reply_suggestion}")
|
112 |
+
|
113 |
+
with st.sidebar:
|
114 |
+
if st.button("New dialog"):
|
115 |
+
st.session_state.messages = []
|
116 |
+
st.session_state.reply_suggestion = None
|
117 |
+
|
118 |
+
st.header("Active dialog")
|
119 |
+
for message in st.session_state.messages:
|
120 |
+
with st.chat_message(message["role"]):
|
121 |
+
st.markdown(message["content"])
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
openai==1.14.2
|