-
Notifications
You must be signed in to change notification settings - Fork 0
/
roast.py
237 lines (197 loc) · 7.79 KB
/
roast.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import os
import psutil
import time
import mss
import cv2
import schedule
import base64
import yaml
import openai
import json
import argparse
import sounddevice as sd
import soundfile as sf
from PIL import Image
from pync import Notifier
from twilio.rest import Client
from glados import tts
from pathlib import Path
from datetime import datetime
def rescale_image(image, max_size):
# Rescale the image to have the longest edge equal to max_size
width, height = image.size
if width > height:
new_width = max_size
new_height = int((max_size / width) * height)
else:
new_height = max_size
new_width = int((max_size / height) * width)
return image.resize((new_width, new_height), Image.LANCZOS)
# Function to take screenshots of all screens
def take_screenshots():
with mss.mss() as sct:
monitors = sct.monitors[1:] # Get all monitors excluding the first (virtual monitor)
screenshots = []
for i, monitor in enumerate(monitors):
filename = f'screenshot_{i}.png'
sct_img = sct.grab(monitor)
img = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX')
img = rescale_image(img, config_data['screenshot_rescale'])
img.save(filename)
screenshots.append(filename)
return screenshots
# Function to capture a photo from the webcam
def take_webcam_photo():
cap = cv2.VideoCapture(0)
time.sleep(2)
if not cap.isOpened():
print("Error: Could not open webcam.")
return None
ret, frame = cap.read()
if ret:
filename = 'webcam_photo.png'
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame)
img = rescale_image(img, config_data['webcam_rescale'])
img.save(filename)
cap.release()
return filename
else:
print("Error: Could not read frame from webcam.")
cap.release()
return None
# Function to encode image to base64
def encode_image_to_base64(image_path):
with open(image_path, 'rb') as image_file:
base64_image = base64.b64encode(image_file.read()).decode('utf-8')
return base64_image
# Function to translate text to audio and save it as a wav file
def text_to_audio(text):
now = datetime.now()
audio = _tts.generate_speech_audio(text)
if config_data['save_generated_audio']:
date_time = now.strftime("%Y-%m-%d-%H-%M-%S")
audio_filename = f"{date_time}-glados-response.wav"
sf.write(audio_filename, audio, tts.RATE)
print(f"GLaDOS text: {text}")
sd.play(audio, tts.RATE)
sd.wait() # Wait until the audio is finished playing
return
# Function to send images to OpenAI API with user-defined prompt
def send_images_to_openai(image_paths, system_prompt, user_prompt, model):
try:
contents = [
{
"type": "text",
"text": user_prompt
}
]
for image_path in image_paths:
base64_image = encode_image_to_base64(image_path)
contents.append({
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}",
"detail": "low"
}
})
payload = {
"messages": [
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": contents
},
]
}
response = openai.ChatCompletion.create(model=model, messages=payload["messages"])
response_text = response["choices"][0]["message"]["content"]
response_json = json.loads(response_text)
status = response_json.get('status')
analysis = response_json.get('analysis')
print(f"GPT-4o: classification: {status}, analysis: {analysis}")
return {'status': status, 'analysis': analysis}
except openai.error.InvalidRequestError as e:
print(f"OpenAI API request failed: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def roast_user(status, analysis, roast_prompt):
try:
if status != 'meeting' and (status == 'distracted' or status == 'absent' or status == 'smartphone'):
voice_payload = {
"messages": [
{
"role": "system",
"content": roast_prompt
},
{
"role": "user",
"content": analysis
},
]
}
voice_response = openai.ChatCompletion.create(model=model, messages=voice_payload["messages"])
response_text = voice_response["choices"][0]["message"]["content"]
Notifier.notify(response_text, title="GLaDOS Alert")
text_to_audio(response_text)
if status == 'smartphone' and config_data['twilio_enabled']:
client = Client(config_data['twilio_account_sid'], config_data['twilio_auth_token'])
twilio_message = client.messages.create(
from_ = config_data['twilio_source_number'],
to = config_data['twilio_dest_number'],
body = response_text
)
except openai.error.InvalidRequestError as e:
print(f"OpenAI API request failed: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Schedule the tasks to run every 1 minute (adjust back to 5 minutes if needed)
def job():
screenshots = take_screenshots()
webcam_photo = take_webcam_photo()
if webcam_photo:
screenshots.append(webcam_photo)
# Step 1: Send screenshots and webcam to OpenAI for evaluation
result = send_images_to_openai(screenshots, system_prompt, user_prompt, model)
# Step 2: Roast the user if distracted using voice
roast_user(result['status'], result['analysis'], core_data['system_prompt'].format(ram = system_memory))
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description="Check for --personality-core parameter.")
# Add the --personality_core argument with a default value
parser.add_argument('--personality-core', type=str, default='glados', help='Set the personality core value.')
# Parse the command-line arguments
args = parser.parse_args()
# Get the value of the --personality_core parameter
personality_core = args.personality_core
# Load main config
roast_config_path = os.path.join('config', 'config.yaml')
with open(roast_config_path, "r") as file:
config_data = yaml.safe_load(file)
# Load personality core config
core_config_path = os.path.join('cores', f"{personality_core}.yaml")
with open(core_config_path, "r") as file:
core_data = yaml.safe_load(file)
system_prompt = config_data['roast_system_prompt'].format(user_role = config_data['roast_user_role'])
user_prompt = config_data['roast_user_prompt']
model = config_data['openai_model']
# Set up OpenAI API key
openai.api_key = config_data['openai_api_key']
# Voice object
_tts = tts.Synthesizer(model_path=os.path.join(Path.cwd(), "models", core_data['voice_model']), use_cuda=False)
# Schedule the job
schedule.every(config_data['roast_interval']).seconds.do(job)
# Print initial info
system_memory = str(round(psutil.virtual_memory().total / (1024.0 **3)))+" gigabytes"
print(f"GLaDOS Roast started. RAM: {system_memory}. Press Ctrl+C to stop.")
if core_data['announcement']:
text_to_audio(core_data['announcement'].format(ram = system_memory))
# Run the scheduled tasks
while True:
try:
schedule.run_pending()
time.sleep(1)
except Exception as e:
print(f"An error occurred during scheduled task execution: {e}")