-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
93 lines (77 loc) · 2.31 KB
/
Main.py
File metadata and controls
93 lines (77 loc) · 2.31 KB
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
import requests
import json
import pyttsx3
from twitch_chat import TwitchChat
import tts
import dotenv
import os
def TTS(text):
engine = pyttsx3.init("espeak-ng")
engine.say(text)
engine.runAndWait()
def SendToLLM(prompt, user = "Tayyab", chatmode = False):
server = "127.0.0.1"
f = open("BasePrompts.txt", "r")
BasePrompt = f.read()
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
params = {
'max_new_tokens': 65,
'do_sample': True,
'temperature': 0.44,
'top_p': 1,
'typical_p': 1,
'repetition_penalty': 1.15,
'encoder_repetition_penalty': 1.0,
'top_k': 0,
'min_length': 0,
'no_repeat_ngram_size': 0,
'num_beams': 1,
'penalty_alpha': 0,
'length_penalty': 1,
'early_stopping': False,
'seed': -1,
'add_bos_token': True,
'truncation_length': 2048,
'ban_eos_token': False,
'skip_special_tokens': True,
'stopping_strings': [],
}
if chatmode == True:
prompt = user + " in chat: " + prompt
else:
prompt = user + ": " + prompt
payload = json.dumps([BasePrompt+"\n"+ prompt + "<ENDOFTURN>" , params])
response = requests.post(f"http://{server}:7860/run/textgen", json={"data": [payload]}).json()
reply = response["data"][0]
#print(reply)
reply = reply[reply.find("Current chat:") + 13:]
#print(reply)
reply = reply[reply.find("<ENDOFTURN>")+11:]
#print(reply)
return reply[reply.find("Sarah:")+6:reply.find("<ENDOFTURN>")]
def main():
msgqueue = []
model = tts.launch_tts()
dotenv.load_dotenv()
oath = os.getenv('OATH')
channel = os.getenv('CHANNEL')
my_chat = TwitchChat(oath=oath, bot_name='sarah', channel_name=channel)
while True:
user, message = my_chat.listen_to_chat()
msgqueue.append((user, message))
if len(msgqueue) > 1:
user, message = msgqueue.pop(0)
msgqueue = []
if message[:6].lower() == "sarah:":
message = message[6:]
resp = SendToLLM(message, user=user, chatmode=True)
print("\nchat reply:\n"+ resp)
tts.run_tts(resp, model)
tts.playtts()
else:
print(f"Error for message:\n{message}")
if __name__ == "__main__":
main()