Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/thomasm1/python_2018
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasm1 committed Jan 29, 2025
2 parents df360fa + a0d3d62 commit 5876d3d
Show file tree
Hide file tree
Showing 687 changed files with 9,381 additions and 203,751 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3.9-slim

WORKDIR /app

COPY simple_server.py .

CMD ["python", "simple_server.py"]

2 changes: 2 additions & 0 deletions PY_AGENTS/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
4 changes: 4 additions & 0 deletions PY_AGENTS/agent-mapl-anthropic/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
.gitignore
env

18 changes: 18 additions & 0 deletions PY_AGENTS/agent-mapl-anthropic/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:3.11-slim

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

#COPY agents.py prompts.py ./

RUN mkdir /app/data


EXPOSE 80
#set the environmen variable
#ENV PYTHONUNBUFFERED=1

CMD ["python", "agents.py"]
2 changes: 2 additions & 0 deletions PY_AGENTS/agent-mapl-anthropic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker run -it -v "C:\Users\thoma\git\_python\PY_AGENTS:/app/data" mapl-agents

117 changes: 117 additions & 0 deletions PY_AGENTS/agent-mapl-anthropic/agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# agent-mapl.py

import os
import csv
import anthropic
from prompts import *

#KEY
if not os.getenv("ANTHROPIC_API_KEY"):
os.environ["ANTHROPIC_API_KEY"] = input("Please enter your Antrhopic API key: ") # prompt the user for anthro api key

#CLIENT
client = anthropic.Anthropic()
sonnet = "claude-3-5-sonnet-20240620" # model name get from docs.anthropic.com/en/docs/about-claude/models

# Function to read the CSV file form the user
def read_csv(file_path):
data = []
with open(file_path, "r", newline="") as csvfile: #open csv in readmode
csv_reader = csv.reader(csvfile) # create csv reader obj
for row in csv_reader:
data.append(row) # add each row to the data list
return data

# Saving
def save_to_csv(data, output_file, headers=None):
mode = 'w' if headers else 'a' # set file mode: w write if headers are provide else a append
with open(output_file, mode, newline="") as f:
writer=csv.writer(f) # create a csv writer object
if headers:
writer.writerow(headers) # write the headers if provided
for row in csv.reader(data.splitlines()): #sp;lie data stirng into rows
writer.writerow(row)


#my personal librarian agent
def analyzer_agent(sample_data):
message = client.messages.create(
model=sonnet,
max_tokens=400, #limit response to 400 tokens
temperature=0.1, # set low temp for more focused, deterministic output
system=ANALYZER_SYSTEM_PROMPT, # use the predefined system prompt for analyzer
messages=[
{
"role":"user",
"content": ANALYZER_USER_PROMPT.format(sample_data=sample_data)
# format user prompt with provided smpale data
}
]
)
return message.content[0].text # return text content of tfirst message

# generator agent
def generator_agent(analysis_result, sample_data, num_rows=30):
message = client.messages.create(
model=sonnet,
max_tokens=1500, # allow for longer response
temperature=1, # high temp for creative, divers output
system=GENERATOR_SYSTEM_PROMPT,
messages=[
{
"role":"user",
"content": GENERATOR_USER_PROMPT.format(
num_rows=num_rows,
analysis_result=analysis_result,
sample_data=sample_data
)
}
]
)
return message.content[0].text


#main fucntion to run analyzer and generator agents: CONTROL THE AGENTS!

#get input
file_path = input("\nPuh-lease enter the name of your CSV file: ")
file_path = os.path.join('/app/data', file_path)
desired_rows = int(input("n now Enter the number of rows you want in the new dataset: "))

#read data
sample_data = read_csv(file_path)
sample_data_str = "\n".join([",".join(row) for row in sample_data]) # convert 2d list to a single string

print("\nLaunching team of Agents")

analysis_result = analyzer_agent(sample_data_str)
print("\n### Analyzer Agent output:###\n")
print(analysis_result)
print("\n=-============\n\nGenerating new data....")

#output
output_file = "/app/data/new_dataset.csv"
headers = sample_data[0]

#creat output with headers
save_to_csv("", output_file, headers)


batch_size = 30 #number of rows to genrate in each batch
generated_rows = 0 # counter to keep track of rows generated

# generate data in batches until rows reached
while generated_rows < desired_rows:
rows_to_generate = min(batch_size, desired_rows - generated rows)
generated_data = generator_agent(analysis_result, sample_data_str, rows_to_generate)
#append gen data to output file
save_to_csv(generated_data, output_file)
#update count of generated rows
generated_rows += rows_to_generate
#print progress update
print(f"Generated {generated_rows} rows out of {desired_rows}")

