diff options
author | t.me/xtekky <98614666+xtekky@users.noreply.github.com> | 2023-04-30 00:48:54 +0200 |
---|---|---|
committer | t.me/xtekky <98614666+xtekky@users.noreply.github.com> | 2023-04-30 00:48:54 +0200 |
commit | 9489dda310af9d91a75390ae4c9388a25b45534a (patch) | |
tree | cd3c53b7fb27cb7b55f94fa5a6dfe3c0cf06dbd5 /gui | |
parent | _ (diff) | |
parent | Merge pull request #282 from DanielShemesh/patch-5 (diff) | |
download | gpt4free-9489dda310af9d91a75390ae4c9388a25b45534a.tar gpt4free-9489dda310af9d91a75390ae4c9388a25b45534a.tar.gz gpt4free-9489dda310af9d91a75390ae4c9388a25b45534a.tar.bz2 gpt4free-9489dda310af9d91a75390ae4c9388a25b45534a.tar.lz gpt4free-9489dda310af9d91a75390ae4c9388a25b45534a.tar.xz gpt4free-9489dda310af9d91a75390ae4c9388a25b45534a.tar.zst gpt4free-9489dda310af9d91a75390ae4c9388a25b45534a.zip |
Diffstat (limited to 'gui')
-rw-r--r-- | gui/query_methods.py | 62 | ||||
-rw-r--r-- | gui/streamlit_app.py | 2 | ||||
-rw-r--r-- | gui/streamlit_chat_app.py | 33 |
3 files changed, 44 insertions, 53 deletions
diff --git a/gui/query_methods.py b/gui/query_methods.py index 1a4a3402..6225453b 100644 --- a/gui/query_methods.py +++ b/gui/query_methods.py @@ -3,11 +3,10 @@ import sys sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) -import forefront, quora, theb, you +from gpt4free import quora, forefront, theb, you import random - def query_forefront(question: str) -> str: # create an account token = forefront.Account.create(logging=False) @@ -15,65 +14,59 @@ def query_forefront(question: str) -> str: response = "" # get a response try: - for i in forefront.StreamingCompletion.create(token = token, prompt = 'hello world', model='gpt-4'): - response += i.completion.choices[0].text - - return response - + return forefront.Completion.create(token=token, prompt='hello world', model='gpt-4').text 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.' + return ( + f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.' + ) def query_quora(question: str) -> str: token = quora.Account.create(logging=False, enable_bot_creation=True) - response = quora.Completion.create( - model='gpt-4', - prompt=question, - token=token - ) - - return response.completion.choices[0].tex + return quora.Completion.create(model='gpt-4', prompt=question, token=token).text 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 - + return ''.join(theb.Completion.create(prompt=question)) + 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.' + return ( + f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.' + ) def query_you(question: str) -> str: # Set cloudflare clearance cookie and get answer from GPT-4 model try: - result = you.Completion.create( - prompt = question) + result = you.Completion.create(prompt=question) 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.' + return ( + f'An error occurred: {e}. Please make sure you are using a valid cloudflare clearance token and user agent.' + ) + # Define a dictionary containing all query methods avail_query_methods = { - "Forefront": query_forefront, - "Poe": query_quora, - "Theb": query_theb, - "You": query_you, - # "Writesonic": query_writesonic, - # "T3nsor": query_t3nsor, - # "Phind": query_phind, - # "Ora": query_ora, - } + "Forefront": query_forefront, + "Poe": query_quora, + "Theb": query_theb, + "You": query_you, + # "Writesonic": query_writesonic, + # "T3nsor": query_t3nsor, + # "Phind": query_phind, + # "Ora": query_ora, +} -def query(user_input: str, selected_method: str = "Random") -> str: +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: @@ -104,4 +97,3 @@ def query(user_input: str, selected_method: str = "Random") -> str: query_methods_list.remove(chosen_query) return result - diff --git a/gui/streamlit_app.py b/gui/streamlit_app.py index d1975bbd..2dba0a7b 100644 --- a/gui/streamlit_app.py +++ b/gui/streamlit_app.py @@ -4,7 +4,7 @@ import sys sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) import streamlit as st -from openai_rev import you +from gpt4free import you def get_answer(question: str) -> str: diff --git a/gui/streamlit_chat_app.py b/gui/streamlit_chat_app.py index dce8ef29..68011229 100644 --- a/gui/streamlit_chat_app.py +++ b/gui/streamlit_chat_app.py @@ -1,6 +1,6 @@ +import atexit import os import sys -import atexit sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) @@ -9,9 +9,9 @@ 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: @@ -31,11 +31,11 @@ def save_conversations(conversations, current_conversation): break if not updated: conversations.append(current_conversation) - + 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) @@ -44,10 +44,10 @@ def exit_handler(): # 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") if 'conversations' not in st.session_state: @@ -61,7 +61,7 @@ if 'selected_conversation' not in st.session_state: 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 @@ -69,19 +69,22 @@ if 'query_method' not in st.session_state: 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"])}') +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']) - + escaped_output = output.encode('utf-8').decode('unicode-escape') + st.session_state.current_conversation['user_inputs'].append(user_input) - st.session_state.current_conversation['generated_responses'].append(output) + st.session_state.current_conversation['generated_responses'].append(escaped_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 - + 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"): @@ -89,11 +92,7 @@ if st.sidebar.button("New Conversation"): 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, - index=0 -) +st.session_state['query_method'] = st.sidebar.selectbox("Select API:", options=avail_query_methods, index=0) # Sidebar st.sidebar.header("Conversation History") |