Skip to content

Commit

Permalink
Merge pull request #26 from F33RNI/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
F33RNI authored May 14, 2023
2 parents 9b40fa8 + 228a2e6 commit 5cc62f1
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ chats/
users.json
conversations/
EdgeGPT_cookies.json
data/
28 changes: 19 additions & 9 deletions BardModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, config: dict, messages: dict, users_handler: UsersHandler.Use
self._enabled = False
self._chatbot = None
self._restart_attempts = 0
self._proxy = None

def initialize(self) -> None:
"""
Expand All @@ -51,8 +52,11 @@ def initialize(self) -> None:
# Set proxy
proxy = self.config["bard"]["proxy"]
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
self._proxy = proxy
self._chatbot.session.proxies.update({"http": proxy,
"https": proxy})
else:
self._proxy = None

# Done?
if self._chatbot is not None:
Expand All @@ -67,14 +71,17 @@ def initialize(self) -> None:

def set_proxy(self, proxy: str) -> None:
"""
Sets new proxy
Sets new proxy from ProxyAutomation
self.config["bard"]["proxy"] must be "auto"
:param proxy: https proxy but in format http://IP:PORT
:return:
"""
if not self._enabled or self._chatbot is None:
if self.config["bard"]["proxy"].strip().lower() != "auto":
return
if self.config["bard"]["proxy"].strip().lower() == "auto":
logging.info("Setting proxy {0} for Bard module".format(proxy))

logging.info("Setting proxy {0} for Bard module".format(proxy))
self._proxy = proxy
if self._enabled and self._chatbot is not None:
self._chatbot.session.proxies.update({"http": proxy,
"https": proxy})

Expand Down Expand Up @@ -192,16 +199,19 @@ def restart(self):
Restarts module and saves proxy
:return:
"""
if not self._enabled or self._chatbot is None:
if not self.config["modules"]["bard"]:
return
logging.info("Restarting Bard module")

# Save proxy
proxy = self._chatbot.session.proxies

# Restart
self.exit()
self.initialize()

# Set proxy
self._chatbot.session.proxies = proxy
try:
if self._proxy is not None:
self._chatbot.session.proxies.update({"http": self._proxy,
"https": self._proxy})
except Exception as e:
logging.error("Error setting back proxy to Bard module!", exc_info=e)

19 changes: 17 additions & 2 deletions BotHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""

import asyncio
import datetime
import logging
import queue
import threading
Expand Down Expand Up @@ -582,18 +583,32 @@ async def bot_command_or_message_request(self, request_type: int,

# Check request
if not request_message or len(request_message) <= 0:
await _send_safe(user["user_id"], self.messages["empty_request"], context)
# Module changed
if self.config["modules"]["auto_module"]:
await _send_safe(user["user_id"],
self.messages["empty_request_module_changed"]
.format(self.messages["modules"][request_type]), context)

# Empty request
else:
await _send_safe(user["user_id"], self.messages["empty_request"], context)
return

# Check queue
if self.queue_handler.requests_queue.full():
await _send_safe(user["user_id"], self.messages["queue_overflow"], context)
return

# Create request timestamp (for data collecting)
request_timestamp = ""
if self.config["data_collecting"]["enabled"]:
request_timestamp = datetime.datetime.now().strftime(self.config["data_collecting"]["timestamp_format"])

# Create request
request_response = RequestResponseContainer.RequestResponseContainer(user, update.message.message_id,
request=request_message,
request_type=request_type)
request_type=request_type,
request_timestamp=request_timestamp)

