add project

This commit is contained in:
@arthurcabon29 2025-03-07 15:57:11 +01:00
commit a97d12443c
34 changed files with 225 additions and 0 deletions

42
arduino/motor/motor.ino Normal file
View File

@ -0,0 +1,42 @@
// const int bouton = 2; // the button is connected to pin 2 of the Adruino board
const int relais_moteur = 2; // the relay is connected to pin 3 of the Adruino board
int etatBouton;
unsigned long currentMillis = 0;
const long interval = 500;
void setup()
{
Serial.begin(9600);
// pinMode(bouton, INPUT);
pinMode(relais_moteur, OUTPUT);
}
void loop()
{
// Check if data is received
if (Serial.available() > 0)
{
char command = Serial.read(); // Read the received command
if (command == 'R') {
digitalWrite(relais_moteur, HIGH);
// currentMillis = millis();
delay(500);
digitalWrite(relais_moteur, LOW);
delay(500);
}
// other method (does not work) :
// if (command == 'R') {
// digitalWrite(relais_moteur, HIGH);
// currentMillis = millis();
// }
// if (currentMillis != 0 && millis() - currentMillis > interval)
// {
// digitalWrite(relais_moteur, LOW);
// currentMillis = 0;
// }
delay(10);
}
}

View File

@ -0,0 +1,30 @@
const int trigPin = 11; // Trigger
const int echoPin = 12; // Echo
long duree; // durée de l'echo
int distance; // distance
void setup() {
pinMode(trigPin, OUTPUT); // Configuration du port du Trigger comme une SORTIE
pinMode(echoPin, INPUT); // Configuration du port de l'Echo comme une ENTREE
Serial.begin(9600); // Démarrage de la communication série
}
void loop() {
// Émission d'un signal de durée 10 microsecondes
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Écoute de l'écho
duree = pulseIn(echoPin, HIGH);
// Calcul de la distance
distance = duree*0.034/2;
// Affichage de la distance dans le Moniteur Série
Serial.print("Distance : ");
Serial.print(distance);
Serial.println("cm");
}

BIN
assets/audio/1.wav Normal file

Binary file not shown.

BIN
assets/audio/10.wav Normal file

Binary file not shown.

BIN
assets/audio/11.wav Normal file

Binary file not shown.

BIN
assets/audio/12.wav Normal file

Binary file not shown.

BIN
assets/audio/13.wav Normal file

Binary file not shown.

BIN
assets/audio/14.wav Normal file

Binary file not shown.

BIN
assets/audio/15.wav Normal file

Binary file not shown.

BIN
assets/audio/16.wav Normal file

Binary file not shown.

BIN
assets/audio/17.wav Normal file

Binary file not shown.

BIN
assets/audio/18.wav Normal file

Binary file not shown.

BIN
assets/audio/19.wav Normal file

Binary file not shown.

BIN
assets/audio/2.wav Normal file

Binary file not shown.

BIN
assets/audio/20.wav Normal file

Binary file not shown.

BIN
assets/audio/21.wav Normal file

Binary file not shown.

BIN
assets/audio/22.wav Normal file

Binary file not shown.

BIN
assets/audio/23.wav Normal file

Binary file not shown.

BIN
assets/audio/24.wav Normal file

Binary file not shown.

BIN
assets/audio/3.wav Normal file

Binary file not shown.

BIN
assets/audio/4.wav Normal file

Binary file not shown.

BIN
assets/audio/5.wav Normal file

Binary file not shown.

BIN
assets/audio/6.wav Normal file

Binary file not shown.

BIN
assets/audio/7.wav Normal file

Binary file not shown.

BIN
assets/audio/8.wav Normal file

Binary file not shown.

BIN
assets/audio/9.wav Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

153
main.py Normal file
View File

