diff options
Diffstat (limited to 'bin/source.py')
| -rw-r--r-- | bin/source.py | 128 |
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 @@ | |||
| 1 | import time | ||
| 2 | import os | ||
| 3 | import ollama | ||
| 4 | import speech_recognition as speech | ||
| 5 | import i2c as LCD | ||
| 6 | from gpiozero import CPUTemperature | ||
| 7 | |||
| 8 | ERROR_BAD_REQUEST = "400 Bad Request" | ||
| 9 | ERROR_UNAUTHORIZED = "401 Unauthorized" | ||
| 10 | ERROR_NOT_FOUND = "404 Not Found" | ||
| 11 | ERROR_TIMEOUT = "408 Request Timeout" | ||
| 12 | |||
| 13 | lcd = LCD() | ||
| 14 | cpu_temp = CPUTemperature() | ||
| 15 | lama = ollama() | ||
| 16 | recognizer = speech.Recognizer() | ||
| 17 | microphone = speech.Microphone() | ||
| 18 | |||
| 19 | |||
| 20 | def 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 | |||
| 31 | def 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 | |||
| 40 | def 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 | |||
| 59 | def 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 | |||
| 72 | def 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 | |||
| 94 | OPTIONS = { | ||
| 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 | |||
| 103 | def 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 | |||
| 119 | def destroy(): | ||
| 120 | lcd.clear() | ||
| 121 | os.system("cls" if os.name == "nt" else "clear") | ||
| 122 | |||
| 123 | |||
| 124 | if __name__ == "__main__": | ||
| 125 | try: | ||
| 126 | main() | ||
| 127 | except KeyboardInterrupt: | ||
| 128 | destroy() | ||
