File size: 2,032 Bytes
608edc4 |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# GUI with Client
import gradio as gr
import socket
HOST = '129.159.146.88'
PORT = 5000
dialog_history = []
MAX_WORDS = 100
def truncate_string(text):
words = text.split()
if len(words) > MAX_WORDS:
words = words[-MAX_WORDS:]
return ' '.join(words)
def sendToServer(message):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
# This mechanism is not working smoothly, meanwhile.
# Take last 4 messages (history of conversation)
dialog_history.append(message)
message = " ".join(dialog_history[-4:])
message = truncate_string(message)
client_socket.connect((HOST, PORT))
client_socket.sendall(message.encode())
try:
data = client_socket.recv(1024)
reply = data.decode('utf-8')
dialog_history.append(reply)
return reply
# print(f'Received string from server: {received_string}')
except: # sometimes there is a problem with the decoding
return "Бот устал и должен отдохнуть"
# print('decoding error, please try again')
finally:
client_socket.close()
def clear_textbox():
return ""
with gr.Blocks() as WarBot:
gr.Markdown(
"""
# Боевой Чат-Бот портала WarOnline
Пока-что это бредогенератор, тренированый на диалогах форума,<br>
Есть куча багов. Но мы работаем над улучшениями! :)
""")
with gr.Row():
input = gr.Textbox(lines=5, placeholder="Введите сообщение...", label="Вопрос:")
output = gr.Textbox(label="Ответ:", lines=5)
send_btn = gr.Button("Послать Сообщение")
send_btn.click(fn=sendToServer, inputs=input, outputs=output)
clr_btn = gr.Button("Очистить")
clr_btn.click(fn=clear_textbox, outputs=input)
WarBot.launch()
|