Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.optim as optim
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from sklearn.preprocessing import LabelEncoder
|
| 8 |
+
|
| 9 |
+
# === Alphabet Gate Definition ===
|
| 10 |
+
class AlphabetGate(nn.Module):
|
| 11 |
+
def __init__(self):
|
| 12 |
+
super(AlphabetGate, self).__init__()
|
| 13 |
+
alphabet = list("abcdefghijklmnopqrstuvwxyz")
|
| 14 |
+
matrix_size = len(alphabet)
|
| 15 |
+
alphabet_matrix = [[ord(char) - ord('a') for char in alphabet[i:] + alphabet[:i]] for i in range(matrix_size)]
|
| 16 |
+
self.alphabet_gate = torch.tensor(alphabet_matrix, dtype=torch.float)
|
| 17 |
+
|
| 18 |
+
def forward(self, x):
|
| 19 |
+
# Randomly select a row from the alphabet gate matrix
|
| 20 |
+
batch_size = x.size(0)
|
| 21 |
+
selected_rows = torch.randint(0, 26, (batch_size,)).to(x.device)
|
| 22 |
+
transformed = torch.stack([self.alphabet_gate[row] for row in selected_rows])
|
| 23 |
+
return torch.matmul(transformed, x.unsqueeze(-1)).squeeze(-1)
|
| 24 |
+
|
| 25 |
+
# === Neural Network with Alphabet Gate ===
|
| 26 |
+
class AlphabetGateNN(nn.Module):
|
| 27 |
+
def __init__(self, input_dim, hidden_dim, output_dim):
|
| 28 |
+
super(AlphabetGateNN, self).__init__()
|
| 29 |
+
self.alphabet_gate = AlphabetGate()
|
| 30 |
+
self.fc1 = nn.Linear(input_dim, hidden_dim)
|
| 31 |
+
self.fc2 = nn.Linear(hidden_dim, output_dim)
|
| 32 |
+
self.activation = nn.ReLU()
|
| 33 |
+
self.softmax = nn.Softmax(dim=1)
|
| 34 |
+
|
| 35 |
+
def forward(self, x):
|
| 36 |
+
x = self.alphabet_gate(x) # Apply Alphabet Gate
|
| 37 |
+
x = self.activation(self.fc1(x))
|
| 38 |
+
x = self.softmax(self.fc2(x))
|
| 39 |
+
return x
|
| 40 |
+
|
| 41 |
+
# === Helper Functions ===
|
| 42 |
+
def process_file(file_path):
|
| 43 |
+
"""
|
| 44 |
+
Process a CSV file and convert it into training-ready tensors.
|
| 45 |
+
Assumes the file has columns 'text' and 'label'.
|
| 46 |
+
"""
|
| 47 |
+
data = pd.read_csv(file_path)
|
| 48 |
+
texts = data['text'].str.lower() # Convert to lowercase
|
| 49 |
+
labels = data['label']
|
| 50 |
+
|
| 51 |
+
# Encode labels
|
| 52 |
+
le = LabelEncoder()
|
| 53 |
+
labels = le.fit_transform(labels)
|
| 54 |
+
|
| 55 |
+
# Transform texts into alphabet-based feature vectors
|
| 56 |
+
def text_to_vector(text):
|
| 57 |
+
vector = [0] * 26
|
| 58 |
+
for char in text:
|
| 59 |
+
if 'a' <= char <= 'z':
|
| 60 |
+
vector[ord(char) - ord('a')] += 1
|
| 61 |
+
return vector
|
| 62 |
+
|
| 63 |
+
X = torch.tensor([text_to_vector(text) for text in texts], dtype=torch.float)
|
| 64 |
+
y = torch.tensor(labels, dtype=torch.long)
|
| 65 |
+
|
| 66 |
+
return X, y
|
| 67 |
+
|
| 68 |
+
def train_on_file(file_path, model, optimizer, criterion):
|
| 69 |
+
"""
|
| 70 |
+
Train the model on data from a given file.
|
| 71 |
+
"""
|
| 72 |
+
X, y = process_file(file_path)
|
| 73 |
+
dataset = torch.utils.data.TensorDataset(X, y)
|
| 74 |
+
dataloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=True)
|
| 75 |
+
|
| 76 |
+
model.train()
|
| 77 |
+
for epoch in range(3): # Train for 3 epochs per file
|
| 78 |
+
for batch_X, batch_y in dataloader:
|
| 79 |
+
optimizer.zero_grad()
|
| 80 |
+
outputs = model(batch_X)
|
| 81 |
+
loss = criterion(outputs, batch_y)
|
| 82 |
+
loss.backward()
|
| 83 |
+
optimizer.step()
|
| 84 |
+
return model
|
| 85 |
+
|
| 86 |
+
# === Streamlit App ===
|
| 87 |
+
# Initialize model, optimizer, and loss function
|
| 88 |
+
input_dim = 26
|
| 89 |
+
hidden_dim = 16
|
| 90 |
+
output_dim = 3
|
| 91 |
+
model = AlphabetGateNN(input_dim=input_dim, hidden_dim=hidden_dim, output_dim=output_dim)
|
| 92 |
+
optimizer = optim.Adam(model.parameters(), lr=0.01)
|
| 93 |
+
criterion = nn.CrossEntropyLoss()
|
| 94 |
+
|
| 95 |
+
st.title("Enterprise-Ready Continuous Training App with Alphabet Gate")
|
| 96 |
+
st.write("""
|
| 97 |
+
### How it Works:
|
| 98 |
+
1. Upload a CSV file with the following format:
|
| 99 |
+
- **text**: Text column containing input strings.
|
| 100 |
+
- **label**: Target labels for classification.
|
| 101 |
+
2. The model will train incrementally on each file you upload.
|
| 102 |
+
3. You can download the updated model once training is complete.
|
| 103 |
+
""")
|
| 104 |
+
|
| 105 |
+
uploaded_file = st.file_uploader("Upload a training file (CSV format)", type="csv")
|
| 106 |
+
|
| 107 |
+
if uploaded_file is not None:
|
| 108 |
+
# Save the uploaded file locally
|
| 109 |
+
file_path = os.path.join("uploads", uploaded_file.name)
|
| 110 |
+
os.makedirs("uploads", exist_ok=True)
|
| 111 |
+
with open(file_path, "wb") as f:
|
| 112 |
+
f.write(uploaded_file.getbuffer())
|
| 113 |
+
|
| 114 |
+
# Train the model on the uploaded file
|
| 115 |
+
st.write("Processing and training on the uploaded file...")
|
| 116 |
+
model = train_on_file(file_path, model, optimizer, criterion)
|
| 117 |
+
st.success("Training complete!")
|
| 118 |
+
|
| 119 |
+
# Save the updated model
|
| 120 |
+
model_path = "trained_model.pth"
|
| 121 |
+
torch.save(model.state_dict(), model_path)
|
| 122 |
+
st.write("Model updated and saved.")
|
| 123 |
+
|
| 124 |
+
# Provide a download button for the trained model
|
| 125 |
+
with open(model_path, "rb") as f:
|
| 126 |
+
st.download_button(
|
| 127 |
+
label="Download Trained Model",
|
| 128 |
+
data=f,
|
| 129 |
+
file_name="trained_model.pth",
|
| 130 |
+
mime="application/octet-stream"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
st.write("Upload more files to continue training!")
|