|
import coremltools |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
import numpy as np |
|
import pandas as pd |
|
from sklearn.cluster import KMeans |
|
from scipy.spatial.distance import cdist |
|
import geopy.distance |
|
import logging |
|
|
|
class AddressBook: |
|
def __init__(self, contacts): |
|
self.contacts = contacts |
|
|
|
def get_location(self, contact): |
|
|
|
latitude = contact["latitude"] |
|
longitude = contact["longitude"] |
|
|
|
|
|
return (latitude, longitude) |
|
|
|
class MessageClassifier: |
|
def __init__(self, model_name): |
|
self.tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
self.model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
|
def classify(self, messages): |
|
|
|
inputs = self.tokenizer(messages, padding=True, truncation=True, return_tensors="pt") |
|
|
|
|
|
outputs = self.model(**inputs) |
|
probs = torch.nn.functional.softmax(outputs[0], dim=-1) |
|
labels = torch.argmax(probs, axis=1) |
|
|
|
|
|
return labels.numpy() |
|
|
|
class AnomalyDetector: |
|
def __init__(self, num_clusters): |
|
self.num_clusters = num_clusters |
|
|
|
def detect(self, location_history): |
|
|
|
df = pd.DataFrame(location_history, columns=["latitude", "longitude"]) |
|
|
|
|
|
distortions = [] |
|
K = range(1, self.num_clusters+1) |
|
for k in K: |
|
kmeans = KMeans(n_clusters=k, random_state=0).fit(df) |
|
distortions.append(sum(np.min(cdist(df, kmeans.cluster_centers_, 'euclidean'), axis=1)) / df.shape[0]) |
|
k_opt = K[np.argmin(distortions)] |
|
|
|
|
|
kmeans = KMeans(n_clusters=k_opt, random_state=0).fit(df) |
|
df["label"] = kmeans.labels_ |
|
|
|
|
|
cluster_counts = df["label"].value_counts() |
|
anomalous_clusters = cluster_counts[cluster_counts < cluster_counts.quantile(0.1)].index |
|
|
|
|
|
anomalous_points = df[df["label"].isin(anomalous_clusters)] |
|
|
|
|
|
return anomalous_points.to_dict("records") |
|
|
|
class GeoLocation: |
|
def __init__(self, location): |
|
self.location = location |
|
|
|
def get_distance(self, contact_location): |
|
|
|
distance = geopy.distance.distance(self.location, contact_location).km |
|
|
|
|
|
return distance |
|
|
|
class OTPProtocolBot: |
|
def __init__(self, protocol): |
|
self.protocol = protocol |
|
|
|
def intercept(self, message): |
|
|
|
if "OTP code" in message: |
|
|
|
otp_code = message.split(":")[-1] |
|
self.protocol.send_otp_code(otp_code) |
|
|
|
class LegacyProtocolBot: |
|
def __init__(self, protocol): |
|
self.protocol = protocol |
|
|
|
def bypass(self): |
|
|
|
self.protocol.use_new_protocol() |
|
|
|
class MLModelConverter: |
|
def convert_model(self, model): |
|
|
|
pass |
|
|
|
return [] |
|
|
|
@authenticate_user |
|
def save_todo_list(todo_list: List[Dict]) -> None: |
|
""" |
|
Save the to-do list to the specified file. |
|
""" |
|
with open(TODO_LIST_FILE, "w") as f: |
|
json.dump(todo_list, f) |
|
|
|
@authenticate_user |
|
def add_task(task: Dict) -> None: |
|
""" |
|
Add a task to the to-do list. |
|
""" |
|
todo_list = load_todo_list() |
|
todo_list.append(task) |
|
save_todo_list(todo_list) |
|
|
|
@authenticate_user |
|
def remove_task(task: Dict) -> None: |
|
""" |
|
Remove a task from the to-do list. |
|
""" |
|
todo_list = load_todo_list() |
|
if task in todo_list: |
|
todo_list.remove(task) |
|
save_todo_list(todo_list) |
|
|
|
def process_task(task: Dict) -> None: |
|
""" |
|
Process a task using the appropriate AWS service. |
|
""" |
|
if "upload" in task: |
|
filename = task["upload"] |
|
s3.upload_file(filename, "my-bucket", filename) |
|
logging.info(f"Uploaded file {filename} to S3 bucket my-bucket") |
|
elif "lambda" in task: |
|
function_name = task["lambda"] |
|
response = lambda_client.invoke(FunctionName=function_name, Payload=json.dumps(task)) |
|
logging.info(f"Invoked Lambda function {function_name} with response {response['StatusCode']}") |
|
elif "comprehend" in task: |
|
text = task["comprehend"] |
|
sentiment = comprehend.detect_sentiment(Text=text, LanguageCode='en') |
|
logging.info(f"Detected sentiment {sentiment['Sentiment']} in text: {text}") |
|
else: |
|
logging.warning(f"Task not recognized: {task}") |
|
|
|
@authenticate_user |
|
def process_todo_list() -> None: |
|
""" |
|
Process all tasks in the to-do list. |
|
""" |
|
todo_list = load_todo_list() |
|
for task in todo_list: |
|
process_task(task) |
|
|
|
|
|
add_task({"upload": "/home/user/data.txt"}) |
|
add_task({"lambda": "my-function", "message": "hello"}) |
|
add_task({"comprehend": "This is a positive message."}) |
|
process_todo_list() |
|
|