From 1d610e79b11a914f6883b84d4511ae1b59a5dea1 Mon Sep 17 00:00:00 2001 From: Nolan Mascrier <63266772+NolanMascrier@users.noreply.github.com> Date: Fri, 7 Mar 2025 11:09:56 +0100 Subject: [PATCH] Added audio playback from AI's answers through Google's Translation TTS. --- server.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/server.py b/server.py index 0fba26c..97283bc 100644 --- a/server.py +++ b/server.py @@ -3,10 +3,13 @@ import requests import json import sys import time +import time +import pyglet +import os from threading import Thread from Cube import Cube +from gtts import gTTS -# Adjust this to match your serial port SERIAL_PORT = "/dev/ttyACM0" # Linux/macOS (Check with `ls /dev/tty*`) BAUD_RATE = 115200 @@ -50,7 +53,21 @@ def read_key(): API_KEY = value -def ask_ia(prompt): +def play_audio(answer): + """Play an audio file through the pyglet library + and the google translation TTS. + + Args: + answer (string): Text that will be read""" + tts = gTTS(text=answer, lang='en') + filename = '/tmp/temp.mp3' + tts.save(filename) + music = pyglet.media.load(filename, streaming=False) + music.play() + time.sleep(music.duration) + os.remove(filename) + +def querry(prompt): """Sends the prompt to the DeepSeek API, and returns its answer. @@ -82,16 +99,21 @@ def ask_ia(prompt): try: if response.status_code == 200: ai_response = response.json()["choices"][0]["message"]["content"] - print(f"Cube : {ai_response}") + print(f"Cube: {ai_response}") return ai_response else: error_message = f"Erreur API DeepSeek: {response.status_code} - {response.text}" - print(f"❌ {error_message}") + print(f"Error: {error_message}") return None except Exception as e: print("WOOPS It exploded ::", e) sys.exit(0) +def ask_ia(prompt): + """Querries the AI and play the resulting audio.""" + answer = querry(prompt) + play_audio(answer) + def generate_response(prompt): """Launches a thread that will query DeepSeek and display its response."""