Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,557 +1,226 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import uuid
|
| 3 |
-
import os
|
| 4 |
import json
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
from typing import Dict, List
|
| 7 |
-
import shutil
|
| 8 |
-
import time
|
| 9 |
-
import threading
|
| 10 |
-
|
| 11 |
-
# Initialize data storage
|
| 12 |
-
peers: Dict[str, Dict] = {}
|
| 13 |
-
jobs: List[Dict] = []
|
| 14 |
-
job_lock = threading.Lock()
|
| 15 |
-
peer_lock = threading.Lock()
|
| 16 |
-
|
| 17 |
-
# Create directories
|
| 18 |
-
os.makedirs("results", exist_ok=True)
|
| 19 |
-
os.makedirs("client", exist_ok=True)
|
| 20 |
-
|
| 21 |
-
# Client code
|
| 22 |
-
CLIENT_CODE = '''import requests
|
| 23 |
-
import subprocess
|
| 24 |
-
import time
|
| 25 |
import os
|
| 26 |
-
import sys
|
| 27 |
from datetime import datetime
|
| 28 |
-
import
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
SERVER_URL = "https://your-username-your-space.hf.space" # ์ค์ Space URL๋ก ๋ณ๊ฒฝํ์ธ์
|
| 33 |
-
|
| 34 |
-
def check_gpu():
|
| 35 |
-
"""GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ"""
|
| 36 |
-
try:
|
| 37 |
-
result = subprocess.run(['nvidia-smi', '--query-gpu=utilization.gpu', '--format=csv,noheader,nounits'],
|
| 38 |
-
capture_output=True, text=True)
|
| 39 |
-
if result.returncode == 0:
|
| 40 |
-
gpu_usage = int(result.stdout.strip())
|
| 41 |
-
return gpu_usage < 20 # GPU ์ฌ์ฉ๋ฅ 20% ๋ฏธ๋ง์ด๋ฉด idle
|
| 42 |
-
except:
|
| 43 |
-
print("GPU๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. CPU ๋ชจ๋๋ก ์คํ๋ฉ๋๋ค.")
|
| 44 |
-
return False
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
response = requests.post(
|
| 50 |
-
f"{SERVER_URL}/run/register_peer",
|
| 51 |
-
json={"data": [PEER_ID]},
|
| 52 |
-
headers={"Content-Type": "application/json"}
|
| 53 |
-
)
|
| 54 |
-
if response.status_code == 200:
|
| 55 |
-
result = response.json()
|
| 56 |
-
if result.get("data") and result["data"][0] == "registered":
|
| 57 |
-
print(f"โ
Peer ๋ฑ๋ก ์ฑ๊ณต: {PEER_ID}")
|
| 58 |
-
return True
|
| 59 |
-
except Exception as e:
|
| 60 |
-
print(f"โ ์๋ฒ ์ฐ๊ฒฐ ์คํจ: {e}")
|
| 61 |
-
return False
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
try:
|
| 66 |
-
response = requests.post(
|
| 67 |
-
f"{SERVER_URL}/run/heartbeat",
|
| 68 |
-
json={"data": [PEER_ID]},
|
| 69 |
-
headers={"Content-Type": "application/json"}
|
| 70 |
-
)
|
| 71 |
-
return response.status_code == 200
|
| 72 |
-
except:
|
| 73 |
-
return False
|
| 74 |
|
| 75 |
-
def
|
| 76 |
-
"""
|
| 77 |
-
|
| 78 |
-
response = requests.post(
|
| 79 |
-
f"{SERVER_URL}/run/request_job",
|
| 80 |
-
json={"data": [PEER_ID]},
|
| 81 |
-
headers={"Content-Type": "application/json"}
|
| 82 |
-
)
|
| 83 |
-
if response.status_code == 200:
|
| 84 |
-
result = response.json()
|
| 85 |
-
if result.get("data") and result["data"][0]:
|
| 86 |
-
return json.loads(result["data"][0])
|
| 87 |
-
except Exception as e:
|
| 88 |
-
print(f"์์
์์ฒญ ์คํจ: {e}")
|
| 89 |
-
return None
|
| 90 |
-
|
| 91 |
-
def generate_image_cpu(prompt, output_path):
|
| 92 |
-
"""CPU๋ก ํ
์คํธ ์ด๋ฏธ์ง ์์ฑ"""
|
| 93 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 94 |
-
|
| 95 |
-
img = Image.new('RGB', (512, 512), color='white')
|
| 96 |
draw = ImageDraw.Draw(img)
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
draw.text((10, 70), f"Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", fill='gray')
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
print(f"๐ ํ
์คํธ ์ด๋ฏธ์ง ์์ฑ ์๋ฃ: {output_path}")
|
| 111 |
-
|
| 112 |
-
def submit_result(job_id, image_path):
|
| 113 |
-
"""์์
๊ฒฐ๊ณผ ์ ์ถ"""
|
| 114 |
-
try:
|
| 115 |
-
# ์ด๋ฏธ์ง๋ฅผ base64๋ก ์ธ์ฝ๋ฉ
|
| 116 |
-
import base64
|
| 117 |
-
with open(image_path, "rb") as f:
|
| 118 |
-
image_data = base64.b64encode(f.read()).decode()
|
| 119 |
-
|
| 120 |
-
response = requests.post(
|
| 121 |
-
f"{SERVER_URL}/run/submit_result",
|
| 122 |
-
json={"data": [job_id, image_data]},
|
| 123 |
-
headers={"Content-Type": "application/json"}
|
| 124 |
-
)
|
| 125 |
-
return response.status_code == 200
|
| 126 |
-
except Exception as e:
|
| 127 |
-
print(f"๊ฒฐ๊ณผ ์ ์ถ ์คํจ: {e}")
|
| 128 |
-
return False
|
| 129 |
-
|
| 130 |
-
def main():
|
| 131 |
-
print("๐ P2P GPU ํด๋ผ์ด์ธํธ ์์...")
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
print("์๋ฒ ์ฐ๊ฒฐ ๋๊น. ์ฌ๋ฑ๋ก ์๋...")
|
| 142 |
-
if not register_peer():
|
| 143 |
-
time.sleep(30)
|
| 144 |
-
continue
|
| 145 |
-
|
| 146 |
-
# ์์
์์ฒญ
|
| 147 |
-
job = request_job()
|
| 148 |
-
if job:
|
| 149 |
-
job_id = job["id"]
|
| 150 |
-
prompt = job["prompt"]
|
| 151 |
-
|
| 152 |
-
print(f"\\n๐ ์ ์์
์์ : {prompt}")
|
| 153 |
-
|
| 154 |
-
# ์ด๋ฏธ์ง ์์ฑ
|
| 155 |
-
output_path = f"{job_id}.png"
|
| 156 |
-
|
| 157 |
-
if check_gpu():
|
| 158 |
-
print("๐ฎ GPU ๋ชจ๋๋ก ์์ฑ ์ค...")
|
| 159 |
-
# ์ค์ GPU ์์ฑ ์ฝ๋๊ฐ ์ฌ๊ธฐ ๋ค์ด๊ฐ๋๋ค
|
| 160 |
-
generate_image_cpu(prompt, output_path)
|
| 161 |
-
else:
|
| 162 |
-
print("๐ป CPU ๋ชจ๋๋ก ์์ฑ ์ค...")
|
| 163 |
-
generate_image_cpu(prompt, output_path)
|
| 164 |
-
|
| 165 |
-
# ๊ฒฐ๊ณผ ์
๋ก๋
|
| 166 |
-
if submit_result(job_id, output_path):
|
| 167 |
-
print("โ
๊ฒฐ๊ณผ ์
๋ก๋ ์ฑ๊ณต")
|
| 168 |
-
else:
|
| 169 |
-
print("โ ๊ฒฐ๊ณผ ์
๋ก๋ ์คํจ")
|
| 170 |
-
|
| 171 |
-
# ์์ ํ์ผ ์ญ์
|
| 172 |
-
os.remove(output_path)
|
| 173 |
-
|
| 174 |
-
time.sleep(10) # 10์ด๋ง๋ค ํ์ธ
|
| 175 |
-
|
| 176 |
-
except KeyboardInterrupt:
|
| 177 |
-
print("\\n๐ ํ๋ก๊ทธ๋จ ์ข
๋ฃ")
|
| 178 |
-
break
|
| 179 |
-
except Exception as e:
|
| 180 |
-
print(f"โ ๏ธ ์ค๋ฅ ๋ฐ์: {e}")
|
| 181 |
-
time.sleep(30)
|
| 182 |
-
|
| 183 |
-
if __name__ == "__main__":
|
| 184 |
-
# ํ์ํ ํจํค์ง ํ์ธ
|
| 185 |
-
try:
|
| 186 |
-
import PIL
|
| 187 |
-
except ImportError:
|
| 188 |
-
print("ํ์ํ ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค...")
|
| 189 |
-
subprocess.run([sys.executable, "-m", "pip", "install", "pillow", "requests"])
|
| 190 |
|
| 191 |
-
|
| 192 |
-
'''
|
| 193 |
-
|
| 194 |
-
# Create client files
|
| 195 |
-
def create_client_files():
|
| 196 |
-
with open("client/peer_agent.py", "w", encoding="utf-8") as f:
|
| 197 |
-
f.write(CLIENT_CODE)
|
| 198 |
-
|
| 199 |
-
with open("client/requirements.txt", "w") as f:
|
| 200 |
-
f.write("requests\npillow\n")
|
| 201 |
|
| 202 |
-
|
| 203 |
-
f.write("""# P2P GPU ํด๋ผ์ด์ธํธ (Windows)
|
| 204 |
-
|
| 205 |
-
## ์ค์น ๋ฐฉ๋ฒ
|
| 206 |
-
1. Python 3.8 ์ด์ ์ค์น
|
| 207 |
-
2. `pip install -r requirements.txt` ์คํ
|
| 208 |
-
3. `peer_agent.py` ํ์ผ์ SERVER_URL์ ์ค์ Hugging Face Space URL๋ก ์์
|
| 209 |
-
4. `python peer_agent.py` ์คํ
|
| 210 |
-
|
| 211 |
-
## GPU ์ง์
|
| 212 |
-
- NVIDIA GPU๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ๊ฐ์ง
|
| 213 |
-
- GPU๊ฐ ์์ผ๋ฉด CPU ๋ชจ๋๋ก ํ
์คํธ ์ด๋ฏธ์ง ์์ฑ
|
| 214 |
-
""")
|
| 215 |
-
|
| 216 |
-
create_client_files()
|
| 217 |
-
|
| 218 |
-
# Cleanup old peers periodically
|
| 219 |
-
def cleanup_old_peers():
|
| 220 |
-
while True:
|
| 221 |
-
time.sleep(60) # Check every minute
|
| 222 |
-
with peer_lock:
|
| 223 |
-
current_time = datetime.now()
|
| 224 |
-
to_remove = []
|
| 225 |
-
for peer_id, peer_info in peers.items():
|
| 226 |
-
if (current_time - peer_info['last_seen']).seconds > 120: # 2 minutes timeout
|
| 227 |
-
to_remove.append(peer_id)
|
| 228 |
-
for peer_id in to_remove:
|
| 229 |
-
del peers[peer_id]
|
| 230 |
-
|
| 231 |
-
# Start cleanup thread
|
| 232 |
-
cleanup_thread = threading.Thread(target=cleanup_old_peers, daemon=True)
|
| 233 |
-
cleanup_thread.start()
|
| 234 |
-
|
| 235 |
-
# Gradio API functions
|
| 236 |
-
def register_peer(peer_id: str):
|
| 237 |
-
"""Peer ๋ฑ๋ก"""
|
| 238 |
-
with peer_lock:
|
| 239 |
-
peers[peer_id] = {
|
| 240 |
-
"status": "idle",
|
| 241 |
-
"last_seen": datetime.now(),
|
| 242 |
-
"jobs_completed": 0
|
| 243 |
-
}
|
| 244 |
-
return "registered"
|
| 245 |
-
|
| 246 |
-
def heartbeat(peer_id: str):
|
| 247 |
-
"""Peer heartbeat"""
|
| 248 |
-
with peer_lock:
|
| 249 |
-
if peer_id in peers:
|
| 250 |
-
peers[peer_id]["last_seen"] = datetime.now()
|
| 251 |
-
return "alive"
|
| 252 |
-
return "unregistered"
|
| 253 |
-
|
| 254 |
-
def submit_job(prompt: str):
|
| 255 |
"""์์
์ ์ถ"""
|
| 256 |
if not prompt:
|
| 257 |
-
return "ํ๋กฌํํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 258 |
|
| 259 |
-
job_id =
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
jobs.append(job)
|
| 268 |
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
def request_job(peer_id: str):
|
| 272 |
-
"""์์
์์ฒญ"""
|
| 273 |
-
with job_lock:
|
| 274 |
-
for job in jobs:
|
| 275 |
-
if job["status"] == "pending":
|
| 276 |
-
job["status"] = "assigned"
|
| 277 |
-
job["peer_id"] = peer_id
|
| 278 |
-
job["assigned_at"] = datetime.now().isoformat()
|
| 279 |
-
return json.dumps(job)
|
| 280 |
-
return None
|
| 281 |
-
|
| 282 |
-
def submit_result(job_id: str, image_base64: str):
|
| 283 |
-
"""์์
๊ฒฐ๊ณผ ์ ์ถ"""
|
| 284 |
-
import base64
|
| 285 |
|
| 286 |
-
#
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
f.write(image_data)
|
| 294 |
-
|
| 295 |
-
with job_lock:
|
| 296 |
-
for job in jobs:
|
| 297 |
-
if job["id"] == job_id:
|
| 298 |
-
job["status"] = "completed"
|
| 299 |
-
job["filename"] = filename
|
| 300 |
-
job["completed_at"] = datetime.now().isoformat()
|
| 301 |
-
|
| 302 |
-
# Peer ํต๊ณ ์
๋ฐ์ดํธ
|
| 303 |
-
if "peer_id" in job:
|
| 304 |
-
with peer_lock:
|
| 305 |
-
if job["peer_id"] in peers:
|
| 306 |
-
peers[job["peer_id"]]["jobs_completed"] += 1
|
| 307 |
-
break
|
| 308 |
-
|
| 309 |
-
return "success"
|
| 310 |
-
except Exception as e:
|
| 311 |
-
print(f"Error saving result: {e}")
|
| 312 |
-
return "error"
|
| 313 |
|
| 314 |
def get_status():
|
| 315 |
-
"""์์คํ
์ํ
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
if (datetime.now() - p['last_seen']).seconds < 60)
|
| 319 |
-
|
| 320 |
-
with job_lock:
|
| 321 |
-
pending = sum(1 for j in jobs if j['status'] == 'pending')
|
| 322 |
-
completed = sum(1 for j in jobs if j['status'] == 'completed')
|
| 323 |
|
| 324 |
status_text = f"""## ๐ ์์คํ
์ํ
|
| 325 |
|
| 326 |
### ํ์ฌ ์ํฉ
|
| 327 |
-
-
|
| 328 |
-
- **๋๊ธฐ์ค ์์
**: {pending}๊ฐ
|
| 329 |
- **์๋ฃ๋ ์์
**: {completed}๊ฐ
|
|
|
|
| 330 |
|
| 331 |
-
### ์ต๊ทผ ์์
|
| 332 |
"""
|
| 333 |
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
if
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
status_text += "\n์์ง ์์
์ด ์์ต๋๋ค."
|
| 342 |
|
| 343 |
return status_text
|
| 344 |
|
| 345 |
def get_gallery():
|
| 346 |
-
"""
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
|
| 355 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
file_path = f"client/{filename}"
|
| 360 |
-
if os.path.exists(file_path):
|
| 361 |
-
return file_path
|
| 362 |
-
return None
|
| 363 |
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
"""
|
| 377 |
|
| 378 |
-
#
|
| 379 |
-
with gr.Blocks(
|
| 380 |
-
|
| 381 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
with gr.Tabs():
|
|
|
|
| 384 |
with gr.Tab("๐จ ์์
์ ์ถ"):
|
| 385 |
with gr.Row():
|
| 386 |
with gr.Column(scale=2):
|
| 387 |
prompt_input = gr.Textbox(
|
| 388 |
-
label="์ด๋ฏธ์ง ํ๋กฌํํธ",
|
| 389 |
-
placeholder="์์ฑํ๊ณ ์ถ์ ์ด๋ฏธ์ง๋ฅผ
|
| 390 |
-
lines=
|
| 391 |
)
|
| 392 |
-
|
| 393 |
-
submit_btn = gr.Button("์์
์ ์ถ", variant="primary", scale=2)
|
| 394 |
-
clear_btn = gr.Button("์ง์ฐ๊ธฐ", scale=1)
|
| 395 |
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
interactive=False,
|
| 399 |
-
lines=2
|
| 400 |
-
)
|
| 401 |
|
| 402 |
with gr.Column(scale=1):
|
| 403 |
gr.Markdown("""
|
| 404 |
### ๐ก ์ฌ์ฉ ๋ฐฉ๋ฒ
|
| 405 |
-
1.
|
| 406 |
-
2.
|
| 407 |
-
3.
|
| 408 |
-
4. ๊ฐค๋ฌ๋ฆฌ์์ ๊ฒฐ๊ณผ ํ์ธ
|
| 409 |
|
| 410 |
-
###
|
| 411 |
-
-
|
| 412 |
-
-
|
| 413 |
-
-
|
| 414 |
""")
|
| 415 |
|
| 416 |
submit_btn.click(
|
| 417 |
-
fn=submit_job,
|
| 418 |
-
inputs=prompt_input,
|
| 419 |
-
outputs=
|
| 420 |
-
)
|
| 421 |
-
clear_btn.click(
|
| 422 |
-
fn=lambda: ("", ""),
|
| 423 |
-
outputs=[prompt_input, result_text]
|
| 424 |
)
|
| 425 |
|
|
|
|
| 426 |
with gr.Tab("๐ ์์คํ
์ํ"):
|
| 427 |
status_display = gr.Markdown()
|
| 428 |
-
|
| 429 |
-
refresh_btn = gr.Button("๐ ์๋ก๊ณ ์นจ", variant="secondary")
|
| 430 |
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
outputs=status_display
|
| 434 |
-
)
|
| 435 |
|
| 436 |
-
|
| 437 |
-
demo.load(fn=
|
| 438 |
|
|
|
|
| 439 |
with gr.Tab("๐ผ๏ธ ๊ฐค๋ฌ๋ฆฌ"):
|
| 440 |
-
gr.Markdown("### ์ต๊ทผ ์์ฑ๋ ์ด๋ฏธ์ง๋ค")
|
| 441 |
gallery = gr.Gallery(
|
| 442 |
-
label="์์ฑ๋
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
rows=3,
|
| 447 |
-
height="auto",
|
| 448 |
-
object_fit="contain",
|
| 449 |
-
preview=True
|
| 450 |
-
)
|
| 451 |
-
refresh_gallery_btn = gr.Button("๐ ๊ฐค๋ฌ๋ฆฌ ์๋ก๊ณ ์นจ", variant="secondary")
|
| 452 |
-
|
| 453 |
-
refresh_gallery_btn.click(
|
| 454 |
-
fn=get_gallery,
|
| 455 |
-
outputs=gallery
|
| 456 |
)
|
|
|
|
| 457 |
|
| 458 |
-
|
| 459 |
demo.load(fn=get_gallery, outputs=gallery)
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
## Windows ํด๋ผ์ด์ธํธ ์ค์ ๊ฐ์ด๋
|
| 464 |
-
|
| 465 |
-
### ๐ฅ 1๋จ๊ณ: ํ์ผ ๋ค์ด๋ก๋
|
| 466 |
-
ํด๋ผ์ด์ธํธ ํ์ผ์ ๋ค์ด๋ก๋ํ๋ ค๋ฉด ์๋ ๋ฒํผ์ ํด๋ฆญํ์ธ์:
|
| 467 |
-
""")
|
| 468 |
-
|
| 469 |
-
with gr.Row():
|
| 470 |
-
download_peer_btn = gr.Button("๐ฅ peer_agent.py ๋ค์ด๋ก๋")
|
| 471 |
-
download_req_btn = gr.Button("๐ฅ requirements.txt ๋ค์ด๋ก๋")
|
| 472 |
-
download_readme_btn = gr.Button("๐ฅ README.md ๋ค์ด๋ก๋")
|
| 473 |
-
|
| 474 |
-
download_output = gr.File(label="๋ค์ด๋ก๋ ํ์ผ", visible=False)
|
| 475 |
-
|
| 476 |
-
download_peer_btn.click(
|
| 477 |
-
fn=lambda: download_client_file("peer_agent.py"),
|
| 478 |
-
outputs=download_output
|
| 479 |
-
)
|
| 480 |
-
download_req_btn.click(
|
| 481 |
-
fn=lambda: download_client_file("requirements.txt"),
|
| 482 |
-
outputs=download_output
|
| 483 |
-
)
|
| 484 |
-
download_readme_btn.click(
|
| 485 |
-
fn=lambda: download_client_file("README.md"),
|
| 486 |
-
outputs=download_output
|
| 487 |
-
)
|
| 488 |
-
|
| 489 |
gr.Markdown("""
|
| 490 |
-
|
| 491 |
-
- Python 3.8 ์ด์ ๋ฒ์ ํ์
|
| 492 |
-
- [python.org](https://python.org)์์ ๋ค์ด๋ก๋
|
| 493 |
|
| 494 |
-
###
|
| 495 |
-
|
| 496 |
-
pip install -r requirements.txt
|
| 497 |
-
```
|
| 498 |
|
| 499 |
-
### โ๏ธ 4๋จ๊ณ: ์ค์
|
| 500 |
-
`peer_agent.py` ํ์ผ์ ์ด๊ณ SERVER_URL์ ์ด Space์ URL๋ก ๋ณ๊ฒฝ:
|
| 501 |
```python
|
| 502 |
-
|
| 503 |
-
```
|
| 504 |
-
|
| 505 |
-
### ๐ 5๋จ๊ณ: ์คํ
|
| 506 |
-
```bash
|
| 507 |
-
python peer_agent.py
|
| 508 |
```
|
| 509 |
|
| 510 |
-
###
|
| 511 |
-
-
|
| 512 |
-
-
|
| 513 |
-
-
|
| 514 |
-
-
|
| 515 |
-
|
| 516 |
-
### ๐ฎ GPU ์ง์
|
| 517 |
-
- NVIDIA GPU๊ฐ ์์ผ๋ฉด ์๋์ผ๋ก ํ์ฉ
|
| 518 |
-
- GPU๊ฐ ์์ด๋ CPU๋ก ํ
์คํธ ๊ฐ๋ฅ
|
| 519 |
-
- GPU ์ฌ์ฉ๋ฅ 20% ๋ฏธ๋ง์ผ ๋๋ง ์์
์ํ
|
| 520 |
""")
|
| 521 |
-
|
| 522 |
-
# Hidden API endpoints for client communication
|
| 523 |
-
gr.Interface(
|
| 524 |
-
fn=register_peer,
|
| 525 |
-
inputs=gr.Textbox(visible=False),
|
| 526 |
-
outputs=gr.Textbox(visible=False),
|
| 527 |
-
api_name="register_peer",
|
| 528 |
-
visible=False
|
| 529 |
-
)
|
| 530 |
-
|
| 531 |
-
gr.Interface(
|
| 532 |
-
fn=heartbeat,
|
| 533 |
-
inputs=gr.Textbox(visible=False),
|
| 534 |
-
outputs=gr.Textbox(visible=False),
|
| 535 |
-
api_name="heartbeat",
|
| 536 |
-
visible=False
|
| 537 |
-
)
|
| 538 |
-
|
| 539 |
-
gr.Interface(
|
| 540 |
-
fn=request_job,
|
| 541 |
-
inputs=gr.Textbox(visible=False),
|
| 542 |
-
outputs=gr.Textbox(visible=False),
|
| 543 |
-
api_name="request_job",
|
| 544 |
-
visible=False
|
| 545 |
-
)
|
| 546 |
-
|
| 547 |
-
gr.Interface(
|
| 548 |
-
fn=submit_result,
|
| 549 |
-
inputs=[gr.Textbox(visible=False), gr.Textbox(visible=False)],
|
| 550 |
-
outputs=gr.Textbox(visible=False),
|
| 551 |
-
api_name="submit_result",
|
| 552 |
-
visible=False
|
| 553 |
-
)
|
| 554 |
|
| 555 |
-
#
|
| 556 |
if __name__ == "__main__":
|
| 557 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import os
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
+
import base64
|
| 6 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
+
import io
|
| 8 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# ๊ฐ๋จํ ๋ฉ๋ชจ๋ฆฌ ์ ์ฅ์
|
| 11 |
+
jobs = []
|
| 12 |
+
results = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# ๋๋ ํ ๋ฆฌ ์์ฑ
|
| 15 |
+
os.makedirs("temp", exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def create_test_image(prompt):
|
| 18 |
+
"""ํ
์คํธ ์ด๋ฏธ์ง ์์ฑ"""
|
| 19 |
+
img = Image.new('RGB', (512, 512), color=(255, 255, 255))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
draw = ImageDraw.Draw(img)
|
| 21 |
|
| 22 |
+
# ๋ฐฐ๊ฒฝ ๊ทธ๋ผ๋ฐ์ด์
|
| 23 |
+
for i in range(512):
|
| 24 |
+
color = int(255 * (1 - i/512))
|
| 25 |
+
draw.rectangle([(0, i), (512, i+1)], fill=(color, color, 255))
|
|
|
|
| 26 |
|
| 27 |
+
# ํ
์คํธ ์ถ๊ฐ
|
| 28 |
+
text_lines = [
|
| 29 |
+
f"Prompt: {prompt[:40]}...",
|
| 30 |
+
f"Generated at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
|
| 31 |
+
f"Job ID: {random.randint(1000, 9999)}"
|
| 32 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
y_offset = 20
|
| 35 |
+
for line in text_lines:
|
| 36 |
+
draw.text((20, y_offset), line, fill='white')
|
| 37 |
+
y_offset += 30
|
| 38 |
|
| 39 |
+
# ๊ฒฉ์ ํจํด
|
| 40 |
+
for i in range(0, 512, 32):
|
| 41 |
+
draw.line([(i, 100), (i, 512)], fill=(200, 200, 200), width=1)
|
| 42 |
+
draw.line([(0, i+100), (512, i+100)], fill=(200, 200, 200), width=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
def submit_job(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
"""์์
์ ์ถ"""
|
| 48 |
if not prompt:
|
| 49 |
+
return "โ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.", None
|
| 50 |
|
| 51 |
+
job_id = f"job_{len(jobs)}_{datetime.now().strftime('%H%M%S')}"
|
| 52 |
+
job = {
|
| 53 |
+
"id": job_id,
|
| 54 |
+
"prompt": prompt,
|
| 55 |
+
"status": "processing",
|
| 56 |
+
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 57 |
+
}
|
| 58 |
+
jobs.append(job)
|
|
|
|
| 59 |
|
| 60 |
+
# ์๋ฎฌ๋ ์ด์
: ๋ฐ๋ก ์ด๋ฏธ์ง ์์ฑ
|
| 61 |
+
img = create_test_image(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
# ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
| 64 |
+
img_path = f"temp/{job_id}.png"
|
| 65 |
+
img.save(img_path)
|
| 66 |
+
results[job_id] = img_path
|
| 67 |
+
job["status"] = "completed"
|
| 68 |
+
|
| 69 |
+
return f"โ
์์
์ด ์ ์ถ๋์์ต๋๋ค!\n์์
ID: {job_id}", img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def get_status():
|
| 72 |
+
"""์์คํ
์ํ"""
|
| 73 |
+
total_jobs = len(jobs)
|
| 74 |
+
completed = sum(1 for j in jobs if j["status"] == "completed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
status_text = f"""## ๐ ์์คํ
์ํ
|
| 77 |
|
| 78 |
### ํ์ฌ ์ํฉ
|
| 79 |
+
- **์ ์ฒด ์์
**: {total_jobs}๊ฐ
|
|
|
|
| 80 |
- **์๋ฃ๋ ์์
**: {completed}๊ฐ
|
| 81 |
+
- **๋๊ธฐ์ค ์์
**: {total_jobs - completed}๊ฐ
|
| 82 |
|
| 83 |
+
### ์ต๊ทผ ์์
(์ต๋ 5๊ฐ)
|
| 84 |
"""
|
| 85 |
|
| 86 |
+
recent_jobs = jobs[-5:][::-1] if jobs else []
|
| 87 |
+
for job in recent_jobs:
|
| 88 |
+
emoji = "โ
" if job["status"] == "completed" else "โณ"
|
| 89 |
+
status_text += f"\n{emoji} **{job['id']}**: {job['prompt'][:30]}... ({job['created_at']})"
|
| 90 |
+
|
| 91 |
+
if not jobs:
|
| 92 |
+
status_text += "\n\n*์์ง ์์
์ด ์์ต๋๋ค.*"
|
|
|
|
| 93 |
|
| 94 |
return status_text
|
| 95 |
|
| 96 |
def get_gallery():
|
| 97 |
+
"""๊ฐค๋ฌ๋ฆฌ ์ด๋ฏธ์ง ๋ชฉ๋ก"""
|
| 98 |
+
gallery_images = []
|
| 99 |
+
|
| 100 |
+
for job in jobs[-12:][::-1]: # ์ต๊ทผ 12๊ฐ
|
| 101 |
+
if job["status"] == "completed" and job["id"] in results:
|
| 102 |
+
img_path = results[job["id"]]
|
| 103 |
+
if os.path.exists(img_path):
|
| 104 |
+
gallery_images.append((img_path, f"{job['prompt'][:50]}..."))
|
| 105 |
|
| 106 |
+
return gallery_images
|
| 107 |
+
|
| 108 |
+
# ํด๋ผ์ด์ธํธ ์ฝ๋
|
| 109 |
+
CLIENT_CODE = """# P2P GPU Client (Demo Version)
|
| 110 |
+
import time
|
| 111 |
+
import random
|
| 112 |
+
from datetime import datetime
|
| 113 |
|
| 114 |
+
print("๐ P2P GPU ํด๋ผ์ด์ธํธ ์๋ฎฌ๋ ์ด์
")
|
| 115 |
+
print("์ค์ ๊ตฌํ์์๋ Gradio API๋ฅผ ํตํด ์๋ฒ์ ํต์ ํฉ๋๋ค.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
while True:
|
| 118 |
+
print(f"\\n[{datetime.now().strftime('%H:%M:%S')}] ์์
ํ์ธ ์ค...")
|
| 119 |
+
|
| 120 |
+
if random.random() > 0.7: # 30% ํ๋ฅ ๋ก ์์
์์
|
| 121 |
+
print("๐ ์ ์์
๋ฐ๊ฒฌ!")
|
| 122 |
+
print("๐จ ์ด๋ฏธ์ง ์์ฑ ๏ฟฝ๏ฟฝ๏ฟฝ...")
|
| 123 |
+
time.sleep(2)
|
| 124 |
+
print("โ
์๋ฃ! ์๋ฒ๋ก ์ ์ก")
|
| 125 |
+
else:
|
| 126 |
+
print("๋๊ธฐ ์ค...")
|
| 127 |
+
|
| 128 |
+
time.sleep(5)
|
| 129 |
"""
|
| 130 |
|
| 131 |
+
# Gradio ์ธํฐํ์ด์ค
|
| 132 |
+
with gr.Blocks(
|
| 133 |
+
title="P2P GPU ์ด๋ฏธ์ง ์์ฑ ํ๋ธ",
|
| 134 |
+
theme=gr.themes.Soft(),
|
| 135 |
+
css="""
|
| 136 |
+
.container { max-width: 1200px; margin: 0 auto; }
|
| 137 |
+
#gallery { min-height: 400px; }
|
| 138 |
+
"""
|
| 139 |
+
) as demo:
|
| 140 |
+
|
| 141 |
+
gr.Markdown("""
|
| 142 |
+
# ๐ค P2P GPU ์ด๋ฏธ์ง ์์ฑ ํ๋ธ
|
| 143 |
+
|
| 144 |
+
์ ํด GPU๋ฅผ ํ์ฉํ ๋ถ์ฐ ์ด๋ฏธ์ง ์์ฑ ์์คํ
(๋ฐ๋ชจ ๋ฒ์ )
|
| 145 |
+
""")
|
| 146 |
|
| 147 |
with gr.Tabs():
|
| 148 |
+
# ์์
์ ์ถ ํญ
|
| 149 |
with gr.Tab("๐จ ์์
์ ์ถ"):
|
| 150 |
with gr.Row():
|
| 151 |
with gr.Column(scale=2):
|
| 152 |
prompt_input = gr.Textbox(
|
| 153 |
+
label="์ด๋ฏธ์ง ํ๋กฌํํธ",
|
| 154 |
+
placeholder="์์ฑํ๊ณ ์ถ์ ์ด๋ฏธ์ง๋ฅผ ์ค๋ช
ํ์ธ์...",
|
| 155 |
+
lines=3
|
| 156 |
)
|
| 157 |
+
submit_btn = gr.Button("๐ ์์
์ ์ถ", variant="primary")
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
result_msg = gr.Textbox(label="์ ์ถ ๊ฒฐ๊ณผ", interactive=False)
|
| 160 |
+
result_img = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง", type="pil")
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
with gr.Column(scale=1):
|
| 163 |
gr.Markdown("""
|
| 164 |
### ๐ก ์ฌ์ฉ ๋ฐฉ๋ฒ
|
| 165 |
+
1. ํ๋กฌํํธ ์
๋ ฅ
|
| 166 |
+
2. ์์
์ ์ถ ํด๋ฆญ
|
| 167 |
+
3. ๊ฒฐ๊ณผ ํ์ธ
|
|
|
|
| 168 |
|
| 169 |
+
### ๐ ์์
|
| 170 |
+
- ํธ๋ฅธ ํ๋๊ณผ ๊ตฌ๋ฆ
|
| 171 |
+
- ์ผ๋ชฐ์ ํด๋ณ ํ๊ฒฝ
|
| 172 |
+
- ๋ฏธ๋์ ์ธ ๋์ ์ ๊ฒฝ
|
| 173 |
""")
|
| 174 |
|
| 175 |
submit_btn.click(
|
| 176 |
+
fn=submit_job,
|
| 177 |
+
inputs=prompt_input,
|
| 178 |
+
outputs=[result_msg, result_img]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
)
|
| 180 |
|
| 181 |
+
# ์์คํ
์ํ ํญ
|
| 182 |
with gr.Tab("๐ ์์คํ
์ํ"):
|
| 183 |
status_display = gr.Markdown()
|
| 184 |
+
refresh_btn = gr.Button("๐ ์๋ก๊ณ ์นจ")
|
|
|
|
| 185 |
|
| 186 |
+
def refresh_status():
|
| 187 |
+
return get_status()
|
|
|
|
|
|
|
| 188 |
|
| 189 |
+
refresh_btn.click(fn=refresh_status, outputs=status_display)
|
| 190 |
+
demo.load(fn=refresh_status, outputs=status_display)
|
| 191 |
|
| 192 |
+
# ๊ฐค๋ฌ๋ฆฌ ํญ
|
| 193 |
with gr.Tab("๐ผ๏ธ ๊ฐค๋ฌ๋ฆฌ"):
|
|
|
|
| 194 |
gallery = gr.Gallery(
|
| 195 |
+
label="์์ฑ๋ ์ด๋ฏธ์ง๋ค",
|
| 196 |
+
columns=3,
|
| 197 |
+
rows=2,
|
| 198 |
+
height="auto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
)
|
| 200 |
+
gallery_refresh_btn = gr.Button("๐ ๊ฐค๋ฌ๋ฆฌ ์๋ก๊ณ ์นจ")
|
| 201 |
|
| 202 |
+
gallery_refresh_btn.click(fn=get_gallery, outputs=gallery)
|
| 203 |
demo.load(fn=get_gallery, outputs=gallery)
|
| 204 |
|
| 205 |
+
# ๋ค์ด๋ก๋ ํญ
|
| 206 |
+
with gr.Tab("๐ป ํด๋ผ์ด์ธํธ"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
gr.Markdown("""
|
| 208 |
+
## ํด๋ผ์ด์ธํธ ํ๋ก๊ทธ๋จ
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
### ๋ฐ๋ชจ ์ฝ๋
|
| 211 |
+
์๋๋ P2P ํด๋ผ์ด์ธํธ์ ๊ฐ๋จํ ๋ฐ๋ชจ ์ฝ๋์
๋๋ค:
|
|
|
|
|
|
|
| 212 |
|
|
|
|
|
|
|
| 213 |
```python
|
| 214 |
+
""" + CLIENT_CODE + """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
```
|
| 216 |
|
| 217 |
+
### ์ค์ ๊ตฌํ ์
|
| 218 |
+
- Gradio API๋ฅผ ํตํ ์๋ฒ ํต์
|
| 219 |
+
- GPU ์๋ ๊ฐ์ง ๋ฐ ํ์ฉ
|
| 220 |
+
- ์ค์ ์ด๋ฏธ์ง ์์ฑ ๋ชจ๋ธ ์ฐ๋
|
| 221 |
+
- ์๋ ์์
์ฒ๋ฆฌ ๋ฐ ๊ฒฐ๊ณผ ์ ์ก
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
# ์ฑ ์คํ
|
| 225 |
if __name__ == "__main__":
|
| 226 |
demo.launch()
|