-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauxinweb.py
327 lines (299 loc) · 11.7 KB
/
auxinweb.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import pyaudio
import wave
import os
import datetime
import speech_recognition as sr
import openai
import re
import threading
import time
import rainbowhat as rh
import colorsys
import sys
from secrets import OPENAI_API_KEY
from secrets import SERVER
from secrets import SERVER_KEY
import math
from pydub import AudioSegment
os.system("sudo ttyecho -n /dev/tty1 clear")
openai.api_key = OPENAI_API_KEY
rh.rainbow.set_all(0, 0, 0)
rh.rainbow.show()
rh.lights.rgb(0, 0, 0)
rh.display.clear()
rh.display.show()
def cleanup():
# Turn off the LEDs and clear the display
rh.rainbow.set_all(0, 0, 0)
rh.rainbow.show()
rh.display.clear()
rh.display.show()
buzzer_notes = [(78, 0.748), (83, 0.48), (78, 0.48), (71, 0.48), (78, 0.48), (83, 0.48), (78, 0.48), (None, .2),
(78, 0.48), (83, 0.48), (78, 0.48), (83, 0.48), (87, 0.75), (85, 0.24),
(82, 0.24), (76, 0.24), (74, 0.24), (78, 0.48), (83, 0.48), (78, 0.48),
(71, 0.24), (71, 0.24), (78, 0.48), (83, 0.48), (78, 0.48), (83, 0.48), (None, 0.24),
(83, 0.24), (82, 0.24), (82, 0.24), (80, 0.24), (78, 0.24), (78, 0.48), (76, 0.48), (74, 1.0)]
def display_message(message):
rh.display.print_str(message)
rh.display.show()
def beep_fallacies(num):
if(num == 0):
return
for n in range(num):
rh.buzzer.midi_note(35, .5)
time.sleep(1)
# State variables to track the light states
red_light_on = False
green_light_on = False
blue_light_on = False
def update_lights():
"""Update the light colors based on their current state."""
red = 1 if red_light_on else 0
green = 1 if green_light_on else 0
blue = 1 if blue_light_on else 0
rh.lights.rgb(red, green, blue)
def light_up_leds_thread(percentage, num):
global stop_rainbow_flag
stop_rainbow_flag = True
display_message(str(num)+" LF")
# Calculate the number of LEDs to light up based on the percentage
num_leds = round(percentage / 100 * 7)
# Turn on the corresponding number of LEDs in red
for i in range(num_leds):
rh.rainbow.set_pixel(i, 255, 0, 0)
for i in range(num_leds, 7):
rh.rainbow.set_pixel(i, 0, 0, 0)
rh.rainbow.show()
if(percentage == 0):
rh.rainbow.set_all(0, 255, 0)
rh.rainbow.show()
if(percentage == -1):
rh.rainbow.set_all(0, 0, 255)
rh.rainbow.show()
# Sleep for 5 seconds
time.sleep(5)
# Turn off the LEDs
# rh.rainbow.set_all(0, 0, 0)
# rh.rainbow.show()
stop_rainbow_flag = False
# rh.display.clear()
# rh.display.show()
def light_up_leds(percentage,number):
threading.Thread(target=light_up_leds_thread, args=(percentage,number,)).start()
# Function to convert MIDI note to frequency
def note_to_freq(note):
return 2**((note-69)/12) * 440
# Set up a flag to signal the thread to stop
global stop_jeopardy_flag
stop_jeopardy_flag = False
global stop_rainbow_flag
stop_rainbow_flag = False
global toggle_flag
toggle_flag = True
# Function to play the buzzer notes on the Rainbow HAT
def play_melody():
global stop_jeopardy_flag, stop_rainbow_flag
stop_rainbow_flag = True
rh.display.clear()
rh.display.show()
while(not stop_jeopardy_flag):
for note, duration in buzzer_notes:
if note is not None and not stop_jeopardy_flag:
frequency = note_to_freq(note)
rh.buzzer.midi_note(note, duration)
time.sleep(duration)
# Turn off the buzzer and LEDs
rh.buzzer.stop()
time.sleep(.2)
def rainbow():
global stop_rainbow_flag
counter = 0
while True:
while not stop_rainbow_flag:
for x in range(7):
delta = time.time() * 75
hue = delta + (x * 2)
hue %= 360 # Clamp to 0-359.9r
hue /= 360.0 # Scale to 0.0 to 1.0
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, 1.0, 1.0)]
rh.rainbow.set_pixel(6 - x, r, g, b, brightness=0.1)
rh.rainbow.show()
if(counter % 13 == 0):
display_message(" L")
if(counter % 13 == 1):
display_message(" LI")
if(counter % 13 == 2):
display_message(" LIS")
if(counter % 13 == 3):
display_message("LIST")
if(counter % 13 == 4):
display_message("ISTE")
if(counter % 13 == 5):
display_message("STEN")
if(counter % 13 == 6):
display_message("TENI")
if(counter % 13 == 7):
display_message("ENIN")
if(counter % 13 == 8):
display_message("NING")
if(counter % 13 == 9):
display_message("ING ")
if(counter % 13 == 10):
display_message("NG ")
if(counter % 13 == 11):
display_message("G ")
if(counter % 13 == 12):
display_message(" ")
counter += 1
def split_at_text(s):
# Split the string at the last occurrence of the phrase
s = s.lower()
index = s.rfind("total number of fallacies")
if index == -1:
return None
else:
parts = [s[:index], s[index:]]
return parts
def extract_number(input_str):
number_match = re.search(r"Total number of fallacies: (\d{1,2})", input_str, re.IGNORECASE)
if number_match:
number = int(number_match.group(1))
return number
else:
return None
def extract_percentage(input_str):
# percentage_match = re.search(r"(\d{1,3})%", input_str, re.IGNORECASE)
percentage_match = re.search(r"(\d{1,3}(?:\.\d{1,2})?)%", input_str, re.IGNORECASE)
if percentage_match:
percentage = float(percentage_match.group(1))
return percentage
else:
return None
def listen():
global stop_rainbow_flag, toggle_flag
recognizer = sr.Recognizer()
microphone = sr.Microphone()
full_audio_data = b"" # Initialize an empty bytes object
#t2 = threading.Thread(target=rainbow)
while True:
if toggle_flag:
with microphone as source:
recognizer.energy_threshold = 300
#recognizer.pause_threshold = 2
recognizer.pause_threshold = 2
print("Listening for speech...")
audio = recognizer.listen(source)
full_audio_data += audio.get_wav_data()
print("Dumping Speech")
# duration = len(audio.frame_data) / (audio.sample_rate * audio.sample_width)
# print("Duration: "+str(duration))
# if duration < 25.0:
full_audio = sr.AudioData(full_audio_data, audio.sample_rate, audio.sample_width)
duration = len(full_audio.get_wav_data()) / 100000.0 # Duration in seconds
print("Duration: "+str(duration))
if duration < 20.0:
print("Recording too short, continuing...")
continue
stop_rainbow_flag = True
rh.buzzer.midi_note(85, .25)
time.sleep(.25)
rh.buzzer.midi_note(95, .25)
print("Speech detected, recording...")
now = datetime.datetime.now()
timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")
file_name = f"audio_{timestamp}.wav"
save_audio_thread = threading.Thread(target=save_audio, args=(audio, file_name))
save_audio_thread.start()
#t1 = threading.Thread(target=play_melody)
#t1.start()
full_audio_data = b""
def save_audio(audio_data, file_name):
#os.system("sudo ttyecho -n /dev/tty1 clear")
# Start the melody and rainbow as separate threads
global stop_jeopardy_flag, stop_rainbow_flag
stop_jeopardy_flag = False
stop_rainbow_flag = True
t1 = threading.Thread(target=play_melody)
t1.start()
rh.rainbow.set_all(0, 0, 0)
rh.rainbow.show()
rh.display.clear()
rh.display.show()
display_message("WAIT")
with open(file_name, "wb") as f:
f.write(audio_data.get_wav_data())
audio_file = open(file_name, "rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file)
text = transcript.text
print(text)
response=openai.ChatCompletion.create(
model="gpt-3.5-turbo",
# model="gpt-4",
temperature=0,
messages=[
{"role": "system", "content": "You analyze statements for logical fallacies and explain the reasoning"},
{"role": "user", "content": "Create a html page, start page with link centered in h1 tags <a href=\""+SERVER+"/files/\">All Analyses</a> evaluate next message in chonological order for all logical fallacies; Output a list of each occuring fallacy with the fallacy number/name in color blue as a h1 tag then the explaination as h2 tag in color blue. cite the offending quote with h3 tag in quote in color red; insert blank line, show \"total number of fallacies:\" as in interger followed by a whole number percentage of how many sentences were fallacious both in h3 tags in color purple" },
{"role": "assistant", "content": text},
]
)
stop_jeopardy_flag = True
resp = response['choices'][0]['message']['content']
print(resp)
now = datetime.datetime.now()
timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")
file_name = f"text_{timestamp}.txt"
with open(file_name, "w") as file:
# Write the variables to the file
file.write(f"{resp}\n")
file.write(f"Transcript: {transcript.text}")
result = split_at_text(resp)
percentage = None
number = None
if(resp.startswith("I'm sorry,")):
percentage = -1
if(result != None and percentage != -1):
explaination = result[0] if len(result) > 0 else None
summary = result[1] if len(result) > 1 else None
explaination = "\n".join([line for line in explaination.split("\n") if line.strip() != ""])
explaination += "\n"
percentage = extract_percentage(summary)
number = extract_number(summary)
light_up_leds(percentage, number)
print(f"Analysis:\n{explaination}")
print(f"Summary:\n{summary}")
file_name2 = f"analysis_{timestamp}.txt"
with open(file_name2, "w") as file:
# Write the variables to the file
file.write(explaination)
time.sleep(.25)
os.system(f"""curl -X POST -H "Authorization: Bearer {SERVER_KEY}" -F "file=@/home/pi/LogicAnalyzer/{file_name}" {SERVER}/upload""")
beep_fallacies(number)
# if percentage is not None and number is not None:
# print("Got Percentage and Number")
# print(f"Percentage: {percentage}")
# print(f"Number: {number}")
# light_up_leds(percentage, number)
else:
os.system(f"""curl -X POST -H "Authorization: Bearer {SERVER_KEY}" -F "file=@/home/pi/LogicAnalyzer/{file_name}" {SERVER}/upload""")
# os.system(f'''sudo ttyecho -n /dev/tty1 "clear && sleep 1 && echo There are no logical fallacies in this message."''')
print("No percentage or number found in the text.")
if(percentage != -1):
percentage = 0
number = 0
light_up_leds(percentage, number)
stop_jeopardy_flag = True
t1.join()
def main():
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 64
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) #, input_device_index=3)
t2 = threading.Thread(target=rainbow)
t2.start()
listen_thread = threading.Thread(target=listen)
listen_thread.start()
if __name__ == "__main__":
# Register the cleanup function to be called on exit
main()