forked from adi-panda/Kuebiko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
151 lines (114 loc) · 5.3 KB
/
main.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
from twitchio.ext import commands
from chat import *
from google.cloud import texttospeech_v1beta1 as texttospeech
import vlc
import os
import time
import nltk
CONVERSATION_LIMIT = 20
class Bot(commands.Bot):
conversation = list()
def __init__(self):
# Initialise our Bot with our access token, prefix and a list of channels to join on boot...
# prefix can be a callable, which returns a list of strings or a string...
# initial_channels can also be a callable which returns a list of strings...
super().__init__(token='', prefix='!', initial_channels=[''])
Bot.conversation.append({ 'role': 'system', 'content': open_file('prompt_chat.txt') })
async def event_ready(self):
# Notify us when everything is ready!
# We are logged in and ready to chat and use commands...
print(f'Logged in as | {self.nick}')
async def event_message(self, message):
# Messages with echo set to True are messages sent by the bot...
# For now we just want to ignore them...
if message.echo:
return
# download the words corpus
nltk.download('words')
# Check if the message contains english words
if not any(word in message.content for word in nltk.corpus.words.words()):
return
# Check if the message is too long
if len(message.content) > 70:
return
print('------------------------------------------------------')
print(message.content)
print(message.author.name)
print(Bot.conversation)
content = message.content.encode(encoding='ASCII',errors='ignore').decode()
Bot.conversation.append({ 'role': 'user', 'content': content })
print(content)
response = gpt3_completion(Bot.conversation)
print('DOGGIEBRO:' , response)
if(Bot.conversation.count({ 'role': 'assistant', 'content': response }) == 0):
Bot.conversation.append({ 'role': 'assistant', 'content': response })
if len(Bot.conversation) > CONVERSATION_LIMIT:
Bot.conversation = Bot.conversation[1:]
client = texttospeech.TextToSpeechClient()
response = message.content + "? " + response
ssml_text = '<speak>'
response_counter = 0
mark_array = []
for s in response.split(' '):
ssml_text += f'<mark name="{response_counter}"/>{s}'
mark_array.append(s)
response_counter += 1
ssml_text += '</speak>'
input_text = texttospeech.SynthesisInput(ssml = ssml_text)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.VoiceSelectionParams(
language_code="en-GB",
name= "en-GB-Wavenet-B",
ssml_gender=texttospeech.SsmlVoiceGender.MALE,
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
)
response = client.synthesize_speech(
request={"input": input_text, "voice": voice, "audio_config": audio_config, "enable_time_pointing": ["SSML_MARK"]}
)
# The response's audio_content is binary.
with open("output.mp3", "wb") as out:
out.write(response.audio_content)
audio_file = os.path.dirname(__file__) + '\output.mp3'
media = vlc.MediaPlayer(audio_file)
media.play()
#playsound(audio_file, winsound.SND_ASYNC)
count = 0
current = 0
for i in range(len(response.timepoints)):
count += 1
current += 1
with open("output.txt", "a", encoding="utf-8") as out:
out.write(mark_array[int(response.timepoints[i].mark_name)] + " ")
if i != len(response.timepoints) - 1:
total_time = response.timepoints[i + 1].time_seconds
time.sleep(total_time - response.timepoints[i].time_seconds)
if current == 25:
open('output.txt', 'w', encoding="utf-8").close()
current = 0
count = 0
elif count % 7 == 0:
with open("output.txt", "a", encoding="utf-8") as out:
out.write("\n")
time.sleep(2)
open('output.txt', 'w').close()
# Print the contents of our message to console...
print('------------------------------------------------------')
os.remove(audio_file)
# Since we have commands and are overriding the default `event_message`
# We must let the bot know we want to handle and invoke our commands...
await self.handle_commands(message)
@commands.command()
async def hello(self, ctx: commands.Context):
# Here we have a command hello, we can invoke our command with our prefix and command name
# e.g ?hello
# We can also give our commands aliases (different names) to invoke with.
# Send a hello back!
# Sending a reply back to the channel is easy... Below is an example.
await ctx.send(f'Hello {ctx.author.name}!')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = ''
bot = Bot()
bot.run()
# bot.run() is blocking and will stop execution of any below code here until stopped or closed.