diff options
Diffstat (limited to 'gui')
-rw-r--r-- | gui/query_methods.py | 29 | ||||
-rw-r--r-- | gui/streamlit_chat_app.py | 24 |
2 files changed, 30 insertions, 23 deletions
diff --git a/gui/query_methods.py b/gui/query_methods.py index 6225453b..2d6adacd 100644 --- a/gui/query_methods.py +++ b/gui/query_methods.py @@ -1,5 +1,6 @@ import os import sys +from typing import Optional sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) @@ -7,14 +8,14 @@ from gpt4free import quora, forefront, theb, you import random -def query_forefront(question: str) -> str: +def query_forefront(question: str, proxy: Optional[str] = None) -> str: # create an account - token = forefront.Account.create(logging=False) + token = forefront.Account.create(logging=False, proxy=proxy) response = "" # get a response try: - return forefront.Completion.create(token=token, prompt='hello world', model='gpt-4').text + return forefront.Completion.create(token=token, prompt='hello world', model='gpt-4', proxy=proxy).text except Exception as e: # Return error message if an exception occurs return ( @@ -22,16 +23,16 @@ def query_forefront(question: str) -> str: ) -def query_quora(question: str) -> str: - token = quora.Account.create(logging=False, enable_bot_creation=True) - return quora.Completion.create(model='gpt-4', prompt=question, token=token).text +def query_quora(question: str, proxy: Optional[str] = None) -> str: + token = quora.Account.create(logging=False, enable_bot_creation=True, proxy=proxy) + return quora.Completion.create(model='gpt-4', prompt=question, token=token, proxy=proxy).text -def query_theb(question: str) -> str: +def query_theb(question: str, proxy: Optional[str] = None) -> str: # Set cloudflare clearance cookie and get answer from GPT-4 model response = "" try: - return ''.join(theb.Completion.create(prompt=question)) + return ''.join(theb.Completion.create(prompt=question, proxy=proxy)) except Exception as e: # Return error message if an exception occurs @@ -40,11 +41,11 @@ def query_theb(question: str) -> str: ) -def query_you(question: str) -> str: +def query_you(question: str, proxy: Optional[str] = None) -> str: # Set cloudflare clearance cookie and get answer from GPT-4 model try: - result = you.Completion.create(prompt=question) - return result["response"] + result = you.Completion.create(prompt=question, proxy=proxy) + return result.text except Exception as e: # Return error message if an exception occurs @@ -66,11 +67,11 @@ avail_query_methods = { } -def query(user_input: str, selected_method: str = "Random") -> str: +def query(user_input: str, selected_method: str = "Random", proxy: Optional[str] = None) -> 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: - return avail_query_methods[selected_method](user_input) + return avail_query_methods[selected_method](user_input, proxy=proxy) except Exception as e: print(f"Error with {selected_method}: {e}") return "😵 Sorry, some error occurred please try again." @@ -89,7 +90,7 @@ def query(user_input: str, selected_method: str = "Random") -> str: chosen_query_name = [k for k, v in avail_query_methods.items() if v == chosen_query][0] try: # Try to call the chosen method with the user input - result = chosen_query(user_input) + result = chosen_query(user_input, proxy=proxy) success = True except Exception as e: print(f"Error with {chosen_query_name}: {e}") diff --git a/gui/streamlit_chat_app.py b/gui/streamlit_chat_app.py index 68011229..fc5c8d8e 100644 --- a/gui/streamlit_chat_app.py +++ b/gui/streamlit_chat_app.py @@ -24,9 +24,9 @@ def load_conversations(): def save_conversations(conversations, current_conversation): updated = False - for i, conversation in enumerate(conversations): + for idx, conversation in enumerate(conversations): if conversation == current_conversation: - conversations[i] = current_conversation + conversations[idx] = current_conversation updated = True break if not updated: @@ -71,19 +71,22 @@ if 'current_conversation' not in st.session_state or st.session_state['current_c input_placeholder = st.empty() user_input = input_placeholder.text_input( - 'You:', key=f'input_text_{len(st.session_state["current_conversation"]["user_inputs"])}' + 'You:', value=st.session_state['input_text'], key=f'input_text_{st.session_state["input_field_key"]}' ) submit_button = st.button("Submit") -if user_input or submit_button: + +if (user_input and user_input != st.session_state['input_text']) 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(escaped_output) save_conversations(st.session_state.conversations, st.session_state.current_conversation) + st.session_state['input_text'] = '' user_input = input_placeholder.text_input( - 'You:', value='', key=f'input_text_{len(st.session_state["current_conversation"]["user_inputs"])}' + 'You:', value=st.session_state['input_text'], key=f'input_text_{st.session_state["input_field_key"]}' ) # Clear the input field # Add a button to create a new conversation @@ -94,13 +97,16 @@ if st.sidebar.button("New Conversation"): st.session_state['query_method'] = st.sidebar.selectbox("Select API:", options=avail_query_methods, index=0) +# Proxy +st.session_state['proxy'] = st.sidebar.text_input("Proxy: ") + # 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] +for idx, conversation in enumerate(st.session_state.conversations): + if st.sidebar.button(f"Conversation {idx + 1}: {conversation['user_inputs'][0]}", key=f"sidebar_btn_{idx}"): + st.session_state['selected_conversation'] = idx + st.session_state['current_conversation'] = st.session_state.conversations[idx] if st.session_state['selected_conversation'] is not None: conversation_to_display = st.session_state.conversations[st.session_state['selected_conversation']] |