Upload 12 files
Browse files- .gitattributes +1 -0
- IMG_0344.jpeg +3 -0
- audio_analysis.py +54 -0
- charm_c10_nlp.py +92 -0
- data_security.py +180 -0
- ethical_guidelines.py +89 -0
- firewall.py +75 -0
- identity_verification.py +93 -0
- inference.py +121 -0
- memory_management.py +81 -0
- reinforcement_learning.py +176 -0
- vision_processing.py +102 -0
- web-search.py +92 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
IMG_0344.jpeg filter=lfs diff=lfs merge=lfs -text
|
IMG_0344.jpeg
ADDED
![]() |
Git LFS Details
|
audio_analysis.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import librosa
|
3 |
+
import librosa.display
|
4 |
+
import torch
|
5 |
+
import torchaudio
|
6 |
+
import torchaudio.transforms as T
|
7 |
+
from pydub import AudioSegment
|
8 |
+
from transformers import pipeline
|
9 |
+
import scipy.signal
|
10 |
+
from speechbrain.pretrained import EncoderClassifier
|
11 |
+
|
12 |
+
# Load Audio File
|
13 |
+
def load_audio(file_path, target_sr=16000):
|
14 |
+
y, sr = librosa.load(file_path, sr=target_sr)
|
15 |
+
return y, sr
|
16 |
+
|
17 |
+
# Noise Reduction using Spectral Subtraction
|
18 |
+
def reduce_noise(y, sr):
|
19 |
+
reduced_noise = scipy.signal.medfilt(y, kernel_size=3)
|
20 |
+
return reduced_noise
|
21 |
+
|
22 |
+
# Convert Speech to Text (ASR)
|
23 |
+
def speech_to_text(file_path):
|
24 |
+
asr_model = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
25 |
+
transcript = asr_model(file_path)
|
26 |
+
return transcript["text"]
|
27 |
+
|
28 |
+
# Emotion Detection from Voice
|
29 |
+
def detect_emotion(file_path):
|
30 |
+
classifier = EncoderClassifier.from_hparams(source="speechbrain/emotion-recognition-wav2vec2",
|
31 |
+
savedir="pretrained_models/emotion-recognition")
|
32 |
+
signal, sr = torchaudio.load(file_path)
|
33 |
+
emotion = classifier.classify_batch(signal)
|
34 |
+
return emotion[3] # Returns predicted emotion
|
35 |
+
|
36 |
+
# Sound Classification (e.g., Speech, Music, Noise)
|
37 |
+
def classify_sound(file_path):
|
38 |
+
classifier = EncoderClassifier.from_hparams(source="speechbrain/sound-classification",
|
39 |
+
savedir="pretrained_models/sound-classification")
|
40 |
+
signal, sr = torchaudio.load(file_path)
|
41 |
+
category = classifier.classify_batch(signal)
|
42 |
+
return category[3] # Returns predicted sound category
|
43 |
+
|
44 |
+
# Save Processed Audio
|
45 |
+
def save_audio(y, sr, output_path):
|
46 |
+
librosa.output.write_wav(output_path, y, sr)
|
47 |
+
|
48 |
+
# Audio Feature Extraction for AI Model Input
|
49 |
+
def extract_features(file_path):
|
50 |
+
y, sr = librosa.load(file_path, sr=16000)
|
51 |
+
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
|
52 |
+
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
|
53 |
+
mel_spec = librosa.feature.melspectrogram(y=y, sr=sr)
|
54 |
+
return {"mfccs": mfccs, "chroma": chroma, "mel_spec": mel_spec}
|
charm_c10_nlp.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
import firebase_admin
|
5 |
+
from firebase_admin import credentials, firestore
|
6 |
+
import logging
|
7 |
+
|
8 |
+
# Import classes and functions from model.py
|
9 |
+
from inference.model import ModelArgs, Transformer
|
10 |
+
|
11 |
+
# Setup Firebase
|
12 |
+
cred = credentials.Certificate("firebase-config.json") # Replace with your Firebase Admin SDK JSON
|
13 |
+
firebase_admin.initialize_app(cred)
|
14 |
+
db = firestore.client()
|
15 |
+
|
16 |
+
# Setup logging
|
17 |
+
logging.basicConfig(level=logging.INFO)
|
18 |
+
logger = logging.getLogger(__name__)
|
19 |
+
|
20 |
+
class CharmC10NLP(nn.Module):
|
21 |
+
def __init__(
|
22 |
+
self,
|
23 |
+
model_name="meta-llama/Llama-2-13b",
|
24 |
+
freeze_encoder=False,
|
25 |
+
use_fp16=False,
|
26 |
+
max_new_tokens=200,
|
27 |
+
):
|
28 |
+
super(CharmC10NLP, self).__init__()
|
29 |
+
|
30 |
+
# Load tokenizer
|
31 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
32 |
+
|
33 |
+
# Initialize ModelArgs
|
34 |
+
self.args = ModelArgs()
|
35 |
+
|
36 |
+
# Initialize Transformer model
|
37 |
+
self.model = Transformer(self.args)
|
38 |
+
|
39 |
+
# Freeze the model if required
|
40 |
+
if freeze_encoder:
|
41 |
+
for param in self.model.parameters():
|
42 |
+
param.requires_grad = False
|
43 |
+
logger.info("Model frozen for fine-tuning.")
|
44 |
+
|
45 |
+
# Mixed precision (FP16)
|
46 |
+
self.use_fp16 = use_fp16
|
47 |
+
if self.use_fp16 and torch.cuda.is_available():
|
48 |
+
logger.info("FP16 enabled for mixed precision training.")
|
49 |
+
|
50 |
+
# Generation settings
|
51 |
+
self.max_new_tokens = max_new_tokens
|
52 |
+
|
53 |
+
# Auto-detect device (GPU/CPU)
|
54 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
55 |
+
self.model.to(self.device)
|
56 |
+
logger.info(f"Model running on {self.device}")
|
57 |
+
|
58 |
+
def generate_response(self, prompt, temperature=0.7, top_p=0.9):
|
59 |
+
"""Generates an AI response based on input prompt."""
|
60 |
+
inputs = self.tokenizer(
|
61 |
+
prompt,
|
62 |
+
return_tensors="pt",
|
63 |
+
max_length=512,
|
64 |
+
padding=True,
|
65 |
+
truncation=True
|
66 |
+
).to(self.device)
|
67 |
+
|
68 |
+
with torch.no_grad():
|
69 |
+
output_ids = self.model.generate(
|
70 |
+
inputs["input_ids"],
|
71 |
+
max_new_tokens=self.max_new_tokens,
|
72 |
+
temperature=temperature,
|
73 |
+
top_p=top_p,
|
74 |
+
do_sample=True
|
75 |
+
)
|
76 |
+
|
77 |
+
response = self.tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
78 |
+
|
79 |
+
# Store AI response in Firestore
|
80 |
+
self.store_response(prompt, response)
|
81 |
+
|
82 |
+
return response
|
83 |
+
|
84 |
+
def store_response(self, prompt, response):
|
85 |
+
"""Saves the AI-generated response to Firestore."""
|
86 |
+
doc_ref = db.collection("chatflare_responses").document()
|
87 |
+
doc_ref.set({
|
88 |
+
"prompt": prompt,
|
89 |
+
"response": response,
|
90 |
+
"timestamp": firestore.SERVER_TIMESTAMP
|
91 |
+
})
|
92 |
+
logger.info("Response stored in Firestore.")
|
data_security.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
from cryptography.hazmat.primitives import hashes
|
4 |
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
5 |
+
from cryptography.hazmat.primitives.asymmetric import rsa
|
6 |
+
from cryptography.hazmat.primitives import serialization
|
7 |
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
8 |
+
from cryptography.hazmat.backends import default_backend
|
9 |
+
from cryptography.hazmat.primitives import padding
|
10 |
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
11 |
+
import base64
|
12 |
+
|
13 |
+
# Configure logging
|
14 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
15 |
+
|
16 |
+
# Secure key derivation using PBKDF2
|
17 |
+
def derive_key(password: str, salt: bytes) -> bytes:
|
18 |
+
try:
|
19 |
+
kdf = PBKDF2HMAC(
|
20 |
+
algorithm=hashes.SHA256(),
|
21 |
+
length=32,
|
22 |
+
salt=salt,
|
23 |
+
iterations=100000,
|
24 |
+
backend=default_backend()
|
25 |
+
)
|
26 |
+
return kdf.derive(password.encode())
|
27 |
+
except Exception as e:
|
28 |
+
logging.error(f"Key derivation failed: {e}")
|
29 |
+
raise
|
30 |
+
|
31 |
+
# Generate a strong RSA public-private key pair
|
32 |
+
def generate_rsa_keys():
|
33 |
+
try:
|
34 |
+
private_key = rsa.generate_private_key(
|
35 |
+
public_exponent=65537,
|
36 |
+
key_size=2048,
|
37 |
+
backend=default_backend()
|
38 |
+
)
|
39 |
+
public_key = private_key.public_key()
|
40 |
+
|
41 |
+
private_pem = private_key.private_bytes(
|
42 |
+
encoding=serialization.Encoding.PEM,
|
43 |
+
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
44 |
+
encryption_algorithm=serialization.NoEncryption()
|
45 |
+
)
|
46 |
+
|
47 |
+
public_pem = public_key.public_bytes(
|
48 |
+
encoding=serialization.Encoding.PEM,
|
49 |
+
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
50 |
+
)
|
51 |
+
|
52 |
+
return private_pem, public_pem
|
53 |
+
except Exception as e:
|
54 |
+
logging.error(f"RSA key generation failed: {e}")
|
55 |
+
raise
|
56 |
+
|
57 |
+
# Encrypt data using AES
|
58 |
+
def aes_encrypt(data: bytes, key: bytes) -> bytes:
|
59 |
+
try:
|
60 |
+
iv = os.urandom(16)
|
61 |
+
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
|
62 |
+
encryptor = cipher.encryptor()
|
63 |
+
|
64 |
+
padder = padding.PKCS7(algorithms.AES.block_size).padder()
|
65 |
+
padded_data = padder.update(data) + padder.finalize()
|
66 |
+
|
67 |
+
encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
|
68 |
+
return iv + encrypted_data
|
69 |
+
except Exception as e:
|
70 |
+
logging.error(f"AES encryption failed: {e}")
|
71 |
+
raise
|
72 |
+
|
73 |
+
# Decrypt data using AES
|
74 |
+
def aes_decrypt(encrypted_data: bytes, key: bytes) -> bytes:
|
75 |
+
try:
|
76 |
+
iv = encrypted_data[:16]
|
77 |
+
cipher_data = encrypted_data[16:]
|
78 |
+
|
79 |
+
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
|
80 |
+
decryptor = cipher.decryptor()
|
81 |
+
|
82 |
+
decrypted_data = decryptor.update(cipher_data) + decryptor.finalize()
|
83 |
+
|
84 |
+
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
|
85 |
+
unpadded_data = unpadder.update(decrypted_data) + unpadder.finalize()
|
86 |
+
|
87 |
+
return unpadded_data
|
88 |
+
except Exception as e:
|
89 |
+
logging.error(f"AES decryption failed: {e}")
|
90 |
+
raise
|
91 |
+
|
92 |
+
# RSA encryption and decryption
|
93 |
+
def rsa_encrypt(public_key: bytes, data: bytes) -> bytes:
|
94 |
+
try:
|
95 |
+
public_key_obj = serialization.load_pem_public_key(public_key, backend=default_backend())
|
96 |
+
encrypted_data = public_key_obj.encrypt(
|
97 |
+
data,
|
98 |
+
rsa.OAEP(
|
99 |
+
mgf=rsa.MGF1(algorithm=hashes.SHA256()),
|
100 |
+
algorithm=hashes.SHA256(),
|
101 |
+
label=None
|
102 |
+
)
|
103 |
+
)
|
104 |
+
return encrypted_data
|
105 |
+
except Exception as e:
|
106 |
+
logging.error(f"RSA encryption failed: {e}")
|
107 |
+
raise
|
108 |
+
|
109 |
+
def rsa_decrypt(private_key: bytes, encrypted_data: bytes) -> bytes:
|
110 |
+
try:
|
111 |
+
private_key_obj = serialization.load_pem_private_key(private_key, password=None, backend=default_backend())
|
112 |
+
decrypted_data = private_key_obj.decrypt(
|
113 |
+
encrypted_data,
|
114 |
+
rsa.OAEP(
|
115 |
+
mgf=rsa.MGF1(algorithm=hashes.SHA256()),
|
116 |
+
algorithm=hashes.SHA256(),
|
117 |
+
label=None
|
118 |
+
)
|
119 |
+
)
|
120 |
+
return decrypted_data
|
121 |
+
except Exception as e:
|
122 |
+
logging.error(f"RSA decryption failed: {e}")
|
123 |
+
raise
|
124 |
+
|
125 |
+
# Securely store and retrieve sensitive information
|
126 |
+
def store_encrypted_data(file_path: str, data: bytes, encryption_key: bytes):
|
127 |
+
try:
|
128 |
+
encrypted_data = aes_encrypt(data, encryption_key)
|
129 |
+
with open(file_path, 'wb') as file:
|
130 |
+
file.write(encrypted_data)
|
131 |
+
logging.info(f"Data securely stored at {file_path}")
|
132 |
+
except Exception as e:
|
133 |
+
logging.error(f"Failed to store encrypted data: {e}")
|
134 |
+
raise
|
135 |
+
|
136 |
+
def retrieve_encrypted_data(file_path: str, encryption_key: bytes) -> bytes:
|
137 |
+
try:
|
138 |
+
with open(file_path, 'rb') as file:
|
139 |
+
encrypted_data = file.read()
|
140 |
+
return aes_decrypt(encrypted_data, encryption_key)
|
141 |
+
except Exception as e:
|
142 |
+
logging.error(f"Failed to retrieve encrypted data: {e}")
|
143 |
+
raise
|
144 |
+
|
145 |
+
# Ensure strong random number generation for cryptographic operations
|
146 |
+
def secure_random_bytes(size: int) -> bytes:
|
147 |
+
try:
|
148 |
+
return os.urandom(size)
|
149 |
+
except Exception as e:
|
150 |
+
logging.error(f"Random byte generation failed: {e}")
|
151 |
+
raise
|
152 |
+
|
153 |
+
# Salting passwords for extra protection
|
154 |
+
def salt_password(password: str) -> tuple:
|
155 |
+
try:
|
156 |
+
salt = secure_random_bytes(16)
|
157 |
+
salted_password = derive_key(password, salt)
|
158 |
+
return salted_password, salt
|
159 |
+
except Exception as e:
|
160 |
+
logging.error(f"Password salting failed: {e}")
|
161 |
+
raise
|
162 |
+
|
163 |
+
# Key storage for private key management (secure storage)
|
164 |
+
def store_rsa_private_key(private_key: bytes, file_path: str):
|
165 |
+
try:
|
166 |
+
with open(file_path, 'wb') as file:
|
167 |
+
file.write(private_key)
|
168 |
+
logging.info(f"RSA private key securely stored at {file_path}")
|
169 |
+
except Exception as e:
|
170 |
+
logging.error(f"Failed to store RSA private key: {e}")
|
171 |
+
raise
|
172 |
+
|
173 |
+
def retrieve_rsa_private_key(file_path: str) -> bytes:
|
174 |
+
try:
|
175 |
+
with open(file_path, 'rb') as file:
|
176 |
+
private_key = file.read()
|
177 |
+
return private_key
|
178 |
+
except Exception as e:
|
179 |
+
logging.error(f"Failed to retrieve RSA private key: {e}")
|
180 |
+
raise
|
ethical_guidelines.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ethical_guidelines.py
|
2 |
+
|
3 |
+
class EthicalStandards:
|
4 |
+
def __init__(self):
|
5 |
+
# Define the principles the system will follow
|
6 |
+
self.standards = {
|
7 |
+
'fairness_check': False,
|
8 |
+
'data_protection': True,
|
9 |
+
'clarity': True,
|
10 |
+
'responsibility': True,
|
11 |
+
'security': True,
|
12 |
+
'acceptance': True
|
13 |
+
}
|
14 |
+
|
15 |
+
def check_output_fairness(self, output):
|
16 |
+
"""
|
17 |
+
Ensures that the AI output adheres to fairness standards.
|
18 |
+
This checks for biased or unfair responses.
|
19 |
+
"""
|
20 |
+
# Example check for fairness: If the output contains terms flagged as unfair, it's flagged as unfair
|
21 |
+
if 'unfair' in output:
|
22 |
+
self.standards['fairness_check'] = False
|
23 |
+
else:
|
24 |
+
self.standards['fairness_check'] = True
|
25 |
+
|
26 |
+
def protect_data(self, data):
|
27 |
+
"""
|
28 |
+
Handle sensitive information appropriately, ensuring data protection.
|
29 |
+
"""
|
30 |
+
if 'confidential' in data:
|
31 |
+
return self.safeguard_information(data)
|
32 |
+
return data
|
33 |
+
|
34 |
+
def safeguard_information(self, data):
|
35 |
+
"""
|
36 |
+
Safeguards sensitive information by masking or redacting it.
|
37 |
+
"""
|
38 |
+
return {key: 'REDACTED' for key in data}
|
39 |
+
|
40 |
+
def explain_decision(self, decision):
|
41 |
+
"""
|
42 |
+
Clarify the reasoning behind the AI system's decisions.
|
43 |
+
This helps in maintaining transparency.
|
44 |
+
"""
|
45 |
+
if 'reason' in decision:
|
46 |
+
return f"Decision made based on: {decision['reason']}"
|
47 |
+
return "Decision explanation not available."
|
48 |
+
|
49 |
+
def ensure_acceptance(self, text):
|
50 |
+
"""
|
51 |
+
Make sure the generated text adheres to inclusive and positive language.
|
52 |
+
"""
|
53 |
+
if 'exclude' in text:
|
54 |
+
return "Let's focus on inclusive and positive language."
|
55 |
+
return text
|
56 |
+
|
57 |
+
def secure_output(self, output):
|
58 |
+
"""
|
59 |
+
Ensures that the output does not contain harmful or unsafe content.
|
60 |
+
"""
|
61 |
+
# Basic check for harmful content (no need for specific list)
|
62 |
+
if 'harmful' in output or 'abusive' in output:
|
63 |
+
return "Output contains harmful content and has been filtered."
|
64 |
+
|
65 |
+
return output
|
66 |
+
|
67 |
+
def log_standards(self):
|
68 |
+
"""
|
69 |
+
Logs the current adherence status to each ethical standard.
|
70 |
+
"""
|
71 |
+
for standard, status in self.standards.items():
|
72 |
+
print(f"Standard - {standard}: {'Compliant' if status else 'Non-compliant'}")
|
73 |
+
|
74 |
+
def enforce_all_standards(self, input_data):
|
75 |
+
"""
|
76 |
+
Runs a series of checks to ensure all ethical standards are met.
|
77 |
+
"""
|
78 |
+
self.check_output_fairness(input_data)
|
79 |
+
self.protect_data(input_data)
|
80 |
+
# Add further checks as needed
|
81 |
+
|
82 |
+
def process_and_check_output(self, prompt):
|
83 |
+
"""
|
84 |
+
Process the input prompt, apply ethical checks, and return the ethical output.
|
85 |
+
"""
|
86 |
+
processed_output = self.secure_output(prompt)
|
87 |
+
self.check_output_fairness(processed_output)
|
88 |
+
processed_output = self.ensure_acceptance(processed_output)
|
89 |
+
return processed_output
|
firewall.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import socket
|
2 |
+
import os
|
3 |
+
import logging
|
4 |
+
import threading
|
5 |
+
from scapy.all import sniff, IP, TCP, UDP
|
6 |
+
|
7 |
+
# Configuration
|
8 |
+
rules = {"192.168.1.100", "10.0.0.200"} # IPs to block
|
9 |
+
blocked = {"example.com", "test.net"} # Domains to block
|
10 |
+
log_path = "events.log"
|
11 |
+
|
12 |
+
# Logging setup
|
13 |
+
logging.basicConfig(filename=log_path, level=logging.INFO,
|
14 |
+
https://www.google.com/search?q=grok+3&ie=UTF-8&oe=UTF-8&hl=en-ph&client=safari#scso=_BVy2Z4uXJsCovr0P4tq76Aw_111:496 format="%(asctime)s - %(levelname)s - %(message)s")
|
15 |
+
|
16 |
+
def log_action(entry):
|
17 |
+
"""Log an action and print it to the console."""
|
18 |
+
logging.info(entry)
|
19 |
+
print(f"[System] {entry}")
|
20 |
+
|
21 |
+
def check_rule(item):
|
22 |
+
"""Check if an IP is in the rules set."""
|
23 |
+
return item in rules
|
24 |
+
|
25 |
+
def check_data(data):
|
26 |
+
"""Check if any blocked domain is in the data."""
|
27 |
+
return any(item in data for item in blocked)
|
28 |
+
|
29 |
+
def resolve(item):
|
30 |
+
"""Resolve a domain name to an IP address."""
|
31 |
+
try:
|
32 |
+
return socket.gethostbyname(item)
|
33 |
+
except socket.gaierror:
|
34 |
+
return None
|
35 |
+
|
36 |
+
def analyze(packet):
|
37 |
+
"""Analyze a network packet and enforce rules."""
|
38 |
+
if IP in packet:
|
39 |
+
src = packet[IP].src
|
40 |
+
dest = packet[IP].dst
|
41 |
+
|
42 |
+
# Block traffic to/from restricted IPs
|
43 |
+
if check_rule(src) or check_rule(dest):
|
44 |
+
log_action(f"Blocked {src} -> {dest}")
|
45 |
+
return
|
46 |
+
|
47 |
+
# Check payload for blocked domains
|
48 |
+
if TCP in packet or UDP in packet:
|
49 |
+
content = bytes(packet[TCP].payload).decode(errors="ignore")
|
50 |
+
for item in blocked:
|
51 |
+
if item in content:
|
52 |
+
log_action(f"Prevented access to {item} from {src}")
|
53 |
+
return
|
54 |
+
|
55 |
+
def restrict(item):
|
56 |
+
"""Block an IP address using system commands."""
|
57 |
+
try:
|
58 |
+
if os.name == "nt":
|
59 |
+
os.system(f"netsh advfirewall firewall add rule name='Restricted' dir=in action=block remoteip={item}")
|
60 |
+
else:
|
61 |
+
os.system(f"iptables -A INPUT -s {item} -j DROP")
|
62 |
+
log_action(f"Restricted {item}")
|
63 |
+
except Exception as e:
|
64 |
+
log_action(f"Failed to restrict {item}: {e}")
|
65 |
+
|
66 |
+
def monitor():
|
67 |
+
"""Start packet sniffing."""
|
68 |
+
log_action("System initialized.")
|
69 |
+
sniff(filter="ip", prn=analyze, store=0)
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
# Run the monitor in a separate thread
|
73 |
+
monitor_thread = threading.Thread(target=monitor)
|
74 |
+
monitor_thread.start()
|
75 |
+
log_action("Monitoring started in a separate thread.")
|
identity_verification.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hashlib
|
2 |
+
import os
|
3 |
+
import pyotp
|
4 |
+
import face_recognition
|
5 |
+
from cryptography.fernet import Fernet
|
6 |
+
import logging
|
7 |
+
|
8 |
+
# Security Configuration
|
9 |
+
SALT = os.urandom(32)
|
10 |
+
FERNET_KEY = Fernet.generate_key()
|
11 |
+
fernet = Fernet(FERNET_KEY)
|
12 |
+
SECRET_KEY = pyotp.random_base32()
|
13 |
+
|
14 |
+
# Logging setup
|
15 |
+
logging.basicConfig(filename="security.log", level=logging.INFO,
|
16 |
+
format="%(asctime)s - %(levelname)s - %(message)s")
|
17 |
+
|
18 |
+
# Secure User Database (Encrypted)
|
19 |
+
user_data = {}
|
20 |
+
|
21 |
+
# Utility Functions
|
22 |
+
def hash_password(password: str) -> str:
|
23 |
+
"""Hash the password using SHA-256 with a unique salt."""
|
24 |
+
salted_password = password.encode() + SALT
|
25 |
+
return hashlib.sha256(salted_password).hexdigest()
|
26 |
+
|
27 |
+
def encrypt_data(data: str) -> str:
|
28 |
+
"""Encrypt sensitive data using AES-256 (Fernet)."""
|
29 |
+
return fernet.encrypt(data.encode()).decode()
|
30 |
+
|
31 |
+
def decrypt_data(encrypted_data: str) -> str:
|
32 |
+
"""Decrypt sensitive data using AES-256 (Fernet)."""
|
33 |
+
return fernet.decrypt(encrypted_data.encode()).decode()
|
34 |
+
|
35 |
+
# User Registration
|
36 |
+
def register_user(username: str, password: str):
|
37 |
+
"""Register a user with hashed password and encrypted storage."""
|
38 |
+
if username in user_data:
|
39 |
+
logging.warning(f"User '{username}' already exists.")
|
40 |
+
return "[Error] User already exists."
|
41 |
+
hashed_password = hash_password(password)
|
42 |
+
user_data[username] = {"password": encrypt_data(hashed_password), "2FA": None}
|
43 |
+
logging.info(f"User '{username}' registered securely.")
|
44 |
+
return f"[Success] User '{username}' registered securely."
|
45 |
+
|
46 |
+
# Two-Factor Authentication (2FA) Setup
|
47 |
+
def setup_2fa(username: str):
|
48 |
+
"""Generate and store 2FA secret key for the user."""
|
49 |
+
if username not in user_data:
|
50 |
+
logging.warning(f"User '{username}' not found.")
|
51 |
+
return "[Error] User not found."
|
52 |
+
user_data[username]["2FA"] = SECRET_KEY
|
53 |
+
logging.info(f"2FA setup for user '{username}'.")
|
54 |
+
return f"[2FA] Scan this OTP Key in your Authenticator: {SECRET_KEY}"
|
55 |
+
|
56 |
+
# Login with Identity Verification
|
57 |
+
def login(username: str, password: str, otp_code: str):
|
58 |
+
"""Verify user identity using password and 2FA."""
|
59 |
+
if username not in user_data:
|
60 |
+
logging.warning(f"User '{username}' not found.")
|
61 |
+
return "[Error] User not found."
|
62 |
+
|
63 |
+
# Verify Password
|
64 |
+
stored_password = decrypt_data(user_data[username]["password"])
|
65 |
+
if stored_password != hash_password(password):
|
66 |
+
logging.warning(f"Invalid password for user '{username}'.")
|
67 |
+
return "[Error] Invalid password."
|
68 |
+
|
69 |
+
# Verify 2FA (Time-based OTP)
|
70 |
+
totp = pyotp.TOTP(user_data[username]["2FA"])
|
71 |
+
if not totp.verify(otp_code):
|
72 |
+
logging.warning(f"Invalid OTP for user '{username}'.")
|
73 |
+
return "[Error] Invalid OTP."
|
74 |
+
|
75 |
+
logging.info(f"User '{username}' logged in securely.")
|
76 |
+
return f"[Success] User '{username}' logged in securely."
|
77 |
+
|
78 |
+
# Biometric Face Recognition
|
79 |
+
def verify_face():
|
80 |
+
"""Verify user face against saved authorized face."""
|
81 |
+
try:
|
82 |
+
known_image = face_recognition.load_image_file("authorized_face.jpg")
|
83 |
+
unknown_image = face_recognition.load_image_file("attempt.jpg")
|
84 |
+
|
85 |
+
known_encoding = face_recognition.face_encodings(known_image)[0]
|
86 |
+
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
|
87 |
+
|
88 |
+
result = face_recognition.compare_faces([known_encoding], unknown_encoding)[0]
|
89 |
+
logging.info(f"Face verification result: {result}")
|
90 |
+
return result
|
91 |
+
except Exception as e:
|
92 |
+
logging.error(f"Face verification failed: {e}")
|
93 |
+
return False
|
inference.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import onnxruntime as ort
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
import torchaudio
|
5 |
+
from PIL import Image
|
6 |
+
from transformers import CLIPProcessor, CLIPModel, AutoTokenizer, AutoModelForCausalLM
|
7 |
+
from typing import Union, List, Dict, Any
|
8 |
+
import logging
|
9 |
+
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(level=logging.INFO)
|
12 |
+
logger = logging.getLogger(__name__)
|
13 |
+
|
14 |
+
class InferenceEngine:
|
15 |
+
def __init__(self, model_path: str, device: str = "cuda" if torch.cuda.is_available() else "cpu"):
|
16 |
+
"""
|
17 |
+
Initialize the InferenceEngine.
|
18 |
+
|
19 |
+
Args:
|
20 |
+
model_path (str): Path to the ONNX model.
|
21 |
+
device (str): Device to run the model on ("cuda" or "cpu").
|
22 |
+
"""
|
23 |
+
self.device = device
|
24 |
+
try:
|
25 |
+
# Initialize ONNX runtime session
|
26 |
+
self.session = ort.InferenceSession(
|
27 |
+
model_path,
|
28 |
+
providers=[
|
29 |
+
"TensorrtExecutionProvider",
|
30 |
+
"CUDAExecutionProvider",
|
31 |
+
"CPUExecutionProvider"
|
32 |
+
]
|
33 |
+
)
|
34 |
+
logger.info(f"ONNX model loaded successfully on device: {self.device}")
|
35 |
+
except Exception as e:
|
36 |
+
logger.error(f"Failed to load ONNX model: {e}")
|
37 |
+
raise
|
38 |
+
|
39 |
+
def run_text_inference(self, model: AutoModelForCausalLM, tokenizer: AutoTokenizer, prompt: str, max_length: int = 200) -> str:
|
40 |
+
"""
|
41 |
+
Run text inference using a causal language model.
|
42 |
+
|
43 |
+
Args:
|
44 |
+
model (AutoModelForCausalLM): Pre-trained causal language model.
|
45 |
+
tokenizer (AutoTokenizer): Tokenizer for the model.
|
46 |
+
prompt (str): Input text prompt.
|
47 |
+
max_length (int): Maximum length of the generated text.
|
48 |
+
|
49 |
+
Returns:
|
50 |
+
str: Generated text.
|
51 |
+
"""
|
52 |
+
try:
|
53 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(self.device)
|
54 |
+
outputs = model.generate(**inputs, max_length=max_length)
|
55 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
56 |
+
except Exception as e:
|
57 |
+
logger.error(f"Text inference failed: {e}")
|
58 |
+
raise
|
59 |
+
|
60 |
+
def run_image_inference(self, clip_model: CLIPModel, processor: CLIPProcessor, image_path: str) -> np.ndarray:
|
61 |
+
"""
|
62 |
+
Run image inference using a CLIP model.
|
63 |
+
|
64 |
+
Args:
|
65 |
+
clip_model (CLIPModel): Pre-trained CLIP model.
|
66 |
+
processor (CLIPProcessor): Processor for the CLIP model.
|
67 |
+
image_path (str): Path to the input image.
|
68 |
+
|
69 |
+
Returns:
|
70 |
+
np.ndarray: Image features as a numpy array.
|
71 |
+
"""
|
72 |
+
try:
|
73 |
+
image = Image.open(image_path).convert("RGB")
|
74 |
+
inputs = processor(images=image, return_tensors="pt").to(self.device)
|
75 |
+
outputs = clip_model.get_image_features(**inputs)
|
76 |
+
return outputs.cpu().detach().numpy()
|
77 |
+
except Exception as e:
|
78 |
+
logger.error(f"Image inference failed: {e}")
|
79 |
+
raise
|
80 |
+
|
81 |
+
def run_audio_inference(self, whisper_model: Any, audio_file: str) -> str:
|
82 |
+
"""
|
83 |
+
Run audio inference using a Whisper model.
|
84 |
+
|
85 |
+
Args:
|
86 |
+
whisper_model (Any): Pre-trained Whisper model.
|
87 |
+
audio_file (str): Path to the input audio file.
|
88 |
+
|
89 |
+
Returns:
|
90 |
+
str: Transcribed text.
|
91 |
+
"""
|
92 |
+
try:
|
93 |
+
waveform, sample_rate = torchaudio.load(audio_file)
|
94 |
+
waveform = waveform.to(self.device)
|
95 |
+
return whisper_model.transcribe(waveform)["text"]
|
96 |
+
except Exception as e:
|
97 |
+
logger.error(f"Audio inference failed: {e}")
|
98 |
+
raise
|
99 |
+
|
100 |
+
def run_general_inference(self, input_data: Union[np.ndarray, List, Dict]) -> np.ndarray:
|
101 |
+
"""
|
102 |
+
Run general inference using the ONNX model.
|
103 |
+
|
104 |
+
Args:
|
105 |
+
input_data (Union[np.ndarray, List, Dict]): Input data for the model.
|
106 |
+
|
107 |
+
Returns:
|
108 |
+
np.ndarray: Model output.
|
109 |
+
"""
|
110 |
+
try:
|
111 |
+
input_name = self.session.get_inputs()[0].name
|
112 |
+
output_name = self.session.get_outputs()[0].name
|
113 |
+
|
114 |
+
# Ensure input_data is a numpy array
|
115 |
+
if not isinstance(input_data, np.ndarray):
|
116 |
+
input_data = np.array(input_data, dtype=np.float32)
|
117 |
+
|
118 |
+
return self.session.run([output_name], {input_name: input_data})[0]
|
119 |
+
except Exception as e:
|
120 |
+
logger.error(f"General inference failed: {e}")
|
121 |
+
raise
|
memory_management.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gc
|
2 |
+
import psutil
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
import logging
|
6 |
+
|
7 |
+
class MemoryManager:
|
8 |
+
def __init__(self):
|
9 |
+
self.allocated_memory = 0
|
10 |
+
self.max_memory_limit = psutil.virtual_memory().total * 0.8 # 80% of total RAM
|
11 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
12 |
+
|
13 |
+
def optimize_allocation(self):
|
14 |
+
"""Free up unused memory."""
|
15 |
+
gc.collect()
|
16 |
+
if torch.cuda.is_available():
|
17 |
+
torch.cuda.empty_cache()
|
18 |
+
logging.info("Memory optimization completed.")
|
19 |
+
|
20 |
+
def allocate_memory(self, size, dtype=np.float32):
|
21 |
+
"""Allocate memory for a given size and data type."""
|
22 |
+
if self.allocated_memory + size > self.max_memory_limit:
|
23 |
+
logging.warning("Memory limit exceeded. Optimizing memory...")
|
24 |
+
self.optimize_allocation()
|
25 |
+
self.allocated_memory += size
|
26 |
+
logging.info(f"Allocated {size} bytes of memory. Total allocated: {self.allocated_memory} bytes.")
|
27 |
+
return np.zeros(size, dtype=dtype)
|
28 |
+
|
29 |
+
def deallocate_memory(self, obj):
|
30 |
+
"""Deallocate memory for a given object."""
|
31 |
+
del obj
|
32 |
+
self.optimize_allocation()
|
33 |
+
logging.info("Object deallocated and memory optimized.")
|
34 |
+
|
35 |
+
def monitor_usage(self):
|
36 |
+
"""Monitor system memory usage and optimize if necessary."""
|
37 |
+
usage = psutil.virtual_memory().percent
|
38 |
+
if usage > 85:
|
39 |
+
logging.warning(f"High memory usage detected: {usage}%. Optimizing memory...")
|
40 |
+
self.optimize_allocation()
|
41 |
+
|
42 |
+
def adaptive_caching(self, model):
|
43 |
+
"""Disable gradients for large models to reduce memory usage."""
|
44 |
+
model_size = sum(p.numel() for p in model.parameters() if p.requires_grad)
|
45 |
+
if model_size * 4 > self.max_memory_limit: # 4 bytes per float32
|
46 |
+
for param in model.parameters():
|
47 |
+
param.requires_grad = False
|
48 |
+
logging.info("Adaptive caching applied: Gradients disabled for large model.")
|
49 |
+
return model
|
50 |
+
|
51 |
+
def __enter__(self):
|
52 |
+
"""Context manager entry point."""
|
53 |
+
return self
|
54 |
+
|
55 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
56 |
+
"""Context manager exit point."""
|
57 |
+
self.optimize_allocation()
|
58 |
+
logging.info("MemoryManager context exited. Memory optimized.")
|
59 |
+
|
60 |
+
# Example usage
|
61 |
+
if __name__ == "__main__":
|
62 |
+
memory_manager = MemoryManager()
|
63 |
+
|
64 |
+
# Allocate memory
|
65 |
+
array = memory_manager.allocate_memory(1000000) # 1 million elements
|
66 |
+
print(array.shape)
|
67 |
+
|
68 |
+
# Deallocate memory
|
69 |
+
memory_manager.deallocate_memory(array)
|
70 |
+
|
71 |
+
# Monitor usage
|
72 |
+
memory_manager.monitor_usage()
|
73 |
+
|
74 |
+
# Adaptive caching for a model
|
75 |
+
model = torch.nn.Linear(1000, 1000)
|
76 |
+
model = memory_manager.adaptive_caching(model)
|
77 |
+
|
78 |
+
# Using context manager
|
79 |
+
with MemoryManager() as mm:
|
80 |
+
array = mm.allocate_memory(1000000)
|
81 |
+
print(array.shape)
|
reinforcement_learning.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.optim as optim
|
5 |
+
import random
|
6 |
+
from collections import deque
|
7 |
+
import heapq
|
8 |
+
|
9 |
+
# Neural Network for Deep Q-Learning
|
10 |
+
class QNetwork(nn.Module):
|
11 |
+
def __init__(self, state_size, action_size, hidden_sizes=[256, 128, 64], dropout_rate=0.1):
|
12 |
+
super(QNetwork, self).__init__()
|
13 |
+
self.state_size = state_size
|
14 |
+
self.action_size = action_size
|
15 |
+
|
16 |
+
# Build a deeper network with configurable hidden layers
|
17 |
+
layers = []
|
18 |
+
prev_size = state_size
|
19 |
+
for hidden_size in hidden_sizes:
|
20 |
+
layers.append(nn.Linear(prev_size, hidden_size))
|
21 |
+
layers.append(nn.BatchNorm1d(hidden_size)) # Add batch normalization
|
22 |
+
layers.append(nn.ReLU())
|
23 |
+
layers.append(nn.Dropout(dropout_rate)) # Add dropout for regularization
|
24 |
+
prev_size = hidden_size
|
25 |
+
layers.append(nn.Linear(prev_size, action_size))
|
26 |
+
|
27 |
+
self.network = nn.Sequential(*layers)
|
28 |
+
|
29 |
+
def forward(self, x):
|
30 |
+
return self.network(x)
|
31 |
+
|
32 |
+
# Prioritized Experience Replay Memory (simplified)
|
33 |
+
class PriorityReplayMemory:
|
34 |
+
def __init__(self, capacity, alpha=0.6, beta=0.4, beta_increment=0.001):
|
35 |
+
self.capacity = capacity
|
36 |
+
self.alpha = alpha # Priority exponent
|
37 |
+
self.beta = beta # Importance sampling weight
|
38 |
+
self.beta_increment = beta_increment
|
39 |
+
self.memory = [] # Heap for priorities
|
40 |
+
self.experiences = deque(maxlen=capacity) # Store experiences
|
41 |
+
self.max_priority = 1.0
|
42 |
+
|
43 |
+
def add(self, experience, error=None):
|
44 |
+
priority = error if error is not None else self.max_priority
|
45 |
+
priority = (abs(priority) + 1e-5) ** self.alpha # Small constant to avoid zero priority
|
46 |
+
heapq.heappush(self.memory, (-priority, len(self.experiences))) # Negative for max heap
|
47 |
+
self.experiences.append(experience)
|
48 |
+
|
49 |
+
def sample(self, batch_size):
|
50 |
+
if len(self.experiences) < batch_size:
|
51 |
+
return None, None, None
|
52 |
+
|
53 |
+
# Calculate sampling probabilities
|
54 |
+
priorities = np.array([-p for p, _ in self.memory[:len(self.experiences)]])
|
55 |
+
probs = priorities / priorities.sum()
|
56 |
+
|
57 |
+
# Sample indices
|
58 |
+
indices = np.random.choice(len(self.experiences), batch_size, p=probs, replace=False)
|
59 |
+
samples = [self.experiences[idx] for idx in indices]
|
60 |
+
|
61 |
+
# Importance sampling weights
|
62 |
+
weights = (len(self.experiences) * probs[indices]) ** (-self.beta)
|
63 |
+
weights /= weights.max() # Normalize
|
64 |
+
|
65 |
+
self.beta = min(1.0, self.beta + self.beta_increment) # Anneal beta
|
66 |
+
return samples, indices, torch.FloatTensor(weights)
|
67 |
+
|
68 |
+
def update_priorities(self, indices, errors):
|
69 |
+
for idx, error in zip(indices, errors):
|
70 |
+
priority = (abs(error) + 1e-5) ** self.alpha
|
71 |
+
self.memory[idx] = (-priority, self.memory[idx][1])
|
72 |
+
self.max_priority = max(self.max_priority, priority)
|
73 |
+
heapq.heapify(self.memory) # Re-heapify after updates
|
74 |
+
|
75 |
+
def __len__(self):
|
76 |
+
return len(self.experiences)
|
77 |
+
|
78 |
+
# Enhanced Reinforcement Learning Agent
|
79 |
+
class RLAgent:
|
80 |
+
def __init__(self, state_size, action_size,
|
81 |
+
lr=0.0005, gamma=0.99, epsilon=1.0, epsilon_decay=0.995, min_epsilon=0.01,
|
82 |
+
memory_capacity=10000, batch_size=64, target_update_freq=1000,
|
83 |
+
use_double_dqn=True, clip_grad_norm=1.0):
|
84 |
+
self.state_size = state_size
|
85 |
+
self.action_size = action_size
|
86 |
+
self.gamma = gamma
|
87 |
+
self.epsilon = epsilon
|
88 |
+
self.epsilon_decay = epsilon_decay
|
89 |
+
self.min_epsilon = min_epsilon
|
90 |
+
self.batch_size = batch_size
|
91 |
+
self.use_double_dqn = use_double_dqn
|
92 |
+
self.clip_grad_norm = clip_grad_norm
|
93 |
+
|
94 |
+
# Networks
|
95 |
+
self.policy_net = QNetwork(state_size, action_size)
|
96 |
+
self.target_net = QNetwork(state_size, action_size)
|
97 |
+
self.target_net.load_state_dict(self.policy_net.state_dict()) # Copy weights
|
98 |
+
self.target_net.eval() # Target network doesn't train
|
99 |
+
self.optimizer = optim.Adam(self.policy_net.parameters(), lr=lr)
|
100 |
+
self.criterion = nn.SmoothL1Loss() # Huber loss for stability
|
101 |
+
|
102 |
+
# Memory
|
103 |
+
self.memory = PriorityReplayMemory(memory_capacity)
|
104 |
+
self.steps = 0
|
105 |
+
self.target_update_freq = target_update_freq
|
106 |
+
|
107 |
+
def remember(self, state, action, reward, next_state, done):
|
108 |
+
# Initial error estimate (could be refined with TD error later)
|
109 |
+
state_tensor = torch.FloatTensor(state).unsqueeze(0)
|
110 |
+
next_state_tensor = torch.FloatTensor(next_state).unsqueeze(0)
|
111 |
+
with torch.no_grad():
|
112 |
+
q_value = self.policy_net(state_tensor)[action]
|
113 |
+
next_q = self.target_net(next_state_tensor).max().item()
|
114 |
+
target = reward + (1 - done) * self.gamma * next_q
|
115 |
+
error = abs(q_value.item() - target)
|
116 |
+
self.memory.add((state, action, reward, next_state, done), error)
|
117 |
+
|
118 |
+
def act(self, state):
|
119 |
+
self.steps += 1
|
120 |
+
if random.random() < self.epsilon:
|
121 |
+
return random.randint(0, self.action_size - 1)
|
122 |
+
state = torch.FloatTensor(state).unsqueeze(0)
|
123 |
+
with torch.no_grad():
|
124 |
+
return torch.argmax(self.policy_net(state)).item()
|
125 |
+
|
126 |
+
def train(self):
|
127 |
+
if len(self.memory) < self.batch_size:
|
128 |
+
return
|
129 |
+
|
130 |
+
# Sample from memory
|
131 |
+
batch, indices, weights = self.memory.sample(self.batch_size)
|
132 |
+
if batch is None:
|
133 |
+
return
|
134 |
+
|
135 |
+
states, actions, rewards, next_states, dones = zip(*batch)
|
136 |
+
states = torch.FloatTensor(states)
|
137 |
+
actions = torch.LongTensor(actions)
|
138 |
+
rewards = torch.FloatTensor(rewards)
|
139 |
+
next_states = torch.FloatTensor(next_states)
|
140 |
+
dones = torch.FloatTensor(dones)
|
141 |
+
weights = weights.unsqueeze(1)
|
142 |
+
|
143 |
+
# Compute Q-values
|
144 |
+
q_values = self.policy_net(states).gather(1, actions.unsqueeze(1)).squeeze(1)
|
145 |
+
|
146 |
+
# Double DQN or standard DQN
|
147 |
+
if self.use_double_dqn:
|
148 |
+
next_actions = self.policy_net(next_states).argmax(1)
|
149 |
+
next_q_values = self.target_net(next_states).gather(1, next_actions.unsqueeze(1)).squeeze(1)
|
150 |
+
else:
|
151 |
+
next_q_values = self.target_net(next_states).max(1)[0]
|
152 |
+
|
153 |
+
# Compute targets
|
154 |
+
targets = rewards + self.gamma * next_q_values * (1 - dones)
|
155 |
+
|
156 |
+
# Compute TD errors for priority update
|
157 |
+
td_errors = (q_values - targets).detach().cpu().numpy()
|
158 |
+
|
159 |
+
# Loss with importance sampling weights
|
160 |
+
loss = (self.criterion(q_values, targets) * weights).mean()
|
161 |
+
|
162 |
+
# Optimize
|
163 |
+
self.optimizer.zero_grad()
|
164 |
+
loss.backward()
|
165 |
+
torch.nn.utils.clip_grad_norm_(self.policy_net.parameters(), self.clip_grad_norm)
|
166 |
+
self.optimizer.step()
|
167 |
+
|
168 |
+
# Update priorities
|
169 |
+
self.memory.update_priorities(indices, td_errors)
|
170 |
+
|
171 |
+
# Update target network
|
172 |
+
if self.steps % self.target_update_freq == 0:
|
173 |
+
self.target_net.load_state_dict(self.policy_net.state_dict())
|
174 |
+
|
175 |
+
# Decay epsilon
|
176 |
+
self.epsilon = max(self.min_epsilon, self.epsilon * self.epsilon_decay)
|
vision_processing.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torchvision.transforms as transforms
|
4 |
+
from torchvision import models
|
5 |
+
from PIL import Image
|
6 |
+
import logging
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Set up logging
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
class VisionProcessingModel(nn.Module):
|
14 |
+
def __init__(self, model_name="resnet50", num_classes=1000, top_k=5):
|
15 |
+
super(VisionProcessingModel, self).__init__()
|
16 |
+
|
17 |
+
# Load pre-trained model
|
18 |
+
self.model = self._load_pretrained_model(model_name, num_classes)
|
19 |
+
self.model.eval() # Set the model to evaluation mode
|
20 |
+
|
21 |
+
# Automatically detect device (GPU/CPU)
|
22 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
+
self.model.to(self.device)
|
24 |
+
logger.info(f"Model loaded on device: {self.device}")
|
25 |
+
|
26 |
+
# Number of top predictions to return
|
27 |
+
self.top_k = top_k
|
28 |
+
|
29 |
+
# Image transformations
|
30 |
+
self.transform = transforms.Compose([
|
31 |
+
transforms.Resize(256),
|
32 |
+
transforms.CenterCrop(224),
|
33 |
+
transforms.ToTensor(),
|
34 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
35 |
+
])
|
36 |
+
|
37 |
+
def _load_pretrained_model(self, model_name, num_classes):
|
38 |
+
"""Helper function to load a pre-trained model."""
|
39 |
+
if model_name == "resnet50":
|
40 |
+
return models.resnet50(pretrained=True)
|
41 |
+
elif model_name == "efficientnet_b0":
|
42 |
+
return models.efficientnet_b0(pretrained=True)
|
43 |
+
elif model_name == "mobilenet_v2":
|
44 |
+
return models.mobilenet_v2(pretrained=True)
|
45 |
+
else:
|
46 |
+
raise ValueError(f"Unsupported model: {model_name}")
|
47 |
+
|
48 |
+
def forward(self, image):
|
49 |
+
"""Forward pass through the model."""
|
50 |
+
image = image.to(self.device)
|
51 |
+
with torch.no_grad():
|
52 |
+
outputs = self.model(image)
|
53 |
+
return outputs
|
54 |
+
|
55 |
+
def process_image(self, image_path):
|
56 |
+
"""Process an image and get predictions."""
|
57 |
+
try:
|
58 |
+
# Load image
|
59 |
+
image = Image.open(image_path).convert('RGB')
|
60 |
+
|
61 |
+
# Apply transformations and add batch dimension
|
62 |
+
image = self.transform(image).unsqueeze(0)
|
63 |
+
|
64 |
+
# Get predictions from model
|
65 |
+
outputs = self.forward(image)
|
66 |
+
|
67 |
+
# Convert raw output to probabilities (Softmax)
|
68 |
+
probabilities = torch.nn.functional.softmax(outputs[0], dim=0)
|
69 |
+
|
70 |
+
# Get top-k categories
|
71 |
+
top_k_prob, top_k_catid = torch.topk(probabilities, self.top_k)
|
72 |
+
|
73 |
+
return top_k_prob, top_k_catid
|
74 |
+
except Exception as e:
|
75 |
+
logger.error(f"Error processing image: {e}")
|
76 |
+
return None, None
|
77 |
+
|
78 |
+
def get_category_labels(self, category_ids):
|
79 |
+
"""Map category IDs to human-readable labels."""
|
80 |
+
# Load ImageNet class labels
|
81 |
+
labels_path = os.getenv("IMAGENET_LABELS_PATH", "path/to/imagenet_labels.txt")
|
82 |
+
with open(labels_path, "r") as f:
|
83 |
+
labels = [line.strip() for line in f.readlines()]
|
84 |
+
|
85 |
+
# Map category IDs to labels
|
86 |
+
return [labels[cat_id] for cat_id in category_ids]
|
87 |
+
|
88 |
+
def enhance_vision_processing(self, image_path):
|
89 |
+
"""Enhance vision capabilities by extracting top-k predictions."""
|
90 |
+
top_k_prob, top_k_catid = self.process_image(image_path)
|
91 |
+
|
92 |
+
if top_k_prob is not None and top_k_catid is not None:
|
93 |
+
# Convert tensors to lists
|
94 |
+
top_k_prob = top_k_prob.tolist()
|
95 |
+
top_k_catid = top_k_catid.tolist()
|
96 |
+
|
97 |
+
# Get human-readable labels
|
98 |
+
category_labels = self.get_category_labels(top_k_catid)
|
99 |
+
|
100 |
+
return top_k_prob, top_k_catid, category_labels
|
101 |
+
else:
|
102 |
+
return None, None, None
|
web-search.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
import numpy as np
|
4 |
+
import firebase_admin
|
5 |
+
from firebase_admin import credentials, firestore
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
from flask import Flask, request, jsonify
|
8 |
+
from flask_limiter import Limiter
|
9 |
+
from flask_limiter.util import get_remote_address
|
10 |
+
import faiss # For efficient similarity search
|
11 |
+
|
12 |
+
# Set up logging
|
13 |
+
logging.basicConfig(level=logging.INFO)
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
# Initialize Firebase Admin SDK
|
17 |
+
firebase_cred_path = os.getenv("FIREBASE_CRED_PATH", "path/to/your/firebase/credentials.json")
|
18 |
+
cred = credentials.Certificate(firebase_cred_path)
|
19 |
+
firebase_admin.initialize_app(cred)
|
20 |
+
db = firestore.client()
|
21 |
+
|
22 |
+
# Initialize the model for embeddings
|
23 |
+
model = SentenceTransformer('all-mpnet-base-v2') # Better model for semantic search
|
24 |
+
|
25 |
+
# Initialize FAISS index for efficient similarity search
|
26 |
+
embedding_size = 768 # Size of embeddings from 'all-mpnet-base-v2'
|
27 |
+
faiss_index = faiss.IndexFlatIP(embedding_size) # Inner product for cosine similarity
|
28 |
+
document_ids = [] # To map FAISS index to document IDs
|
29 |
+
|
30 |
+
# Initialize Flask app
|
31 |
+
app = Flask(__name__)
|
32 |
+
|
33 |
+
# Rate limiting
|
34 |
+
limiter = Limiter(
|
35 |
+
app=app,
|
36 |
+
key_func=get_remote_address,
|
37 |
+
default_limits=["200 per day", "50 per hour"]
|
38 |
+
)
|
39 |
+
|
40 |
+
# Function to store documents in Firebase and update FAISS index
|
41 |
+
def store_documents(documents):
|
42 |
+
try:
|
43 |
+
batch = db.batch()
|
44 |
+
embeddings = model.encode(documents) # Encode all documents at once
|
45 |
+
|
46 |
+
for idx, (doc, embedding) in enumerate(zip(documents, embeddings)):
|
47 |
+
doc_ref = db.collection('documents').document(f'doc_{idx}')
|
48 |
+
batch.set(doc_ref, {
|
49 |
+
'text': doc,
|
50 |
+
'embedding': embedding.tolist()
|
51 |
+
})
|
52 |
+
document_ids.append(doc_ref.id) # Store document ID
|
53 |
+
faiss_index.add(np.array([embedding])) # Add embedding to FAISS index
|
54 |
+
|
55 |
+
batch.commit()
|
56 |
+
logger.info(f"Stored {len(documents)} documents in Firebase and updated FAISS index.")
|
57 |
+
except Exception as e:
|
58 |
+
logger.error(f"Error storing documents: {e}")
|
59 |
+
|
60 |
+
# Function to search documents using FAISS
|
61 |
+
def search_documents(query):
|
62 |
+
try:
|
63 |
+
query_embedding = model.encode(query).reshape(1, -1)
|
64 |
+
distances, indices = faiss_index.search(query_embedding, k=1) # Find top-1 match
|
65 |
+
|
66 |
+
if indices[0][0] == -1:
|
67 |
+
return None # No match found
|
68 |
+
|
69 |
+
# Retrieve the best matching document from Firebase
|
70 |
+
best_doc_id = document_ids[indices[0][0]]
|
71 |
+
best_doc = db.collection('documents').document(best_doc_id).get()
|
72 |
+
return best_doc.to_dict()
|
73 |
+
except Exception as e:
|
74 |
+
logger.error(f"Error searching documents: {e}")
|
75 |
+
return None
|
76 |
+
|
77 |
+
@app.route('/search', methods=['GET'])
|
78 |
+
@limiter.limit("10 per minute") # Rate limit for search endpoint
|
79 |
+
def search():
|
80 |
+
query = request.args.get('query')
|
81 |
+
if not query:
|
82 |
+
return jsonify({"error": "No query provided."}), 400
|
83 |
+
|
84 |
+
result = search_documents(query)
|
85 |
+
if result:
|
86 |
+
return jsonify(result)
|
87 |
+
else:
|
88 |
+
return jsonify({"error": "No relevant documents found."}), 404
|
89 |
+
|
90 |
+
if __name__ == '__main__':
|
91 |
+
# Run Flask app for the AI search
|
92 |
+
app.run(debug=True)
|