forked from Shirros/desktop-pet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
85 lines (66 loc) · 2.08 KB
/
util.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
import openai
import os
import platform
import pyttsx3
import pyttsx4
import subprocess
import random
import threading
from gtts import gTTS
from dotenv import load_dotenv
def normalize(list):
mag = sum(list)
return [v / mag for v in list]
def make_cum(list):
acc = 0
for i in range(len(list)):
temp = list[i]
list[i] = acc
acc += temp
return list
class WeightedRandomMap:
def __init__(self, list):
self.names = [obj["name"] for obj in list]
self.P = make_cum(normalize([obj["probability"] for obj in list]))
assert len(self.names) == len(self.P)
def get_rand(self):
val = random.random()
for i, p in enumerate(self.P):
if p > val:
return self.names[i - 1]
return self.names[-1]
def openai_query(message):
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
try:
response = openai.Completion.create(model="text-davinci-003", prompt=message, temperature=.95, max_tokens=2500)
return response["choices"][0]["text"]
except Exception:
message = "Open AI is not reachable nyaa! You might need to store your API key as an environment variable nyaa."
return message
def speak(message, callback):
if platform.system() == "Windows":
engine = pyttsx3.init()
engine.setProperty("rate", 175)
engine.say(message)
def f():
engine.runAndWait()
callback()
threading.Thread(target=f).start()
elif platform.system() == "Darwin":
tts = gTTS(text=message)
output_filename = "output.mp3"
tts.save(output_filename)
def f():
subprocess.run(["afplay", "-r", "1", output_filename])
os.remove(output_filename)
callback()
threading.Thread(target=f).start()
elif platform.system() == "Linux":
engine = pyttsx4.init()
engine.setProperty("rate", 175)
engine.say(message)
def f():
engine.runAndWait()
callback()
threading.Thread(target=f).start()