-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathapp.py
154 lines (128 loc) · 5.01 KB
/
app.py
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
65
66
67
68
69
70
71
72
73
74
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from dataclasses import asdict
from io import StringIO
import json
import os
import streamlit as st
from data_driven_characters.character import generate_character_definition, Character
from data_driven_characters.corpus import (
generate_corpus_summaries,
generate_docs,
)
from data_driven_characters.chatbots import (
SummaryChatBot,
RetrievalChatBot,
SummaryRetrievalChatBot,
)
from data_driven_characters.interfaces import reset_chat, clear_user_input, converse
@st.cache_resource()
def create_chatbot(character_definition, corpus_summaries, chatbot_type):
if chatbot_type == "summary":
chatbot = SummaryChatBot(character_definition=character_definition)
elif chatbot_type == "retrieval":
chatbot = RetrievalChatBot(
character_definition=character_definition,
documents=corpus_summaries,
)
elif chatbot_type == "summary with retrieval":
chatbot = SummaryRetrievalChatBot(
character_definition=character_definition,
documents=corpus_summaries,
)
else:
raise ValueError(f"Unknown chatbot type: {chatbot_type}")
return chatbot
@st.cache_data(persist="disk")
def process_corpus(corpus):
# load docs
docs = generate_docs(
corpus=corpus,
chunk_size=2048,
chunk_overlap=64,
)
# generate summaries
corpus_summaries = generate_corpus_summaries(docs=docs, summary_type="map_reduce")
return corpus_summaries
@st.cache_data(persist="disk")
def get_character_definition(name, corpus_summaries):
character_definition = generate_character_definition(
name=name,
corpus_summaries=corpus_summaries,
)
return asdict(character_definition)
def main():
st.title("Data-Driven Characters")
st.write(
"Upload a corpus in the sidebar to generate a character chatbot that is grounded in the corpus content."
)
openai_api_key = st.text_input(
label="Your OpenAI API KEY",
placeholder="Your OpenAI API KEY",
type="password",
)
os.environ["OPENAI_API_KEY"] = openai_api_key
with st.sidebar:
uploaded_file = st.file_uploader("Upload corpus")
if uploaded_file is not None:
corpus_name = os.path.splitext(os.path.basename(uploaded_file.name))[0]
# read file
stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
corpus = stringio.read()
# scrollable text
st.markdown(
f"""
<div style='overflow: auto; height: 200px; border: 1px solid gray; border-radius: 5px; padding: 10px'>
{corpus}</div>
""",
unsafe_allow_html=True,
)
st.divider()
# get character name
character_name = st.text_input(f"Enter a character name from {corpus_name}")
if character_name:
if not openai_api_key:
st.error(
"You must enter an API key to use the OpenAI API. Please enter an API key in the sidebar."
)
return
if (
"character_name" in st.session_state
and st.session_state["character_name"] != character_name
):
clear_user_input()
reset_chat()
st.session_state["character_name"] = character_name
with st.spinner("Processing corpus (this will take a while)..."):
corpus_summaries = process_corpus(corpus)
with st.spinner("Generating character definition..."):
# get character definition
character_definition = get_character_definition(
name=character_name,
corpus_summaries=corpus_summaries,
)
print(json.dumps(character_definition, indent=4))
chatbot_type = st.selectbox(
"Select a memory type",
options=["summary", "retrieval", "summary with retrieval"],
index=2,
)
if (
"chatbot_type" in st.session_state
and st.session_state["chatbot_type"] != chatbot_type
):
clear_user_input()
reset_chat()
st.session_state["chatbot_type"] = chatbot_type
st.markdown(
f"[Export to character.ai](https://beta.character.ai/editing):"
)
st.write(character_definition)
if uploaded_file is not None and character_name:
st.divider()
chatbot = create_chatbot(
character_definition=Character(**character_definition),
corpus_summaries=corpus_summaries,
chatbot_type=chatbot_type,
)
converse(chatbot)
if __name__ == "__main__":
main()