Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,54 +26,59 @@ import os
|
|
| 26 |
import sys
|
| 27 |
from datetime import datetime
|
| 28 |
|
| 29 |
-
#
|
| 30 |
PEER_ID = f"peer-{os.getenv('COMPUTERNAME', 'unknown')}-{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
| 31 |
-
SERVER_URL = "https://your-username-your-space.hf.space" #
|
| 32 |
|
| 33 |
def check_gpu():
|
| 34 |
-
"""
|
| 35 |
try:
|
| 36 |
result = subprocess.run(['nvidia-smi', '--query-gpu=utilization.gpu', '--format=csv,noheader,nounits'],
|
| 37 |
capture_output=True, text=True)
|
| 38 |
if result.returncode == 0:
|
| 39 |
gpu_usage = int(result.stdout.strip())
|
| 40 |
-
return gpu_usage < 20 # GPU
|
| 41 |
except:
|
| 42 |
-
print("GPU
|
| 43 |
return False
|
| 44 |
|
| 45 |
def register_peer():
|
| 46 |
-
"""
|
| 47 |
try:
|
| 48 |
response = requests.post(f"{SERVER_URL}/api/peers/register", params={"peer_id": PEER_ID})
|
| 49 |
if response.status_code == 200:
|
| 50 |
-
print(f"โ
Peer
|
| 51 |
return True
|
| 52 |
except Exception as e:
|
| 53 |
-
print(f"โ
|
| 54 |
return False
|
| 55 |
|
| 56 |
def generate_image_cpu(prompt, output_path):
|
| 57 |
-
"""
|
| 58 |
from PIL import Image, ImageDraw, ImageFont
|
| 59 |
|
| 60 |
img = Image.new('RGB', (512, 512), color='white')
|
| 61 |
draw = ImageDraw.Draw(img)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
text = f"Prompt: {prompt[:50]}..."
|
| 65 |
draw.text((10, 10), text, fill='black')
|
| 66 |
draw.text((10, 40), f"Generated by: {PEER_ID}", fill='gray')
|
| 67 |
draw.text((10, 70), f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", fill='gray')
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
img.save(output_path)
|
| 70 |
-
print(f"๐
|
| 71 |
|
| 72 |
def main():
|
| 73 |
-
print("๐
|
| 74 |
|
| 75 |
if not register_peer():
|
| 76 |
-
print("
|
| 77 |
return
|
| 78 |
|
| 79 |
while True:
|
|
@@ -81,7 +86,7 @@ def main():
|
|
| 81 |
# Heartbeat
|
| 82 |
requests.post(f"{SERVER_URL}/api/peers/heartbeat", params={"peer_id": PEER_ID})
|
| 83 |
|
| 84 |
-
#
|
| 85 |
response = requests.get(f"{SERVER_URL}/api/jobs/request", params={"peer_id": PEER_ID})
|
| 86 |
if response.status_code == 200:
|
| 87 |
job_data = response.json()
|
|
@@ -91,20 +96,20 @@ def main():
|
|
| 91 |
job_id = job["id"]
|
| 92 |
prompt = job["prompt"]
|
| 93 |
|
| 94 |
-
print(f"\\n๐
|
| 95 |
|
| 96 |
-
#
|
| 97 |
output_path = f"{job_id}.png"
|
| 98 |
|
| 99 |
if check_gpu():
|
| 100 |
-
print("๐ฎ
|
| 101 |
-
#
|
| 102 |
generate_image_cpu(prompt, output_path)
|
| 103 |
else:
|
| 104 |
-
print("๐ป
|
| 105 |
generate_image_cpu(prompt, output_path)
|
| 106 |
|
| 107 |
-
#
|
| 108 |
with open(output_path, 'rb') as f:
|
| 109 |
files = {'file': (output_path, f, 'image/png')}
|
| 110 |
response = requests.post(
|
|
@@ -114,26 +119,26 @@ def main():
|
|
| 114 |
)
|
| 115 |
|
| 116 |
if response.status_code == 200:
|
| 117 |
-
print("โ
|
| 118 |
|
| 119 |
-
#
|
| 120 |
os.remove(output_path)
|
| 121 |
|
| 122 |
-
time.sleep(10) #
|
| 123 |
|
| 124 |
except KeyboardInterrupt:
|
| 125 |
-
print("\\n๐
|
| 126 |
break
|
| 127 |
except Exception as e:
|
| 128 |
-
print(f"โ ๏ธ
|
| 129 |
time.sleep(30)
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
-
#
|
| 133 |
try:
|
| 134 |
import PIL
|
| 135 |
except ImportError:
|
| 136 |
-
print("
|
| 137 |
subprocess.run([sys.executable, "-m", "pip", "install", "pillow", "requests"])
|
| 138 |
|
| 139 |
main()
|
|
@@ -147,34 +152,34 @@ with open("client/requirements.txt", "w") as f:
|
|
| 147 |
f.write("requests\npillow\n")
|
| 148 |
|
| 149 |
with open("client/README.md", "w", encoding="utf-8") as f:
|
| 150 |
-
f.write("""# P2P GPU
|
| 151 |
|
| 152 |
-
##
|
| 153 |
-
1.
|
| 154 |
-
2.
|
| 155 |
-
3.
|
| 156 |
-
4.
|
| 157 |
|
| 158 |
-
## GPU
|
| 159 |
-
-
|
| 160 |
-
-
|
| 161 |
""")
|
| 162 |
|
| 163 |
# FastAPI app with lifespan
|
| 164 |
@asynccontextmanager
|
| 165 |
async def lifespan(app: FastAPI):
|
| 166 |
# Startup
|
| 167 |
-
print("
|
| 168 |
yield
|
| 169 |
# Shutdown
|
| 170 |
-
print("
|
| 171 |
|
| 172 |
app = FastAPI(lifespan=lifespan)
|
| 173 |
|
| 174 |
# API endpoints
|
| 175 |
@app.get("/api/status")
|
| 176 |
async def get_status():
|
| 177 |
-
"""
|
| 178 |
active_peers = sum(1 for p in peers.values()
|
| 179 |
if (datetime.now() - p['last_seen']).seconds < 60)
|
| 180 |
pending_jobs = sum(1 for j in jobs if j['status'] == 'pending')
|
|
@@ -194,7 +199,7 @@ async def get_status():
|
|
| 194 |
|
| 195 |
@app.post("/api/peers/register")
|
| 196 |
async def register_peer(peer_id: str):
|
| 197 |
-
"""
|
| 198 |
peers[peer_id] = {
|
| 199 |
"status": "idle",
|
| 200 |
"last_seen": datetime.now(),
|
|
@@ -204,7 +209,7 @@ async def register_peer(peer_id: str):
|
|
| 204 |
|
| 205 |
@app.post("/api/peers/heartbeat")
|
| 206 |
async def heartbeat(peer_id: str):
|
| 207 |
-
"""
|
| 208 |
if peer_id in peers:
|
| 209 |
peers[peer_id]["last_seen"] = datetime.now()
|
| 210 |
return {"status": "alive"}
|
|
@@ -212,7 +217,7 @@ async def heartbeat(peer_id: str):
|
|
| 212 |
|
| 213 |
@app.post("/api/jobs/submit")
|
| 214 |
async def submit_job(request: Request):
|
| 215 |
-
"""
|
| 216 |
data = await request.json()
|
| 217 |
job_id = str(uuid.uuid4())
|
| 218 |
job = {
|
|
@@ -226,7 +231,7 @@ async def submit_job(request: Request):
|
|
| 226 |
|
| 227 |
@app.get("/api/jobs/request")
|
| 228 |
async def request_job(peer_id: str):
|
| 229 |
-
"""
|
| 230 |
for job in jobs:
|
| 231 |
if job["status"] == "pending":
|
| 232 |
job["status"] = "assigned"
|
|
@@ -238,7 +243,7 @@ async def request_job(peer_id: str):
|
|
| 238 |
|
| 239 |
@app.post("/api/jobs/result")
|
| 240 |
async def submit_result(job_id: str, file: UploadFile = File(...)):
|
| 241 |
-
"""
|
| 242 |
filename = f"{job_id}.png"
|
| 243 |
file_path = f"results/{filename}"
|
| 244 |
|
|
@@ -259,7 +264,7 @@ async def submit_result(job_id: str, file: UploadFile = File(...)):
|
|
| 259 |
|
| 260 |
@app.get("/api/results/{filename}")
|
| 261 |
async def get_result(filename: str):
|
| 262 |
-
"""
|
| 263 |
file_path = f"results/{filename}"
|
| 264 |
if os.path.exists(file_path):
|
| 265 |
return FileResponse(file_path)
|
|
@@ -267,7 +272,7 @@ async def get_result(filename: str):
|
|
| 267 |
|
| 268 |
@app.get("/api/client/{filename}")
|
| 269 |
async def get_client_file(filename: str):
|
| 270 |
-
"""
|
| 271 |
file_path = f"client/{filename}"
|
| 272 |
if os.path.exists(file_path):
|
| 273 |
return FileResponse(file_path, filename=filename)
|
|
@@ -275,9 +280,9 @@ async def get_client_file(filename: str):
|
|
| 275 |
|
| 276 |
# Gradio interface functions
|
| 277 |
def gradio_submit_job(prompt):
|
| 278 |
-
"""
|
| 279 |
if not prompt:
|
| 280 |
-
return "
|
| 281 |
|
| 282 |
job_id = str(uuid.uuid4())
|
| 283 |
job = {
|
|
@@ -287,34 +292,40 @@ def gradio_submit_job(prompt):
|
|
| 287 |
"created_at": datetime.now()
|
| 288 |
}
|
| 289 |
jobs.append(job)
|
| 290 |
-
return f"
|
| 291 |
|
| 292 |
def gradio_get_status():
|
| 293 |
-
"""
|
| 294 |
active_peers = sum(1 for p in peers.values()
|
| 295 |
if (datetime.now() - p['last_seen']).seconds < 60)
|
| 296 |
pending = sum(1 for j in jobs if j['status'] == 'pending')
|
| 297 |
completed = sum(1 for j in jobs if j['status'] == 'completed')
|
| 298 |
|
| 299 |
-
status_text = f"""
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
-
|
|
|
|
|
|
|
| 303 |
|
| 304 |
-
###
|
| 305 |
"""
|
| 306 |
|
| 307 |
# Add recent jobs
|
| 308 |
recent_jobs = jobs[-5:][::-1] # Last 5 jobs, reversed
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
return status_text
|
| 313 |
|
| 314 |
def gradio_get_gallery():
|
| 315 |
-
"""
|
| 316 |
image_files = []
|
| 317 |
-
for job in jobs[-20:]: # Last 20 jobs
|
| 318 |
if job['status'] == 'completed' and 'filename' in job:
|
| 319 |
file_path = f"results/{job['filename']}"
|
| 320 |
if os.path.exists(file_path):
|
|
@@ -322,89 +333,152 @@ def gradio_get_gallery():
|
|
| 322 |
|
| 323 |
return image_files
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
# Create Gradio interface
|
| 326 |
-
with gr.Blocks(title="P2P GPU
|
| 327 |
-
gr.Markdown("# ๐ค P2P GPU
|
| 328 |
-
gr.Markdown("
|
| 329 |
|
| 330 |
with gr.Tabs():
|
| 331 |
-
with gr.Tab("
|
| 332 |
with gr.Row():
|
| 333 |
-
with gr.Column():
|
| 334 |
prompt_input = gr.Textbox(
|
| 335 |
-
label="
|
| 336 |
-
placeholder="
|
| 337 |
-
lines=
|
| 338 |
)
|
| 339 |
-
|
| 340 |
-
|
|
|
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
-
with gr.Tab("
|
| 349 |
status_display = gr.Markdown()
|
| 350 |
-
|
|
|
|
|
|
|
| 351 |
|
| 352 |
refresh_btn.click(
|
| 353 |
fn=gradio_get_status,
|
| 354 |
outputs=status_display
|
| 355 |
)
|
| 356 |
|
| 357 |
-
# Auto-refresh status
|
| 358 |
demo.load(fn=gradio_get_status, outputs=status_display)
|
| 359 |
|
| 360 |
-
with gr.Tab("
|
|
|
|
| 361 |
gallery = gr.Gallery(
|
| 362 |
-
label="
|
| 363 |
-
show_label=
|
| 364 |
elem_id="gallery",
|
| 365 |
-
columns=
|
| 366 |
-
rows=
|
| 367 |
-
height="auto"
|
|
|
|
|
|
|
| 368 |
)
|
| 369 |
-
refresh_gallery_btn = gr.Button("
|
| 370 |
|
| 371 |
refresh_gallery_btn.click(
|
| 372 |
fn=gradio_get_gallery,
|
| 373 |
outputs=gallery
|
| 374 |
)
|
| 375 |
|
| 376 |
-
# Auto-load gallery
|
| 377 |
demo.load(fn=gradio_get_gallery, outputs=gallery)
|
| 378 |
|
| 379 |
-
with gr.Tab("
|
| 380 |
gr.Markdown("""
|
| 381 |
-
## Windows
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
|
|
|
| 387 |
|
| 388 |
-
2
|
|
|
|
|
|
|
| 389 |
|
| 390 |
-
3
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
|
| 395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
# Mount Gradio app to FastAPI
|
| 406 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 407 |
|
| 408 |
-
# For
|
| 409 |
if __name__ == "__main__":
|
| 410 |
demo.launch()
|
|
|
|
| 26 |
import sys
|
| 27 |
from datetime import datetime
|
| 28 |
|
| 29 |
+
# ์ค์
|
| 30 |
PEER_ID = f"peer-{os.getenv('COMPUTERNAME', 'unknown')}-{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
| 31 |
+
SERVER_URL = "https://your-username-your-space.hf.space" # ์ค์ Space URL๋ก ๋ณ๊ฒฝํ์ธ์
|
| 32 |
|
| 33 |
def check_gpu():
|
| 34 |
+
"""GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ"""
|
| 35 |
try:
|
| 36 |
result = subprocess.run(['nvidia-smi', '--query-gpu=utilization.gpu', '--format=csv,noheader,nounits'],
|
| 37 |
capture_output=True, text=True)
|
| 38 |
if result.returncode == 0:
|
| 39 |
gpu_usage = int(result.stdout.strip())
|
| 40 |
+
return gpu_usage < 20 # GPU ์ฌ์ฉ๋ฅ 20% ๋ฏธ๋ง์ด๋ฉด idle
|
| 41 |
except:
|
| 42 |
+
print("GPU๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. CPU ๋ชจ๋๋ก ์คํ๋ฉ๋๋ค.")
|
| 43 |
return False
|
| 44 |
|
| 45 |
def register_peer():
|
| 46 |
+
"""์๋ฒ์ Peer ๋ฑ๋ก"""
|
| 47 |
try:
|
| 48 |
response = requests.post(f"{SERVER_URL}/api/peers/register", params={"peer_id": PEER_ID})
|
| 49 |
if response.status_code == 200:
|
| 50 |
+
print(f"โ
Peer ๋ฑ๋ก ์ฑ๊ณต: {PEER_ID}")
|
| 51 |
return True
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"โ ์๋ฒ ์ฐ๊ฒฐ ์คํจ: {e}")
|
| 54 |
return False
|
| 55 |
|
| 56 |
def generate_image_cpu(prompt, output_path):
|
| 57 |
+
"""CPU๋ก ํ
์คํธ ์ด๋ฏธ์ง ์์ฑ"""
|
| 58 |
from PIL import Image, ImageDraw, ImageFont
|
| 59 |
|
| 60 |
img = Image.new('RGB', (512, 512), color='white')
|
| 61 |
draw = ImageDraw.Draw(img)
|
| 62 |
|
| 63 |
+
# ํ
์คํธ ๊ทธ๋ฆฌ๊ธฐ
|
| 64 |
text = f"Prompt: {prompt[:50]}..."
|
| 65 |
draw.text((10, 10), text, fill='black')
|
| 66 |
draw.text((10, 40), f"Generated by: {PEER_ID}", fill='gray')
|
| 67 |
draw.text((10, 70), f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", fill='gray')
|
| 68 |
|
| 69 |
+
# ํ
์คํธ ํจํด ๊ทธ๋ฆฌ๊ธฐ
|
| 70 |
+
for i in range(0, 512, 32):
|
| 71 |
+
draw.line([(i, 0), (i, 512)], fill='lightgray')
|
| 72 |
+
draw.line([(0, i), (512, i)], fill='lightgray')
|
| 73 |
+
|
| 74 |
img.save(output_path)
|
| 75 |
+
print(f"๐ ํ
์คํธ ์ด๋ฏธ์ง ์์ฑ ์๋ฃ: {output_path}")
|
| 76 |
|
| 77 |
def main():
|
| 78 |
+
print("๐ P2P GPU ํด๋ผ์ด์ธํธ ์์...")
|
| 79 |
|
| 80 |
if not register_peer():
|
| 81 |
+
print("์๋ฒ ๋ฑ๋ก ์คํจ. ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.")
|
| 82 |
return
|
| 83 |
|
| 84 |
while True:
|
|
|
|
| 86 |
# Heartbeat
|
| 87 |
requests.post(f"{SERVER_URL}/api/peers/heartbeat", params={"peer_id": PEER_ID})
|
| 88 |
|
| 89 |
+
# ์์
์์ฒญ
|
| 90 |
response = requests.get(f"{SERVER_URL}/api/jobs/request", params={"peer_id": PEER_ID})
|
| 91 |
if response.status_code == 200:
|
| 92 |
job_data = response.json()
|
|
|
|
| 96 |
job_id = job["id"]
|
| 97 |
prompt = job["prompt"]
|
| 98 |
|
| 99 |
+
print(f"\\n๐ ์ ์์
์์ : {prompt}")
|
| 100 |
|
| 101 |
+
# ์ด๋ฏธ์ง ์์ฑ
|
| 102 |
output_path = f"{job_id}.png"
|
| 103 |
|
| 104 |
if check_gpu():
|
| 105 |
+
print("๐ฎ GPU ๋ชจ๋๋ก ์์ฑ ์ค...")
|
| 106 |
+
# ์ค์ GPU ์์ฑ ์ฝ๋๊ฐ ์ฌ๊ธฐ ๋ค์ด๊ฐ๋๋ค
|
| 107 |
generate_image_cpu(prompt, output_path)
|
| 108 |
else:
|
| 109 |
+
print("๐ป CPU ๋ชจ๋๋ก ์์ฑ ์ค...")
|
| 110 |
generate_image_cpu(prompt, output_path)
|
| 111 |
|
| 112 |
+
# ๊ฒฐ๊ณผ ์
๋ก๋
|
| 113 |
with open(output_path, 'rb') as f:
|
| 114 |
files = {'file': (output_path, f, 'image/png')}
|
| 115 |
response = requests.post(
|
|
|
|
| 119 |
)
|
| 120 |
|
| 121 |
if response.status_code == 200:
|
| 122 |
+
print("โ
๊ฒฐ๊ณผ ์
๋ก๋ ์ฑ๊ณต")
|
| 123 |
|
| 124 |
+
# ์์ ํ์ผ ์ญ์
|
| 125 |
os.remove(output_path)
|
| 126 |
|
| 127 |
+
time.sleep(10) # 10์ด๋ง๋ค ํ์ธ
|
| 128 |
|
| 129 |
except KeyboardInterrupt:
|
| 130 |
+
print("\\n๐ ํ๋ก๊ทธ๋จ ์ข
๋ฃ")
|
| 131 |
break
|
| 132 |
except Exception as e:
|
| 133 |
+
print(f"โ ๏ธ ์ค๋ฅ ๋ฐ์: {e}")
|
| 134 |
time.sleep(30)
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
+
# ํ์ํ ํจํค์ง ํ์ธ
|
| 138 |
try:
|
| 139 |
import PIL
|
| 140 |
except ImportError:
|
| 141 |
+
print("ํ์ํ ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค...")
|
| 142 |
subprocess.run([sys.executable, "-m", "pip", "install", "pillow", "requests"])
|
| 143 |
|
| 144 |
main()
|
|
|
|
| 152 |
f.write("requests\npillow\n")
|
| 153 |
|
| 154 |
with open("client/README.md", "w", encoding="utf-8") as f:
|
| 155 |
+
f.write("""# P2P GPU ํด๋ผ์ด์ธํธ (Windows)
|
| 156 |
|
| 157 |
+
## ์ค์น ๋ฐฉ๋ฒ
|
| 158 |
+
1. Python 3.8 ์ด์ ์ค์น
|
| 159 |
+
2. `pip install -r requirements.txt` ์คํ
|
| 160 |
+
3. `peer_agent.py` ํ์ผ์ SERVER_URL์ ์ค์ Hugging Face Space URL๋ก ์์
|
| 161 |
+
4. `python peer_agent.py` ์คํ
|
| 162 |
|
| 163 |
+
## GPU ์ง์
|
| 164 |
+
- NVIDIA GPU๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ๊ฐ์ง
|
| 165 |
+
- GPU๊ฐ ์์ผ๋ฉด CPU ๋ชจ๋๋ก ํ
์คํธ ์ด๋ฏธ์ง ์์ฑ
|
| 166 |
""")
|
| 167 |
|
| 168 |
# FastAPI app with lifespan
|
| 169 |
@asynccontextmanager
|
| 170 |
async def lifespan(app: FastAPI):
|
| 171 |
# Startup
|
| 172 |
+
print("P2P GPU Hub ์์...")
|
| 173 |
yield
|
| 174 |
# Shutdown
|
| 175 |
+
print("P2P GPU Hub ์ข
๋ฃ...")
|
| 176 |
|
| 177 |
app = FastAPI(lifespan=lifespan)
|
| 178 |
|
| 179 |
# API endpoints
|
| 180 |
@app.get("/api/status")
|
| 181 |
async def get_status():
|
| 182 |
+
"""์์คํ
์ํ ์กฐํ"""
|
| 183 |
active_peers = sum(1 for p in peers.values()
|
| 184 |
if (datetime.now() - p['last_seen']).seconds < 60)
|
| 185 |
pending_jobs = sum(1 for j in jobs if j['status'] == 'pending')
|
|
|
|
| 199 |
|
| 200 |
@app.post("/api/peers/register")
|
| 201 |
async def register_peer(peer_id: str):
|
| 202 |
+
"""Peer ๋ฑ๋ก"""
|
| 203 |
peers[peer_id] = {
|
| 204 |
"status": "idle",
|
| 205 |
"last_seen": datetime.now(),
|
|
|
|
| 209 |
|
| 210 |
@app.post("/api/peers/heartbeat")
|
| 211 |
async def heartbeat(peer_id: str):
|
| 212 |
+
"""Peer ์ํ ์
๋ฐ์ดํธ"""
|
| 213 |
if peer_id in peers:
|
| 214 |
peers[peer_id]["last_seen"] = datetime.now()
|
| 215 |
return {"status": "alive"}
|
|
|
|
| 217 |
|
| 218 |
@app.post("/api/jobs/submit")
|
| 219 |
async def submit_job(request: Request):
|
| 220 |
+
"""์์
์ ์ถ"""
|
| 221 |
data = await request.json()
|
| 222 |
job_id = str(uuid.uuid4())
|
| 223 |
job = {
|
|
|
|
| 231 |
|
| 232 |
@app.get("/api/jobs/request")
|
| 233 |
async def request_job(peer_id: str):
|
| 234 |
+
"""์์
์์ฒญ"""
|
| 235 |
for job in jobs:
|
| 236 |
if job["status"] == "pending":
|
| 237 |
job["status"] = "assigned"
|
|
|
|
| 243 |
|
| 244 |
@app.post("/api/jobs/result")
|
| 245 |
async def submit_result(job_id: str, file: UploadFile = File(...)):
|
| 246 |
+
"""์์
๊ฒฐ๊ณผ ์ ์ถ"""
|
| 247 |
filename = f"{job_id}.png"
|
| 248 |
file_path = f"results/{filename}"
|
| 249 |
|
|
|
|
| 264 |
|
| 265 |
@app.get("/api/results/{filename}")
|
| 266 |
async def get_result(filename: str):
|
| 267 |
+
"""์์ฑ๋ ์ด๋ฏธ์ง ์กฐํ"""
|
| 268 |
file_path = f"results/{filename}"
|
| 269 |
if os.path.exists(file_path):
|
| 270 |
return FileResponse(file_path)
|
|
|
|
| 272 |
|
| 273 |
@app.get("/api/client/{filename}")
|
| 274 |
async def get_client_file(filename: str):
|
| 275 |
+
"""ํด๋ผ์ด์ธํธ ํ์ผ ๋ค์ด๋ก๋"""
|
| 276 |
file_path = f"client/{filename}"
|
| 277 |
if os.path.exists(file_path):
|
| 278 |
return FileResponse(file_path, filename=filename)
|
|
|
|
| 280 |
|
| 281 |
# Gradio interface functions
|
| 282 |
def gradio_submit_job(prompt):
|
| 283 |
+
"""Gradio๋ฅผ ํตํ ์์
์ ์ถ"""
|
| 284 |
if not prompt:
|
| 285 |
+
return "ํ๋กฌํํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 286 |
|
| 287 |
job_id = str(uuid.uuid4())
|
| 288 |
job = {
|
|
|
|
| 292 |
"created_at": datetime.now()
|
| 293 |
}
|
| 294 |
jobs.append(job)
|
| 295 |
+
return f"โ
์์
์ด ์ ์ถ๋์์ต๋๋ค!\n์์
ID: {job_id[:8]}..."
|
| 296 |
|
| 297 |
def gradio_get_status():
|
| 298 |
+
"""Gradio๋ฅผ ํตํ ์ํ ์กฐํ"""
|
| 299 |
active_peers = sum(1 for p in peers.values()
|
| 300 |
if (datetime.now() - p['last_seen']).seconds < 60)
|
| 301 |
pending = sum(1 for j in jobs if j['status'] == 'pending')
|
| 302 |
completed = sum(1 for j in jobs if j['status'] == 'completed')
|
| 303 |
|
| 304 |
+
status_text = f"""## ๐ ์์คํ
์ํ
|
| 305 |
+
|
| 306 |
+
### ํ์ฌ ์ํฉ
|
| 307 |
+
- **ํ์ฑ Peer**: {active_peers}๊ฐ
|
| 308 |
+
- **๋๊ธฐ์ค ์์
**: {pending}๊ฐ
|
| 309 |
+
- **์๋ฃ๋ ์์
**: {completed}๊ฐ
|
| 310 |
|
| 311 |
+
### ์ต๊ทผ ์์
๋ชฉ๋ก
|
| 312 |
"""
|
| 313 |
|
| 314 |
# Add recent jobs
|
| 315 |
recent_jobs = jobs[-5:][::-1] # Last 5 jobs, reversed
|
| 316 |
+
if recent_jobs:
|
| 317 |
+
for job in recent_jobs:
|
| 318 |
+
status_emoji = "โ
" if job['status'] == 'completed' else "โณ" if job['status'] == 'pending' else "๐"
|
| 319 |
+
status_text += f"\n{status_emoji} **{job['id'][:8]}**: {job['prompt'][:50]}... ({job['status']})"
|
| 320 |
+
else:
|
| 321 |
+
status_text += "\n์์ง ์์
์ด ์์ต๋๋ค."
|
| 322 |
|
| 323 |
return status_text
|
| 324 |
|
| 325 |
def gradio_get_gallery():
|
| 326 |
+
"""๊ฐค๋ฌ๋ฆฌ์ฉ ์ด๋ฏธ์ง ๊ฐ์ ธ์ค๊ธฐ"""
|
| 327 |
image_files = []
|
| 328 |
+
for job in jobs[-20:][::-1]: # Last 20 jobs, newest first
|
| 329 |
if job['status'] == 'completed' and 'filename' in job:
|
| 330 |
file_path = f"results/{job['filename']}"
|
| 331 |
if os.path.exists(file_path):
|
|
|
|
| 333 |
|
| 334 |
return image_files
|
| 335 |
|
| 336 |
+
# Custom CSS
|
| 337 |
+
custom_css = """
|
| 338 |
+
.container {
|
| 339 |
+
max-width: 1200px;
|
| 340 |
+
margin: 0 auto;
|
| 341 |
+
}
|
| 342 |
+
#gallery {
|
| 343 |
+
min-height: 400px;
|
| 344 |
+
}
|
| 345 |
+
.gradio-container {
|
| 346 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
| 347 |
+
}
|
| 348 |
+
"""
|
| 349 |
+
|
| 350 |
# Create Gradio interface
|
| 351 |
+
with gr.Blocks(title="P2P GPU ์ด๋ฏธ์ง ์์ฑ ํ๋ธ", css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 352 |
+
gr.Markdown("# ๐ค P2P GPU ์ด๋ฏธ์ง ์์ฑ ํ๋ธ")
|
| 353 |
+
gr.Markdown("์ ํด GPU๋ฅผ ํ์ฉํ ๋ถ์ฐ ์ด๋ฏธ์ง ์์ฑ ์์คํ
")
|
| 354 |
|
| 355 |
with gr.Tabs():
|
| 356 |
+
with gr.Tab("๐จ ์์
์ ์ถ"):
|
| 357 |
with gr.Row():
|
| 358 |
+
with gr.Column(scale=2):
|
| 359 |
prompt_input = gr.Textbox(
|
| 360 |
+
label="์ด๋ฏธ์ง ํ๋กฌํํธ",
|
| 361 |
+
placeholder="์์ฑํ๊ณ ์ถ์ ์ด๋ฏธ์ง๋ฅผ ์ค๋ช
ํด์ฃผ์ธ์...\n์: ํธ๋ฅธ ํ๋ ์๋ ๋ฒ๊ฝ์ด ๋ง๊ฐํ ๊ณต์",
|
| 362 |
+
lines=4
|
| 363 |
)
|
| 364 |
+
with gr.Row():
|
| 365 |
+
submit_btn = gr.Button("์์
์ ์ถ", variant="primary", scale=2)
|
| 366 |
+
clear_btn = gr.Button("์ง์ฐ๊ธฐ", scale=1)
|
| 367 |
|
| 368 |
+
result_text = gr.Textbox(
|
| 369 |
+
label="์ ์ถ ๊ฒฐ๊ณผ",
|
| 370 |
+
interactive=False,
|
| 371 |
+
lines=2
|
| 372 |
)
|
| 373 |
+
|
| 374 |
+
with gr.Column(scale=1):
|
| 375 |
+
gr.Markdown("""
|
| 376 |
+
### ๐ก ์ฌ์ฉ ๋ฐฉ๋ฒ
|
| 377 |
+
1. ์ํ๋ ์ด๋ฏธ์ง๋ฅผ ์ค๋ช
ํ์ธ์
|
| 378 |
+
2. '์์
์ ์ถ' ๋ฒํผ์ ํด๋ฆญ
|
| 379 |
+
3. Peer๊ฐ ์์
์ ์ฒ๋ฆฌํฉ๋๋ค
|
| 380 |
+
4. ๊ฐค๋ฌ๋ฆฌ์์ ๊ฒฐ๊ณผ ํ์ธ
|
| 381 |
+
|
| 382 |
+
### ๐ฏ ์ข์ ํ๋กฌํํธ ์์
|
| 383 |
+
- ๊ตฌ์ฒด์ ์ธ ์ค๋ช
ํฌํจ
|
| 384 |
+
- ์คํ์ผ์ด๋ ๋ถ์๊ธฐ ๋ช
์
|
| 385 |
+
- ์์, ์กฐ๋ช
๋ฑ ์ธ๋ถ์ฌํญ
|
| 386 |
+
""")
|
| 387 |
+
|
| 388 |
+
submit_btn.click(
|
| 389 |
+
fn=gradio_submit_job,
|
| 390 |
+
inputs=prompt_input,
|
| 391 |
+
outputs=result_text
|
| 392 |
+
)
|
| 393 |
+
clear_btn.click(
|
| 394 |
+
fn=lambda: ("", ""),
|
| 395 |
+
outputs=[prompt_input, result_text]
|
| 396 |
+
)
|
| 397 |
|
| 398 |
+
with gr.Tab("๐ ์์คํ
์ํ"):
|
| 399 |
status_display = gr.Markdown()
|
| 400 |
+
with gr.Row():
|
| 401 |
+
refresh_btn = gr.Button("๐ ์๋ก๊ณ ์นจ", variant="secondary")
|
| 402 |
+
auto_refresh = gr.Checkbox(label="์๋ ์๋ก๊ณ ์นจ (10์ด)", value=True)
|
| 403 |
|
| 404 |
refresh_btn.click(
|
| 405 |
fn=gradio_get_status,
|
| 406 |
outputs=status_display
|
| 407 |
)
|
| 408 |
|
| 409 |
+
# Auto-refresh status
|
| 410 |
demo.load(fn=gradio_get_status, outputs=status_display)
|
| 411 |
|
| 412 |
+
with gr.Tab("๐ผ๏ธ ๊ฐค๋ฌ๋ฆฌ"):
|
| 413 |
+
gr.Markdown("### ์ต๊ทผ ์์ฑ๋ ์ด๋ฏธ์ง๋ค")
|
| 414 |
gallery = gr.Gallery(
|
| 415 |
+
label="์์ฑ๋ ์ด๋ฏธ์ง",
|
| 416 |
+
show_label=False,
|
| 417 |
elem_id="gallery",
|
| 418 |
+
columns=4,
|
| 419 |
+
rows=3,
|
| 420 |
+
height="auto",
|
| 421 |
+
object_fit="contain",
|
| 422 |
+
preview=True
|
| 423 |
)
|
| 424 |
+
refresh_gallery_btn = gr.Button("๐ ๊ฐค๋ฌ๋ฆฌ ์๋ก๊ณ ์นจ", variant="secondary")
|
| 425 |
|
| 426 |
refresh_gallery_btn.click(
|
| 427 |
fn=gradio_get_gallery,
|
| 428 |
outputs=gallery
|
| 429 |
)
|
| 430 |
|
| 431 |
+
# Auto-load gallery
|
| 432 |
demo.load(fn=gradio_get_gallery, outputs=gallery)
|
| 433 |
|
| 434 |
+
with gr.Tab("๐ป ํด๋ผ์ด์ธํธ ๋ค์ด๋ก๋"):
|
| 435 |
gr.Markdown("""
|
| 436 |
+
## Windows ํด๋ผ์ด์ธํธ ์ค์ ๊ฐ์ด๋
|
| 437 |
|
| 438 |
+
### ๐ฅ 1๋จ๊ณ: ํ์ผ ๋ค์ด๋ก๋
|
| 439 |
+
๋ค์ ํ์ผ๋ค์ ๋ค์ด๋ก๋ํ์ธ์:
|
| 440 |
+
- [peer_agent.py](/api/client/peer_agent.py) - ๋ฉ์ธ ํด๋ผ์ด์ธํธ
|
| 441 |
+
- [requirements.txt](/api/client/requirements.txt) - ํ์ํ ํจํค์ง ๋ชฉ๋ก
|
| 442 |
+
- [README.md](/api/client/README.md) - ์ค๋ช
์
|
| 443 |
|
| 444 |
+
### ๐ 2๋จ๊ณ: Python ์ค์น
|
| 445 |
+
- Python 3.8 ์ด์ ๋ฒ์ ํ์
|
| 446 |
+
- [python.org](https://python.org)์์ ๋ค์ด๋ก๋
|
| 447 |
|
| 448 |
+
### ๐ฆ 3๋จ๊ณ: ํจํค์ง ์ค์น
|
| 449 |
+
```bash
|
| 450 |
+
pip install -r requirements.txt
|
| 451 |
+
```
|
| 452 |
|
| 453 |
+
### โ๏ธ 4๋จ๊ณ: ์ค์
|
| 454 |
+
`peer_agent.py` ํ์ผ์ ์ด๊ณ SERVER_URL์ ์ด Space์ URL๋ก ๋ณ๊ฒฝ:
|
| 455 |
+
```python
|
| 456 |
+
SERVER_URL = "https://๋น์ ์-space-url.hf.space"
|
| 457 |
+
```
|
| 458 |
|
| 459 |
+
### ๐ 5๋จ๊ณ: ์คํ
|
| 460 |
+
```bash
|
| 461 |
+
python peer_agent.py
|
| 462 |
+
```
|
| 463 |
|
| 464 |
+
### โจ ํน์ง
|
| 465 |
+
- GPU ์๋ ๊ฐ์ง
|
| 466 |
+
- CPU ํด๋ฐฑ ์ง์
|
| 467 |
+
- ์๋ ์์
์ฒ๋ฆฌ
|
| 468 |
+
- 10์ด๋ง๋ค ์ ์์
ํ์ธ
|
| 469 |
""")
|
| 470 |
+
|
| 471 |
+
with gr.Row():
|
| 472 |
+
gr.Markdown("""
|
| 473 |
+
### ๐ฎ GPU ์ง์
|
| 474 |
+
- NVIDIA GPU๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ํ์ฉ
|
| 475 |
+
- GPU๊ฐ ์์ด๋ CPU๋ก ํ
์คํธ ๊ฐ๋ฅ
|
| 476 |
+
- GPU ์ฌ์ฉ๋ฅ 20% ๋ฏธ๋ง์ผ ๋๋ง ์์
์ํ
|
| 477 |
+
""")
|
| 478 |
|
| 479 |
# Mount Gradio app to FastAPI
|
| 480 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 481 |
|
| 482 |
+
# For local testing
|
| 483 |
if __name__ == "__main__":
|
| 484 |
demo.launch()
|