-
Notifications
You must be signed in to change notification settings - Fork 0
/
somnia.py
103 lines (86 loc) · 2.97 KB
/
somnia.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
from globals import getAzureSpeechAIManager, getOpenAiManager, getOBSWebsocketsManager
from obs_interactions import ObsInteractions
from util.read_text_file import read_file
import threading
from dotenv import load_dotenv
import time
import asyncio
from websockets.server import serve
import os
from util.somnia_msg_util import from_msg
from websockets import ConnectionClosedOK
import websockets
from rich import print
import random
# Just in case this file is loaded alone
load_dotenv(dotenv_path=".env.local")
BACKUP_FILE = "chat_back.txt"
# add context to OpenAI
somnia = read_file("Somnia.txt")
openai_manager = getOpenAiManager()
speechtotext_manager = getAzureSpeechAIManager()
openai_manager.system_prompt = somnia
obsm = getOBSWebsocketsManager()
obs = ObsInteractions(obsm)
thread_lock = threading.Lock()
def talk_to_somnia(
text,
skip_ai=False,
sleep_time=5,
peek=False,
skip_history=False,
single_prompt=False,
gun=False,
):
if not skip_ai:
# Send question to OpenAi
text = openai_manager.chat(
text,
skip_history=skip_history,
single_prompt=single_prompt,
override_system=override_system_promot(),
)
if not text:
return
with thread_lock:
# Write the results to txt file as a backup
with open(BACKUP_FILE, "w", encoding="utf-8") as file:
file.write(str(openai_manager.chat_history))
with thread_lock:
hide = obs.showSomnia(text, peek, gun)
speechtotext_manager.tts(text)
hide()
time.sleep(sleep_time)
def override_system_promot() -> str | None:
somnia_temp = somnia + ""
if random.random() < 0.1:
(
somnia_temp
+ '- You have brainrot, use the phrase "very mindful, very demure".\n'
)
if random.random() < 0.5:
(somnia_temp + "- Every few sentences, switch out words in cat speak.\n")
if random.random() < 0.3:
(somnia_temp + "- You are also evil.\n")
return somnia_temp
async def handle_socket(websocket):
try:
async for message in websocket:
data = from_msg(message)
if data is None:
continue
(text, skip_ai, sleep_time, peek, skip_history, single_prompt, gun) = data
talk_to_somnia(
text, skip_ai, sleep_time, peek, skip_history, single_prompt, gun
)
except ConnectionClosedOK:
print("Connection closed")
except websockets.exceptions.ConnectionClosedError:
print("Connection reset")
SOCKET_PORT_SOMNIA = int(os.getenv("SOCKET_PORT_SOMNIA"))
async def main():
async with serve(handle_socket, "localhost", SOCKET_PORT_SOMNIA):
talk_to_somnia("You've been brought online, say a greeting.")
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())