-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twitch_Chat.py
169 lines (135 loc) · 6.04 KB
/
Twitch_Chat.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#imports
import socket
import logging
from emoji import demojize
from datetime import datetime
import pandas as pd
import re,os,json
from GPTBOT import context,get_completion_from_messages,speak
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
#Logging config
sock = socket.socket()
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s — %(message)s',
datefmt='%Y-%m-%d_%H:%M:%S',
handlers=[logging.FileHandler('chat.log', encoding='utf-8')])
#Subtitle save
def save_subtitle(text,save_name,split_1,split_2):
with open(save_name,"w",encoding="utf-8") as f:
for line in text.split(split_1):
if split_2 in line:
for sub_line in line.split(split_2):
f.write(sub_line+"\n")
else:
f.write(line+" \n")
f.close()
#Server chat to df
def get_chat_dataframe(file):
data = []
with open(file, 'r', encoding='utf-8') as f:
lines = f.read().split('\n\n\n')
for line in lines:
try:
time_logged = line.split('—')[0].strip()
time_logged = datetime.strptime(time_logged, '%Y-%m-%d_%H:%M:%S')
username_message = line.split('—')[1:]
username_message = '—'.join(username_message).strip()
username, channel, message = re.search(
':(.*)\!.*@.*\.tmi\.twitch\.tv PRIVMSG #(.*) :(.*)', username_message
).groups()
d = {
'dt': time_logged,
'channel': channel,
'username': username,
'message': message
}
data.append(d)
except Exception:
pass
return pd.DataFrame().from_records(data)
def AIChatListen(cfg,CONFIG_MEMORY=False,trigger_key="AICHAT",chatname="Reylene",audio=True):
#Connect
sock.connect((cfg["server"],cfg["port"]))
sock.send(f'PASS {cfg["token"]}\n'.encode('utf-8'))
sock.send(f'NICK {cfg["nickname"]}\n'.encode('utf-8'))
sock.send(f'JOIN {cfg["channel"]}\n'.encode('utf-8'))
print(sock)
#Bot up message
sock.send("PRIVMSG #{} :{}\r\n".format(cfg['channel'].replace("#",""),
f"Bot:{chatname} is up and ready to take questions, use {trigger_key} to talk to {chatname}").encode("utf-8"))
sock.send("PRIVMSG #{} :{}\r\n".format(cfg['channel'].replace("#",""),
f"ボット:{trigger_key}が起動し、質問を受け付ける準備が整いましたので、{trigger_key}を使用して{chatname}と会話してください。").encode("utf-8"))
if CONFIG_MEMORY: #Load config json.
context=json.load(open("context.json","r"))
context=list(context)
from GPTBOT import base
context.append({"role":"system", "content":base})
else:
from GPTBOT import context
while True:
resp = sock.recv(2048).decode('utf-8')
#Recive time
print(resp)
user=resp[1:resp.find("!")]
#Find twitch user
i_1=resp.find("#")
i_2=i_1+resp[i_1:].find(" :")
message=resp[i_2+1:]
time=datetime.now()
#AI chat
if trigger_key in message:
#Print user messages and time
print("user",user)
print("message",message)
print(user,message,time)
print("Run GPT")
prompt=f"""
<user>{user}
<message>{message.replace(f"/{trigger_key}","")}
"""
context.append({'role':'user', 'content':f"{prompt}"})
print(len(context))
#save questsion to subfile
save_subtitle(prompt.replace(" ",""),"question_subfile.txt",".",",")
save_subtitle("Thinking","eng_subfile.txt",".",",")
save_subtitle("考え中","jp_subfiles.txt","、","。")
response = get_completion_from_messages(context,temperature=0.2)
response=response.replace("Raylene:","")
print(response)
#English transulation
eng_response=get_completion_from_messages([{"role":"user","content": f"次の文章は英語に翻訳してください。「{response}」"}])
gpt_endtime=datetime.now()
#Twitch message reply with text
print("GPT Complete",gpt_endtime-time)
#sock.send(f"{responses}".encode('utf-8'))
sock.send("PRIVMSG #{} :{}\r\n".format(cfg['channel'].replace("#",""), "JP "+response).encode("utf-8"))
sock.send("PRIVMSG #{} :{}\r\n".format(cfg['channel'].replace("#",""), "Eng "+eng_response).encode("utf-8"))
#Audio loc
#Save responces 2 subs
save_subtitle(eng_response,"eng_subfile.txt",".",",")
save_subtitle(response,"jp_subfiles.txt","、","。")
#AUdio speak
if audio:
print("Speak")
audio_files="./static/audio/"
i=len([f for f in os.listdir(audio_files) if ".wav" in f])
audiofile=audio_files+f"{i+1}"
speak(response.replace(chatname+":",""),audiofile)
speak_endtime=datetime.now()
print("Speach Done",speak_endtime-gpt_endtime)
context.append({'role':'assistant', 'content':f"{response}"})
#Save contect
if CONFIG_MEMORY:
with open("context.json","w" ) as f:
contexts_json=json.dumps(context,indent=2, ensure_ascii=False)
f.write(contexts_json)
if __name__ == "__main__":
cfg={
"server" : os.getenv("server"),
"port" : os.getenv("port"),
"nickname" : os.getenv("port"),
"token" :os.getenv("token"),
"channel": os.getenv("channel"),
}
AIChatListen(cfg,CONFIG_MEMORY=False,audio=False)