commit 51fd4d9ce2f44702304116c8a4c07e1dbf7d0c80
parent 1c971cce0dcfebe013456b3f2e28d57d437fd4d0
Author: khanumballz <[email protected]>
Date: Thu, 16 May 2024 07:33:29 +0100
Piper tts and Ollama Bindings
Diffstat:
1 file changed, 72 insertions(+), 0 deletions(-)
diff --git a/python/animation/voice-and-motor.py b/python/animation/voice-and-motor.py
@@ -0,0 +1,72 @@
+import serial
+import threading
+import time
+from dimits import Dimits
+import ollama
+
+def send_command(ser, command):
+ ser.write(command.encode())
+ ser.write(b'\n') # Assuming commands are terminated with a newline character
+
+def play_tts(text, command_type):
+ # Initialize Dimits with the desired voice model
+ dt = Dimits("en_US-amy-medium")
+ # Convert text to audio and play it using the aplay engine
+ dt.text_2_speech(text, engine="aplay", command=command_type)
+
+def main():
+ # Open serial port
+ ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
+
+ if not ser.is_open:
+ print("Failed to open serial port.")
+ return
+
+ print("Serial port opened successfully.")
+
+ prompt_text = input("Enter the prompt for the robot: ")
+
+ stream = ollama.chat(
+ model='dolphin-phi2-usb',
+ messages=[{'role': 'user', 'content': prompt_text}],
+ stream=True,
+ )
+
+ generated_text = ""
+ for chunk in stream:
+ generated_text += chunk['message']['content']
+ #print(generated_text)
+ if len(generated_text.split()) >= 15:
+ print(generated_text)
+ user_text = generated_text
+ break
+
+ # Prompt the user to enter text for TTS
+ #user_text = input("Enter the text for speech: ")
+
+ # Determine the command type based on the length of the input
+ if len(user_text.split()) > 7: # Check if the input has more than 5 words
+ command_type = "1sm" # Use long command for speech
+ elif len(user_text.split()) > 4:
+ command_type = "1so" # Use short command for speech
+ else:
+ command_type = "1sn"
+
+ # Create a thread to play the TTS audio with the user's text
+ tts_thread = threading.Thread(target=play_tts, args=(user_text, command_type))
+ tts_thread.start()
+
+ # Delay to ensure synchronization (adjust as needed)
+ time.sleep(3.5) # Adjust the delay based on the duration of the TTS audio
+
+ # Initiate animation sequence after the delay
+ send_command(ser, "1s3")
+ send_command(ser, command_type) # Use variable command_type
+ print("Sent motor command")
+ time.sleep(12)
+
+ # Wait for the TTS thread to finish before exiting
+ tts_thread.join()
+
+if __name__ == "__main__":
+ main()