# inform process complete
print(f"\nGenerated data has been saved to {output_file}")

# THE END
4 changes: 4 additions & 0 deletions PY_AGENTS/agent-mapl-anthropic/input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Type,Indicators,Name,Instructions
bookwork,studious,iWorm,read and teach
taskrunner,heavy duty,ichabod,pick up and carry

32 changes: 32 additions & 0 deletions PY_AGENTS/agent-mapl-anthropic/prompts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#prompts.py
ANALYZER_SYSTEM_PROMPT = """You are an AI agent that analyzes the CSV provided by the user.
The focus of your analysis should be on what the data is, how it is formatted, what each column stands for, and how new data should be interpreted."""

GENERATOR_SYSTEM_PROMPT = """You are an AI agent that generates new CSV rows based on analyssi results and sample data.
Follow the exact formatting and don't outpu any extra text. You only output formatted data, never any other text."""

ANALZER_USER_PROMPT = """ Analyze the structure and patterns of this sample dataset:
{sampl_data}
Provide a concise summary of the following:
1. formatting of the dataset, be crystal clear when describing the structure of the CSV
2. what the dataset represets, what each column stands for
3. how new data should look like, based on the patterns you've identified
"""

GENERATOR_USER_PROMPT = """Generate {num_rows} new CSV rows based on this analysis and sample data:
Analysis:
{analysis_result}
Sample Data:
{sample_data}
Use the exact same formatting as the orginal data. Output onlyh the generated code, no extra text.
DO NOT USE ANY TEXT BEFVOFE/AFTER THE DATA. JUST START BY OUTPUTTING THE NEW ROWS. NO EXTRA TEXT!!!!"""



2 changes: 2 additions & 0 deletions PY_AGENTS/agent-mapl-anthropic/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
anthropic

1 change: 1 addition & 0 deletions PY_AGENTS/n8n-ai-starter-kit
Submodule n8n-ai-starter-kit added at 9c99ce
11 changes: 11 additions & 0 deletions PY_AI/blockchain_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Transaction_ID,Timestamp,Sender,Receiver,Amount,Transaction_Fee
tx_1,2025-01-22 10:00:00,wallet_1,wallet_11,100,0.01
tx_2,2025-01-22 10:05:00,wallet_2,wallet_12,250,0.015
tx_3,2025-01-22 10:10:00,wallet_3,wallet_13,300,0.02
tx_4,2025-01-22 10:15:00,wallet_4,wallet_14,150,0.01
tx_5,2025-01-22 10:20:00,wallet_5,wallet_15,200,0.025
tx_6,2025-01-22 10:25:00,wallet_6,wallet_16,350,0.02
tx_7,2025-01-22 10:30:00,wallet_7,wallet_17,400,0.03
tx_8,2025-01-22 10:35:00,wallet_8,wallet_18,50,0.01
tx_9,2025-01-22 10:40:00,wallet_9,wallet_19,75,0.015
tx_10,2025-01-22 10:45:00,wallet_10,wallet_20,125,0.02
21 changes: 21 additions & 0 deletions PY_AI/py_torch/blockchain-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# dataset.py
import pandas as pd
from torch.utils.data import Dataset

class BlockchainDataset(Dataset):
def __init__(self, csv_file):
self.data = pd.read_csv(csv_file)

def __len__(self):
return len(self.data)

def __getitem__(self, idx):
row = self.data.iloc[idx]
# Extract input features and labels (e.g., predict "Amount")
features = {
'sender': row['Sender'],
'receiver': row['Receiver'],
'fee': row['Transaction_Fee'],
}
label = row['Amount']
return features, label
17 changes: 17 additions & 0 deletions PY_AI/py_torch/blockchain-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# pipeline.py
import torch
from torch.utils.data import DataLoader
from sklearn.preprocessing import LabelEncoder

def preprocess_data(dataset):
# Encode sender and receiver addresses as integers
sender_encoder = LabelEncoder()
receiver_encoder = LabelEncoder()

dataset.data['Sender'] = sender_encoder.fit_transform(dataset.data['Sender'])
dataset.data['Receiver'] = receiver_encoder.fit_transform(dataset.data['Receiver'])

return dataset