@ -0,0 +1,153 @@
import requests
import whisper
import sounddevice as sd
import numpy as np
import soundfile as sf
import pyttsx3
import random
import re
import pygame
import time
import serial
# list of tics audios
# 3, 16, 17, 18, 20, 22, 23, 24 have been removed
PATHS_AUDIO = [
'assets/audio/1.wav',
'assets/audio/2.wav',
'assets/audio/4.wav',
'assets/audio/5.wav',
'assets/audio/6.wav',
'assets/audio/7.wav',
'assets/audio/8.wav',
'assets/audio/9.wav',
'assets/audio/10.wav',
'assets/audio/11.wav',
'assets/audio/12.wav',
'assets/audio/13.wav',
'assets/audio/14.wav',
'assets/audio/15.wav',
'assets/audio/19.wav',
'assets/audio/21.wav',
'assets/audio/claquement-2.wav',
'assets/audio/claquement-3.wav',
'assets/audio/claquement-4.wav',
'assets/audio/claquement-5.wav',
]
# Mistral AI API Key
API_KEY = "MY_API_KEY"
# Initializing the speech synthesis engine
engine = pyttsx3.init()
# Loading the Whisper model
print("Initialisation du Brainrot...")
whisper_model = whisper.load_model("base")
# Creation of the prompt that will be sent to the API
def create_prompte(text):
prompte = "Je souhaiterais que tu incarne du debut a la fin de ta reponse quelqu'un avec des tics. \
La personne qui tu incarne ne devras pas avoir plus de 5 tics dans sa réponse. \
Tout les tics seront representés entre crochets, comme ceci : [tic] \
Plusieurs tics d'affilés soront recrésentés entre crochets et non séparés par des espaces. \
Voici la phrese auquel je veux que tu repondes : "
prompte += text
return (prompte)
# Function to record audio
def record_audio(filename="output.wav", duration=5, samplerate=44100):
print("Parle maintenant...")
recording = sd.rec(int(samplerate * duration), samplerate=samplerate, channels=1, dtype=np.float32)
sd.wait()
sf.write(filename, recording, samplerate)
print("Enregistrement terminé.")
# Function to transcribe audio to text
def transcribe_audio(filename="output.wav"):
print("Transcription de l'audio en cours...")
result = whisper_model.transcribe(filename)
text = result["text"].strip()
print(f"Transcription détectée : '{text}'")
return text
# Function to send the request to Mistral AI
def generate_response(prompt):
if not prompt:
return "Je n'ai rien compris, peux-tu répéter ?"
print("Génération de la réponse...")
url = "https://api.mistral.ai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "mistral-tiny",
"messages": [{"role": "user", "content": create_prompte(prompt)}]
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
ai_response = response.json()["choices"][0]["message"]["content"]
print(f"{ai_response}")
return ai_response
else:
error_message = f"Erreur API Mistral: {response.status_code} - {response.text}"
print(f"{error_message}")
return error_message
def play(path):
pygame.mixer.music.load(path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
def get_audio_path():
num_tic = random.randint(0, len(PATHS_AUDIO) - 1)
return (PATHS_AUDIO[num_tic])
# Function to make AI speak
# Possibility to give him different tics
def speak(text, arduino):
tab_text = re.split(r"[\[\]]", text)
for text_i in tab_text:
match text_i.lower():
case "tic":
if (random.randint(0, 2) == 0):
play(get_audio_path())
else:
print("test")
arduino.write(b"R")
case _:
engine.say(text_i)
engine.runAndWait()
# Main function
def main():
pygame.init()
pygame.mixer.init()
print("Connexion a la carte arduino...")
arduino = serial.Serial('COM4', 9600, timeout=1)
time.sleep(2)
print("Connexion réalisé !")
while True:
record_audio() # Records the user's voice
user_text = transcribe_audio() # Transcribes audio to text
# possibility to send prompts
# user_text = input("Ecrivez votre prompte : ")
print(user_text)
if user_text.lower() in ["stop", "quitte", "exit"]:
print("Fin du programme.")
break
response = generate_response(user_text) # Send the text to Mistral AI
speak(response, arduino) # Make AI talk with pyttsx3
if __name__ == "__main__":
main()