LUNA – My Personal Assistant

Terinspirasi dari Jarvis pada film Ironman, yang merupakan asisten pribadi Tony Stark dalam setiap menjalankan aksi-aksinya. Baru-baru ini CEO Facebook (Mark Zuckerberg) juga sudah mengembangkan sistem serupa untuk asisten pribadi dirumahnya (real life).

Sambil mengisi waktu luang, saya pun tertarik untuk mengembangkan sistem aplikasi serupa (namun tak sama) sebagai asisten pribadi masa depan. Luna, adalah sebuah program aplikasi berbasis speech-recognition yang diprogram menggunakan bahasa pemrograman Python. Mengapa Python? Karena menurut saya pribadi lebih simple, easy-to-use, dan saat ini sudah banyak module/library diluar sana untuk mendukung aplikasi LUNA ini. Berikut adalah beberapa module yang saya gunakan dalam mengembangkan aplikasi ini:

  • SpeechRecognition (3.5.0)
  • PyTTSX (offline mode), or gTTS (online mode)
  • PyAudio (0.2.9)
  • PocketSphinx (0.1.3)

Semua module tersebut diinstall dan dijalankan pada OS Linux Mint-17.3 (Rosa) dan Python 2.7. Baik, agar meng-efisiensikan waktu yang ada, untuk detil cara instalasi masing-masing module bisa disearch di Google.

Sejauh ini, Luna baru bisa menjalankan/merespon beberapa perintah dasar dari saya (misal: greeting, check current time, closed system, dan sebagainya). Saya akan tunjukkan masing-masing code yang sudah saya deploy sampai saat ini dan kedepannya bisa diaplikasikan sesuai dengan keinginan:

  • main.py
#Import Module
import getpass
import time
import sys

#Import Class
from voiceEngine import Voice
from greetingEngine import Greeting

#Banner Motd
welcome = """
\t888       888          888                                         
\t888   o   888          888                                         
\t888  d8b  888          888                                         
\t888 d888b 888  .d88b.  888  .d8888b .d88b.  88888b.d88b.   .d88b.  
\t888d88888b888 d8P  Y8b 888 d88P"   d88""88b 888 "888 "88b d8P  Y8b 
\t88888P Y88888 88888888 888 888     888  888 888  888  888 88888888 
\t8888P   Y8888 Y8b.     888 Y88b.   Y88..88P 888  888  888 Y8b.     
\t888P     Y888  "Y8888  888  "Y8888P "Y88P"  888  888  888  "Y8888  
"""
banner = """
Project Name\t: Luna
Written By\t: MacGyver
Version\t\t: 0.0 (Alpha, 01/21/2017)

This application system is a basic AI for smart-home assistant. It's deployed as 
our personal usage. Access to this system is monitored by administrator, 
unauthorised access is prohibitted. Disconnect IMMEDIATELY if you're not 
authorised personel, OR you'll be reported to local law! 

Developed under GNU/Linux license by Open Source Center. Please visit us at
https://rpoernama.wordpress.com
"""
print(welcome)
print(80*"="+banner)
print(30*" "+"LEARN | CODE | SHARE"+"\n"+"="*80)

#Local Date & Time
tanggal = time.strftime('%B %d, %Y -')
pukul = time.strftime('%l:%M%p %Z')
print 24*" ",tanggal,pukul
print ""

#Login System
#uid = raw_input('Username: ')
#pwd = getpass.getpass('Password: ')

#My LUNA
whoami = """
hi my name is luna. i have been programmed to be a personal assistant. 
i'm still under development. please be patient while i'm still learning.
nice to know you!
"""
suara = Voice()
salam = Greeting()
def luna(in_data):
	if in_data !="":
		if "morning" in in_data:
			salam.pagi()
		elif "afternoon" in in_data:
			salam.siang()
		elif "evening" in in_data:
			salam.sore()
		elif "night" in in_data:
			salam.malam()
		elif "what time" in in_data:
			jam = time.strftime('%l:%M%p local timezone')
			suara.speaking('Current time is: '+jam)
		elif "who are you" in in_data:
			suara.speaking(whoami)
		elif "how are you" in in_data:
			suara.speaking('hi, i\'m fine today. how about you sir?')
		else:
			print(in_data)
			suara.speaking('sorry, i don\'t understand sir.')
	else:
		pass
#Initiate Program
runProgram = raw_input('Do you want to run this program, (yes/no)?: ')
if runProgram.lower() == 'yes':
	time.sleep(1)
	while 1:
		perintah = suara.listening()
		
		if 'exit' in perintah:
			suara.speaking('system exit. good bye!')
			sys.exit(0)
		else:
			luna(perintah)

