Spaces:
Runtime error
Runtime error
File size: 11,569 Bytes
d490ea9 cb6bdc7 d490ea9 49c1c17 d490ea9 cb6bdc7 49c1c17 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 49c1c17 cb6bdc7 49c1c17 2d8ca2e 49c1c17 2d8ca2e 49c1c17 2d8ca2e 49c1c17 2d8ca2e 49c1c17 2d8ca2e 49c1c17 cb6bdc7 49c1c17 d490ea9 49c1c17 d490ea9 cb6bdc7 2d8ca2e 49c1c17 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 49c1c17 cb6bdc7 49c1c17 cb6bdc7 49c1c17 cb6bdc7 49c1c17 cb6bdc7 49c1c17 cb6bdc7 49c1c17 cb6bdc7 49c1c17 cb6bdc7 49c1c17 cb6bdc7 d490ea9 49c1c17 cb6bdc7 49c1c17 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 d490ea9 cb6bdc7 49c1c17 cb6bdc7 d490ea9 49c1c17 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
import os
import stat
import xml.etree.ElementTree as ET
import torch
import torch.nn as nn
import torch.nn.functional as F
import logging
import requests
from collections import defaultdict
from typing import List, Dict, Any
from colorama import Fore, Style, init
from accelerate import Accelerator
from torch.utils.data import DataLoader, TensorDataset
from transformers import AutoTokenizer, AutoModel
from sentence_transformers import SentenceTransformer
import numpy as np
# Initialize colorama
init(autoreset=True)
logging.basicConfig(level=logging.INFO, format='\033[92m%(asctime)s - %(levelname)s - %(message)s\033[0m')
file_path = 'data/'
output_path = 'output/'
# Create output path if it doesn't exist
if not os.path.exists(output_path):
try:
os.makedirs(output_path)
os.chmod(output_path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # Set full r/w permissions
except PermissionError:
print(f"Permission denied: '{output_path}'")
# Handle the error or try a different path
# Ensure necessary files are created with full r/w permissions
def ensure_file(file_path):
if not os.path.exists(file_path):
with open(file_path, 'w') as f:
pass
os.chmod(file_path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # Set full r/w permissions
class MagicStateLayer(nn.Module):
def __init__(self, size):
super().__init__()
self.state = nn.Parameter(torch.randn(size))
def forward(self, x):
return x + self.state
class MemoryAugmentationLayer(nn.Module):
def __init__(self, size):
super().__init__()
self.memory = nn.Parameter(torch.randn(size))
def forward(self, x):
return x + self.memory
class HybridAttentionLayer(nn.Module):
def __init__(self, size):
super().__init__()
self.attention = nn.MultiheadAttention(size, num_heads=8)
def forward(self, x):
x = x.unsqueeze(1)
attn_output, _ = self.attention(x, x, x)
return attn_output.squeeze(1)
class DynamicFlashAttentionLayer(nn.Module):
def __init__(self, size):
super().__init__()
self.attention = nn.MultiheadAttention(size, num_heads=8)
def forward(self, x):
x = x.unsqueeze(1)
attn_output, _ = self.attention(x, x, x)
return attn_output.squeeze(1)
class DynamicModel(nn.Module):
def __init__(self, sections: Dict[str, List[Dict[str, Any]]]):
super().__init__()
self.sections = nn.ModuleDict({sn: nn.ModuleList([self.create_layer(lp) for lp in layers]) for sn, layers in sections.items()})
def create_layer(self, lp):
layers = [nn.Linear(lp['input_size'], lp['output_size'])]
if lp.get('batch_norm', True):
layers.append(nn.BatchNorm1d(lp['output_size']))
activation = lp.get('activation', 'relu')
if activation == 'relu':
layers.append(nn.ReLU(inplace=True))
elif activation == 'tanh':
layers.append(nn.Tanh())
elif activation == 'sigmoid':
layers.append(nn.Sigmoid())
elif activation == 'leaky_relu':
layers.append(nn.LeakyReLU(negative_slope=0.01, inplace=True))
elif activation == 'elu':
layers.append(nn.ELU(alpha=1.0, inplace=True))
if dropout := lp.get('dropout', 0.1):
layers.append(nn.Dropout(p=dropout))
if lp.get('memory_augmentation', True):
layers.append(MemoryAugmentationLayer(lp['output_size']))
if lp.get('hybrid_attention', True):
layers.append(HybridAttentionLayer(lp['output_size']))
if lp.get('dynamic_flash_attention', True):
layers.append(DynamicFlashAttentionLayer(lp['output_size']))
if lp.get('magic_state', True):
layers.append(MagicStateLayer(lp['output_size']))
return nn.Sequential(*layers)
def forward(self, x, section_name=None):
if section_name:
for layer in self.sections[section_name]:
x = layer(x)
else:
for section_name, layers in self.sections.items():
for layer in layers:
x = layer(x)
return x
def parse_xml_file(file_path):
tree, root, layers = ET.parse(file_path), ET.parse(file_path).getroot(), []
for layer in root.findall('.//label'):
lp = {
'input_size': int(layer.get('input_size', 128)),
'output_size': int(layer.get('output_size', 256)),
'activation': layer.get('activation', 'relu').lower()
}
if lp['activation'] not in ['relu', 'tanh', 'sigmoid', 'none']:
raise ValueError(f"Unsupported activation function: {lp['activation']}")
if lp['input_size'] <= 0 or lp['output_size'] <= 0:
raise ValueError("Layer dimensions must be positive integers")
layers.append(lp)
if not layers:
layers.append({'input_size': 128, 'output_size': 256, 'activation': 'relu'})
return layers
def create_model_from_folder(folder_path):
sections = defaultdict(list)
if not os.path.exists(folder_path):
logging.warning(f"Folder {folder_path} does not exist. Creating model with default configuration.")
return DynamicModel({})
xml_files_found = False
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.xml'):
xml_files_found = True
file_path = os.path.join(root, file)
try:
sections[os.path.basename(root).replace('.', '_')].extend(parse_xml_file(file_path))
except Exception as e:
logging.error(f"Error processing {file_path}: {str(e)}")
if not xml_files_found:
logging.warning("No XML files found. Creating model with default configuration.")
return DynamicModel({})
return DynamicModel(dict(sections))
def create_embeddings_and_stores(folder_path, model_name="sentence-transformers/all-MiniLM-L6-v2"):
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
doc_store = []
embeddings_list = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.xml'):
file_path = os.path.join(root, file)
try:
tree, root = ET.parse(file_path), ET.parse(file_path).getroot()
for elem in root.iter():
if elem.text:
text = elem.text.strip()
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
embeddings = model(**inputs).last_hidden_state.mean(dim=1).cpu().numpy()
embeddings_list.append(embeddings)
doc_store.append(text)
except Exception as e:
logging.error(f"Error processing {file_path}: {str(e)}")
return embeddings_list, doc_store
def query_embeddings(query, embeddings_list, doc_store, model_name="sentence-transformers/all-MiniLM-L6-v2"):
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
inputs = tokenizer(query, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
query_embedding = model(**inputs).last_hidden_state.mean(dim=1).cpu().numpy()
similarities = [np.dot(query_embedding, emb.T) for emb in embeddings_list]
top_k_indices = np.argsort(similarities, axis=0)[-5:][::-1]
return [doc_store[i] for i in top_k_indices]
def fetch_courtlistener_data(query):
base_url = "https://nzlii.org/cgi-bin/sinosrch.cgi"
params = {"method": "auto", "query": query, "meta": "/nz", "results": "50", "format": "json"}
try:
response = requests.get(base_url, params=params, headers={"Accept": "application/json"}, timeout=10)
response.raise_for_status()
return [{"title": r.get("title", ""), "citation": r.get("citation", ""), "date": r.get("date", ""), "court": r.get("court", ""), "summary": r.get("summary", ""), "url": r.get("url", "")} for r in response.json().get("results", [])]
except requests.exceptions.RequestException as e:
logging.error(f"Failed to fetch data from NZLII API: {str(e)}")
return []
class CustomModel(nn.Module):
def __init__(self, model_name="distilbert-base-uncased"):
super().__init__()
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.encoder = AutoModel.from_pretrained(model_name)
self.hidden_size = self.encoder.config.hidden_size
self.dropout = nn.Dropout(p=0.3)
self.fc1 = nn.Linear(self.hidden_size, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 32)
self.fc4 = nn.Linear(32, 16)
self.memory = nn.LSTM(self.hidden_size, 64, bidirectional=True, batch_first=True)
self.memory_fc1 = nn.Linear(64 * 2, 32)
self.memory_fc2 = nn.Linear(32, 16)
def forward(self, data):
tokens = self.tokenizer(data, return_tensors="pt", truncation=True, padding=True)
outputs = self.encoder(**tokens)
x = outputs.last_hidden_state.mean(dim=1)
x = self.dropout(F.relu(self.fc1(x)))
x = self.dropout(F.relu(self.fc2(x)))
x = self.dropout(F.relu(self.fc3(x)))
x = self.fc4(x)
return x
def training_step(self, data, labels, optimizer, criterion):
optimizer.zero_grad()
outputs = self.forward(data)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
return loss.item()
def validation_step(self, data, labels, criterion):
with torch.no_grad():
outputs = self.forward(data)
loss = criterion(outputs, labels)
return loss.item()
def predict(self, input):
self.eval()
with torch.no_grad():
return self.forward(input)
def main():
folder_path = 'data'
model = create_model_from_folder(folder_path)
logging.info(f"Created dynamic PyTorch model with sections: {list(model.sections.keys())}")
embeddings_list, doc_store = create_embeddings_and_stores(folder_path)
accelerator = Accelerator()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
num_epochs = 10
dataset = TensorDataset(torch.randn(100, 128), torch.randint(0, 2, (100,)))
dataloader = DataLoader(dataset, batch_size=16, shuffle=True)
model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader)
for epoch in range(num_epochs):
model.train()
total_loss = 0
for batch_data, batch_labels in dataloader:
optimizer.zero_grad()
outputs = model(batch_data)
loss = criterion(outputs, batch_labels)
accelerator.backward(loss)
optimizer.step()
total_loss += loss.item()
avg_loss = total_loss / len(dataloader)
logging.info(f"Epoch {epoch+1}/{num_epochs}, Average Loss: {avg_loss:.4f}")
query = "example query text"
results = query_embeddings(query, embeddings_list, doc_store)
logging.info(f"Query results: {results}")
courtlistener_data = fetch_courtlistener_data(query)
logging.info(f"CourtListener API results: {courtlistener_data}")
if __name__ == "__main__":
main() |