From 952f7dbee9e702f12dfa0a25f26159a9c8421cee Mon Sep 17 00:00:00 2001 From: noptuno Date: Thu, 27 Apr 2023 19:15:50 -0400 Subject: First implementation of streamlit chat app in gui folder --- gui/streamlit_chat_app.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 gui/streamlit_chat_app.py (limited to 'gui/streamlit_chat_app.py') diff --git a/gui/streamlit_chat_app.py b/gui/streamlit_chat_app.py new file mode 100644 index 00000000..ed3474fd --- /dev/null +++ b/gui/streamlit_chat_app.py @@ -0,0 +1,97 @@ +import os +import sys + +sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) + +import streamlit as st +from streamlit_chat import message +from query_methods import query, avail_query_methods +import pickle + + +conversations_file = "conversations.pkl" + +def load_conversations(): + try: + with open(conversations_file, "rb") as f: + return pickle.load(f) + except FileNotFoundError: + return [] + +def save_conversations(conversations, current_conversation): + updated = False + for i, conversation in enumerate(conversations): + if conversation == current_conversation: + conversations[i] = current_conversation + updated = True + break + if not updated: + conversations.append(current_conversation) + with open(conversations_file, "wb") as f: + pickle.dump(conversations, f) + +st.header("Chat Placeholder") + +if 'conversations' not in st.session_state: + st.session_state['conversations'] = load_conversations() + +if 'input_text' not in st.session_state: + st.session_state['input_text'] = '' + +if 'selected_conversation' not in st.session_state: + st.session_state['selected_conversation'] = None + +if 'input_field_key' not in st.session_state: + st.session_state['input_field_key'] = 0 + +if 'query_method' not in st.session_state: + st.session_state['query_method'] = query + +# Initialize new conversation +if 'current_conversation' not in st.session_state or st.session_state['current_conversation'] is None: + st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []} + + +input_placeholder = st.empty() +user_input = input_placeholder.text_input('You:', key=f'input_text_{len(st.session_state["current_conversation"]["user_inputs"])}') +submit_button = st.button("Submit") + +if user_input or submit_button: + output = query(user_input, st.session_state['query_method']) + + st.session_state.current_conversation['user_inputs'].append(user_input) + st.session_state.current_conversation['generated_responses'].append(output) + save_conversations(st.session_state.conversations, st.session_state.current_conversation) + user_input = input_placeholder.text_input('You:', value='', key=f'input_text_{len(st.session_state["current_conversation"]["user_inputs"])}') # Clear the input field + + +# Add a button to create a new conversation +if st.sidebar.button("New Conversation"): + st.session_state['selected_conversation'] = None + st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []} + st.session_state['input_field_key'] += 1 + + +st.session_state['query_method'] = st.sidebar.selectbox( + "Select API:", + options=avail_query_methods.keys(), + index=0 +) + +# Sidebar +st.sidebar.header("Conversation History") + +for i, conversation in enumerate(st.session_state.conversations): + if st.sidebar.button(f"Conversation {i + 1}: {conversation['user_inputs'][0]}", key=f"sidebar_btn_{i}"): + st.session_state['selected_conversation'] = i + st.session_state['current_conversation'] = st.session_state.conversations[i] + +if st.session_state['selected_conversation'] is not None: + conversation_to_display = st.session_state.conversations[st.session_state['selected_conversation']] +else: + conversation_to_display = st.session_state.current_conversation + +if conversation_to_display['generated_responses']: + for i in range(len(conversation_to_display['generated_responses']) - 1, -1, -1): + message(conversation_to_display["generated_responses"][i], key=f"display_generated_{i}") + message(conversation_to_display['user_inputs'][i], is_user=True, key=f"display_user_{i}") -- cgit v1.2.3 From 355dee533bb34a571b9367820a63cccb668cf866 Mon Sep 17 00:00:00 2001 From: noptuno Date: Thu, 27 Apr 2023 20:29:30 -0400 Subject: Merging PR_218 openai_rev package with new streamlit chat app --- gui/streamlit_chat_app.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 gui/streamlit_chat_app.py (limited to 'gui/streamlit_chat_app.py') diff --git a/gui/streamlit_chat_app.py b/gui/streamlit_chat_app.py new file mode 100644 index 00000000..f64d43f0 --- /dev/null +++ b/gui/streamlit_chat_app.py @@ -0,0 +1,96 @@ +import os +import sys + +sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) + +import streamlit as st +from streamlit_chat import message +from query_methods import query, avail_query_methods +import pickle +import openai_rev + +conversations_file = "conversations.pkl" + +def load_conversations(): + try: + with open(conversations_file, "rb") as f: + return pickle.load(f) + except FileNotFoundError: + return [] + +def save_conversations(conversations, current_conversation): + updated = False + for i, conversation in enumerate(conversations): + if conversation == current_conversation: + conversations[i] = current_conversation + updated = True + break + if not updated: + conversations.append(current_conversation) + with open(conversations_file, "wb") as f: + pickle.dump(conversations, f) + +st.header("Chat Placeholder") + +if 'conversations' not in st.session_state: + st.session_state['conversations'] = load_conversations() + +if 'input_text' not in st.session_state: + st.session_state['input_text'] = '' + +if 'selected_conversation' not in st.session_state: + st.session_state['selected_conversation'] = None + +if 'input_field_key' not in st.session_state: + st.session_state['input_field_key'] = 0 + +if 'query_method' not in st.session_state: + st.session_state['query_method'] = query + +# Initialize new conversation +if 'current_conversation' not in st.session_state or st.session_state['current_conversation'] is None: + st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []} + + +input_placeholder = st.empty() +user_input = input_placeholder.text_input('You:', key=f'input_text_{len(st.session_state["current_conversation"]["user_inputs"])}') +submit_button = st.button("Submit") + +if user_input or submit_button: + output = query(user_input, st.session_state['query_method']) + + st.session_state.current_conversation['user_inputs'].append(user_input) + st.session_state.current_conversation['generated_responses'].append(output) + save_conversations(st.session_state.conversations, st.session_state.current_conversation) + user_input = input_placeholder.text_input('You:', value='', key=f'input_text_{len(st.session_state["current_conversation"]["user_inputs"])}') # Clear the input field + + +# Add a button to create a new conversation +if st.sidebar.button("New Conversation"): + st.session_state['selected_conversation'] = None + st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []} + st.session_state['input_field_key'] += 1 + +st.session_state['query_method'] = st.sidebar.selectbox( + "Select API:", + options=openai_rev.Provider.__members__.keys(), + index=0 +) + +# Sidebar +st.sidebar.header("Conversation History") + +for i, conversation in enumerate(st.session_state.conversations): + if st.sidebar.button(f"Conversation {i + 1}: {conversation['user_inputs'][0]}", key=f"sidebar_btn_{i}"): + st.session_state['selected_conversation'] = i + st.session_state['current_conversation'] = st.session_state.conversations[i] + +if st.session_state['selected_conversation'] is not None: + conversation_to_display = st.session_state.conversations[st.session_state['selected_conversation']] +else: + conversation_to_display = st.session_state.current_conversation + +if conversation_to_display['generated_responses']: + for i in range(len(conversation_to_display['generated_responses']) - 1, -1, -1): + message(conversation_to_display["generated_responses"][i], key=f"display_generated_{i}") + message(conversation_to_display['user_inputs'][i], is_user=True, key=f"display_user_{i}") -- cgit v1.2.3 From f34f561617c5e6c9c875afb46d420a839c97084b Mon Sep 17 00:00:00 2001 From: noptuno Date: Thu, 27 Apr 2023 20:38:18 -0400 Subject: Resolved merge conflicts and merged pr_218 into STREAMLIT_CHAT_IMPLEMENTATION --- gui/streamlit_chat_app.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'gui/streamlit_chat_app.py') diff --git a/gui/streamlit_chat_app.py b/gui/streamlit_chat_app.py index ed3474fd..3ced571a 100644 --- a/gui/streamlit_chat_app.py +++ b/gui/streamlit_chat_app.py @@ -7,7 +7,7 @@ import streamlit as st from streamlit_chat import message from query_methods import query, avail_query_methods import pickle - +import openai_rev conversations_file = "conversations.pkl" @@ -71,9 +71,10 @@ if st.sidebar.button("New Conversation"): st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []} st.session_state['input_field_key'] += 1 - +print(openai_rev.Provider.__methods__.keys()) st.session_state['query_method'] = st.sidebar.selectbox( "Select API:", + options=avail_query_methods.keys(), index=0 ) -- cgit v1.2.3 From 104e58a3421665fc3acfd7fea8e1317fe1181c6d Mon Sep 17 00:00:00 2001 From: noptuno Date: Thu, 27 Apr 2023 20:48:00 -0400 Subject: merged pr #218 with the new streamlit --- gui/streamlit_chat_app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gui/streamlit_chat_app.py') diff --git a/gui/streamlit_chat_app.py b/gui/streamlit_chat_app.py index 09d53bcf..aee3563e 100644 --- a/gui/streamlit_chat_app.py +++ b/gui/streamlit_chat_app.py @@ -5,10 +5,11 @@ sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) import streamlit as st from streamlit_chat import message -from query_methods import query, avail_query_methods +from query_methods import query import pickle import openai_rev + conversations_file = "conversations.pkl" def load_conversations(): @@ -71,7 +72,6 @@ if st.sidebar.button("New Conversation"): st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []} st.session_state['input_field_key'] += 1 -print(openai_rev.Provider.__methods__.keys()) st.session_state['query_method'] = st.sidebar.selectbox( "Select API:", options=openai_rev.Provider.__members__.keys(), -- cgit v1.2.3