else:
	pass
  • voiceEngine.py

Ini adalah core engine untuk menerima/merespon perintah berbasis suara.

#Import Module
import speech_recognition as sr
import pyttsx
import pyaudio
from gtts import gTTS
import os
import random

#Create class Voice Comms
class Voice:
	
	#Method for convert text to speech (with PyTTSX - Offline Mode)
	def talking(self, somewords):
		self.talkEngine = pyttsx.init()
		self.talkEngine.setProperty('rate', 150)
		self.talkEngine.say(somewords)
		self.talkEngine.runAndWait()
	
	#Method for convert text to speech (with gTTS - Online Mode)
	def speaking(self, someWords):
		print(someWords)
		#self.langValue = ['en','en-us','en-au']
		self.tts = gTTS(text=someWords, lang='en-us')
		self.tts.save("audiorec.mp3")
		os.system("mpg321 audiorec.mp3")
	
	#Method for convert speech to text (with PocketSphinx - Offline Mode)
	def hearing(self):
		self.recorder = sr.Recognizer()
		with sr.Microphone() as source:
			print("I\'m listening...")
			self.recorder.adjust_for_ambient_noise(source)
			self.audio = self.recorder.listen(source)
		
		self.data = ""
		try:
			self.data = self.recorder.recognize_sphinx(self.audio)
			#self.data = self.recorder.recognize_google(self.audio)
			print("You said: "+self.data)
		except sr.UnknownValueError:
			print("Speech Recognition Engine could not understand audio")
		except sr.RequestError as e:
			print("Speech Recognition Engine error; {0}".format(e))
			
		return self.data

	#Method for convert speech to text (with PocketSphinx - Online Mode)
	def listening(self):
		self.recorder = sr.Recognizer()
		with sr.Microphone() as source:
			print("I\'m listening...")
			self.recorder.adjust_for_ambient_noise(source)
			self.audio = self.recorder.listen(source)
		
		self.data = ""
		try:
			#self.data = self.recorder.recognize_sphinx(self.audio)
			self.data = self.recorder.recognize_google(self.audio)
			print("You said: "+self.data)
		except sr.UnknownValueError:
			print("Speech Recognition Engine could not understand audio")
		except sr.RequestError as e:
			print("Speech Recognition Engine error; {0}".format(e))
			
		return self.data
  • greetingEngine.py

Sedangkan ini adalah salah satu class yang dimiliki Luna untuk mengucapkan salam (greeting) ketika berkomunikasi dengan kita.

#Import Module
import random

#Import Classes
from voiceEngine import Voice

class Greeting:
	
	def __init__(self):
		self.vcGreeting = Voice()
	
	def pagi(self):
		self.morningGreet = ['good morning mr, what\'s your plan today?',
							 'hello sir, good morning. have a nice day!',
							 'hello, morning sir. i hope the best for you today!']
		return self.vcGreeting.speaking(random.choice(self.morningGreet))
	
	def siang(self):
		self.noonGreet = ['good afternoon mr, how are you today?',
						  'afternoon sir, nice to know you!',
						  'hello sir, good afternoon. are you ok today?']
		return self.vcGreeting.speaking(random.choice(self.noonGreet))
		
	def sore(self):
		self.eveningGreet = ['hello mr, good evening!',
							 'good evening sir, may i help you?',
							 'hello, good evening master. can i help you?']
		return self.vcGreeting.speaking(random.choice(self.eveningGreet))
		
	def malam(self):
		self.nightGreet = ['good night sir! have a nice dream.',
							 'hello mr, good night and have a good sleep.',
							 'good night mr! enjoy your rest and see you tomorrow.']
		return self.vcGreeting.speaking(random.choice(self.nightGreet))

Simpan semua code file tersebut dalam satu folder untuk memudahkan akses antar class file. Setelah semua sudah code tersimpan, kita cukup menjalankan perintah berikut pada console (CLI):

macgyver@myweapon:folderPath/LokasiFile> python main.py

Berikut adalah tampilan ketika program dijalankan, lalu saya coba memberikan perintah “exit” melalui microphone:

Demikian sedikit tutorial dan perkenalan singkat saya bersama Luna (My Personal Assistant). Kedepan saya berencana untuk mengaplikasikannya pada mesin Raspberry Pi dan juga sedikit penambahan fitur-fitur untuk membantu kegiatan operasional di rumah. Terimakasih, dan salam Open Source!

Reference:
https://pythonspot.com/en/personal-assistant-jarvis-in-python/

Advertisements