Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.datasets import make_circles
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.preprocessing import StandardScaler
|
6 |
+
from tensorflow.keras.models import Sequential
|
7 |
+
from tensorflow.keras.layers import Dense
|
8 |
+
from tensorflow.keras.optimizers import Adam
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
|
11 |
+
# Set Streamlit page config
|
12 |
+
st.set_page_config(page_title="ML Playground", layout="centered")
|
13 |
+
|
14 |
+
st.title("🧠 TensorFlow Playground Clone")
|
15 |
+
st.markdown("Train a simple neural network on a synthetic dataset like circles")
|
16 |
+
|
17 |
+
# Sidebar controls
|
18 |
+
st.sidebar.header("Model Settings")
|
19 |
+
|
20 |
+
# Dataset
|
21 |
+
dataset = st.sidebar.selectbox("Dataset", ["Circle"])
|
22 |
+
n_samples = st.sidebar.slider("Number of Samples", 100, 1000, 300)
|
23 |
+
|
24 |
+
# Model settings
|
25 |
+
layers = st.sidebar.text_input("Network Shape (comma-separated)", "4,2")
|
26 |
+
activation = st.sidebar.selectbox("Activation", ["relu", "tanh", "sigmoid"])
|
27 |
+
learning_rate = st.sidebar.slider("Learning Rate", 0.001, 0.1, 0.03, step=0.001)
|
28 |
+
epochs = st.sidebar.slider("Epochs", 10, 200, 50)
|
29 |
+
|
30 |
+
# Generate dataset
|
31 |
+
X, y = make_circles(n_samples=n_samples, factor=0.5, noise=0.05, random_state=0)
|
32 |
+
X = StandardScaler().fit_transform(X)
|
33 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)
|
34 |
+
|
35 |
+
# Build model
|
36 |
+
model = Sequential()
|
37 |
+
layer_sizes = [int(n.strip()) for n in layers.split(",") if n.strip().isdigit()]
|
38 |
+
input_dim = X.shape[1]
|
39 |
+
|
40 |
+
# Input + hidden layers
|
41 |
+
model.add(Dense(layer_sizes[0], input_dim=input_dim, activation=activation))
|
42 |
+
for size in layer_sizes[1:]:
|
43 |
+
model.add(Dense(size, activation=activation))
|
44 |
+
|
45 |
+
# Output layer
|
46 |
+
model.add(Dense(1, activation='sigmoid'))
|
47 |
+
|
48 |
+
optimizer = Adam(learning_rate=learning_rate)
|
49 |
+
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
|
50 |
+
|
51 |
+
# Training
|
52 |
+
with st.spinner("Training model..."):
|
53 |
+
history = model.fit(X_train, y_train, epochs=epochs, verbose=0, validation_data=(X_test, y_test))
|
54 |
+
|
55 |
+
# Plotting
|
56 |
+
fig, ax = plt.subplots()
|
57 |
+
ax.plot(history.history['accuracy'], label='Train Accuracy')
|
58 |
+
ax.plot(history.history['val_accuracy'], label='Val Accuracy')
|
59 |
+
ax.set_title("Training Progress")
|
60 |
+
ax.set_xlabel("Epoch")
|
61 |
+
ax.set_ylabel("Accuracy")
|
62 |
+
ax.legend()
|
63 |
+
st.pyplot(fig)
|
64 |
+
|
65 |
+
# Final Accuracy
|
66 |
+
train_acc = model.evaluate(X_train, y_train, verbose=0)[1]
|
67 |
+
test_acc = model.evaluate(X_test, y_test, verbose=0)[1]
|
68 |
+
st.success(f"✅ Final Training Accuracy: {train_acc:.2f}")
|
69 |
+
st.success(f"✅ Final Testing Accuracy: {test_acc:.2f}")
|