-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathrun.py
151 lines (115 loc) · 3.76 KB
/
run.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
import pytchat
import openai
import json
from pytchat import LiveChat, SpeedCalculator
import time
import requests
from pydub import AudioSegment
from pydub.playback import play
import io
import pyttsx3
import sys
import argparse
def initTTS():
global engine
engine = pyttsx3.init()
engine.setProperty('rate', 180)
engine.setProperty('volume', 1)
voice = engine.getProperty('voices')
engine.setProperty('voice', voice[1].id)
def initVar():
global EL_key
global OAI_key
global EL_voice
global video_id
global tts_type
global OAI
global EL
try:
with open("config.json", "r") as json_file:
data = json.load(json_file)
except:
print("Unable to open JSON file.")
exit()
class OAI:
key = data["keys"][0]["OAI_key"]
model = data["OAI_data"][0]["model"]
prompt = data["OAI_data"][0]["prompt"]
temperature = data["OAI_data"][0]["temperature"]
max_tokens = data["OAI_data"][0]["max_tokens"]
top_p = data["OAI_data"][0]["top_p"]
frequency_penalty = data["OAI_data"][0]["frequency_penalty"]
presence_penalty = data["OAI_data"][0]["presence_penalty"]
class EL:
key = data["keys"][0]["EL_key"]
voice = data["EL_data"][0]["voice"]
tts_list = ["pyttsx3", "EL"]
parser = argparse.ArgumentParser()
parser.add_argument("-id", "--video_id", type=str)
parser.add_argument("-tts", "--tts_type", default="pyttsx3", choices=tts_list, type=str)
args = parser.parse_args()
video_id = args.video_id
tts_type = args.tts_type
if tts_type == "pyttsx3":
initTTS()
def Controller_TTS(message):
if tts_type == "EL":
EL_TTS(message)
elif tts_type == "pyttsx3":
pyttsx3_TTS(message)
def pyttsx3_TTS(message):
engine.say(message)
engine.runAndWait()
def EL_TTS(message):
url = f'https://api.elevenlabs.io/v1/text-to-speech/{EL.voice}'
headers = {
'accept': 'audio/mpeg',
'xi-api-key': EL.key,
'Content-Type': 'application/json'
}
data = {
'text': message,
'voice_settings': {
'stability': 0.75,
'similarity_boost': 0.75
}
}
response = requests.post(url, headers=headers, json=data, stream=True)
audio_content = AudioSegment.from_file(io.BytesIO(response.content), format="mp3")
play(audio_content)
def read_chat():
chat = pytchat.create(video_id=video_id)
schat = pytchat.create(video_id=video_id, processor = SpeedCalculator(capacity = 20))
while chat.is_alive():
for c in chat.get().sync_items():
print(f"\n{c.datetime} [{c.author.name}]- {c.message}\n")
message = c.message
response = llm(message)
print(response)
Controller_TTS(response)
if schat.get() >= 20:
chat.terminate()
schat.terminate()
return
time.sleep(1)
def llm(message):
openai.api_key = OAI.key
start_sequence = " #########"
response = openai.Completion.create(
model= OAI.model,
prompt= OAI.prompt + "\n\n#########\n" + message + "\n#########\n",
temperature = OAI.temperature,
max_tokens = OAI.max_tokens,
top_p = OAI.top_p,
frequency_penalty = OAI.frequency_penalty,
presence_penalty = OAI.presence_penalty
)
json_object = json.loads(str(response))
return(json_object['choices'][0]['text'])
if __name__ == "__main__":
initVar()
print("\n\Running!\n\n")
while True:
read_chat()
print("\n\nReset!\n\n")
time.sleep(2)