# Add request to the queue
logging.info("Adding new {0} request from {1} ({2}) to the queue".format(request_type,
Expand Down
41 changes: 27 additions & 14 deletions ChatGPTModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, config: dict, messages: dict, users_handler: UsersHandler.Use
self._exit_flag = False
self._processing_flag = False
self._restart_attempts = 0
self._proxy = None

def initialize(self) -> None:
"""
Expand All @@ -55,7 +56,7 @@ def initialize(self) -> None:
return

# Get API type from config
self._api_type = int(self.config["modules"]["chatgpt_api_type"])
self._api_type = int(self.config["chatgpt"]["api_type"])

# Get conversations directory
self._conversations_dir = self.config["files"]["conversations_dir"]
Expand All @@ -77,9 +78,13 @@ def initialize(self) -> None:
logging.info("Initializing ChatGPT module with API type 3")
from revChatGPT.V3 import Chatbot
engine = str(self.config["chatgpt"]["engine"])
proxy = str(self.config["chatgpt"]["proxy"])
if proxy.strip().lower() == "auto":

proxy = self.config["edgegpt"]["proxy"]
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
self._proxy = proxy
else:
proxy = ""
self._proxy = None

if len(engine) > 0:
self._chatbot = Chatbot(str(self.config["chatgpt"]["api_key"]),
Expand All @@ -104,14 +109,17 @@ def initialize(self) -> None:

def set_proxy(self, proxy: str) -> None:
"""
Sets new proxy
Sets new proxy from ProxyAutomation
self.config["chatgpt"]["proxy"] must be "auto"
:param proxy: https proxy but in format http://IP:PORT
:return:
"""
if not self._enabled or self._chatbot is None:
if self.config["chatgpt"]["proxy"].strip().lower() != "auto":
return
if self.config["chatgpt"]["proxy"].strip().lower() == "auto":
logging.info("Setting proxy {0} for ChatGPT module".format(proxy))

logging.info("Setting proxy {0} for ChatGPT module".format(proxy))
self._proxy = proxy
if self._enabled and self._chatbot is not None:
self._chatbot.proxy = proxy

def process_request(self, request_response: RequestResponseContainer) -> None:
Expand Down Expand Up @@ -312,19 +320,20 @@ def restart(self):
Restarts module and saves proxy
:return:
"""
if not self._enabled or self._chatbot is None:
if not self.config["modules"]["chatgpt"]:
return
logging.info("Restarting ChatGPT module")

# Save proxy
proxy = self._chatbot.proxy

# Restart
self.exit()
self.initialize()

# Set proxy
self._chatbot.proxy = proxy
try:
if self._proxy is not None:
self._chatbot.proxy = self._proxy
except Exception as e:
logging.error("Error setting back proxy to ChatGPT module!", exc_info=e)

def _save_conversation(self, conversation_id) -> bool:
"""
Expand Down Expand Up @@ -458,8 +467,12 @@ def _get_chatbot_config(self) -> dict:
raise Exception("Error! No credentials to login!")

# Add proxy
if len(self.config["chatgpt"]["proxy"]) > 0 and self.config["chatgpt"]["proxy"].strip().lower() != "auto":
config["proxy"] = self.config["chatgpt"]["proxy"]
proxy = self.config["edgegpt"]["proxy"]
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
self._proxy = proxy
config["proxy"] = proxy
else:
self._proxy = None

# Paid?
config["paid"] = self.config["chatgpt"]["paid"]
Expand Down
27 changes: 17 additions & 10 deletions DALLEModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self, config: dict, messages: dict, users_handler: UsersHandler.Use

self._enabled = False
self._restart_attempts = 0
self._proxy = None

def initialize(self) -> None:
"""
Expand All @@ -50,7 +51,10 @@ def initialize(self) -> None:
# Proxy for DALL-E
proxy = self.config["dalle"]["proxy"]
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
self._proxy = proxy
openai.proxy = proxy
else:
self._proxy = None

# Done?
logging.info("DALL-E module initialized")
Expand All @@ -62,15 +66,17 @@ def initialize(self) -> None:

def set_proxy(self, proxy: str) -> None:
"""
Sets new proxy
Sets new proxy from ProxyAutomation
self.config["dalle"]["proxy"] must be "auto"
:param proxy: https proxy but in format http://IP:PORT
:return:
"""
if not self._enabled:
if self.config["dalle"]["proxy"].strip().lower() != "auto":
return
if self.config["dalle"]["proxy"].strip().lower() == "auto":
logging.info("Setting proxy {0} for DALL-E module".format(proxy))
openai.proxy = proxy

logging.info("Setting proxy {0} for DALL-E module".format(proxy))
self._proxy = proxy
openai.proxy = proxy

def process_request(self, request_response: RequestResponseContainer) -> None:
"""
Expand Down Expand Up @@ -138,15 +144,16 @@ def restart(self):
Restarts module and saves proxy
:return:
"""
if not self._enabled:
if not self.config["modules"]["dalle"]:
return
logging.info("Restarting DALL-E module")

# Save proxy
proxy = openai.proxy

# Restart
self.initialize()

# Set proxy
openai.proxy = proxy
try:
if self._proxy is not None:
openai.proxy = self._proxy
except Exception as e:
logging.error("Error setting back proxy to DALL-E module!", exc_info=e)
25 changes: 16 additions & 9 deletions EdgeGPTModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self, config: dict, messages: dict, users_handler: UsersHandler.Use
self._enabled = False
self._chatbot = None
self._restart_attempts = 0
self._proxy = None

def initialize(self) -> None:
"""
Expand All @@ -72,8 +73,10 @@ def initialize(self) -> None:
if proxy and len(proxy) > 1 and proxy.strip().lower() != "auto":
async_helper(self._chatbot.create(cookie_path=self.config["edgegpt"]["cookie_file"],
proxy=proxy))
self._proxy = proxy
else:
async_helper(self._chatbot.create(cookie_path=self.config["edgegpt"]["cookie_file"]))
self._proxy = None

# Check
if self._chatbot is not None:
Expand All @@ -86,14 +89,17 @@ def initialize(self) -> None:

def set_proxy(self, proxy: str) -> None:
"""
Sets new proxy
Sets new proxy from ProxyAutomation
self.config["edgegpt"]["proxy"] must be "auto"
:param proxy: https proxy but in format http://IP:PORT
:return:
"""
if not self._enabled or self._chatbot is None:
if self.config["edgegpt"]["proxy"].strip().lower() != "auto":
return
if self.config["edgegpt"]["proxy"].strip().lower() == "auto":
logging.info("Setting proxy {0} for EdgeGPT module".format(proxy))

logging.info("Setting proxy {0} for EdgeGPT module".format(proxy))
self._proxy = proxy
if self._enabled and self._chatbot is not None:
self._chatbot.proxy = proxy

def process_request(self, request_response: RequestResponseContainer) -> None:
Expand Down Expand Up @@ -227,16 +233,17 @@ def restart(self):
Restarts module and saves proxy
:return:
"""
if not self._enabled or self._chatbot is None:
if not self.config["modules"]["edgegpt"]:
return
logging.info("Restarting EdgeGPT module")

# Save proxy
proxy = self._chatbot.proxy

# Restart
self.exit()
self.initialize()

# Set proxy
self._chatbot.proxy = proxy
try:
if self._proxy is not None:
self._chatbot.proxy = self._proxy
except Exception as e:
logging.error("Error setting back proxy to EdgeGPT module!", exc_info=e)
4 changes: 2 additions & 2 deletions ProxyAutomation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def proxy_tester_process(test_proxy_queue: multiprocessing.Queue,
response = session.get(check_url, timeout=timeout)

# Check result
if len(str(response.headers)) > 1:
if len(str(response.headers)) > 1 and response.status_code == 200:
# Put working proxy to the queue
working_proxy_queue.put(proxy_to_test, block=True, timeout=1)

Expand Down Expand Up @@ -248,7 +248,7 @@ def _automation_loop(self) -> None:
try:
response = session.get(self.config["proxy_automation"]["check_url"],
timeout=self.config["proxy_automation"]["check_timeout_seconds"])
is_proxy_working = len(str(response.headers)) > 1
is_proxy_working = len(str(response.headers)) > 1 and response.status_code == 200
except Exception as e:
logging.error("Error checking proxy: {0}".format(str(e)))
session.close()
Expand Down
Loading

0 comments on commit 5cc62f1

Please sign in to comment.