Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,83 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import zipfile
|
| 4 |
-
import gradio as gr
|
| 5 |
import subprocess
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
with gr.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
log_box = gr.Textbox(label="Live Training Logs", lines=20, interactive=False, value="")
|
| 87 |
-
download_file = gr.File(label="π₯ Download Trained Model (.zip)", visible=False)
|
| 88 |
-
|
| 89 |
-
train_btn.click(
|
| 90 |
-
fn=start_training,
|
| 91 |
-
inputs=[status_box, time_box, download_file, model_size_box, log_box],
|
| 92 |
-
outputs=[status_box]
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
|
|
|
|
|
|
| 3 |
import subprocess
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
uploaded_file_path = "dataset.jsonl"
|
| 8 |
+
log_path = "train.log"
|
| 9 |
+
model_dir = "trained_model"
|
| 10 |
+
zip_file = "trained_model.zip"
|
| 11 |
+
|
| 12 |
+
# Try loading the generator model once at the top (for better performance)
|
| 13 |
+
try:
|
| 14 |
+
generator = pipeline("text-generation", model=model_dir, tokenizer="distilgpt2")
|
| 15 |
+
except:
|
| 16 |
+
generator = None
|
| 17 |
+
|
| 18 |
+
def upload_file(file):
|
| 19 |
+
if file is None:
|
| 20 |
+
return "No file uploaded."
|
| 21 |
+
shutil.copy(file.name, uploaded_file_path)
|
| 22 |
+
return "β
File uploaded successfully."
|
| 23 |
+
|
| 24 |
+
def start_training():
|
| 25 |
+
with open(log_path, "w") as log_file:
|
| 26 |
+
process = subprocess.Popen(
|
| 27 |
+
["python", "train.py", "--dataset", uploaded_file_path],
|
| 28 |
+
stdout=log_file,
|
| 29 |
+
stderr=subprocess.STDOUT
|
| 30 |
+
)
|
| 31 |
+
process.wait()
|
| 32 |
+
|
| 33 |
+
if os.path.exists(model_dir):
|
| 34 |
+
shutil.make_archive("trained_model", "zip", model_dir)
|
| 35 |
+
return "β
Training complete!", zip_file
|
| 36 |
+
else:
|
| 37 |
+
return "β Training failed.", None
|
| 38 |
+
|
| 39 |
+
def read_logs():
|
| 40 |
+
if os.path.exists(log_path):
|
| 41 |
+
with open(log_path, "r") as f:
|
| 42 |
+
return f.read()
|
| 43 |
+
return "Waiting for logs..."
|
| 44 |
+
|
| 45 |
+
def generate_response(prompt):
|
| 46 |
+
try:
|
| 47 |
+
if generator is None:
|
| 48 |
+
return "β Model not loaded. Please train or upload a valid model."
|
| 49 |
+
result = generator(
|
| 50 |
+
prompt,
|
| 51 |
+
max_length=256,
|
| 52 |
+
do_sample=True,
|
| 53 |
+
temperature=0.7,
|
| 54 |
+
truncation=True # β
Fix warning and enforce consistent length
|
| 55 |
+
)[0]["generated_text"]
|
| 56 |
+
return result
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"β Error: {e}"
|
| 59 |
+
|
| 60 |
+
# === UI ===
|
| 61 |
+
with gr.Blocks() as app:
|
| 62 |
+
with gr.Tab("π§ Train AI"):
|
| 63 |
+
gr.Markdown("## π₯ Upload your dataset and π― train a Godot AI")
|
| 64 |
+
file_input = gr.File(label="Upload JSONL Dataset")
|
| 65 |
+
upload_btn = gr.Button("Upload")
|
| 66 |
+
status_box = gr.Textbox(label="Upload Status")
|
| 67 |
+
|
| 68 |
+
start_btn = gr.Button("π Start Training")
|
| 69 |
+
log_output = gr.Textbox(label="π Training Logs", lines=15)
|
| 70 |
+
download_btn = gr.File(label="π₯ Download Trained Model", visible=False)
|
| 71 |
+
|
| 72 |
+
upload_btn.click(fn=upload_file, inputs=file_input, outputs=status_box)
|
| 73 |
+
start_btn.click(fn=start_training, outputs=[status_box, download_btn])
|
| 74 |
+
start_btn.click(fn=read_logs, outputs=log_output)
|
| 75 |
+
|
| 76 |
+
with gr.Tab("π Test AI"):
|
| 77 |
+
gr.Markdown("## π‘ Try your trained Godot AI below")
|
| 78 |
+
prompt_input = gr.Textbox(label="Enter Prompt")
|
| 79 |
+
test_btn = gr.Button("π Test AI")
|
| 80 |
+
response_output = gr.Textbox(label="AI Response", lines=10)
|
| 81 |
+
test_btn.click(fn=generate_response, inputs=prompt_input, outputs=response_output)
|
| 82 |
+
|
| 83 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|