Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
import threading
|
| 6 |
+
from gradio_client import Client
|
| 7 |
+
|
| 8 |
+
# Configuration
|
| 9 |
+
SPACE_ID = "Sachin5112/Bihgfgh"
|
| 10 |
+
STORAGE_PATH = "/data/evolution_state.json" if os.path.exists("/data") else "evolution_state.json"
|
| 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>Evolution loop starting...</p></div></body></html>"""
|
| 15 |
+
self.gen = 0
|
| 16 |
+
self.is_running = False
|
| 17 |
+
self.logs = []
|
| 18 |
+
self.load()
|
| 19 |
+
|
| 20 |
+
def save(self):
|
| 21 |
+
data = {"code": self.code, "gen": self.gen, "is_running": self.is_running}
|
| 22 |
+
with open(STORAGE_PATH, "w") as f:
|
| 23 |
+
json.dump(data, f)
|
| 24 |
+
|
| 25 |
+
def load(self):
|
| 26 |
+
if os.path.exists(STORAGE_PATH):
|
| 27 |
+
try:
|
| 28 |
+
with open(STORAGE_PATH, "r") as f:
|
| 29 |
+
data = json.load(f)
|
| 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 |
+
pass
|
| 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) > 20: self.logs.pop(0)
|
| 40 |
+
|
| 41 |
+
state = ArchitectState()
|
| 42 |
+
|
| 43 |
+
def extract_html(text):
|
| 44 |
+
start = text.lower().find("<html")
|
| 45 |
+
end = text.lower().rfind("</html>")
|
| 46 |
+
if start != -1 and end != -1:
|
| 47 |
+
return text[start:end+7]
|
| 48 |
+
return text
|
| 49 |
+
|
| 50 |
+
def evolution_loop():
|
| 51 |
+
while True:
|
| 52 |
+
if state.is_running:
|
| 53 |
+
try:
|
| 54 |
+
state.add_log(f"Starting Gen {state.gen + 1}...")
|
| 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 |
+
# Connect to the AI Space
|
| 59 |
+
result = client.predict(
|
| 60 |
+
message=prompt,
|
| 61 |
+
api_name="/chat"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
raw_response = result if isinstance(result, str) else result[0]
|
| 65 |
+
new_code = extract_html(raw_response)
|
| 66 |
+
|
| 67 |
+
if len(new_code) > 100:
|
| 68 |
+
state.code = new_code
|
| 69 |
+
state.gen += 1
|
| 70 |
+
state.add_log(f"Gen {state.gen} success. Saved to persistent storage.")
|
| 71 |
+
state.save()
|
| 72 |
+
else:
|
| 73 |
+
state.add_log("Error: AI returned invalid code.")
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
state.add_log(f"Loop Error: {str(e)}")
|
| 77 |
+
|
| 78 |
+
time.sleep(15) # Safety pause
|
| 79 |
+
else:
|
| 80 |
+
time.sleep(2)
|
| 81 |
+
|
| 82 |
+
# Start background thread
|
| 83 |
+
thread = threading.Thread(target=evolution_loop, daemon=True)
|
| 84 |
+
thread.start()
|
| 85 |
+
|
| 86 |
+
# Gradio Interface
|
| 87 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="footer {display:none !important}") as demo:
|
| 88 |
+
gr.Markdown("# 🏗️ ARCHITECT V15 - BACKGROUND EVOLUTION")
|
| 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 |
+
with gr.Accordion("View Source Code", open=False):
|
| 101 |
+
code_view = gr.Code(value=state.code, language="html", interactive=False)
|
| 102 |
+
|
| 103 |
+
def start_loop():
|
| 104 |
+
state.is_running = True
|
| 105 |
+
state.save()
|
| 106 |
+
return gr.update(value="### 🟢 STATUS: RUNNING"), "\n".join(state.logs)
|
| 107 |
+
|
| 108 |
+
def stop_loop():
|
| 109 |
+
state.is_running = False
|
| 110 |
+
state.save()
|
| 111 |
+
return gr.update(value="### 🔴 STATUS: STOPPED"), "\n".join(state.logs)
|
| 112 |
+
|
| 113 |
+
def update_ui():
|
| 114 |
+
return state.code, f"**Current Gen:** {state.gen}", "\n".join(state.logs), state.code
|
| 115 |
+
|
| 116 |
+
run_btn.click(start_loop, None, [status_label, log_display])
|
| 117 |
+
stop_btn.click(stop_loop, None, [status_label, log_display])
|
| 118 |
+
|
| 119 |
+
# Auto-refresh UI every 5 seconds
|
| 120 |
+
timer = gr.Timer(5)
|
| 121 |
+
timer.tick(update_ui, None, [preview, status_label, log_display, code_view])
|
| 122 |
+
|
| 123 |
+
demo.launch()
|