2024-09-26 16:37:18 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from flask i                mport Flask, request, jsonify
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 12:46:52 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from flask_cors import CORS
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import secrets
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:01:39 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import threading
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 09:03:46 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from ai import AI
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 11:15:42 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from db import DB
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 14:18:57 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from weather import Weather
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from voice import VoiceRecognition
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-25 16:34:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								from tts import TTS
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class API:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def __init__(self):
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 14:18:57 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.crypt_size = 4096
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.app = Flask(__name__)
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 12:46:52 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.ai_response = {}
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.ai = AI()
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 11:15:42 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.db = DB()
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 14:18:57 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.weather = Weather()
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.voice = VoiceRecognition()
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-25 16:34:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.tts = TTS()
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 16:06:40 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.db.load_database()
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:01:39 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.ai_response_lock = threading.Lock()
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 12:46:52 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        CORS(self.app)
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def run(self):
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        @self.app.route('/interstellar_ai/api/ai_create', methods=['GET'])
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        def create_ai():
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 14:40:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            access_token = secrets.token_urlsafe(self.crypt_size)
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 14:18:57 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 16:42:36 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            while access_token in self.ai_response:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                access_token = secrets.token_urlsafe(self.crypt_size)
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 14:18:57 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 16:42:36 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self.ai_response[access_token] = ""
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return jsonify({'status': 200, 'access_token': access_token})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        @self.app.route('/interstellar_ai/api/ai_send', methods=['POST'])
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        def send_ai():
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            data = request.get_json()
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 16:24:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            messages = data.get('messages')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 15:46:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            model_type = data.get('model_type')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            ai_model = data.get('ai_model')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            access_token = data.get('access_token')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 12:46:52 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if access_token not in self.ai_response:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 401, 'error': 'Invalid access token'})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 15:46:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if model_type == "local":
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:01:39 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                thread = threading.Thread(target=self.ai.process_local, args=(ai_model, messages, self, access_token))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                thread.start()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                thread.join()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 200})
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            elif model_type == "mistral":
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                api_key = data.get('api_key')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 09:24:31 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                thread = threading.Thread(target=self.ai.process_mistralai,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                          args=(ai_model, messages, self, access_token, api_key))
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:01:39 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                thread.start()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                thread.join()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 200})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:57:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            elif model_type == "openai":
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                api_key = data.get('api_key')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 09:24:31 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                thread = threading.Thread(target=self.ai.process_openai,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                          args=(ai_model, messages, self, access_token, api_key))
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:57:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                thread.start()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                thread.join()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 200})
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            elif model_type == "anthropic":
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                api_key = data.get('api_key')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 09:24:31 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                thread = threading.Thread(target=self.ai.process_anthropic,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                                          args=(ai_model, messages, self, access_token, api_key))
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:57:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                thread.start()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                thread.join()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 200})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:01:39 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return jsonify({'status': 401, 'error': 'Invalid AI model type'})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        @self.app.route('/interstellar_ai/api/ai_get', methods=['GET'])
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        def get_ai():
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            data = request.args.get('access_token')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 12:46:52 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if data not in self.ai_response:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 401, 'error': 'Invalid access token'})
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return jsonify({'status': 200, 'response': self.ai_response[data]})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-19 09:41:00 +02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        @self.app.route('/interstellar_ai/db', methods=['POST'])
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 11:15:42 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        def db_manipulate():
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            action = request.args.get('action')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 11:34:21 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            data = request.args.get('data')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 11:15:42 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if action == "create_account":
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 200, 'response': self.db.add_user(data)})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 15:46:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            elif action == "change_password":
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 200, 'response': self.db.update_password(data)})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 15:46:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            elif action == "get_data":
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 200, 'response': self.db.get_data(data)})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 15:46:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            elif action == "change_data":
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                return jsonify({'status': 200, 'response': self.db.change_data(data)})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 15:46:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            elif action == "check_credentials":
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 10:36:12 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                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'])
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 16:42:36 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        def voice_recognition():
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-26 09:46:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            audio = request.files.get('audio')
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-26 11:01:15 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            text = self.voice.recognition(audio)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return jsonify({'status': 200, 'response': text})
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-20 11:15:42 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 14:18:57 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        @self.app.route('/interstellar_ai/api/weather', methods=['POST'])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        def get_weather():
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            unit_type = request.args.get('unit_type')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            city = request.args.get('city')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return jsonify({'status': 200, 'response': self.weather.getweather(unit_type, city)})
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 11:01:39 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.app.run(debug=True, host='0.0.0.0', port=5000)
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 09:24:31 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-25 16:34:02 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        @self.app.route('/interstellar_ai/api/tts', methods=['POST'])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        def tts():
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            text = request.args.get('text')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            return jsonify({'status': 200, 'response': self.tts.gen_tts(text)})
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-24 09:24:31 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								api = API()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								api.run()
							 |