Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 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 |
-
const
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
gr.Interface(fn=lambda: None, inputs=[], outputs=gr.HTML(html), live=True).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
def make_html(training_text, iterations, gen_length):
|
| 4 |
+
# Sanitize text for JS (escape quotes and backslashes)
|
| 5 |
+
safe_text = training_text.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n")
|
| 6 |
+
|
| 7 |
+
html = f"""
|
| 8 |
+
<div>
|
| 9 |
+
<h3>Output:</h3>
|
| 10 |
+
<pre id="output">Training...</pre>
|
| 11 |
+
<script src="https://karpathy.github.io/recurrentjs/recurrent.js"></script>
|
| 12 |
+
<script>
|
| 13 |
+
const text = "{safe_text}".split("");
|
| 14 |
+
const iterations = {iterations};
|
| 15 |
+
const genLength = {gen_length};
|
| 16 |
+
|
| 17 |
+
const lstm = new RNN("lstm", {{ hiddenSizes: [128] }});
|
| 18 |
+
const trainer = new RNNTrainer(lstm, {{ learningRate: 0.01, momentum: 0.1, batchSize: 5 }});
|
| 19 |
+
|
| 20 |
+
for (let i = 0; i < iterations; i++) {{
|
| 21 |
+
const idx = Math.floor(Math.random() * (text.length - 10));
|
| 22 |
+
const input = text.slice(idx, idx + 5);
|
| 23 |
+
const output = text.slice(idx + 1, idx + 6);
|
| 24 |
+
trainer.train(input, output);
|
| 25 |
+
}}
|
| 26 |
+
|
| 27 |
+
const generated = lstm.sample(["H"], genLength).join("");
|
| 28 |
+
document.getElementById("output").innerText = generated;
|
| 29 |
+
</script>
|
| 30 |
+
</div>
|
| 31 |
+
"""
|
| 32 |
+
return html
|
| 33 |
+
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("## 🧠 LSTM Trainer with recurrent.js (Browser-Based)")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
training_text = gr.Textbox(label="Training Text", lines=8, placeholder="Paste your training text here...")
|
| 39 |
+
|
| 40 |
+
with gr.Row():
|
| 41 |
+
iterations = gr.Slider(1, 100, value=20, step=1, label="Training Iterations")
|
| 42 |
+
gen_length = gr.Slider(10, 500, value=100, step=10, label="Characters to Generate")
|
| 43 |
+
|
| 44 |
+
run_button = gr.Button("Train and Generate")
|
| 45 |
+
output_html = gr.HTML()
|
| 46 |
+
|
| 47 |
+
run_button.click(fn=make_html, inputs=[training_text, iterations, gen_length], outputs=output_html)
|
| 48 |
+
|
| 49 |
+
demo.launch()
|
|
|
|
|
|