# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'VoiceAsst.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

import webbrowser
import pyttsx3
import speech_recognition as sr

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(890, 516)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(0, 0, 891, 521))
        self.label.setStyleSheet("background-image: url(:/newPrefix/VoiceA.PNG);")
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap(":/newPrefix/VoiceA.PNG"))
        self.label.setScaledContents(True)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(130, 30, 631, 81))
        self.label_2.setObjectName("label_2")
        self.textBrowser = QtWidgets.QTextBrowser(Form)
        self.textBrowser.setGeometry(QtCore.QRect(40, 370, 330, 50))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.textBrowser.setFont(font)
        self.textBrowser.setObjectName("textBrowser")
        self.textBrowser_2 = QtWidgets.QTextBrowser(Form)
        self.textBrowser_2.setGeometry(QtCore.QRect(40, 440, 330, 50))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.textBrowser_2.setFont(font)
        self.textBrowser_2.setObjectName("textBrowser_2")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(430, 450, 93, 28))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.takeCommand)
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(560, 450, 93, 28))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName("pushButton_2")

        self.retranslateUi(Form)
        self.pushButton_2.clicked.connect(self.textBrowser.clear)
        self.pushButton_2.clicked.connect(self.textBrowser_2.clear)
        QtCore.QMetaObject.connectSlotsByName(Form)

 
    def takeCommand(self,r):

        engine = pyttsx3.init('sapi5')
        voices = engine.getProperty('voices')
        engine.setProperty('voice', voices[0].id)

        def speak(audio):
            engine.say(audio)
            engine.runAndWait()
    
        r = sr.Recognizer()
    
        with sr.Microphone() as source:
            speak("Hello How can I help you sir")
            print("Listening...")
            r.pause_threshold = 1
            audio = r.listen(source)

        try:
            print("Recognizing...")    
            query = r.recognize_google(audio, language='en-in')
            self.textBrowser.setText(query)
            self.commands(query)

        except Exception as e:
            print(e)  
            speak("Unable to recognize your voice please try again")  
            self.textBrowser.setText("Unable to Recognize your voice.") 
            self.textBrowser_2.setText("Please try again !!") 
            return "None"

    def commands(self, query):

        engine = pyttsx3.init('sapi5')
        voices = engine.getProperty('voices')
        engine.setProperty('voice', voices[0].id)

        def speak(audio):
            engine.say(audio)
            engine.runAndWait()
    
        r = sr.Recognizer()
    
        with sr.Microphone() as source:
            r.pause_threshold = 1
            audio = r.listen(source)

        if 'Wikipedia' == query:
            speak("Searching Please Wait")
            webbrowser.open("wikipedia.org")
            self.textBrowser_2.setText("Search Complete")
 
        elif 'open YouTube' == query:
            speak("Searching Please Wait")
            webbrowser.open("youtube.com")
            self.textBrowser_2.setText("Search Complete")
 
        elif 'open Google' == query:
            speak("Searching Please Wait")
            webbrowser.open("google.com")
            self.textBrowser_2.setText("Search Complete")

        elif 'how are you assistant' == query:
            speak("I am fine, What about you Bishwa")
            self.textBrowser_2.setText("I am fine, What about you Bishwa")

        elif 'who is your developer' == query:
            speak("Bishwa is my Developer")
            self.textBrowser_2.setText("Bishwa is my Developer")

        elif 'what is your name' == query:
            speak("My name is Desmond")
            self.textBrowser_2.setText("My name is Desmond")

        elif 'whose assistant are you' == query:
            speak("I am Bishwa's Assistant")
            self.textBrowser_2.setText("I am Bishwa's Assistant")

        elif 'tell the name of my friends' == query:
            speak("Sagar, Jayant and Nikhil")
            self.textBrowser_2.setText("Sagar, Jayant, and Nikhil")
            
        elif 'what do we prefer to play mostly' == query:
            speak("You and you friends mostly play PUBGM")
            self.textBrowser_2.setText("You and you friends mostly play PUBGM")

        elif 'who I love the most' == query:
            speak("You Love your Family Most")
            self.textBrowser_2.setText("You Love your Family Most")

        elif 'tell me which game I like the most' == query:
            speak("You like Assassin's Creed")
            self.textBrowser_2.setText("Assassin's Creed")

        elif 'what is my full name' == query:
            speak("Mister Bishwa Pratap Behera")
            self.textBrowser_2.setText("Mr. Bishwa Pratap Behera")

        elif 'what is my father name' == query:
            speak("Mister Bhagaban Behera")
            self.textBrowser_2.setText("Mr. Bhagaban Behera")

        elif 'what is my mother name' == query:
            speak("Misses Mansi Rani Behera")
            self.textBrowser_2.setText("Mrs. Mansi Rani Behera")

        elif 'what is my sister name' == query:
            speak("Miss Rajeshwary Behera")
            self.textBrowser_2.setText("Ms. Rajeshwary Behera")

        elif 'what is my favourite sport' == query:
            speak("football is your favourite sport")
            self.textBrowser_2.setText("Football")

        elif 'very good Desmond' == query:
            speak("Thankyou Bishwa")
            self.textBrowser_2.setText("Thank you")
 
        elif 'exit' == query:
            exit()

    
    
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label_2.setText(_translate("Form", "<html><head/><body><p align=\"center\"><span style=\" font-size:22pt; font-weight:600; color:#00ffff;\">Voice Assistant</span></p></body></html>"))
        self.pushButton.setText(_translate("Form", "Mic"))
        self.pushButton_2.setText(_translate("Form", "Reset"))

import Voice

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())
