mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-11-05 00:01:00 +00:00
updated query methods in streamlit
This commit is contained in:
parent
54b4c789a7
commit
afcaf7b70c
@ -1,8 +1,12 @@
|
|||||||
from re import findall
|
|
||||||
from json import loads
|
from json import loads
|
||||||
from queue import Queue, Empty
|
from queue import Queue, Empty
|
||||||
|
from re import findall
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
from curl_cffi import requests
|
from curl_cffi import requests
|
||||||
|
from fake_useragent import UserAgent
|
||||||
|
|
||||||
|
|
||||||
class Completion:
|
class Completion:
|
||||||
# experimental
|
# experimental
|
||||||
@ -14,29 +18,29 @@ class Completion:
|
|||||||
message_queue = Queue()
|
message_queue = Queue()
|
||||||
stream_completed = False
|
stream_completed = False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def request(prompt: str):
|
def request(prompt: str):
|
||||||
headers = {
|
headers = {
|
||||||
'authority': 'chatbot.theb.ai',
|
'authority': 'chatbot.theb.ai',
|
||||||
'content-type': 'application/json',
|
'content-type': 'application/json',
|
||||||
'origin': 'https://chatbot.theb.ai',
|
'origin': 'https://chatbot.theb.ai',
|
||||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
|
'user-agent': UserAgent().random,
|
||||||
}
|
}
|
||||||
|
|
||||||
requests.post('https://chatbot.theb.ai/api/chat-process', headers=headers,
|
requests.post(
|
||||||
content_callback = Completion.handle_stream_response,
|
'https://chatbot.theb.ai/api/chat-process',
|
||||||
json = {
|
headers=headers,
|
||||||
'prompt': prompt,
|
content_callback=Completion.handle_stream_response,
|
||||||
'options': {}
|
json={'prompt': prompt, 'options': {}},
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
Completion.stream_completed = True
|
Completion.stream_completed = True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(prompt: str):
|
def create(prompt: str) -> Generator[str, None, None]:
|
||||||
Thread(target=Completion.request, args=[prompt]).start()
|
Thread(target=Completion.request, args=[prompt]).start()
|
||||||
|
|
||||||
while Completion.stream_completed != True or not Completion.message_queue.empty():
|
while not Completion.stream_completed or not Completion.message_queue.empty():
|
||||||
try:
|
try:
|
||||||
message = Completion.message_queue.get(timeout=0.01)
|
message = Completion.message_queue.get(timeout=0.01)
|
||||||
for message in findall(Completion.regex, message):
|
for message in findall(Completion.regex, message):
|
||||||
|
@ -7,7 +7,6 @@ from gpt4free import quora, forefront, theb, you
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def query_forefront(question: str) -> str:
|
def query_forefront(question: str) -> str:
|
||||||
# create an account
|
# create an account
|
||||||
token = forefront.Account.create(logging=False)
|
token = forefront.Account.create(logging=False)
|
||||||
@ -15,50 +14,44 @@ def query_forefront(question: str) -> str:
|
|||||||
response = ""
|
response = ""
|
||||||
# get a response
|
# get a response
|
||||||
try:
|
try:
|
||||||
for i in forefront.StreamingCompletion.create(token = token, prompt ='hello world', model='gpt-4'):
|
return forefront.Completion.create(token=token, prompt='hello world', model='gpt-4').text
|
||||||
response += i.completion.choices[0].text
|
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Return error message if an exception occurs
|
# 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:
|
def query_quora(question: str) -> str:
|
||||||
token = quora.Account.create(logging=False, enable_bot_creation=True)
|
token = quora.Account.create(logging=False, enable_bot_creation=True)
|
||||||
response = quora.Completion.create(
|
return quora.Completion.create(model='gpt-4', prompt=question, token=token).text
|
||||||
model='gpt-4',
|
|
||||||
prompt=question,
|
|
||||||
token=token
|
|
||||||
)
|
|
||||||
|
|
||||||
return response.completion.choices[0].tex
|
|
||||||
|
|
||||||
|
|
||||||
def query_theb(question: str) -> str:
|
def query_theb(question: str) -> str:
|
||||||
# Set cloudflare clearance cookie and get answer from GPT-4 model
|
# Set cloudflare clearance cookie and get answer from GPT-4 model
|
||||||
response = ""
|
response = ""
|
||||||
try:
|
try:
|
||||||
result = theb.Completion.create(
|
return ''.join(theb.Completion.create(prompt=question))
|
||||||
prompt = question)
|
|
||||||
return result
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Return error message if an exception occurs
|
# 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:
|
def query_you(question: str) -> str:
|
||||||
# Set cloudflare clearance cookie and get answer from GPT-4 model
|
# Set cloudflare clearance cookie and get answer from GPT-4 model
|
||||||
try:
|
try:
|
||||||
result = you.Completion.create(
|
result = you.Completion.create(prompt=question)
|
||||||
prompt = question)
|
|
||||||
return result["response"]
|
return result["response"]
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Return error message if an exception occurs
|
# 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
|
# Define a dictionary containing all query methods
|
||||||
avail_query_methods = {
|
avail_query_methods = {
|
||||||
@ -70,10 +63,10 @@ avail_query_methods = {
|
|||||||
# "T3nsor": query_t3nsor,
|
# "T3nsor": query_t3nsor,
|
||||||
# "Phind": query_phind,
|
# "Phind": query_phind,
|
||||||
# "Ora": query_ora,
|
# "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 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:
|
if selected_method != "Random" and selected_method in avail_query_methods:
|
||||||
try:
|
try:
|
||||||
@ -104,4 +97,3 @@ def query(user_input: str, selected_method: str = "Random") -> str:
|
|||||||
query_methods_list.remove(chosen_query)
|
query_methods_list.remove(chosen_query)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
import atexit
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import atexit
|
|
||||||
|
|
||||||
sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir))
|
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
|
from query_methods import query, avail_query_methods
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
|
|
||||||
conversations_file = "conversations.pkl"
|
conversations_file = "conversations.pkl"
|
||||||
|
|
||||||
|
|
||||||
def load_conversations():
|
def load_conversations():
|
||||||
try:
|
try:
|
||||||
with open(conversations_file, "rb") as f:
|
with open(conversations_file, "rb") as f:
|
||||||
@ -44,10 +44,10 @@ def exit_handler():
|
|||||||
# Perform cleanup operations here, like saving data or closing open files.
|
# Perform cleanup operations here, like saving data or closing open files.
|
||||||
save_conversations(st.session_state.conversations, st.session_state.current_conversation)
|
save_conversations(st.session_state.conversations, st.session_state.current_conversation)
|
||||||
|
|
||||||
|
|
||||||
# Register the exit_handler function to be called when the program is closing.
|
# Register the exit_handler function to be called when the program is closing.
|
||||||
atexit.register(exit_handler)
|
atexit.register(exit_handler)
|
||||||
|
|
||||||
|
|
||||||
st.header("Chat Placeholder")
|
st.header("Chat Placeholder")
|
||||||
|
|
||||||
if 'conversations' not in st.session_state:
|
if 'conversations' not in st.session_state:
|
||||||
@ -69,9 +69,10 @@ if 'query_method' not in st.session_state:
|
|||||||
if 'current_conversation' not in st.session_state or st.session_state['current_conversation'] is None:
|
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': []}
|
st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []}
|
||||||
|
|
||||||
|
|
||||||
input_placeholder = st.empty()
|
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")
|
submit_button = st.button("Submit")
|
||||||
|
|
||||||
if user_input or submit_button:
|
if user_input or submit_button:
|
||||||
@ -81,8 +82,9 @@ if user_input or submit_button:
|
|||||||
st.session_state.current_conversation['user_inputs'].append(user_input)
|
st.session_state.current_conversation['user_inputs'].append(user_input)
|
||||||
st.session_state.current_conversation['generated_responses'].append(escaped_output)
|
st.session_state.current_conversation['generated_responses'].append(escaped_output)
|
||||||
save_conversations(st.session_state.conversations, st.session_state.current_conversation)
|
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
|
# Add a button to create a new conversation
|
||||||
if st.sidebar.button("New Conversation"):
|
if st.sidebar.button("New Conversation"):
|
||||||
@ -90,11 +92,7 @@ if st.sidebar.button("New Conversation"):
|
|||||||
st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []}
|
st.session_state['current_conversation'] = {'user_inputs': [], 'generated_responses': []}
|
||||||
st.session_state['input_field_key'] += 1
|
st.session_state['input_field_key'] += 1
|
||||||
|
|
||||||
st.session_state['query_method'] = st.sidebar.selectbox(
|
st.session_state['query_method'] = st.sidebar.selectbox("Select API:", options=avail_query_methods, index=0)
|
||||||
"Select API:",
|
|
||||||
options=avail_query_methods,
|
|
||||||
index=0
|
|
||||||
)
|
|
||||||
|
|
||||||
# Sidebar
|
# Sidebar
|
||||||
st.sidebar.header("Conversation History")
|
st.sidebar.header("Conversation History")
|
||||||
|
Loading…
Reference in New Issue
Block a user