Spaces:
Sleeping
Sleeping
Commit ·
05048a9
1
Parent(s): 778a398
CIZ
Browse files
app.py
CHANGED
|
@@ -44,8 +44,13 @@ class TextNet(nn.Module):
|
|
| 44 |
def train_model(epochs=5, continual=False, progress=gr.Progress()):
|
| 45 |
global model, vectorizer
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
texts = dataset["train"]["text"][:6000]
|
| 50 |
labels = dataset["train"]["label"][:6000]
|
| 51 |
|
|
@@ -58,7 +63,7 @@ def train_model(epochs=5, continual=False, progress=gr.Progress()):
|
|
| 58 |
y = torch.tensor(labels)
|
| 59 |
|
| 60 |
X_train, X_val, y_train, y_val = train_test_split(
|
| 61 |
-
X, y, test_size=0.2
|
| 62 |
)
|
| 63 |
|
| 64 |
X_train = torch.tensor(X_train.toarray(), dtype=torch.float32).to(device)
|
|
@@ -83,11 +88,9 @@ def train_model(epochs=5, continual=False, progress=gr.Progress()):
|
|
| 83 |
loss.backward()
|
| 84 |
optimizer.step()
|
| 85 |
|
| 86 |
-
# Accuracy
|
| 87 |
preds = outputs.argmax(dim=1)
|
| 88 |
acc = (preds == y_train).float().mean().item()
|
| 89 |
|
| 90 |
-
# Validation
|
| 91 |
model.eval()
|
| 92 |
with torch.no_grad():
|
| 93 |
val_out = model(X_val)
|
|
@@ -115,7 +118,7 @@ def train_model(epochs=5, continual=False, progress=gr.Progress()):
|
|
| 115 |
def load_model():
|
| 116 |
global model, vectorizer
|
| 117 |
|
| 118 |
-
if not os.path.exists(MODEL_PATH):
|
| 119 |
return "No saved model found."
|
| 120 |
|
| 121 |
vectorizer = joblib.load(VEC_PATH)
|
|
@@ -148,7 +151,7 @@ def predict(text):
|
|
| 148 |
# -------------------------
|
| 149 |
with gr.Blocks() as demo:
|
| 150 |
gr.Markdown("# 🧠 Neural Network AI (Gradio Blank)")
|
| 151 |
-
gr.Markdown("From-scratch neural network with continual learning.")
|
| 152 |
|
| 153 |
with gr.Row():
|
| 154 |
epochs = gr.Slider(1, 10, value=5, step=1, label="Epochs")
|
|
|
|
| 44 |
def train_model(epochs=5, continual=False, progress=gr.Progress()):
|
| 45 |
global model, vectorizer
|
| 46 |
|
| 47 |
+
try:
|
| 48 |
+
progress(0.05, desc="Loading dataset...")
|
| 49 |
+
# Force latest revision to avoid broken cached commit
|
| 50 |
+
dataset = load_dataset("ag_news", revision="main")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error loading dataset: {e}"
|
| 53 |
+
|
| 54 |
texts = dataset["train"]["text"][:6000]
|
| 55 |
labels = dataset["train"]["label"][:6000]
|
| 56 |
|
|
|
|
| 63 |
y = torch.tensor(labels)
|
| 64 |
|
| 65 |
X_train, X_val, y_train, y_val = train_test_split(
|
| 66 |
+
X, y, test_size=0.2, random_state=42
|
| 67 |
)
|
| 68 |
|
| 69 |
X_train = torch.tensor(X_train.toarray(), dtype=torch.float32).to(device)
|
|
|
|
| 88 |
loss.backward()
|
| 89 |
optimizer.step()
|
| 90 |
|
|
|
|
| 91 |
preds = outputs.argmax(dim=1)
|
| 92 |
acc = (preds == y_train).float().mean().item()
|
| 93 |
|
|
|
|
| 94 |
model.eval()
|
| 95 |
with torch.no_grad():
|
| 96 |
val_out = model(X_val)
|
|
|
|
| 118 |
def load_model():
|
| 119 |
global model, vectorizer
|
| 120 |
|
| 121 |
+
if not os.path.exists(MODEL_PATH) or not os.path.exists(VEC_PATH):
|
| 122 |
return "No saved model found."
|
| 123 |
|
| 124 |
vectorizer = joblib.load(VEC_PATH)
|
|
|
|
| 151 |
# -------------------------
|
| 152 |
with gr.Blocks() as demo:
|
| 153 |
gr.Markdown("# 🧠 Neural Network AI (Gradio Blank)")
|
| 154 |
+
gr.Markdown("From-scratch neural network with continual learning on AG News.")
|
| 155 |
|
| 156 |
with gr.Row():
|
| 157 |
epochs = gr.Slider(1, 10, value=5, step=1, label="Epochs")
|