interstellar_ai/py/api.py

188 lines
8.7 KiB
Python
Raw Permalink Normal View History

2024-10-01 08:18:14 +02:00
from time import sleep
2024-09-27 13:59:27 +02:00
from flask import 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
from voice import VoiceRecognition
2024-09-25 16:34:02 +02:00
from tts import TTS
2024-10-08 16:23:21 +02:00
2024-09-19 09:41:00 +02:00
class API:
def __init__(self):
2024-10-11 10:12:25 +02:00
# Initialize the API class with necessary components and configurations
self.crypt_size = 64 # Size for generating secure tokens
self.app = Flask(__name__) # Create a Flask app instance
self.ai_response = {} # Dictionary to store AI responses keyed by access token
self.ai = AI() # AI processing instance
self.db = DB() # Database instance
self.weather = Weather() # Weather data retrieval instance
self.voice = VoiceRecognition() # Voice recognition instance
self.tts = TTS() # Text-to-Speech instance
self.db.load_database() # Load the database on startup
self.ai_response_lock = threading.Lock() # Lock for thread-safe access to AI responses
CORS(self.app) # Enable Cross-Origin Resource Sharing
2024-09-19 09:41:00 +02:00
def run(self):
2024-10-11 10:12:25 +02:00
# Route to create a new AI session
2024-10-08 16:23:21 +02:00
@self.app.route("/interstellar_ai/api/ai_create", methods=["GET"])
2024-09-19 09:41:00 +02:00
def create_ai():
2024-10-11 10:12:25 +02:00
access_token = secrets.token_urlsafe(self.crypt_size) # Generate a secure access token
2024-09-24 14:18:57 +02:00
2024-10-11 10:12:25 +02:00
# Ensure the access token is unique
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-10-11 10:12:25 +02:00
self.ai_response[access_token] = "" # Initialize the response for the new session
2024-10-08 16:23:21 +02:00
return jsonify({"status": 200, "access_token": access_token})
2024-09-19 09:41:00 +02:00
2024-10-11 10:12:25 +02:00
# Route to send messages to the AI
2024-10-08 16:23:21 +02:00
@self.app.route("/interstellar_ai/api/ai_send", methods=["POST"])
2024-09-19 09:41:00 +02:00
def send_ai():
2024-10-11 10:12:25 +02:00
data = request.get_json() # Get JSON data from the request
messages = data.get("messages") # Extract messages
model_type = data.get("model_type") # Extract model type
ai_model = data.get("ai_model") # Extract AI model name
access_token = data.get("access_token") # Extract access token
2024-10-09 22:04:06 +02:00
2024-10-11 10:12:25 +02:00
print(model_type) # Debugging output
2024-09-19 12:46:52 +02:00
if access_token not in self.ai_response:
2024-10-11 10:12:25 +02:00
return jsonify({"status": 401, "error": "Invalid access token"}) # Token validation
2024-10-11 10:12:25 +02:00
# Start a new thread to process AI requests based on the model type
if model_type == "local":
2024-10-08 16:23:21 +02:00
thread = threading.Thread(
target=self.ai.process_local,
args=(ai_model, messages, self, access_token),
)
2024-09-23 11:01:39 +02:00
thread.start()
2024-10-11 10:12:25 +02:00
thread.join() # Wait for the thread to complete
sleep(0.5) # Sleep for a short duration
2024-10-08 16:23:21 +02:00
return jsonify({"status": 200})
2024-09-23 11:01:39 +02:00
elif model_type == "mistral":
2024-10-11 10:12:25 +02:00
print(model_type) # Debugging output
api_key = data.get("api_key") # Get API key
2024-10-08 16:23:21 +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()
2024-10-01 08:18:14 +02:00
sleep(0.5)
2024-10-08 16:23:21 +02:00
return jsonify({"status": 200})
2024-09-23 11:57:16 +02:00
elif model_type == "openai":
2024-10-11 10:12:25 +02:00
api_key = data.get("api_key") # Get API key
2024-10-08 16:23:21 +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()
2024-10-01 08:18:14 +02:00
sleep(0.5)
2024-10-08 16:23:21 +02:00
return jsonify({"status": 200})
2024-09-23 11:57:16 +02:00
elif model_type == "anthropic":
2024-10-11 10:12:25 +02:00
api_key = data.get("api_key") # Get API key
2024-10-08 16:23:21 +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()
2024-10-01 08:18:14 +02:00
sleep(0.5)
2024-10-08 16:23:21 +02:00
return jsonify({"status": 200})
elif model_type == "google":
2024-10-11 10:12:25 +02:00
api_key = data.get("api_key") # Get API key
2024-10-08 16:23:21 +02:00
thread = threading.Thread(
target=self.ai.process_google,
args=(ai_model, messages, self, access_token, api_key),
)
thread.start()
thread.join()
2024-10-01 08:18:14 +02:00
sleep(0.5)
2024-10-08 16:23:21 +02:00
return jsonify({"status": 200})
2024-09-23 11:01:39 +02:00
2024-10-11 10:12:25 +02:00
return jsonify({"status": 401, "error": "Invalid AI model type"}) # Model type validation
2024-09-19 09:41:00 +02:00
2024-10-11 10:12:25 +02:00
# Route to retrieve AI response based on access token
2024-10-08 16:23:21 +02:00
@self.app.route("/interstellar_ai/api/ai_get", methods=["GET"])
2024-09-19 09:41:00 +02:00
def get_ai():
2024-10-11 10:12:25 +02:00
data = request.args.get("access_token") # Get access token from query parameters
2024-09-19 12:46:52 +02:00
if data not in self.ai_response:
2024-10-11 10:12:25 +02:00
return jsonify({"status": 401, "error": "Invalid access token"}) # Token validation
return jsonify({"status": 200, "response": self.ai_response[data]}) # Return AI response
2024-09-19 09:41:00 +02:00
2024-10-11 10:12:25 +02:00
# Route for database operations
2024-10-08 16:23:21 +02:00
@self.app.route("/interstellar_ai/db", methods=["POST"])
2024-09-20 11:15:42 +02:00
def db_manipulate():
2024-10-11 10:12:25 +02:00
sent_data = request.get_json() # Get JSON data from the request
action = sent_data.get("action") # Extract action type
2024-09-20 11:15:42 +02:00
if action == "create_account":
2024-10-11 10:12:25 +02:00
return jsonify({"status": 200, "response": self.db.add_user(sent_data)}) # Add user
elif action == "change_password":
2024-10-08 16:23:21 +02:00
return jsonify(
2024-10-11 10:12:25 +02:00
{"status": 200, "response": self.db.update_password(sent_data)} # Update password
2024-10-08 16:23:21 +02:00
)
2024-10-02 14:14:20 +02:00
elif action == "get_settings":
2024-10-08 16:23:21 +02:00
return jsonify(
2024-10-11 10:12:25 +02:00
{"status": 200, "response": self.db.get_settings(sent_data)} # Get user settings
2024-10-08 16:23:21 +02:00
)
2024-10-02 14:14:20 +02:00
elif action == "change_settings":
2024-10-08 16:23:21 +02:00
return jsonify(
2024-10-11 10:12:25 +02:00
{"status": 200, "response": self.db.change_settings(sent_data)} # Change user settings
2024-10-08 16:23:21 +02:00
)
2024-10-02 14:14:20 +02:00
elif action == "get_history":
2024-10-08 16:23:21 +02:00
return jsonify(
2024-10-11 10:12:25 +02:00
{"status": 200, "response": self.db.get_history(sent_data)} # Get user history
2024-10-08 16:23:21 +02:00
)
2024-10-02 14:14:20 +02:00
elif action == "change_history":
2024-10-08 16:23:21 +02:00
return jsonify(
2024-10-11 10:12:25 +02:00
{"status": 200, "response": self.db.change_history(sent_data)} # Change user history
2024-10-08 16:23:21 +02:00
)
elif action == "check_credentials":
2024-10-08 16:23:21 +02:00
return jsonify(
2024-10-11 10:12:25 +02:00
{"status": 200, "response": self.db.check_credentials(sent_data)} # Check user credentials
2024-10-08 16:23:21 +02:00
)
2024-09-30 09:35:58 +02:00
elif action == "delete_account":
2024-10-08 16:23:21 +02:00
return jsonify(
2024-10-11 10:12:25 +02:00
{"status": 200, "response": self.db.delete_user(sent_data)} # Delete user account
2024-10-08 16:23:21 +02:00
)
2024-10-01 15:34:43 +02:00
elif action == "get_email":
2024-10-08 16:23:21 +02:00
return jsonify(
2024-10-11 10:12:25 +02:00
{"status": 200, "response": self.db.get_email(sent_data)} # Get user email
2024-10-08 16:23:21 +02:00
)
2024-10-01 15:34:43 +02:00
elif action == "get_name":
2024-10-11 10:12:25 +02:00
return jsonify({"status": 200, "response": self.db.get_name(sent_data)}) # Get user name
2024-10-11 10:12:25 +02:00
return jsonify({"status": 401, "response": "Invalid action"}) # Action validation
2024-10-11 10:12:25 +02:00
# Route for voice recognition
2024-10-08 16:23:21 +02:00
@self.app.route("/interstellar_ai/api/voice_recognition", methods=["POST"])
2024-09-24 16:42:36 +02:00
def voice_recognition():
2024-10-11 10:12:25 +02:00
audio = request.files.get("audio") # Get audio file from request
text = self.voice.recognition(audio) # Perform voice recognition
return jsonify({"status": 200, "response": text}) # Return recognized text
2024-09-20 11:15:42 +02:00
2024-10-11 10:12:25 +02:00
# Route for weather information retrieval
2024-10-08 16:23:21 +02:00
@self.app.route("/interstellar_ai/api/weather", methods=["POST"])
2024-09-24 14:18:57 +02:00
def get_weather():
2024-10-11 10:12:25 +02:00
sent_data = request.get_json() # Get JSON data from the request
unit_type = sent_data.get("unit_type") # Extract unit type (metric, imperial)
city = sent_data.get("city") # Extract city name
weather_data = self.weather.getweather(unit_type, city) # Get weather data
return jsonify({"status": 200, "response": weather_data}) # Return weather data
2024-09-24 14:18:57 +02:00
2024-10-11 10:12:25 +02:00
self.app.run(debug=True, host="0.0.0.0", port=5000) # Start the Flask app
2024-09-24 09:24:31 +02:00
2024-10-11 10:12:25 +02:00
# Route for Text-to-Speech conversion
2024-10-08 16:23:21 +02:00
@self.app.route("/interstellar_ai/api/tts", methods=["POST"])
2024-09-25 16:34:02 +02:00
def tts():
2024-10-11 10:12:25 +02:00
text = request.args.get("text") # Get text from query parameters
return jsonify({"status": 200, "response": self.tts.gen_tts(text)}) # Generate TTS and return response
2024-09-25 16:34:02 +02:00
2024-09-24 09:24:31 +02:00
2024-10-11 10:12:25 +02:00
# Initialize the API class and run the application
2024-09-24 09:24:31 +02:00
api = API()
api.run()