From 1fe4a6dafe768ed0e92cde02443532217903ffee Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Mon, 16 Sep 2024 14:24:22 +0200 Subject: [PATCH 1/3] Backend more --- py/static/index.html | 10 +++++----- py/web_flask.py | 27 +++++++++++++++++---------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/py/static/index.html b/py/static/index.html index d688702..cef6982 100644 --- a/py/static/index.html +++ b/py/static/index.html @@ -79,11 +79,11 @@ -
- - - -
+
+ + + +
diff --git a/py/web_flask.py b/py/web_flask.py index ce616ed..35223f0 100644 --- a/py/web_flask.py +++ b/py/web_flask.py @@ -1,14 +1,21 @@ -from flask import Flask, send_from_directory +from flask import Flask, request + +APP = Flask(__name__) -class WebHost: - @staticmethod - def main_page(): - app = Flask(__name__) +@APP.route('/', methods=['GET', 'POST']) +def index(): + if request.method == 'POST': + name = request.form['name'] + option = request.form['option'] - @app.route('/') - def index(): - return app.send_static_file('index.html') + if option == "voice": + print(name) + elif option == "chat": + print(name) - if __name__ == '__main__': - app.run(debug=True) + return APP.send_static_file('index.html') + + +if __name__ == '__main__': + APP.run(debug=True) \ No newline at end of file From 8df4abce9b485fd810076dc69b2892db2646276e Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Mon, 16 Sep 2024 14:39:11 +0200 Subject: [PATCH 2/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__': From 72538174b297f5407165e53ebf1808705a3d9025 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Mon, 16 Sep 2024 15:14:24 +0200 Subject: [PATCH 3/3] Fixed embeds --- py/api.py | 3 +++ py/venv.sh | 3 +++ py/web_flask.py | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/py/api.py b/py/api.py index 413f951..d3db341 100644 --- a/py/api.py +++ b/py/api.py @@ -4,6 +4,7 @@ from transformers import AutoTokenizer, LlamaForCausalLM class API: + # This method processes a message via transformers. (NOT FINISHED!) @staticmethod def process_text_transformers(prompt, model): model = LlamaForCausalLM.from_pretrained(model) @@ -14,6 +15,7 @@ class API: generate_ids = model.generate(inputs.input_ids, max_length=30) return tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] + # This method processes a message via ollama @staticmethod def process_text_local(prompt, model): ollama_url = "http://localhost:11434" @@ -34,6 +36,7 @@ class API: else: return "Error: " + response.text + # This method sends a message to a certain AI. def send_message(self, message, model): if model == 1: answer = self.process_text_local(message, "phi3.5") diff --git a/py/venv.sh b/py/venv.sh index f710380..3d70f23 100755 --- a/py/venv.sh +++ b/py/venv.sh @@ -5,3 +5,6 @@ source venv/bin/activate pip install transformers pip install torch pip install flask +pip install SpeechRecognition +pip install pyaudio +pip install pocketsphinx diff --git a/py/web_flask.py b/py/web_flask.py index 0f160e8..cf200dc 100644 --- a/py/web_flask.py +++ b/py/web_flask.py @@ -1,11 +1,15 @@ from flask import Flask, request, render_template from api import API +from voice_recognition import Voice APP = Flask(__name__) api = API() +voice = Voice() + messages = [] +# The following method shows the user the GUI and does the backend connections to the API. @APP.route('/', methods=['GET', 'POST']) def index(): global messages @@ -16,6 +20,7 @@ def index(): user_message = request.form['user_message'] if option == "voice": + user_message = voice.listen() messages.append(f"User: {user_message}") elif option == "chat": messages.append(f"User: {user_message}")