# =================================================================# # ____ _ _ __ __ __ ___ _ _ ____ # # | _ \| \ | | / \ ___ ____ | \/ |_ _| \ | | _ \ ___ # # | | | | \| || /\ | / _ \ | __| | |\/| || || \| | | | |/ / # # | |_| | |\ || -- | | |_| || _| | | | || || |\ | |_| |\ \ # # |____/|_| \_|| || | \___/ |_| |_| |_|___|_| \_|____//__/ # # # # Code by DNA of MINDs # # =================================================================# # created 06/2025 # # =================================================================# """ Description: Use the script to build your own J.A.R.V.I.S. AI assistant with the help of ChatGPT Please generate and use your own OpenAI Key for it """ import os import time import numpy as np import sounddevice as sd from piper.voice import PiperVoice from openai import OpenAI import speech_recognition as sr import wave import pygame import RPi.GPIO as gpio import threading class SpeakingAI: def __init__(self, name_init, client_init, recognizer_init, voice_init, stream_init): self.name = name_init self.client = client_init self.recognizer = recognizer_init self.voice = voice_init self.stream = stream_init self.recognize = False self.request = False self.speaking = False self.stop = False self.exit = False self.shutdown = False self.thread_pool = [] self.sentence_list = [] self.bytes_array = [] def play(self, path, waiting): audio = pygame.mixer.Sound(path) playing = audio.play() if waiting == True: while playing.get_busy(): pygame.time.delay(100) def request_gpt(self, message): completion = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": message}] ) return completion.choices[0].message.content def text_to_speech(self, message): synthesize_thread = threading.Thread(target=self.synthesize_sentence, args=(message,)) speaking_thread = threading.Thread(target=self.speaking_sentence, args=()) self.thread_pool = [synthesize_thread, speaking_thread] synthesize_thread.start() synthesize_thread.join() speaking_thread.start() speaking_thread.join() def synthesize_sentence(self, message): for sentence in message.split(". "): self.bytes_array = self.voice.synthesize_stream_raw(sentence) self.sentence_list.append(self.bytes_array) def speaking_sentence(self): self.stream.start() while len(self.sentence_list) > 0: for audio_bytes in self.sentence_list[0]: int_data = np.frombuffer(audio_bytes, dtype=np.int16) self.stream.write(int_data) self.sentence_list.pop(0) if self.stop == True: self.sentence_list = [] self.stream.abort() self.stop = False self.stream.stop() def run(self): input_text = "" output_text = "" with sr.Microphone() as source: audio_data = recognizer.listen(source) try: input_text = recognizer.recognize_google(audio_data, language="de-De") print("Ich: " + input_text) except sr.UnknownValueError: print("AI: Ich kann dich leider nicht verstehen.") except sr.RequestError as e: print("AI: Fehler") if "Stop" in input_text or "stop" in input_text: self.request = False self.stop = True output_text = "Ok" if self.stream.stopped: if self.name in input_text: self.request = True output_text = "Ja" elif "Jarvis" in input_text or "jarvis" in input_text: self.request = True output_text = "Willkommen!" elif "Tschüss" in input_text or "tschüss" in input_text: self.request = False output_text = "Tschüss" elif "Beenden" in input_text or "beenden" in input_text: self.request = False self.exit = True output_text = "Ok, ich werde mich beenden." elif "Herunterfahren" in input_text or "herunterfahren" in input_text: self.request = False self.exit = True self.shutdown = True output_text = "Ok, ich werde mich ausschalten." elif self.request == True and len(input_text.split(' ')) >= 1 and len(input_text.split(' ')) <= 15: output_text = self.request_gpt(input_text) if len(input_text) >= 2 and len(input_text.split(' ')) >= 1 and len(output_text) >= 1: self.text_to_speech(output_text) print("AI: " + output_text) def deinit(self): self.thread_pool[0].join() self.thread_pool[1].join() self.stream.stop() self.stream.close() if self.shutdown: os.system('sudo shutdown now') class Interaction(threading.Thread): def __init__(self, name_init): threading.Thread.__init__(self) self.name = name_init self.shutdown = False self.pin23 = gpio.PWM(23, 1000) self.pin24 = gpio.PWM(24, 1000) def run(self): self.pin23.start(0) self.pin24.start(0) while self.shutdown is False: for duty in range(0, 101, 1): self.pin23.ChangeDutyCycle(duty) time.sleep(0.01) for duty in range(100, -1, -1): self.pin23.ChangeDutyCycle(duty) time.sleep(0.01) self.pin24.ChangeDutyCycle(GPIO.input(22)) self.pin23.stop() self.pin24.stop() def deinit(self): self.shutdown = True if __name__ == "__main__": # open ai client = OpenAI(api_key="BITTE_DURCH_EIGENEN_OPENAI_KEY_ERSETZEN") # speech recognizer recognizer = sr.Recognizer() recognizer.energy_threshold = 800 recognizer.pause_threshold = 1 # speech model model = "./jarvis.onnx" voice = PiperVoice.load(model) stream = sd.OutputStream(samplerate=voice.config.sample_rate, channels=1, dtype='int16') # audio mixer pygame.mixer.init() # start ai interface ai = SpeakingAI("Jarvis", client, recognizer, voice, stream) #ai.play('./Starting.mp3', False) # gpio interface gpio.setmode(gpio.BCM) gpio.setup(22, gpio.IN) gpio.setup(23, gpio.OUT) gpio.setup(24, gpio.OUT) gpio_thread = Interaction("Light") gpio_thread.start() # run ai interface while ai.exit is False: ai.run() # stop ai interface #ai.play('./Loading.mp3', True) gpio_thread.deinit() gpio_thread.join() ai.deinit()