Spaces:
Sleeping
Sleeping
File size: 4,499 Bytes
1ba6b4e 886fb78 1ba6b4e 886fb78 1ba6b4e 886fb78 1ba6b4e 886fb78 1ba6b4e 5e8907b 1ba6b4e 886fb78 1ba6b4e 886fb78 1ba6b4e 886fb78 1ba6b4e | 1 2 3 4 5 6 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 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()
|