summaryrefslogtreecommitdiff
path: root/bin/source.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/source.py')
-rw-r--r--bin/source.py128
1 files changed, 128 insertions, 0 deletions
diff --git a/bin/source.py b/bin/source.py
new file mode 100644
index 0000000..c232351
--- /dev/null
+++ b/bin/source.py
@@ -0,0 +1,128 @@
1import time
2import os
3import ollama
4import speech_recognition as speech
5import i2c as LCD
6from gpiozero import CPUTemperature
7
8ERROR_BAD_REQUEST = "400 Bad Request"
9ERROR_UNAUTHORIZED = "401 Unauthorized"
10ERROR_NOT_FOUND = "404 Not Found"
11ERROR_TIMEOUT = "408 Request Timeout"
12
13lcd = LCD()
14cpu_temp = CPUTemperature()
15lama = ollama()
16recognizer = speech.Recognizer()
17microphone = speech.Microphone()
18
19
20def display_cpu_info():
21 lcd.clear()
22 while True:
23 load = os.getloadavg()[0] # 1-minute load average
24 temperature = cpu_temp.temperature
25 lcd.clear()
26 lcd.display_text(f"CPU Load:i {load}", 1)
27 lcd.display_text(f"Temp: {temperature:}C", 2)
28 time.sleep(5)
29
30
31def display_uptime():
32 lcd.clear()
33 with open("/proc/uptime") as f:
34 uptime_seconds = float(f.readline().split()[0])
35 uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds))
36 lcd.clear()
37 lcd.display_text(f"Uptime: {uptime_str}", 1, "center")
38
39
40def recognize_speech():
41 lcd.clear()
42 try:
43 with microphone as source:
44 recognizer.adjust_for_ambient_noise(source)
45 print("Listening...")
46 audio = recognizer.listen(source)
47 text = recognizer.recognize_google(audio)
48 lcd.clear()
49 lcd.display_text(text, 1)
50 print("Speech recognized:", text)
51 except speech.UnknownValueError:
52 lcd.display_text(ERROR_BAD_REQUEST, 1)
53 print(ERROR_BAD_REQUEST)
54 except speech.RequestError:
55 lcd.display_text(ERROR_UNAUTHORIZED, 1)
56 print(ERROR_UNAUTHORIZED)
57
58
59def save_notes():
60 PRINT_REQUEST = True
61 EXIT_CODES = ['stop', 'break', 'quit', 'exit']
62 if PRINT_REQUEST == True:
63 while True:
64 OUTPUT = input()
65 print(OUTPUT)
66 lcd.display_text(OUTPUT, 1)
67 time.sleep(2)
68 for i in EXIT_CODES:
69 if OUTPUT == i:
70 PRINT_REQUEST == False
71
72def lama():
73 QUESTION_REQUEST = True
74 while QUESTION_REQUEST == True:
75 QUESTION_REQUEST = input
76 ("do you want to ask a question") == 'yes'
77 if QUESTION_REQUEST == True:
78 USER_QUESTION = input()
79 response = lama.chat(
80 model="llama3.2",
81 messages=[
82 {
83 "role": "user",
84 "content": USER_QUESTION,
85 },
86 ],
87 )
88 print(response["messages"]["content"])
89 else:
90 break
91
92
93
94OPTIONS = {
95 "LAMA": ollama(),
96 "CPU_INFO": display_cpu_info(),
97 "UPTIME": display_uptime(),
98 "SPEECH_TRANSCRIBER": recognize_speech(),
99 "NOTES": save_notes(),
100}
101
102
103def main():
104 lcd.clear()
105 print("WELCOME TO THE I2C COMMAND LINE CENTER")
106 print("Options:", ", ".join(OPTIONS.keys()))
107
108 while True:
109 user_input = input("Enter command: ").upper()
110 action = OPTIONS.get(user_input)
111
112 if action:
113 action()
114 else:
115 lcd.display_text(ERROR_NOT_FOUND, 1)
116 print(ERROR_NOT_FOUND)
117
118
119def destroy():
120 lcd.clear()
121 os.system("cls" if os.name == "nt" else "clear")
122
123
124if __name__ == "__main__":
125 try:
126 main()
127 except KeyboardInterrupt:
128 destroy()