From de4a6145afead5af686e2efcdf3be27934206243 Mon Sep 17 00:00:00 2001
From: Patrick_Pluto <patrick_pluto@noreply.codeberg.org>
Date: Tue, 24 Sep 2024 10:36:12 +0200
Subject: [PATCH] Fixed the Backend and added Voice recognition API

---
 app/components/Settings.tsx |  2 +-
 py/api.py                   | 32 +++++++++++++++++++++++---------
 py/voice.py                 |  4 +++-
 3 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/app/components/Settings.tsx b/app/components/Settings.tsx
index ecc8a34..7875724 100644
--- a/app/components/Settings.tsx
+++ b/app/components/Settings.tsx
@@ -16,7 +16,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (
   const [newEmail, setNewEmail] = useState('');
   const [newPassword, setNewPassword] = useState('');
 
-    // Theme settings state
+  // Theme settings state
   const [backgroundColor, setBackgroundColor] = useState<string>(getComputedStyle(document.documentElement).getPropertyValue('--background-color').trim());
   const [textColor, setTextColor] = useState<string>(getComputedStyle(document.documentElement).getPropertyValue('--text-color').trim());
   const [inputBackgroundColor, setInputBackgroundColor] = useState<string>(getComputedStyle(document.documentElement).getPropertyValue('--input-background-color').trim());
diff --git a/py/api.py b/py/api.py
index 508a9bd..228f567 100644
--- a/py/api.py
+++ b/py/api.py
@@ -4,6 +4,7 @@ import secrets
 import threading
 from ai import AI
 from db import DB
+from voice import VoiceRecognition
 
 
 class API:
@@ -13,18 +14,19 @@ class API:
         self.ai_response = {}
         self.ai = AI()
         self.db = DB()
+        self.voice = VoiceRecognition()
         self.db.load_database()
         self.ai_response_lock = threading.Lock()
         CORS(self.app)
 
     def run(self):
-        @self.app.route('/interstellar/api/ai_create', methods=['GET'])
+        @self.app.route('/interstellar_ai/api/ai_create', methods=['GET'])
         def create_ai():
             access_token = secrets.token_urlsafe(self.crypt_size)
             self.ai_response[access_token] = ""
             return jsonify({'status': 200, 'access_token': access_token})
 
-        @self.app.route('/interstellar/api/ai_send', methods=['POST'])
+        @self.app.route('/interstellar_ai/api/ai_send', methods=['POST'])
         def send_ai():
             data = request.get_json()
             messages = data.get('messages')
@@ -63,27 +65,39 @@ class API:
 
             return jsonify({'status': 401, 'error': 'Invalid AI model type'})
 
-        @self.app.route('/interstellar/api/ai_get', methods=['GET'])
+        @self.app.route('/interstellar_ai/api/ai_get', methods=['GET'])
         def get_ai():
             data = request.args.get('access_token')
             if data not in self.ai_response:
                 return jsonify({'status': 401, 'error': 'Invalid access token'})
             return jsonify({'status': 200, 'response': self.ai_response[data]})
 
-        @self.app.route('/interstellar/api/db', methods=['POST'])
+        @self.app.route('/interstellar_ai/db', methods=['POST'])
         def db_manipulate():
             action = request.args.get('action')
             data = request.args.get('data')
             if action == "create_account":
-                self.db.add_user(data)
+                return jsonify({'status': 200, 'response': self.db.add_user(data)})
             elif action == "change_password":
-                self.db.update_password(data)
+                return jsonify({'status': 200, 'response': self.db.update_password(data)})
             elif action == "get_data":
-                self.db.get_data(data)
+                return jsonify({'status': 200, 'response': self.db.get_data(data)})
             elif action == "change_data":
-                self.db.change_data(data)
+                return jsonify({'status': 200, 'response': self.db.change_data(data)})
             elif action == "check_credentials":
-                self.db.check_credentials(data)
+                return jsonify({'status': 200, 'response': self.db.check_credentials(data)})
+
+            return jsonify({'status': 401, 'response': "Invalid action"})
+
+        @self.app.route('/interstellar_ai/api/voice_recognition', methods=['POST'])
+        def db_manipulate():
+            recognition_type = request.args.get('type')
+            audio = request.args.get('audio_data')
+            option = request.args.get('option')
+            if recognition_type == "basic":
+                return jsonify({'status': 200, 'response': self.voice.basic_recognition(audio, option)})
+
+            return jsonify({'status': 401, 'response': "Invalid type"})
 
         self.app.run(debug=True, host='0.0.0.0', port=5000)
 
diff --git a/py/voice.py b/py/voice.py
index 98ddeba..7ead0a5 100644
--- a/py/voice.py
+++ b/py/voice.py
@@ -7,5 +7,7 @@ class VoiceRecognition:
         r = sr.Recognizer()
         if option == "online":
             return r.recognize_google_cloud(audio)
-        if option == "offline":
+        elif option == "offline":
             return r.recognize_sphinx(audio)
+
+        return False