Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,23 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
# Sanitize training text for JS
|
| 5 |
safe_text = training_text.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
<h3>Live LSTM Training Output (updates every 10s)</h3>
|
| 10 |
<pre id="output">Starting training...</pre>
|
| 11 |
-
<script src="https://karpathy.github.io/recurrentjs/recurrent.js"></script>
|
| 12 |
<script>
|
| 13 |
const text = "{safe_text}".split("");
|
| 14 |
const totalIterations = {iterations};
|
| 15 |
const genLength = {gen_length};
|
| 16 |
|
| 17 |
-
const lstm = new RNN("lstm", {{ hiddenSizes: [{
|
| 18 |
const trainer = new RNNTrainer(lstm, {{ learningRate: 0.01, momentum: 0.1, batchSize: 5 }});
|
| 19 |
|
| 20 |
let iteration = 0;
|
|
@@ -33,28 +36,35 @@ def make_html(training_text, iterations, gen_length, layer1_size, layer2_size):
|
|
| 33 |
clearInterval(interval);
|
| 34 |
document.getElementById("output").innerText += "\\n\\nTraining complete!";
|
| 35 |
}}
|
| 36 |
-
}}, 10000);
|
| 37 |
</script>
|
| 38 |
-
</
|
|
|
|
| 39 |
"""
|
| 40 |
-
return html
|
| 41 |
-
|
| 42 |
-
with gr.Blocks() as demo:
|
| 43 |
-
gr.Markdown("## 🧠 Live LSTM Trainer with recurrent.js (Browser-Based)")
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gen_length = gr.Slider(20, 500, value=100, step=10, label="Characters to Generate")
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
run_button = gr.Button("Start Training")
|
| 56 |
-
|
| 57 |
|
| 58 |
-
run_button.click(fn=
|
|
|
|
|
|
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
|
| 4 |
+
def generate_html(training_text, iterations, gen_length, layer1, layer2):
|
|
|
|
| 5 |
safe_text = training_text.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n")
|
| 6 |
|
| 7 |
+
html_content = f"""
|
| 8 |
+
<html>
|
| 9 |
+
<head>
|
| 10 |
+
<script src="https://karpathy.github.io/recurrentjs/recurrent.js"></script>
|
| 11 |
+
</head>
|
| 12 |
+
<body>
|
| 13 |
<h3>Live LSTM Training Output (updates every 10s)</h3>
|
| 14 |
<pre id="output">Starting training...</pre>
|
|
|
|
| 15 |
<script>
|
| 16 |
const text = "{safe_text}".split("");
|
| 17 |
const totalIterations = {iterations};
|
| 18 |
const genLength = {gen_length};
|
| 19 |
|
| 20 |
+
const lstm = new RNN("lstm", {{ hiddenSizes: [{layer1}, {layer2}] }});
|
| 21 |
const trainer = new RNNTrainer(lstm, {{ learningRate: 0.01, momentum: 0.1, batchSize: 5 }});
|
| 22 |
|
| 23 |
let iteration = 0;
|
|
|
|
| 36 |
clearInterval(interval);
|
| 37 |
document.getElementById("output").innerText += "\\n\\nTraining complete!";
|
| 38 |
}}
|
| 39 |
+
}}, 10000);
|
| 40 |
</script>
|
| 41 |
+
</body>
|
| 42 |
+
</html>
|
| 43 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Save the HTML to the /tmp directory (accessible in Hugging Face Spaces)
|
| 46 |
+
html_path = "/tmp/train.html"
|
| 47 |
+
with open(html_path, "w") as f:
|
| 48 |
+
f.write(html_content)
|
| 49 |
+
|
| 50 |
+
# Return iframe HTML to embed the file
|
| 51 |
+
iframe_code = f'<iframe src="file={html_path}" width="100%" height="400px"></iframe>'
|
| 52 |
+
return iframe_code
|
| 53 |
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("## 🧠 Live recurrent.js LSTM Trainer (in-browser training)")
|
|
|
|
| 56 |
|
| 57 |
+
training_text = gr.Textbox(label="Training Text", lines=6, placeholder="Paste text here")
|
| 58 |
+
iterations = gr.Slider(10, 200, value=50, step=10, label="Training Epochs")
|
| 59 |
+
gen_length = gr.Slider(20, 500, value=100, step=10, label="Characters to Generate")
|
| 60 |
+
layer1 = gr.Slider(16, 256, value=128, step=16, label="Neurons in Layer 1")
|
| 61 |
+
layer2 = gr.Slider(16, 256, value=128, step=16, label="Neurons in Layer 2")
|
| 62 |
|
| 63 |
run_button = gr.Button("Start Training")
|
| 64 |
+
html_output = gr.HTML()
|
| 65 |
|
| 66 |
+
run_button.click(fn=generate_html,
|
| 67 |
+
inputs=[training_text, iterations, gen_length, layer1, layer2],
|
| 68 |
+
outputs=html_output)
|
| 69 |
|
| 70 |
demo.launch()
|