From fe441f69a4632e5245588574923ef8dc467eced5 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Thu, 14 Nov 2024 19:31:03 +0100 Subject: tried implementing ollama --- .../UserInterfaceState.xcuserstate | Bin 28421 -> 37489 bytes .../xcdebugger/Breakpoints_v2.xcbkptlist | 40 ++++++ I2C/I2C/ContentView.swift | 27 +--- I2C/I2CUITests/I2CUITests.swift | 1 - bin/__pycache__/source.cpython-313.pyc | Bin 0 -> 5737 bytes bin/i2c.py | 47 +++++++ bin/icon.png | Bin 3754 -> 0 bytes bin/kasper_gui.py | 6 - bin/kasper_source.py | 154 --------------------- bin/main.py | 9 ++ bin/source.py | 128 +++++++++++++++++ webconsole/index.html | 12 ++ webconsole/main.js | 0 13 files changed, 241 insertions(+), 183 deletions(-) create mode 100644 I2C/I2C.xcodeproj/xcuserdata/nsrddyn.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist create mode 100644 bin/__pycache__/source.cpython-313.pyc create mode 100644 bin/i2c.py delete mode 100644 bin/icon.png delete mode 100644 bin/kasper_gui.py delete mode 100644 bin/kasper_source.py create mode 100644 bin/main.py create mode 100644 bin/source.py create mode 100644 webconsole/index.html create mode 100644 webconsole/main.js diff --git a/I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate b/I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate index 78be191..07340b1 100644 Binary files a/I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate and b/I2C/I2C.xcodeproj/project.xcworkspace/xcuserdata/nsrddyn.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/I2C/I2C.xcodeproj/xcuserdata/nsrddyn.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/I2C/I2C.xcodeproj/xcuserdata/nsrddyn.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..214cbf9 --- /dev/null +++ b/I2C/I2C.xcodeproj/xcuserdata/nsrddyn.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,40 @@ + + + + + + + + + + + + + diff --git a/I2C/I2C/ContentView.swift b/I2C/I2C/ContentView.swift index e21bcb2..6d0448d 100644 --- a/I2C/I2C/ContentView.swift +++ b/I2C/I2C/ContentView.swift @@ -1,34 +1,17 @@ import SwiftUI struct ContentView: View { - var header: some View { - VStack { - Text("Hello world!") - } - - } + let options = ["CPU INFO", "CPU UPTIME", "SPEECH TRANSCRIBER", "NOTES"] var body: some View { VStack { - Circle() - .fill(.blue) - .frame(width: 100, height: 100) - .padding() - header - Button("VOICE RECOGNITION"){ - - } - Button("CPU UPTIME"){ - - } - Button("CPU INFO"){ - - } + Button( + RoundedRectangle(cornerRadius: 25) + .fill(Color.red) + ) } } } - - #Preview { ContentView() } diff --git a/I2C/I2CUITests/I2CUITests.swift b/I2C/I2CUITests/I2CUITests.swift index c225d79..a98bb08 100644 --- a/I2C/I2CUITests/I2CUITests.swift +++ b/I2C/I2CUITests/I2CUITests.swift @@ -3,7 +3,6 @@ // I2CUITests // // Created by nsrddyn on 09/11/2024. -// import XCTest diff --git a/bin/__pycache__/source.cpython-313.pyc b/bin/__pycache__/source.cpython-313.pyc new file mode 100644 index 0000000..79aee1d Binary files /dev/null and b/bin/__pycache__/source.cpython-313.pyc differ diff --git a/bin/i2c.py b/bin/i2c.py new file mode 100644 index 0000000..a79d9d1 --- /dev/null +++ b/bin/i2c.py @@ -0,0 +1,47 @@ +import smbus2 as SMBus +import time + +LCD_BACKLIGHT = 0x08 +LCD_NOBACKLIGHT = 0x00 +ENABLE_BIT = 0b00000100 +LINES = {1: 0x80, 2: 0xC0} +ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} + + +class LCD: + + def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): + self.address = address + self.bus = SMBus(bus) + self.width = width + self.rows = rows + self.backlight_status = backlight + self.delay = 0.0005 + + for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01): + self.write(cmd) + time.sleep(self.delay) + + def write(self, byte, mode=0): + backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT + self._write_byte(mode | ((byte << 4) & 0xF0) | backlight) + + def _write_byte(self, byte): + self.bus.write_byte(self.address, byte) + self.bus.write_byte(self.address, (byte | ENABLE_BIT)) + time.sleep(self.delay) + self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) + time.sleep(self.delay) + + def display_text(self, text, line=1, align="left"): + self.write(LINES.get(line, LINES[1])) + aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) + for char in aligned_text: + self.write(ord(char), mode=1) + + def clear(self): + self.write(0x01) + + def set_backlight(self, turn_on=True): + self.backlight_status = turn_on + self.write(0) diff --git a/bin/icon.png b/bin/icon.png deleted file mode 100644 index c372c49..0000000 Binary files a/bin/icon.png and /dev/null differ diff --git a/bin/kasper_gui.py b/bin/kasper_gui.py deleted file mode 100644 index e5edc6a..0000000 --- a/bin/kasper_gui.py +++ /dev/null @@ -1,6 +0,0 @@ -import tkinter - -display = tkinter.Tk() -display.title("Kasper") - -display.mainloop() \ No newline at end of file diff --git a/bin/kasper_source.py b/bin/kasper_source.py deleted file mode 100644 index 8067997..0000000 --- a/bin/kasper_source.py +++ /dev/null @@ -1,154 +0,0 @@ -from smbus import SMBus -from gpiozero import CPUTemperature -import speech_recognition as speech -import os -import time -from time import sleep - -# LCD Constants -LCD_BACKLIGHT = 0x08 -LCD_NOBACKLIGHT = 0x00 -ENABLE_BIT = 0b00000100 -LINES = {1: 0x80, 2: 0xC0, 3: 0x94, 4: 0xD4} -ALIGN_FUNC = {"left": "ljust", "right": "rjust", "center": "center"} - -# Error Messages -ERROR_BAD_REQUEST = "400 Bad Request" -ERROR_UNAUTHORIZED = "401 Unauthorized" -ERROR_NOT_FOUND = "404 Not Found" -ERROR_TIMEOUT = "408 Request Timeout" - -# LCD Control Class -class LCD: - - def __init__(self, address=0x27, bus=1, width=20, rows=4, backlight=True): - self.address = address - self.bus = SMBus(bus) - self.width = width - self.rows = rows - self.backlight_status = backlight - self.delay = 0.0005 - - # LCD Initialization - for cmd in (0x33, 0x32, 0x06, 0x0C, 0x28, 0x01): - self.write(cmd) - time.sleep(self.delay) - - def write(self, byte, mode=0): - """Send a command or character to the LCD.""" - backlight = LCD_BACKLIGHT if self.backlight_status else LCD_NOBACKLIGHT - self._write_byte(mode | ((byte << 4) & 0xF0) | backlight) - - def _write_byte(self, byte): - """Write a byte to the I2C bus.""" - self.bus.write_byte(self.address, byte) - self.bus.write_byte(self.address, (byte | ENABLE_BIT)) - time.sleep(self.delay) - self.bus.write_byte(self.address, (byte & ~ENABLE_BIT)) - time.sleep(self.delay) - - def display_text(self, text, line=1, align="left"): - """Display text on a specified line with alignment.""" - self.write(LINES.get(line, LINES[1])) - aligned_text = getattr(text, ALIGN_FUNC.get(align, "ljust"))(self.width) - for char in aligned_text: - self.write(ord(char), mode=1) - - def clear(self): - """Clear the display.""" - self.write(0x01) - - def set_backlight(self, turn_on=True): - """Toggle backlight on or off.""" - self.backlight_status = turn_on - self.write(0) - -# Initialize components -lcd = LCD() -cpu_temp = CPUTemperature() -recognizer = speech.Recognizer() -microphone = speech.Microphone() - - -# Display Functions -def display_cpu_info(): - # clearing the display before accessing it - lcd.clear() - """Display CPU load and temperature on the LCD.""" - while True: - load = os.getloadavg()[0] # 1-minute load average - temperature = cpu_temp.temperature - lcd.clear() - lcd.display_text(f"CPU Load: {load:.2f}", 1) - lcd.display_text(f"Temp: {temperature:.1f}C", 2) - time.sleep(5) - - -def display_uptime(): - # clearing the display before accessing it - lcd.clear() - """Display system uptime on the LCD.""" - with open("/proc/uptime") as f: - uptime_seconds = float(f.readline().split()[0]) - uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) - lcd.clear() - lcd.display_text(f"Uptime: {uptime_str}", 1) - - -def recognize_speech(): - # clearing the display before accessing it - lcd.clear() - """Capture and transcribe speech input.""" - try: - with microphone as source: - recognizer.adjust_for_ambient_noise(source) - print("Listening...") - audio = recognizer.listen(source) - text = recognizer.recognize_google(audio) - lcd.clear() - lcd.display_text(text, 1) - print("Speech recognized:", text) - except speech.UnknownValueError: - lcd.display_text(ERROR_BAD_REQUEST, 1) - print(ERROR_BAD_REQUEST) - except speech.RequestError: - lcd.display_text(ERROR_UNAUTHORIZED, 1) - print(ERROR_UNAUTHORIZED) - -def notes(): - while True: - OUTPUT = input() - print(OUTPUT) - lcd.display_text(OUTPUT, 1) - sleep(2) - - -# Main Program Options -OPTIONS = { - "CPU_INFO": display_cpu_info, - "UPTIME": display_uptime, - "SPEECH_TRANSCRIBER": recognize_speech, - "NOTES": notes, -} - - -def main(): - # clearing the display before doing anything - lcd.clear() - # Main program loop to accept user commands. - print("WELCOME TO THE I2C COMMAND LINE CENTER") - print("Options:", ", ".join(OPTIONS.keys())) - - while True: - user_input = input("Enter command: ").upper() - action = OPTIONS.get(user_input) - - if action: - action() - else: - lcd.display_text(ERROR_NOT_FOUND, 1) - print(ERROR_NOT_FOUND) - - -if __name__ == "__main__": - main() diff --git a/bin/main.py b/bin/main.py new file mode 100644 index 0000000..2231547 --- /dev/null +++ b/bin/main.py @@ -0,0 +1,9 @@ +import tk +import source as main + +source = main() + +display = tk.Tk() +display.title("Kasper") + +display.mainloop() \ No newline at end of file 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 @@ +import time +import os +import ollama +import speech_recognition as speech +import i2c as LCD +from gpiozero import CPUTemperature + +ERROR_BAD_REQUEST = "400 Bad Request" +ERROR_UNAUTHORIZED = "401 Unauthorized" +ERROR_NOT_FOUND = "404 Not Found" +ERROR_TIMEOUT = "408 Request Timeout" + +lcd = LCD() +cpu_temp = CPUTemperature() +lama = ollama() +recognizer = speech.Recognizer() +microphone = speech.Microphone() + + +def display_cpu_info(): + lcd.clear() + while True: + load = os.getloadavg()[0] # 1-minute load average + temperature = cpu_temp.temperature + lcd.clear() + lcd.display_text(f"CPU Load:i {load}", 1) + lcd.display_text(f"Temp: {temperature:}C", 2) + time.sleep(5) + + +def display_uptime(): + lcd.clear() + with open("/proc/uptime") as f: + uptime_seconds = float(f.readline().split()[0]) + uptime_str = time.strftime("%H:%M:%S", time.gmtime(uptime_seconds)) + lcd.clear() + lcd.display_text(f"Uptime: {uptime_str}", 1, "center") + + +def recognize_speech(): + lcd.clear() + try: + with microphone as source: + recognizer.adjust_for_ambient_noise(source) + print("Listening...") + audio = recognizer.listen(source) + text = recognizer.recognize_google(audio) + lcd.clear() + lcd.display_text(text, 1) + print("Speech recognized:", text) + except speech.UnknownValueError: + lcd.display_text(ERROR_BAD_REQUEST, 1) + print(ERROR_BAD_REQUEST) + except speech.RequestError: + lcd.display_text(ERROR_UNAUTHORIZED, 1) + print(ERROR_UNAUTHORIZED) + + +def save_notes(): + PRINT_REQUEST = True + EXIT_CODES = ['stop', 'break', 'quit', 'exit'] + if PRINT_REQUEST == True: + while True: + OUTPUT = input() + print(OUTPUT) + lcd.display_text(OUTPUT, 1) + time.sleep(2) + for i in EXIT_CODES: + if OUTPUT == i: + PRINT_REQUEST == False + +def lama(): + QUESTION_REQUEST = True + while QUESTION_REQUEST == True: + QUESTION_REQUEST = input + ("do you want to ask a question") == 'yes' + if QUESTION_REQUEST == True: + USER_QUESTION = input() + response = lama.chat( + model="llama3.2", + messages=[ + { + "role": "user", + "content": USER_QUESTION, + }, + ], + ) + print(response["messages"]["content"]) + else: + break + + + +OPTIONS = { + "LAMA": ollama(), + "CPU_INFO": display_cpu_info(), + "UPTIME": display_uptime(), + "SPEECH_TRANSCRIBER": recognize_speech(), + "NOTES": save_notes(), +} + + +def main(): + lcd.clear() + print("WELCOME TO THE I2C COMMAND LINE CENTER") + print("Options:", ", ".join(OPTIONS.keys())) + + while True: + user_input = input("Enter command: ").upper() + action = OPTIONS.get(user_input) + + if action: + action() + else: + lcd.display_text(ERROR_NOT_FOUND, 1) + print(ERROR_NOT_FOUND) + + +def destroy(): + lcd.clear() + os.system("cls" if os.name == "nt" else "clear") + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + destroy() diff --git a/webconsole/index.html b/webconsole/index.html new file mode 100644 index 0000000..812011f --- /dev/null +++ b/webconsole/index.html @@ -0,0 +1,12 @@ + + + + + + CONSOLE + + + + + + \ No newline at end of file diff --git a/webconsole/main.js b/webconsole/main.js new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3-70-g09d2