Upload 4 files
Browse filesFixed several major bugs
- Train_WarBot_of_GPT.ipynb +0 -0
- WarClient.py +1 -0
- WarOnline_Chat.py +34 -5
- WarServer.py +2 -1
Train_WarBot_of_GPT.ipynb
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
WarClient.py
CHANGED
|
@@ -11,6 +11,7 @@ def getReply(message):
|
|
| 11 |
message = message.rstrip(".")
|
| 12 |
if message.endswith(" ?"):
|
| 13 |
message = message.replace(" ?","?")
|
|
|
|
| 14 |
|
| 15 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
|
| 16 |
try:
|
|
|
|
| 11 |
message = message.rstrip(".")
|
| 12 |
if message.endswith(" ?"):
|
| 13 |
message = message.replace(" ?","?")
|
| 14 |
+
mesage = message.replace(" .",".")
|
| 15 |
|
| 16 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
|
| 17 |
try:
|
WarOnline_Chat.py
CHANGED
|
@@ -36,9 +36,13 @@ def fixString(S):
|
|
| 36 |
S = S.replace("&", "")
|
| 37 |
S = S.replace("&", "")
|
| 38 |
S = S.replace("ен,ицхак", "ен-ицхак")
|
|
|
|
| 39 |
S = S.replace("(,", "(")
|
| 40 |
S = S.replace("?.", "?")
|
| 41 |
-
S = S.replace("#","")
|
|
|
|
|
|
|
|
|
|
| 42 |
return S
|
| 43 |
|
| 44 |
def compare_pages(url1, url2):
|
|
@@ -47,9 +51,28 @@ def compare_pages(url1, url2):
|
|
| 47 |
|
| 48 |
def remove_non_english_russian_chars(s):
|
| 49 |
# Regular expression to match all characters that are not in English or Russian
|
| 50 |
-
pattern = '[^
|
| 51 |
# Replace all matched characters with an empty string
|
| 52 |
return re.sub(pattern, '', s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def login(username=username, password=password, thread_url=thread_url):
|
| 54 |
# Log-In to the forum and redirect to thread
|
| 55 |
|
|
@@ -191,11 +214,16 @@ def getMessages(thread_url=thread_url, quotedUser="", startingPage=1):
|
|
| 191 |
# Core Engine of the Client
|
| 192 |
def WarOnlineBot():
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
login(username=username, password=password, thread_url=thread_url)
|
| 195 |
#print("logged in")
|
| 196 |
|
| 197 |
# All messages (with quotes) by ALL users:
|
| 198 |
-
allMessages = getMessages(thread_url=thread_url, quotedUser='', startingPage=
|
| 199 |
|
| 200 |
# IDs of the quoted messages, replied by the bot:
|
| 201 |
messages_by_bot_IDs = []
|
|
@@ -208,7 +236,7 @@ def WarOnlineBot():
|
|
| 208 |
messages_by_bot_IDs = list(set([elem for elem in messages_by_bot_IDs if elem]))
|
| 209 |
|
| 210 |
# All messages (with quotes) sent _FOR_ the Bot:
|
| 211 |
-
messagesForBot = getMessages(thread_url=thread_url, quotedUser=username, startingPage=
|
| 212 |
|
| 213 |
# IDs of the messages, quoting the bot:
|
| 214 |
messages_for_bot_IDs = []
|
|
@@ -230,7 +258,8 @@ def WarOnlineBot():
|
|
| 230 |
if originalQuote == "": # Just images, no text
|
| 231 |
continue
|
| 232 |
else:
|
| 233 |
-
quote = remove_non_english_russian_chars(msg['reply'])
|
|
|
|
| 234 |
|
| 235 |
message = ""
|
| 236 |
|
|
|
|
| 36 |
S = S.replace("&", "")
|
| 37 |
S = S.replace("&", "")
|
| 38 |
S = S.replace("ен,ицхак", "ен-ицхак")
|
| 39 |
+
S = S.replace("СШа", "США")
|
| 40 |
S = S.replace("(,", "(")
|
| 41 |
S = S.replace("?.", "?")
|
| 42 |
+
S = S.replace("#", "")
|
| 43 |
+
S = S.replace("()", "")
|
| 44 |
+
S = S.strip(',')
|
| 45 |
+
S = S.strip()
|
| 46 |
return S
|
| 47 |
|
| 48 |
def compare_pages(url1, url2):
|
|
|
|
| 51 |
|
| 52 |
def remove_non_english_russian_chars(s):
|
| 53 |
# Regular expression to match all characters that are not in English or Russian
|
| 54 |
+
pattern = '[^A-Za-zА-Яа-яЁё(),.!?"\s-]'
|
| 55 |
# Replace all matched characters with an empty string
|
| 56 |
return re.sub(pattern, '', s)
|
| 57 |
+
|
| 58 |
+
def remove_extra_spaces(s):
|
| 59 |
+
s = re.sub(r"\s+", " ", s) # replace all sequences of whitespace with a single space
|
| 60 |
+
s = re.sub(r"\s+([.,])", r"\1", s) # remove spaces before period or comma
|
| 61 |
+
return(s)
|
| 62 |
+
|
| 63 |
+
def getLastPage(thread_url=thread_url):
|
| 64 |
+
# Returns the number of the last page
|
| 65 |
+
page = 1 # Starting page
|
| 66 |
+
lastPage = False
|
| 67 |
+
|
| 68 |
+
while not lastPage:
|
| 69 |
+
if not compare_pages(thread_url + 'page-' + str(page), thread_url + 'page-' + str(page + 1)):
|
| 70 |
+
page += 1
|
| 71 |
+
else:
|
| 72 |
+
lastPage = True
|
| 73 |
+
return page
|
| 74 |
+
|
| 75 |
+
|
| 76 |
def login(username=username, password=password, thread_url=thread_url):
|
| 77 |
# Log-In to the forum and redirect to thread
|
| 78 |
|
|
|
|
| 214 |
# Core Engine of the Client
|
| 215 |
def WarOnlineBot():
|
| 216 |
|
| 217 |
+
lookUpPages = 5 # How many pages back to look in the thread
|
| 218 |
+
startingPage = getLastPage(thread_url=thread_url) - lookUpPages
|
| 219 |
+
if startingPage < 1:
|
| 220 |
+
startingPage = 1 # Starting page cannot be less than 1
|
| 221 |
+
|
| 222 |
login(username=username, password=password, thread_url=thread_url)
|
| 223 |
#print("logged in")
|
| 224 |
|
| 225 |
# All messages (with quotes) by ALL users:
|
| 226 |
+
allMessages = getMessages(thread_url=thread_url, quotedUser='', startingPage=startingPage)
|
| 227 |
|
| 228 |
# IDs of the quoted messages, replied by the bot:
|
| 229 |
messages_by_bot_IDs = []
|
|
|
|
| 236 |
messages_by_bot_IDs = list(set([elem for elem in messages_by_bot_IDs if elem]))
|
| 237 |
|
| 238 |
# All messages (with quotes) sent _FOR_ the Bot:
|
| 239 |
+
messagesForBot = getMessages(thread_url=thread_url, quotedUser=username, startingPage=startingPage)
|
| 240 |
|
| 241 |
# IDs of the messages, quoting the bot:
|
| 242 |
messages_for_bot_IDs = []
|
|
|
|
| 258 |
if originalQuote == "": # Just images, no text
|
| 259 |
continue
|
| 260 |
else:
|
| 261 |
+
quote = remove_non_english_russian_chars(msg['reply'])
|
| 262 |
+
quote = remove_extra_spaces(quote)
|
| 263 |
|
| 264 |
message = ""
|
| 265 |
|
WarServer.py
CHANGED
|
@@ -24,7 +24,8 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
|
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
while not response:
|
| 27 |
-
response = WarBot.get_response(received_string, model, tokenizer, model_punct, temperature=0.
|
|
|
|
| 28 |
response_string = response
|
| 29 |
|
| 30 |
conn.sendall(response_string.encode())
|
|
|
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
while not response:
|
| 27 |
+
response = WarBot.get_response(received_string, model, tokenizer, model_punct, temperature=0.6)
|
| 28 |
+
|
| 29 |
response_string = response
|
| 30 |
|
| 31 |
conn.sendall(response_string.encode())
|