from __future__ import annotations import time import traceback import gradio as gr from enhancer import enhance_prompt from runner import render_task from runner_client import RunnerClient ORIGIN = "https://peerpixel.cc" def make_client(): return RunnerClient(ORIGIN) def run_once(worker_key: str): client = make_client() client.connect(worker_key) yield "Waiting for a PeerPixel task", 0, "" task = client.claim() if task is None: return try: if task.get("stage") == "enhance": yield "Enhancing the prompt on CPU", 0, "" client.complete_enhancement(enhance_prompt(task["prompt"])) yield "Enhanced — render is ready to claim", 0, "" return yield "Task found — requesting ZeroGPU", 0, "" source = client.source() client.state = "rendering" result = render_task(task, source) yield "Uploading your completed image", 0, "" client.upload(result) yield "Completed — waiting for another task", 1, "" except Exception as error: traceback.print_exc() client.release(type(error).__name__) yield "That task was released safely. You can reconnect.", 0, "" def begin(worker_key: str): if not str(worker_key or "").strip(): raise gr.Error("Paste the worker key an admin gave you first.") client = make_client() client.connect(worker_key) return client, "Waiting for a PeerPixel task", "" def handle_task(client: RunnerClient, task: dict, completed: int): if task.get("stage") == "enhance": client.state = "enhancing" client.complete_enhancement(enhance_prompt(task["prompt"])) return "Enhanced prompt on CPU — render is ready", completed client.state = "rendering" result = render_task(task, client.source()) client.upload(result) return "Completed — waiting for another task", completed + 1 def wait_and_run(client: RunnerClient): completed = 0 while client and client.state != "disconnected": task = client.claim() if task is None: yield "Waiting for a PeerPixel task", completed time.sleep(3) continue try: yield ("Enhancing prompt on CPU" if task.get("stage") == "enhance" else "Task found — requesting ZeroGPU"), completed status, completed = handle_task(client, task, completed) yield status, completed except Exception as error: traceback.print_exc() client.release(type(error).__name__) yield "Task released safely — waiting for another", completed def disconnect(client): if client: client.disconnect() return None, "Disconnected" CSS = """ body { background: #f4efe5; color: #182019; } .gradio-container { max-width: 760px !important; margin: 0 auto !important; } #shell { border: 1px solid #182019; border-radius: 0; padding: 2rem; box-shadow: 8px 8px 0 #e66b43; } h1 { font-family: Georgia, serif; letter-spacing: -.04em; } button.primary { background: #182019 !important; color: #fff !important; border-radius: 0 !important; } """ def build_app(): with gr.Blocks(css=CSS, title="PeerPixel Community Runner") as demo: client_state = gr.State(None) with gr.Column(elem_id="shell"): gr.Markdown("# Lend a GPU minute. Earn an image.\nPaste the permanent worker key an admin gave you. Keep this page open; ZeroGPU is requested only when PeerPixel assigns your account a render.") key = gr.Textbox(label="Worker key", type="password", placeholder="ppw_…") connect = gr.Button("Start community runner", variant="primary") disconnect_button = gr.Button("Disconnect") status = gr.Textbox(label="Status", value="Disconnected", interactive=False) completed = gr.Number(label="Completed this session", value=0, interactive=False) started = connect.click(begin, inputs=key, outputs=[client_state, status, key]) running = started.then(wait_and_run, inputs=client_state, outputs=[status, completed]) disconnect_button.click(disconnect, inputs=client_state, outputs=[client_state, status], cancels=[running]) gr.Markdown("Your key stays in this server session and is cleared from the form after connection. Closing the page stops new claims.") return demo demo = build_app() if __name__ == "__main__": demo.queue().launch()