def create_dataloader(dataset, batch_size=2):
return DataLoader(dataset, batch_size=batch_size, shuffle=True)
14 changes: 14 additions & 0 deletions PY_AI/py_torch/blockchain-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# model.py
import torch.nn as nn

class TransactionPredictor(nn.Module):
def __init__(self, input_dim):
super(TransactionPredictor, self).__init__()
self.fc = nn.Sequential(
nn.Linear(input_dim, 32),
nn.ReLU(),
nn.Linear(32, 1) # Output a single value (Amount)
)

def forward(self, x):
return self.fc(x)
36 changes: 36 additions & 0 deletions PY_AI/py_torch/blockchain-4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# training.py
import torch
import torch.optim as optim
from torch.nn import MSELoss
from dataset import BlockchainDataset
from pipeline import preprocess_data, create_dataloader
from model import TransactionPredictor

csv_file = "blockchain_data.csv"
dataset = BlockchainDataset(csv_file)
dataset = preprocess_data(dataset)
dataloader = create_dataloader(dataset)

# Initialize model, optimizer, and loss function
model = TransactionPredictor(input_dim=3) # sender, receiver, fee
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = MSELoss()

# Training loop
for epoch in range(10):
for batch in dataloader:
features, labels = batch
sender = features['sender'].float()
receiver = features['receiver'].float()
fee = features['fee'].float()

inputs = torch.stack([sender, receiver, fee], dim=1)
labels = labels.float()

optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs.squeeze(), labels)
loss.backward()
optimizer.step()

print(f"Epoch {epoch + 1}, Loss: {loss.item():.4f}")
19 changes: 19 additions & 0 deletions PY_AI/py_torch/blockchain-5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# sankey.py
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.sankey import Sankey

def create_sankey(csv_file):
data = pd.read_csv(csv_file)

sankey = Sankey()
for _, row in data.iterrows():
sankey.add(flows=[-row['Amount'], row['Amount']],
labels=[row['Sender'], row['Receiver']],
orientations=[-1, 1])
sankey.finish()
plt.title('Blockchain Transactions Sankey')
plt.show()

if __name__ == "__main__":
create_sankey("blockchain_data.csv")
11 changes: 11 additions & 0 deletions PY_AI/py_torch/blockchain_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Transaction_ID,Timestamp,Sender,Receiver,Amount,Transaction_Fee
tx_1,2025-01-22 10:00:00,wallet_1,wallet_11,100,0.01
tx_2,2025-01-22 10:05:00,wallet_2,wallet_12,250,0.015
tx_3,2025-01-22 10:10:00,wallet_3,wallet_13,300,0.02
tx_4,2025-01-22 10:15:00,wallet_4,wallet_14,150,0.01
tx_5,2025-01-22 10:20:00,wallet_5,wallet_15,200,0.025
tx_6,2025-01-22 10:25:00,wallet_6,wallet_16,350,0.02
tx_7,2025-01-22 10:30:00,wallet_7,wallet_17,400,0.03
tx_8,2025-01-22 10:35:00,wallet_8,wallet_18,50,0.01
tx_9,2025-01-22 10:40:00,wallet_9,wallet_19,75,0.015
tx_10,2025-01-22 10:45:00,wallet_10,wallet_20,125,0.02
24 changes: 24 additions & 0 deletions PY_AI/py_torch/data_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pandas as pd

# Mock data for blockchain transactions
data = {
"Transaction_ID": [f"tx_{i}" for i in range(1, 11)],
"Timestamp": [
"2025-01-22 10:00:00", "2025-01-22 10:05:00", "2025-01-22 10:10:00",
"2025-01-22 10:15:00", "2025-01-22 10:20:00", "2025-01-22 10:25:00",
"2025-01-22 10:30:00", "2025-01-22 10:35:00", "2025-01-22 10:40:00",
"2025-01-22 10:45:00"
],
"Sender": [f"wallet_{i}" for i in range(1, 11)],
"Receiver": [f"wallet_{i+10}" for i in range(1, 11)],
"Amount": [100, 250, 300, 150, 200, 350, 400, 50, 75, 125],
"Transaction_Fee": [0.01, 0.015, 0.02, 0.01, 0.025, 0.02, 0.03, 0.01, 0.015, 0.02]
}

# Create a DataFrame
blockchain_df = pd.DataFrame(data)

# Save to a CSV file
file_path = "/mnt/data/blockchain_data.csv"
blockchain_df.to_csv(file_path, index=False)
file_path
Loading

0 comments on commit 5876d3d

Please sign in to comment.