kertser commited on
Commit
608edc4
1 Parent(s): dd2dd36

Upload main.py

Browse files

GUI with Client. Added history...

Files changed (1) hide show
  1. main.py +64 -0
main.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GUI with Client
2
+ import gradio as gr
3
+ import socket
4
+
5
+ HOST = '129.159.146.88'
6
+ PORT = 5000
7
+
8
+ dialog_history = []
9
+ MAX_WORDS = 100
10
+
11
+
12
+ def truncate_string(text):
13
+ words = text.split()
14
+ if len(words) > MAX_WORDS:
15
+ words = words[-MAX_WORDS:]
16
+ return ' '.join(words)
17
+
18
+ def sendToServer(message):
19
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
20
+ # This mechanism is not working smoothly, meanwhile.
21
+ # Take last 4 messages (history of conversation)
22
+ dialog_history.append(message)
23
+ message = " ".join(dialog_history[-4:])
24
+ message = truncate_string(message)
25
+
26
+ client_socket.connect((HOST, PORT))
27
+ client_socket.sendall(message.encode())
28
+
29
+ try:
30
+ data = client_socket.recv(1024)
31
+
32
+ reply = data.decode('utf-8')
33
+ dialog_history.append(reply)
34
+ return reply
35
+ # print(f'Received string from server: {received_string}')
36
+ except: # sometimes there is a problem with the decoding
37
+ return "Бот устал и должен отдохнуть"
38
+ # print('decoding error, please try again')
39
+ finally:
40
+ client_socket.close()
41
+
42
+
43
+ def clear_textbox():
44
+ return ""
45
+
46
+
47
+ with gr.Blocks() as WarBot:
48
+ gr.Markdown(
49
+ """
50
+ # Боевой Чат-Бот портала WarOnline
51
+ Пока-что это бредогенератор, тренированый на диалогах форума,<br>
52
+ Есть куча багов. Но мы работаем над улучшениями! :)
53
+ """)
54
+
55
+ with gr.Row():
56
+ input = gr.Textbox(lines=5, placeholder="Введите сообщение...", label="Вопрос:")
57
+ output = gr.Textbox(label="Ответ:", lines=5)
58
+
59
+ send_btn = gr.Button("Послать Сообщение")
60
+ send_btn.click(fn=sendToServer, inputs=input, outputs=output)
61
+ clr_btn = gr.Button("Очистить")
62
+ clr_btn.click(fn=clear_textbox, outputs=input)
63
+
64
+ WarBot.launch()