diff --git a/cli/voice_assistant.py b/cli/voice_assistant.py index b1a0cf1..462da4e 100755 --- a/cli/voice_assistant.py +++ b/cli/voice_assistant.py @@ -15,19 +15,22 @@ def clear(): return os.system('clear') clear() -def main(): - func(listen.listen()) +# def main(): +# func(listen.listen()) def func(command): if "hello" in command: current_time = int(strftime('%H')) if current_time < 12: - bot.bot("Hello, Good morning, this is your voice assistant.") + # bot.bot("Hello, Good morning, this is your voice assistant.") + return ("Hello, Good morning, this is your voice assistant.") elif 12 <= current_time < 16: - bot.bot("Hello, Good afternoon, this is your voice assistant.") + # bot.bot("Hello, Good afternoon, this is your voice assistant.") + return ("Hello, Good afternoon, this is your voice assistant.") else: - bot.bot("Hello, Good evening, this is your voice assistant.") + # bot.bot("Hello, Good evening, this is your voice assistant.") + return ("Hello, Good evening, this is your voice assistant.") elif "who made you" in command: bot.bot("I was developed by The Team SkyDocs.") @@ -134,4 +137,5 @@ def func(command): sys.exit() else: - bot.bot("I am sorry, I am unable to process your request.") + # bot.bot("I am sorry, I am unable to process your request.") + return command diff --git a/voice_assistant_cli.py b/voice_assistant_cli.py deleted file mode 100644 index 7c54474..0000000 --- a/voice_assistant_cli.py +++ /dev/null @@ -1,4 +0,0 @@ -from cli import voice_assistant as vs - -while True: - vs.main() diff --git a/voice_assistant_cloud.py b/voice_assistant_cloud.py new file mode 100644 index 0000000..baf1cfd --- /dev/null +++ b/voice_assistant_cloud.py @@ -0,0 +1,32 @@ +import base64 +from flask import Flask, jsonify, request +from flask_cors import CORS + +from cli import voice_assistant as vs + + +app = Flask("Personalised Voice Assistant") + +CORS(app) + +@app.route('/', methods=['POST']) + +def predict(): + data_ret = request.get_json() + command = data_ret["command"] + command = base64.b64decode(command) + command = str(command, "utf-8") + + # call the main function + response = vs.func(command) + + response = { + "response": response + } + response = jsonify(response) + + return response + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=8080) \ No newline at end of file diff --git a/voice_assistant_front_end.py b/voice_assistant_front_end.py new file mode 100644 index 0000000..0d7e5ec --- /dev/null +++ b/voice_assistant_front_end.py @@ -0,0 +1,53 @@ +import base64 +import requests +import speech_recognition as sr +import pyttsx3 +import json + + +def listen(): + mic = sr.Microphone() + r = sr.Recognizer() + + with mic as source: + audio = r.listen(source, phrase_time_limit = 5) + try: + command = r.recognize_google(audio).lower() + except sr.UnknownValueError: + listen() + return command + +# command = listen() + +command = "hello" +# command = command.encode("utf-8") + +command = base64.b64encode(command.encode("utf-8")) +command = str(command, "utf-8") + +# command = command.decode("utf-8") + +url = "http://127.0.0.1:8080" + +response = requests.post(url,json = {"command":command}) + +response = response.text.strip() +print(response) + +response = json.loads(response) + +print(response['response']) + +voice_id = "english-north" + +engine = pyttsx3.init() +rate = engine.getProperty('rate') +engine.setProperty('rate', 190) +volume = engine.getProperty('volume') +engine.setProperty('volume', 1.0) +sound = engine.getProperty('voices') +engine.setProperty('voice', voice_id) + +for i in str(response).splitlines(): + engine.say(response) +engine.runAndWait() \ No newline at end of file