Spaces:
Running
Running
File size: 2,962 Bytes
59d5667 5431cb0 59d5667 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import pandas as pd
import streamlit as st
from streamlit.logger import get_logger
import langchain
from app_config import ENVIRON
from utils.memory_utils import change_memories
from models.model_seeds import seeds
langchain.verbose = ENVIRON =="dev"
logger = get_logger(__name__)
# TODO: Include more variable and representative names
DEFAULT_NAMES = ["Olivia", "Kit", "Abby", "Tom", "Carolyne", "Jessiny"]
DEFAULT_NAMES_DF = pd.read_csv("./utils/names.csv")
def get_random_name(gender="Neutral", ethnical_group="Neutral", names_df=None):
if names_df is None:
names_df = pd.DataFrame(DEFAULT_NAMES, columns=['name'])
names_df["gender"] = "Neutral"
names_df["ethnical_group"] = "Neutral"
dfi = names_df
if gender != "Neutral":
dfi = dfi.query(f"gender=='{gender}'")
if ethnical_group != "Neutral":
dfi = dfi.query(f"ethnical_group=='{ethnical_group}'")
if len(dfi) <=0 :
dfi = names_df
return dfi.sample(1)['name'].values[0]
def divide_messages(str_memory, str_ai_prefix="texter", str_human_prefix="helper", include_colon=True):
message_delimiter = "$%$"
# Split str memory in messaages according to previous prefix and flatten list
colon = ":" if include_colon else ""
str_memory = f"{message_delimiter}{str_ai_prefix}{colon}".join(str_memory.split(f"{str_ai_prefix}{colon}"))
str_memory = f"{message_delimiter}{str_human_prefix}{colon}".join(str_memory.split(f"{str_human_prefix}{colon}"))
return str_memory.split(message_delimiter)
def add_initial_message(issue, language, memory, str_ai_prefix="texter", str_human_prefix="helper", include_colon=True,
texter_name="", counselor_name=""):
initial_mem_str = seeds.get(issue, "GCT")['memory'].format(counselor_name=counselor_name, texter_name=texter_name)
message_list = divide_messages(initial_mem_str, str_ai_prefix, str_human_prefix, include_colon)
colon = ":" if include_colon else ""
for i, message in enumerate(message_list):
message = message.strip("\n")
message = message.strip()
if message is None or message == "":
pass
elif message.startswith(str_human_prefix):
memory.chat_memory.add_user_message(message.lstrip(f"{str_human_prefix}{colon}").strip())
elif message.startswith(str_ai_prefix):
memory.chat_memory.add_ai_message(message.lstrip(f"{str_ai_prefix}{colon}").strip())
def create_memory_add_initial_message(memories, issue, language, changed_source=False, texter_name="", counselor_name=""):
change_memories(memories, language, changed_source=changed_source)
for memory, _ in memories.items():
if len(st.session_state[memory].buffer_as_messages) < 1:
add_initial_message(issue, language, st.session_state[memory], texter_name=texter_name, counselor_name=counselor_name)
|