Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ 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 |
|
|
@@ -15,7 +14,7 @@ class ArchitectState:
|
|
| 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 = ""
|
| 19 |
self.logs = []
|
| 20 |
self.load()
|
| 21 |
|
|
@@ -54,11 +53,11 @@ def extract_html(text):
|
|
| 54 |
return text
|
| 55 |
|
| 56 |
def evolution_worker():
|
| 57 |
-
"""Background thread
|
| 58 |
while True:
|
| 59 |
if state.is_running:
|
| 60 |
try:
|
| 61 |
-
state.add_log(f"Gen {state.gen + 1}: Connecting
|
| 62 |
client = Client(SPACE_ID)
|
| 63 |
|
| 64 |
prompt = (
|
|
@@ -68,69 +67,67 @@ def evolution_worker():
|
|
| 68 |
f"Current Code: {state.code[:1000]}..."
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# Use submit
|
| 72 |
job = client.submit(message=prompt, api_name="/chat")
|
| 73 |
|
| 74 |
-
|
| 75 |
-
state.add_log("Streaming started...")
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
#
|
| 90 |
-
final_raw =
|
| 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 = ""
|
| 97 |
state.add_log(f"Success! Gen {state.gen} saved to bucket.")
|
| 98 |
state.save()
|
| 99 |
else:
|
| 100 |
-
state.add_log("Warning:
|
| 101 |
|
| 102 |
except Exception as e:
|
| 103 |
state.add_log(f"Stream Error: {str(e)}")
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
time.sleep(10)
|
| 107 |
else:
|
| 108 |
time.sleep(2)
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
worker = threading.Thread(target=evolution_worker, daemon=True)
|
| 112 |
worker.start()
|
| 113 |
|
| 114 |
-
# --- UI
|
| 115 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=".log-box textarea { font-family: monospace; font-size: 11px; }") as demo:
|
| 116 |
-
gr.Markdown("# π§± ARCHITECT
|
| 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
|
| 126 |
-
stop_btn = gr.Button("π STOP
|
| 127 |
-
logs = gr.Textbox(label="
|
| 128 |
|
| 129 |
with gr.Tabs():
|
| 130 |
-
with gr.Tab("Live Stream
|
| 131 |
-
live_code = gr.Code(label="Streaming
|
| 132 |
-
with gr.Tab("Last
|
| 133 |
-
full_code = gr.Code(label="
|
| 134 |
|
| 135 |
def toggle_start():
|
| 136 |
state.is_running = True
|
|
@@ -143,11 +140,8 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=".log-box textarea { font-famil
|
|
| 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,
|
| 151 |
f"### STATUS: {'RUNNING' if state.is_running else 'STOPPED'}\n**Generation:** {state.gen}",
|
| 152 |
"\n".join(state.logs),
|
| 153 |
state.current_stream,
|
|
@@ -157,7 +151,6 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=".log-box textarea { font-famil
|
|
| 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 |
|
|
|
|
| 6 |
from gradio_client import Client
|
| 7 |
|
| 8 |
# Configuration for HF Persistent Storage
|
|
|
|
| 9 |
STORAGE_DIR = "/data"
|
| 10 |
STORAGE_PATH = os.path.join(STORAGE_DIR, "evolution_state.json") if os.path.exists(STORAGE_DIR) else "evolution_state.json"
|
| 11 |
|
|
|
|
| 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>Waiting for evolution loop to start...</p></div></body></html>"""
|
| 15 |
self.gen = 0
|
| 16 |
self.is_running = False
|
| 17 |
+
self.current_stream = ""
|
| 18 |
self.logs = []
|
| 19 |
self.load()
|
| 20 |
|
|
|
|
| 53 |
return text
|
| 54 |
|
| 55 |
def evolution_worker():
|
| 56 |
+
"""Background thread using the corrected Gradio iterator for live streaming."""
|
| 57 |
while True:
|
| 58 |
if state.is_running:
|
| 59 |
try:
|
| 60 |
+
state.add_log(f"Gen {state.gen + 1}: Connecting...")
|
| 61 |
client = Client(SPACE_ID)
|
| 62 |
|
| 63 |
prompt = (
|
|
|
|
| 67 |
f"Current Code: {state.code[:1000]}..."
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Use submit to get a job
|
| 71 |
job = client.submit(message=prompt, api_name="/chat")
|
| 72 |
|
| 73 |
+
state.add_log("Stream started...")
|
|
|
|
| 74 |
|
| 75 |
+
# The correct way to stream in recent Gradio versions is iterating over the job
|
| 76 |
+
for output in job:
|
| 77 |
+
if not state.active_check(): break
|
| 78 |
+
|
| 79 |
+
# Gradio chat usually returns a list or tuple; we want the string part
|
| 80 |
+
if isinstance(output, (list, tuple)):
|
| 81 |
+
chunk = output[0]
|
| 82 |
+
else:
|
| 83 |
+
chunk = output
|
| 84 |
+
|
| 85 |
+
state.current_stream = chunk
|
| 86 |
+
|
| 87 |
+
# Final processing
|
| 88 |
+
final_raw = state.current_stream
|
| 89 |
processed_code = extract_html(final_raw)
|
| 90 |
|
| 91 |
if len(processed_code) > 200:
|
| 92 |
state.code = processed_code
|
| 93 |
state.gen += 1
|
| 94 |
+
state.current_stream = ""
|
| 95 |
state.add_log(f"Success! Gen {state.gen} saved to bucket.")
|
| 96 |
state.save()
|
| 97 |
else:
|
| 98 |
+
state.add_log("Warning: Invalid code received. Retrying...")
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
state.add_log(f"Stream Error: {str(e)}")
|
| 102 |
|
| 103 |
+
time.sleep(12) # Cooldown
|
|
|
|
| 104 |
else:
|
| 105 |
time.sleep(2)
|
| 106 |
|
| 107 |
+
state.active_check = lambda: state.is_running
|
| 108 |
+
|
| 109 |
+
# Start worker
|
| 110 |
worker = threading.Thread(target=evolution_worker, daemon=True)
|
| 111 |
worker.start()
|
| 112 |
|
| 113 |
+
# --- UI ---
|
| 114 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=".log-box textarea { font-family: monospace; font-size: 11px; }") as demo:
|
| 115 |
+
gr.Markdown("# π§± ARCHITECT V17 - FIXED STREAMER")
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
with gr.Column(scale=3):
|
|
|
|
| 119 |
preview = gr.HTML(value=state.code)
|
|
|
|
| 120 |
with gr.Column(scale=1):
|
| 121 |
status = gr.Markdown(f"### STATUS: {'RUNNING' if state.is_running else 'STOPPED'}\n**Generation:** {state.gen}")
|
| 122 |
+
start_btn = gr.Button("π START", variant="primary")
|
| 123 |
+
stop_btn = gr.Button("π STOP")
|
| 124 |
+
logs = gr.Textbox(label="Logs", value="\n".join(state.logs), lines=8, interactive=False, elem_classes="log-box")
|
| 125 |
|
| 126 |
with gr.Tabs():
|
| 127 |
+
with gr.Tab("Live Stream"):
|
| 128 |
+
live_code = gr.Code(label="Streaming...", value=state.current_stream, language="html", interactive=False)
|
| 129 |
+
with gr.Tab("Last Stable"):
|
| 130 |
+
full_code = gr.Code(label="Current Saved Code", value=state.code, language="html", interactive=False)
|
| 131 |
|
| 132 |
def toggle_start():
|
| 133 |
state.is_running = True
|
|
|
|
| 140 |
return "### STATUS: STOPPED", "\n".join(state.logs)
|
| 141 |
|
| 142 |
def refresh_ui():
|
|
|
|
|
|
|
|
|
|
| 143 |
return (
|
| 144 |
+
state.code,
|
| 145 |
f"### STATUS: {'RUNNING' if state.is_running else 'STOPPED'}\n**Generation:** {state.gen}",
|
| 146 |
"\n".join(state.logs),
|
| 147 |
state.current_stream,
|
|
|
|
| 151 |
start_btn.click(toggle_start, None, [status, logs])
|
| 152 |
stop_btn.click(toggle_stop, None, [status, logs])
|
| 153 |
|
|
|
|
| 154 |
timer = gr.Timer(2)
|
| 155 |
timer.tick(refresh_ui, None, [preview, status, logs, live_code, full_code])
|
| 156 |
|