Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,22 +5,27 @@ import time
|
|
| 5 |
import threading
|
| 6 |
from gradio_client import Client
|
| 7 |
|
| 8 |
-
# Configuration
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
class ArchitectState:
|
| 13 |
def __init__(self):
|
| 14 |
-
self.code = """<!DOCTYPE html><html><body style="background:#000;color:#555;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;font-family:sans-serif;"><div><h1>SYSTEM READY</h1><p>
|
| 15 |
self.gen = 0
|
| 16 |
self.is_running = False
|
|
|
|
| 17 |
self.logs = []
|
| 18 |
self.load()
|
| 19 |
|
| 20 |
def save(self):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def load(self):
|
| 26 |
if os.path.exists(STORAGE_PATH):
|
|
@@ -30,15 +35,16 @@ class ArchitectState:
|
|
| 30 |
self.code = data.get("code", self.code)
|
| 31 |
self.gen = data.get("gen", self.gen)
|
| 32 |
self.is_running = data.get("is_running", False)
|
| 33 |
-
except:
|
| 34 |
-
|
| 35 |
|
| 36 |
def add_log(self, text):
|
| 37 |
timestamp = time.strftime("%H:%M:%S")
|
| 38 |
self.logs.append(f"[{timestamp}] {text}")
|
| 39 |
-
if len(self.logs) >
|
| 40 |
|
| 41 |
state = ArchitectState()
|
|
|
|
| 42 |
|
| 43 |
def extract_html(text):
|
| 44 |
start = text.lower().find("<html")
|
|
@@ -47,77 +53,112 @@ def extract_html(text):
|
|
| 47 |
return text[start:end+7]
|
| 48 |
return text
|
| 49 |
|
| 50 |
-
def
|
|
|
|
| 51 |
while True:
|
| 52 |
if state.is_running:
|
| 53 |
try:
|
| 54 |
-
state.add_log(f"
|
| 55 |
client = Client(SPACE_ID)
|
| 56 |
-
prompt = f"Task: Improved 3D Minecraft Mobile Clone using Three.js. Gen: {state.gen+1}. Output ONLY full <html> code. Current: {state.code[:500]}..."
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
)
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
state.gen += 1
|
| 70 |
-
state.
|
|
|
|
| 71 |
state.save()
|
| 72 |
else:
|
| 73 |
-
state.add_log("
|
| 74 |
-
|
| 75 |
except Exception as e:
|
| 76 |
-
state.add_log(f"
|
| 77 |
|
| 78 |
-
|
|
|
|
| 79 |
else:
|
| 80 |
time.sleep(2)
|
| 81 |
|
| 82 |
-
# Start
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
with gr.Blocks(theme=gr.themes.
|
| 88 |
-
gr.Markdown("#
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
with gr.Column(scale=3):
|
|
|
|
| 92 |
preview = gr.HTML(value=state.code)
|
| 93 |
-
status_label = gr.Markdown(f"**Current Gen:** {state.gen}")
|
| 94 |
-
|
| 95 |
-
with gr.Column(scale=1):
|
| 96 |
-
run_btn = gr.Button("START EVOLUTION", variant="primary")
|
| 97 |
-
stop_btn = gr.Button("STOP")
|
| 98 |
-
log_display = gr.Textbox(label="System Logs", lines=10, value="\n".join(state.logs), interactive=False)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
state.is_running = True
|
| 105 |
state.save()
|
| 106 |
-
return
|
| 107 |
|
| 108 |
-
def
|
| 109 |
state.is_running = False
|
| 110 |
state.save()
|
| 111 |
-
return
|
| 112 |
|
| 113 |
-
def
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
stop_btn.click(
|
| 118 |
-
|
| 119 |
-
#
|
| 120 |
-
timer = gr.Timer(
|
| 121 |
-
timer.tick(
|
| 122 |
|
| 123 |
demo.launch()
|
|
|
|
| 5 |
import threading
|
| 6 |
from gradio_client import Client
|
| 7 |
|
| 8 |
+
# Configuration for HF Persistent Storage
|
| 9 |
+
# The /data directory is the standard mount point for HF persistent buckets
|
| 10 |
+
STORAGE_DIR = "/data"
|
| 11 |
+
STORAGE_PATH = os.path.join(STORAGE_DIR, "evolution_state.json") if os.path.exists(STORAGE_DIR) else "evolution_state.json"
|
| 12 |
|
| 13 |
class ArchitectState:
|
| 14 |
def __init__(self):
|
| 15 |
+
self.code = """<!DOCTYPE html><html><body style="background:#000;color:#555;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;font-family:sans-serif;"><div><h1>SYSTEM READY</h1><p>Waiting for evolution loop to start...</p></div></body></html>"""
|
| 16 |
self.gen = 0
|
| 17 |
self.is_running = False
|
| 18 |
+
self.current_stream = "" # For live typing effect
|
| 19 |
self.logs = []
|
| 20 |
self.load()
|
| 21 |
|
| 22 |
def save(self):
|
| 23 |
+
try:
|
| 24 |
+
data = {"code": self.code, "gen": self.gen, "is_running": self.is_running}
|
| 25 |
+
with open(STORAGE_PATH, "w") as f:
|
| 26 |
+
json.dump(data, f)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Save error: {e}")
|
| 29 |
|
| 30 |
def load(self):
|
| 31 |
if os.path.exists(STORAGE_PATH):
|
|
|
|
| 35 |
self.code = data.get("code", self.code)
|
| 36 |
self.gen = data.get("gen", self.gen)
|
| 37 |
self.is_running = data.get("is_running", False)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Load error: {e}")
|
| 40 |
|
| 41 |
def add_log(self, text):
|
| 42 |
timestamp = time.strftime("%H:%M:%S")
|
| 43 |
self.logs.append(f"[{timestamp}] {text}")
|
| 44 |
+
if len(self.logs) > 15: self.logs.pop(0)
|
| 45 |
|
| 46 |
state = ArchitectState()
|
| 47 |
+
SPACE_ID = "Sachin5112/Bihgfgh"
|
| 48 |
|
| 49 |
def extract_html(text):
|
| 50 |
start = text.lower().find("<html")
|
|
|
|
| 53 |
return text[start:end+7]
|
| 54 |
return text
|
| 55 |
|
| 56 |
+
def evolution_worker():
|
| 57 |
+
"""Background thread that runs forever, streaming tokens and saving to disk."""
|
| 58 |
while True:
|
| 59 |
if state.is_running:
|
| 60 |
try:
|
| 61 |
+
state.add_log(f"Gen {state.gen + 1}: Connecting to {SPACE_ID}...")
|
| 62 |
client = Client(SPACE_ID)
|
|
|
|
| 63 |
|
| 64 |
+
prompt = (
|
| 65 |
+
f"Task: Improve the 3D Minecraft Mobile Clone using Three.js. "
|
| 66 |
+
f"Current Generation: {state.gen}. "
|
| 67 |
+
f"STRICT: Output ONLY the full <html> source code. No conversational text. "
|
| 68 |
+
f"Current Code: {state.code[:1000]}..."
|
| 69 |
)
|
| 70 |
+
|
| 71 |
+
# Use submit for streaming
|
| 72 |
+
job = client.submit(message=prompt, api_name="/chat")
|
| 73 |
|
| 74 |
+
temp_output = ""
|
| 75 |
+
state.add_log("Streaming started...")
|
| 76 |
|
| 77 |
+
while not job.done():
|
| 78 |
+
# Get current updates from the stream
|
| 79 |
+
updates = job.communicator.get_updates()
|
| 80 |
+
if updates:
|
| 81 |
+
# Grab the latest data chunk
|
| 82 |
+
latest_data = updates[-1].data
|
| 83 |
+
if latest_data and len(latest_data) > 0:
|
| 84 |
+
temp_output = latest_data[0]
|
| 85 |
+
# Update global state live so UI can pick it up
|
| 86 |
+
state.current_stream = temp_output
|
| 87 |
+
time.sleep(0.1)
|
| 88 |
+
|
| 89 |
+
# Finalize after stream ends
|
| 90 |
+
final_raw = job.outputs()[-1][0] if job.outputs() else temp_output
|
| 91 |
+
processed_code = extract_html(final_raw)
|
| 92 |
+
|
| 93 |
+
if len(processed_code) > 200:
|
| 94 |
+
state.code = processed_code
|
| 95 |
state.gen += 1
|
| 96 |
+
state.current_stream = "" # Clear stream
|
| 97 |
+
state.add_log(f"Success! Gen {state.gen} saved to bucket.")
|
| 98 |
state.save()
|
| 99 |
else:
|
| 100 |
+
state.add_log("Warning: Received code too short. Retrying...")
|
| 101 |
+
|
| 102 |
except Exception as e:
|
| 103 |
+
state.add_log(f"Stream Error: {str(e)}")
|
| 104 |
|
| 105 |
+
# Wait 10 seconds before starting next gen
|
| 106 |
+
time.sleep(10)
|
| 107 |
else:
|
| 108 |
time.sleep(2)
|
| 109 |
|
| 110 |
+
# Start the worker thread once
|
| 111 |
+
worker = threading.Thread(target=evolution_worker, daemon=True)
|
| 112 |
+
worker.start()
|
| 113 |
|
| 114 |
+
# --- UI Setup ---
|
| 115 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), css=".log-box textarea { font-family: monospace; font-size: 11px; }") as demo:
|
| 116 |
+
gr.Markdown("# 🧱 ARCHITECT V16 - BACKGROUND STREAMER")
|
| 117 |
|
| 118 |
with gr.Row():
|
| 119 |
with gr.Column(scale=3):
|
| 120 |
+
# The Live Preview
|
| 121 |
preview = gr.HTML(value=state.code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
with gr.Column(scale=1):
|
| 124 |
+
status = gr.Markdown(f"### STATUS: {'RUNNING' if state.is_running else 'STOPPED'}\n**Generation:** {state.gen}")
|
| 125 |
+
start_btn = gr.Button("🚀 START LOOP", variant="primary")
|
| 126 |
+
stop_btn = gr.Button("🛑 STOP LOOP")
|
| 127 |
+
logs = gr.Textbox(label="Live Logs", value="\n".join(state.logs), lines=8, interactive=False, elem_classes="log-box")
|
| 128 |
|
| 129 |
+
with gr.Tabs():
|
| 130 |
+
with gr.Tab("Live Stream (Raw)"):
|
| 131 |
+
live_code = gr.Code(label="Streaming View", value=state.current_stream, language="html", interactive=False)
|
| 132 |
+
with gr.Tab("Last Success (Full)"):
|
| 133 |
+
full_code = gr.Code(label="Stable Code", value=state.code, language="html", interactive=False)
|
| 134 |
+
|
| 135 |
+
def toggle_start():
|
| 136 |
state.is_running = True
|
| 137 |
state.save()
|
| 138 |
+
return "### STATUS: RUNNING", "\n".join(state.logs)
|
| 139 |
|
| 140 |
+
def toggle_stop():
|
| 141 |
state.is_running = False
|
| 142 |
state.save()
|
| 143 |
+
return "### STATUS: STOPPED", "\n".join(state.logs)
|
| 144 |
|
| 145 |
+
def refresh_ui():
|
| 146 |
+
# This function updates the UI with the latest state from the background thread
|
| 147 |
+
# If streaming is happening, show the stream. Otherwise show the saved code.
|
| 148 |
+
display_code = state.current_stream if state.current_stream else state.code
|
| 149 |
+
return (
|
| 150 |
+
state.code, # update preview
|
| 151 |
+
f"### STATUS: {'RUNNING' if state.is_running else 'STOPPED'}\n**Generation:** {state.gen}",
|
| 152 |
+
"\n".join(state.logs),
|
| 153 |
+
state.current_stream,
|
| 154 |
+
state.code
|
| 155 |
+
)
|
| 156 |
|
| 157 |
+
start_btn.click(toggle_start, None, [status, logs])
|
| 158 |
+
stop_btn.click(toggle_stop, None, [status, logs])
|
| 159 |
+
|
| 160 |
+
# Refresh every 2 seconds to show live typing
|
| 161 |
+
timer = gr.Timer(2)
|
| 162 |
+
timer.tick(refresh_ui, None, [preview, status, logs, live_code, full_code])
|
| 163 |
|
| 164 |
demo.launch()
|