1
1
import atexit
2
+ import Levenshtein
2
3
import os
3
4
import sys
4
5
@@ -64,13 +65,16 @@ def exit_handler():
64
65
if 'query_method' not in st .session_state :
65
66
st .session_state ['query_method' ] = query
66
67
68
+ if 'search_query' not in st .session_state :
69
+ st .session_state ['search_query' ] = ''
70
+
67
71
# Initialize new conversation
68
72
if 'current_conversation' not in st .session_state or st .session_state ['current_conversation' ] is None :
69
73
st .session_state ['current_conversation' ] = {'user_inputs' : [], 'generated_responses' : []}
70
74
71
75
input_placeholder = st .empty ()
72
76
user_input = input_placeholder .text_input (
73
- 'You:' , value = st .session_state ['input_text' ], key = f'input_text_{ st .session_state ["input_field_key" ]} '
77
+ 'You:' , value = st .session_state ['input_text' ], key = f'input_text_-1' # {st.session_state["input_field_key"]}
74
78
)
75
79
submit_button = st .button ("Submit" )
76
80
@@ -79,7 +83,7 @@ def exit_handler():
79
83
80
84
escaped_output = output .encode ('utf-8' ).decode ('unicode-escape' )
81
85
82
- st .session_state . current_conversation ['user_inputs' ].append (user_input )
86
+ st .session_state [ ' current_conversation' ] ['user_inputs' ].append (user_input )
83
87
st .session_state .current_conversation ['generated_responses' ].append (escaped_output )
84
88
save_conversations (st .session_state .conversations , st .session_state .current_conversation )
85
89
st .session_state ['input_text' ] = ''
@@ -98,20 +102,35 @@ def exit_handler():
98
102
# Proxy
99
103
st .session_state ['proxy' ] = st .sidebar .text_input ("Proxy: " )
100
104
105
+ # Searchbar
106
+ search_query = st .sidebar .text_input ("Search Conversations:" , value = st .session_state .get ('search_query' , '' ), key = 'search' )
107
+
108
+ if search_query :
109
+ filtered_conversations = []
110
+ for conversation in st .session_state .conversations :
111
+ if search_query in conversation ['user_inputs' ][0 ]:
112
+ filtered_conversations .append (conversation )
113
+
114
+ conversations = sorted (filtered_conversations , key = lambda c : Levenshtein .distance (search_query , c ['user_inputs' ][0 ]))
115
+ sidebar_header = f"Search Results ({ len (conversations )} )"
116
+ else :
117
+ conversations = st .session_state .conversations
118
+ sidebar_header = "Conversation History"
119
+
101
120
# Sidebar
102
- st .sidebar .header ("Conversation History" )
121
+ st .sidebar .header (sidebar_header )
103
122
104
- for idx , conversation in enumerate (st . session_state . conversations ):
123
+ for idx , conversation in enumerate (conversations ):
105
124
if st .sidebar .button (f"Conversation { idx + 1 } : { conversation ['user_inputs' ][0 ]} " , key = f"sidebar_btn_{ idx } " ):
106
125
st .session_state ['selected_conversation' ] = idx
107
- st .session_state ['current_conversation' ] = st . session_state . conversations [ idx ]
126
+ st .session_state ['current_conversation' ] = conversation
108
127
109
128
if st .session_state ['selected_conversation' ] is not None :
110
- conversation_to_display = st . session_state . conversations [st .session_state ['selected_conversation' ]]
129
+ conversation_to_display = conversations [st .session_state ['selected_conversation' ]]
111
130
else :
112
131
conversation_to_display = st .session_state .current_conversation
113
132
114
133
if conversation_to_display ['generated_responses' ]:
115
134
for i in range (len (conversation_to_display ['generated_responses' ]) - 1 , - 1 , - 1 ):
116
135
message (conversation_to_display ["generated_responses" ][i ], key = f"display_generated_{ i } " )
117
- message (conversation_to_display ['user_inputs' ][i ], is_user = True , key = f"display_user_{ i } " )
136
+ message (conversation_to_display ['user_inputs' ][i ], is_user = True , key = f"display_user_{ i } " )
0 commit comments