diff options
Diffstat (limited to '')
-rw-r--r-- | gui/query_methods.py | 33 | ||||
-rw-r--r-- | gui/streamlit_app.py | 3 | ||||
-rw-r--r-- | gui/streamlit_chat_app.py | 25 |
3 files changed, 43 insertions, 18 deletions
diff --git a/gui/query_methods.py b/gui/query_methods.py index 49946900..1a4a3402 100644 --- a/gui/query_methods.py +++ b/gui/query_methods.py @@ -1,17 +1,24 @@ -import openai_rev -from openai_rev import forefront, quora, theb, you +import os +import sys + +sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) + +import forefront, quora, theb, you import random + def query_forefront(question: str) -> str: # create an account - token = forefront.Account.create(logging=True) - + token = forefront.Account.create(logging=False) + + response = "" # get a response try: - result = forefront.StreamingCompletion.create(token = token, prompt = 'hello world', model='gpt-4') + for i in forefront.StreamingCompletion.create(token = token, prompt = 'hello world', model='gpt-4'): + response += i.completion.choices[0].text - return result['response'] + return response except Exception as e: # Return error message if an exception occurs @@ -31,11 +38,11 @@ def query_quora(question: str) -> str: def query_theb(question: str) -> str: # Set cloudflare clearance cookie and get answer from GPT-4 model + response = "" try: result = theb.Completion.create( prompt = question) - - return result['response'] + return result except Exception as e: # Return error message if an exception occurs @@ -47,16 +54,14 @@ def query_you(question: str) -> str: try: result = you.Completion.create( prompt = question) - return result.text + return result["response"] except Exception as e: # Return error message if an exception occurs return f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.' - -def query(user_input: str, selected_method: str = "Random") -> str: - # Define a dictionary containing all query methods - avail_query_methods = { +# Define a dictionary containing all query methods +avail_query_methods = { "Forefront": query_forefront, "Poe": query_quora, "Theb": query_theb, @@ -67,6 +72,8 @@ def query(user_input: str, selected_method: str = "Random") -> str: # "Ora": query_ora, } +def query(user_input: str, selected_method: str = "Random") -> str: + # If a specific query method is selected (not "Random") and the method is in the dictionary, try to call it if selected_method != "Random" and selected_method in avail_query_methods: try: diff --git a/gui/streamlit_app.py b/gui/streamlit_app.py index 2bbf86e0..d1975bbd 100644 --- a/gui/streamlit_app.py +++ b/gui/streamlit_app.py @@ -38,9 +38,10 @@ st.header('GPT4free GUI') question_text_area = st.text_area('🤖 Ask Any Question :', placeholder='Explain quantum computing in 50 words') if st.button('🧠Think'): answer = get_answer(question_text_area) + escaped = answer.encode('utf-8').decode('unicode-escape') # Display answer st.caption("Answer :") - st.markdown(answer) + st.markdown(escaped) # Hide Streamlit footer hide_streamlit_style = """ diff --git a/gui/streamlit_chat_app.py b/gui/streamlit_chat_app.py index aee3563e..dce8ef29 100644 --- a/gui/streamlit_chat_app.py +++ b/gui/streamlit_chat_app.py @@ -1,13 +1,13 @@ import os import sys +import atexit 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 +from query_methods import query, avail_query_methods import pickle -import openai_rev conversations_file = "conversations.pkl" @@ -18,6 +18,9 @@ def load_conversations(): return pickle.load(f) except FileNotFoundError: return [] + except EOFError: + return [] + def save_conversations(conversations, current_conversation): updated = False @@ -28,8 +31,22 @@ def save_conversations(conversations, current_conversation): break if not updated: conversations.append(current_conversation) - with open(conversations_file, "wb") as f: + + temp_conversations_file = "temp_" + conversations_file + with open(temp_conversations_file, "wb") as f: pickle.dump(conversations, f) + + os.replace(temp_conversations_file, conversations_file) + + +def exit_handler(): + print("Exiting, saving data...") + # Perform cleanup operations here, like saving data or closing open files. + save_conversations(st.session_state.conversations, st.session_state.current_conversation) + +# Register the exit_handler function to be called when the program is closing. +atexit.register(exit_handler) + st.header("Chat Placeholder") @@ -74,7 +91,7 @@ if st.sidebar.button("New Conversation"): st.session_state['query_method'] = st.sidebar.selectbox( "Select API:", - options=openai_rev.Provider.__members__.keys(), + options=avail_query_methods, index=0 ) |