File size: 1,865 Bytes
6e1eccf
ddf7ac7
 
 
8f30cba
17e09e4
 
 
8f30cba
 
 
 
ddf7ac7
17e09e4
ddf7ac7
 
 
8f30cba
ddf7ac7
 
 
 
 
 
 
 
 
8f30cba
 
ddf7ac7
 
6e1eccf
8f30cba
6e1eccf
8f30cba
 
 
 
 
 
 
 
 
4c41d5e
ddf7ac7
 
 
6e1eccf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# WarServer - replace HOST with 'localhost' for local testing
import socket
import WarBot

# Kill all warnings
import warnings
warnings.filterwarnings("ignore")

imgWord = 'как выглядит'

# Initialize the base models
model,tokenizer,model_punct,translation_model,translation_tokenizer,imgModel_version = WarBot.initialize()

HOST = '10.0.0.125'
PORT = 5000

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
    # Server sockets operation
    server_socket.bind((HOST, PORT))
    server_socket.listen()
    print(f'Server is listening on port {PORT}')
    while True:
        conn, addr = server_socket.accept()
        with conn:
            print(f'Connected by {addr}')
            data = conn.recv(1024)
            received_string = data.decode()
            #received_string = data.decode('utf-8')

            print(f'Received string from client: {received_string}')

            response = ""

            while not response:
                if received_string.lower().startswith(imgWord): # check if that is a call for an image:
                    received_string = received_string.lower().split(imgWord)[1:][0].strip()  # cut the code word for image
                    # Translate it to english:
                    translated_string = WarBot.translate(received_string, translation_model=translation_model,
                                                         translation_tokenizer=translation_tokenizer)
                    # Generated image url
                    response = WarBot.generate_image(prompt=translated_string, imgModel_version=imgModel_version)
                else:
                    response = WarBot.get_response(received_string, model, tokenizer, model_punct, temperature=0.6)

            response_string = response

            conn.sendall(response_string.encode())
            conn.close()