From d2f8d47f5c44baa914217047c76e16d7810bc958 Mon Sep 17 00:00:00 2001 From: YasinOnm08 Date: Mon, 16 Sep 2024 14:28:41 +0200 Subject: [PATCH 1/3] simple_chat, voice_recognition --- py/simple_chat.py | 1 - py/voice_recognition.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 py/voice_recognition.py diff --git a/py/simple_chat.py b/py/simple_chat.py index 0c3165f..879e1f1 100644 --- a/py/simple_chat.py +++ b/py/simple_chat.py @@ -1,6 +1,5 @@ from api import API - class CLIChat: @staticmethod def chat(): diff --git a/py/voice_recognition.py b/py/voice_recognition.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/py/voice_recognition.py @@ -0,0 +1 @@ + -- 2.39.5 From 26013232543eed6045ec60a5b57548a132793918 Mon Sep 17 00:00:00 2001 From: YasinOnm08 Date: Mon, 16 Sep 2024 14:38:45 +0200 Subject: [PATCH 2/3] voice_recognition --- py/voice_recognition.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/py/voice_recognition.py b/py/voice_recognition.py index 8b13789..0ee41d0 100644 --- a/py/voice_recognition.py +++ b/py/voice_recognition.py @@ -1 +1,33 @@ +import speech_recognition as sr + + +class Voice: + @staticmethod + def listen(): + recognizer = sr.Recognizer() + + try: + recognizer = sr.Recognizer() + with sr.Microphone() as source: + print("Adjusting for ambient noise...") + recognizer.adjust_for_ambient_noise(source) + print("Listening...") + audio_data = recognizer.listen(source) + print("Audio captured") + try: + text = recognizer.recognize_sphinx(audio_data) # Using Google Web Speech API + except sr.UnknownValueError: + text = "ERROR" + except sr.RequestError as e: + text = "ERROR" + + except sr.RequestError as e: + text = "ERROR" + except sr.UnknownValueError: + text = "ERROR" + except Exception as e: + text = "ERROR" + return text + print(listen()) + -- 2.39.5 From 8df4abce9b485fd810076dc69b2892db2646276e Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Mon, 16 Sep 2024 14:39:11 +0200 Subject: [PATCH 3/3] wooo, ai chat! --- py/{static => templates}/index.html | 13 ++++++++----- py/web_flask.py | 23 ++++++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) rename py/{static => templates}/index.html (88%) diff --git a/py/static/index.html b/py/templates/index.html similarity index 88% rename from py/static/index.html rename to py/templates/index.html index cef6982..3425d0a 100644 --- a/py/static/index.html +++ b/py/templates/index.html @@ -71,16 +71,19 @@
-
User: What is the weather today?
-
AI: It's sunny with a slight breeze.
-
User: Great! Thank you!
-
AI: You're welcome!
+ {% for message in messages %} + {% if message.startswith('User:') %} +
{{ message }}
+ {% else %} +
{{ message }}
+ {% endif %} + {% endfor %}
- +
diff --git a/py/web_flask.py b/py/web_flask.py index 35223f0..0f160e8 100644 --- a/py/web_flask.py +++ b/py/web_flask.py @@ -1,20 +1,29 @@ -from flask import Flask, request +from flask import Flask, request, render_template +from api import API APP = Flask(__name__) +api = API() +messages = [] @APP.route('/', methods=['GET', 'POST']) def index(): + global messages + if request.method == 'POST': - name = request.form['name'] option = request.form['option'] - if option == "voice": - print(name) - elif option == "chat": - print(name) + user_message = request.form['user_message'] - return APP.send_static_file('index.html') + if option == "voice": + messages.append(f"User: {user_message}") + elif option == "chat": + messages.append(f"User: {user_message}") + + ai_response = "AI: " + api.send_message(user_message, 1) + messages.append(ai_response) + + return render_template('index.html', messages=messages) if __name__ == '__main__': -- 2.39.5