-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.py
364 lines (314 loc) · 12.6 KB
/
ui.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import streamlit as st
import json
import pandas as pd
import matplotlib.pyplot as plt
from objects.state_machine import StateMachine
from objects.AI_model import get_ai_model
from generate_conversations import generate_convs
from show_graph import get_graph_dot
from utils_streamlit import json_to_streamlit_flow, highlight_conv_path
from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
from streamlit_flow.state import StreamlitFlowState
from streamlit_flow.layouts import TreeLayout
from streamlit_flow import streamlit_flow
import objects.hotel_machine as hotel_machine
import objects.doctor_machine as doctor_machine
import objects.recipe_machine as recipe_machine
import objects.rent_car_machine as rent_car_machine
import objects.worker_agenda_machine as worker_agenda_machine
use_unieval = False
if use_unieval:
import torch
from unieval.eval_conv import unieval_eval
# if torch.cuda.is_available():
# use_unieval = True
st.set_page_config(
page_title="Conversation generator",
page_icon="🗣️",
layout="wide",
initial_sidebar_state="expanded",
)
# Streamlit app
st.title("🗣️ Conversation dataset generator")
st.write("Create a task oriented dialogue conversation dataset using a state machine.")
st.write("Modify the example below or create your own graph.")
st.write(
"Right click to add nodes, and then drag and drop to connect them. Right click on nodes or edges to modify their label or delete them. The conversation flow goes from left to right."
)
# API Key input
with st.sidebar:
st.title("Conversation generator")
# add a two button for openai and azure openai
selected_provider = st.radio(
"👇 First, select a provider",
[("OpenAI", "openai"), ("Azure OpenAI", "azure_openai")],
horizontal=True,
format_func=lambda x: x[0],
)[1]
model_name = st.text_input("Enter the name of the model you want to use")
api_key = st.text_input("Enter your OpenAI API Key", type="password")
if selected_provider == "azure_openai":
deployment_endpoint = st.text_input("Enter your Deployment Endpoint", "")
# add a markdown delimiter
st.markdown("---")
if (api_key and model_name and selected_provider == "openai") or (
api_key
and deployment_endpoint
and model_name
and selected_provider == "azure_openai"
):
if selected_provider == "openai":
deployment_endpoint = None
get_ai_model().set_model(
type=selected_provider,
model_name=model_name,
api_key=api_key,
endpoint=deployment_endpoint,
)
# TODO: add a button to charge the graph and change between the different state machines
# TODO: add a button to empty the graph window
graph = hotel_machine.HotelMachine()
nodes, edges = json_to_streamlit_flow(graph.to_json()["transitions_graph"])
new_state = streamlit_flow(
"fully_interactive_flow",
StreamlitFlowState(
nodes, edges
), # Start with an empty state, or with some pre-initialized state
fit_view=True,
show_controls=True,
allow_new_edges=True,
animate_new_edges=True,
layout=TreeLayout("right"),
enable_pane_menu=True,
enable_edge_menu=True,
enable_node_menu=True,
show_minimap=True,
hide_watermark=True,
)
col1, col2 = st.columns(2)
col1.metric("Nodes", len(new_state.nodes))
col2.metric("Edges", len(new_state.edges))
def extract_states_edges(edges, nodes):
states = {}
val = {node.id: node.data["content"] for node in nodes}
list_edges = [o for o in edges]
for edge in list_edges:
source = val[edge.source]
target = val[edge.target]
if source not in states:
states[source] = {}
states[source][edge.label] = target
return states
json_edges = extract_states_edges(new_state.edges, new_state.nodes)
initial_sentence = st.text_input(
"Enter your initial sentence",
"Hello, I'm your assistant, how can I help you ?",
placeholder="Hello my name is John, how can I help you ?",
)
custom_api_url = st.text_input(
"Enter the custom API URL",
placeholder="https://my_custom_api.com/",
)
# slider to generate from 1 to 1000 samples
num_samples = st.slider("Number of conversation to generate", 1, 1000, 100)
# add a panel where you can assign transition to api endpoint
df = pd.DataFrame(
{
"Transition": ["select_hotel", "search_hotels", "ask_for_more_hotels"],
"API Endpoint": ["select_i", "/hotel/search", "/hotel/search"],
}
)
with st.expander("Function calling settings"):
function_calling_df = st.data_editor(
# pd.DataFrame(columns=["Transition", "API Endpoint"]),
# df,
# hide_index=True,
# use_container_width=True,
# num_rows="dynamic",
pd.DataFrame(columns=["Transition", "API Endpoint"]),
hide_index=True,
use_container_width=True,
num_rows="dynamic",
)
st.write(
"To use the API special transitions, host it in localhost and add those transitions to the table above."
)
st.write(df)
# Convert function calling to a json "transition": "api_endpoint"
function_calling = function_calling_df.dropna().to_dict(orient="records")
function_calling = {
item["Transition"]: item["API Endpoint"] for item in function_calling
}
if "count" not in st.session_state:
st.session_state.count = 0
if "eval_disabled" not in st.session_state:
st.session_state.eval_disabled = True
if "eval_button" not in st.session_state:
st.session_state.eval_button = False
if "gen_clicked" not in st.session_state:
st.session_state.gen_clicked = False
if "conv_generated" not in st.session_state:
st.session_state.conv_generated = ""
if "conv_generated_jsonl" not in st.session_state:
st.session_state.conv_generated_jsonl = ""
if "highlighted_flow" not in st.session_state:
st.session_state.highlighted_flow = ""
def increment_counter():
st.session_state.count += 1
st.rerun()
def reset_counter():
st.session_state.count = 0
def click_button():
st.session_state.gen_clicked = True
def click_eval_button():
st.session_state.eval_button = True
generate_button = st.button(
"Generate conversation dataset",
use_container_width=True,
type="primary",
key="generate_button",
on_click=click_button,
)
# launcheval_button = st.button(
# "Evaluate the generated conversations (GPU needed)",
# use_container_width=True,
# type="primary",
# key="launcheval_button",
# disabled=st.session_state.eval_disabled,
# on_click=click_eval_button,
# )
config_data = {
"transitions_graph": json_edges if json_edges else "",
"function_call": function_calling,
"datafile": "",
"initial_sentence": initial_sentence,
"api_adress": custom_api_url,
}
with st.sidebar:
st.download_button(
"Download json config",
data=json.dumps(config_data, indent=4, ensure_ascii=False),
use_container_width=True,
file_name="graph_config.json",
mime="application/json",
)
if st.session_state.gen_clicked:
try:
state_graph = json_edges
sm = StateMachine(
state_graph,
function_calling,
initial_sentence=initial_sentence,
api_adress=custom_api_url,
)
print(sm.function_calling)
with st.spinner("Generating conversations..."):
generated_conversations = generate_convs(sm, num_samples)
# Convert the list of dict to a jsonlines file
generated_conversations_jsonl = "\n".join(
[
json.dumps(conv, ensure_ascii=False)
for conv in generated_conversations
]
)
st.session_state.conv_generated = generated_conversations
st.session_state.conv_generated_jsonl = generated_conversations_jsonl
st.session_state.gen_clicked = False
st.session_state.eval_disabled = False
except json.JSONDecodeError:
st.error("Invalid JSON input. Please correct it and try again.")
if use_unieval:
if st.session_state.eval_button:
# Show Unieval evaluation of those conversations
st.markdown("---")
st.subheader("Evaluate the generated conversations (GPU needed)")
eval_convs = unieval_eval(
st.session_state.conv_generated_jsonl,
["naturalness", "coherence", "understandability"],
mode="full",
type="graphtod",
)
st.write(eval_convs)
if st.session_state.conv_generated:
st.write("Conversation number {}".format(st.session_state.count + 1))
if len(st.session_state.conv_generated) == st.session_state.count:
st.session_state.count = 0
counter_conv = st.button("Next conversation", on_click=reset_counter)
else:
counter_conv = st.button("Next conversation", on_click=increment_counter)
st.session_state.eval_disabled = True
if len(st.session_state.conv_generated) > 1:
if st.session_state.count >= len(st.session_state.conv_generated):
st.session_state.count = 0
else:
st.session_state.count = 0
st.markdown("---")
r_col1 = st.columns(1)[0]
with r_col1:
flow = highlight_conv_path(
new_state,
st.session_state.conv_generated[st.session_state.count],
)
highlight_flow = streamlit_flow(
"highlighted_flow",
StreamlitFlowState(
flow.nodes, flow.edges
), # Start with an empty state, or with some pre-initialized state
fit_view=True,
# show_controls=True,
# allow_new_edges=True,
# animate_new_edges=True,
layout=TreeLayout("right"),
# enable_pane_menu=True,
# enable_edge_menu=True,
# enable_node_menu=True,
# show_minimap=True,
hide_watermark=True,
)
# Show a conversation example
st.markdown("---")
r_col1, r_col2 = st.columns(2)
with r_col1:
st.subheader("Example of generated conversation")
example_conv = st.session_state.conv_generated[st.session_state.count][
"conversation"
]
st.write(
" <br />".join(
[f"[{line['role']}]: {line['text']}" for line in example_conv]
),
unsafe_allow_html=True,
)
# with r_col2:
# # make stats about users
# st.subheader("Stats about generated conversations persona")
# users_df = pd.DataFrame(
# [conv["user"] for conv in st.session_state.conv_generated]
# )
# # Create a pie chart for gender distribution
# gender_counts = users_df["gender"].value_counts()
# plt.figure(figsize=(8, 6))
# plt.pie(gender_counts, labels=gender_counts.index, autopct="%1.1f%%")
# plt.title("Gender Distribution")
# plt.axis("equal")
# st.pyplot(plt)
# # Create a pie chart for age distribution
# # 18/24, 25/34, 35/49, 50/64, et 65 et plus
# age_bins = [18, 25, 35, 50, 65, 100]
# age_labels = ["18-24", "25-34", "35-49", "50-64", "65+"]
# users_df["age"] = pd.cut(users_df["age"], bins=age_bins, labels=age_labels)
# age_counts = users_df["age"].value_counts()
# plt.figure(figsize=(8, 6))
# plt.pie(age_counts, labels=age_counts.index, autopct="%1.1f%%")
# plt.title("Age Distribution")
# plt.axis("equal")
# st.pyplot(plt)
#
st.write("<center>", unsafe_allow_html=True)
st.download_button(
label="Download generated dataset as a jsonlines",
data=st.session_state.conv_generated_jsonl,
file_name="generated_conversations.jsonl",
mime="application/json",
)
st.write("</center>", unsafe_allow_html=True)