Spaces:
Running
Running
File size: 8,741 Bytes
98de093 a7c7b59 c03517d fb399bc c03517d dfac5b8 45219af 98de093 dfc872f fc818cf 1b00295 98de093 c03517d c74cf86 c03517d 98de093 c03517d 9180f7f c03517d 98de093 c03517d 98de093 c03517d 98de093 c03517d 968537c c03517d 98de093 c03517d 98de093 c03517d 98de093 c03517d eee6092 98de093 c03517d 98de093 c03517d dd917db c03517d c4ebdb1 c03517d 98de093 c03517d 98de093 c03517d bee0860 98de093 c4ebdb1 2e0d8dc c4ebdb1 98de093 c4ebdb1 dd917db c4ebdb1 98de093 c4ebdb1 98de093 c4ebdb1 bee0860 98de093 fc818cf 27976ce 1b00295 27976ce 1b00295 98de093 8bf3378 fc818cf 8bf3378 3871875 8bf3378 3871875 8bf3378 fc818cf 1b00295 fc818cf 1b00295 8bf3378 08f2847 8bf3378 a7c7b59 8bf3378 1929bcc 8bf3378 |
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
from fastapi import FastAPI, Request, WebSocket
from fastapi.responses import HTMLResponse
import subprocess
import asyncio
import os
from threading import Thread
from queue import Queue, Empty
os.environ['LD_LIBRARY_PATH'] = '/engines/maia/lib:' + os.environ.get('LD_LIBRARY_PATH', '')
user_playing = {
'total': 0,
'stockfish': 0,
'trollfish': 0,
'rodent': 0,
'patricia': 0,
'maia': 0
}
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
class EngineChess:
def __init__(self, path_engine):
self.path_engine = path_engine
self._stockfish = subprocess.Popen(
path_engine,
universal_newlines=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
self.queueOutput = Queue()
self.thread = Thread(target=enqueue_output, args=(self._stockfish.stdout, self.queueOutput))
self.thread.daemon = True # thread dies with the program
self.thread.start()
self._has_quit_command_been_sent = False
self._debug_view = True
def _put(self, command):
if not self._stockfish.stdin:
raise BrokenPipeError()
if self._stockfish.poll() is None and not self._has_quit_command_been_sent:
self._stockfish.stdin.write(f"{command}\n")
self._stockfish.stdin.flush()
if command == "quit":
self._has_quit_command_been_sent = True
def _read_line(self) -> str:
if not self._stockfish.stdout:
raise BrokenPipeError()
if self._stockfish.poll() is not None:
raise StockfishException("The Stockfish process has crashed")
try:
line = self.queueOutput.get_nowait() # or q.get(timeout=.1)
except Empty:
return ""
if self._debug_view:
print(f"Engine {self.path_engine[0]}:", line.strip())
return line.strip()
def _is_ready(self) -> None:
self._put("isready")
while self._read_line() != "readyok":
pass
def put(self, cmd):
return self._put(cmd)
def read_line(self) -> str:
return self._read_line()
app = FastAPI()
# WebSocket endpoints
@app.websocket("/stockfish-{version}")
async def websocket_endpoint(websocket: WebSocket, version: str):
await websocket.accept()
user_playing['total'] += 1
user_playing['stockfish'] += 1
stockfish = EngineChess([f"./engines/stockfish/stockfish-{version}-uci"])
async def read_from_socket(websocket: WebSocket):
async for data in websocket.iter_text():
print(f"Stockfish Client: {data}")
stockfish.put(data)
asyncio.create_task(read_from_socket(websocket))
try:
while True:
while True:
res = stockfish.read_line()
if res:
await websocket.send_text(f"{res}")
else:
break
await asyncio.sleep(0.1)
finally:
user_playing['total'] -= 1
user_playing['stockfish'] -= 1
@app.websocket("/maia-{elo}")
async def websocket_endpoint(websocket: WebSocket, elo: str):
await websocket.accept()
user_playing['total'] += 1
user_playing['maia'] += 1
stockfish = EngineChess([f"./engines/maia/maia-{elo}/lc0", "--backend=trivial"])
async def read_from_socket(websocket: WebSocket):
async for data in websocket.iter_text():
print(f"Maia Client: {data}")
stockfish.put(data)
asyncio.create_task(read_from_socket(websocket))
try:
while True:
while True:
res = stockfish.read_line()
if res:
await websocket.send_text(f"{res}")
else:
break
await asyncio.sleep(0.1)
finally:
user_playing['total'] -= 1
user_playing['maia'] -= 1
@app.websocket("/rodent3-{personality}")
async def websocket_endpoint(websocket: WebSocket, personality: str):
await websocket.accept()
user_playing['total'] += 1
user_playing['rodent'] += 1
stockfish = EngineChess([f"./engines/RodentIII/rodentIII-debug"])
await asyncio.sleep(1)
stockfish.put(f"setoption name PersonalityFile value {personality}.txt")
async def read_from_socket(websocket: WebSocket):
async for data in websocket.iter_text():
print(f"Rodent Client: {data}")
stockfish.put(data)
asyncio.create_task(read_from_socket(websocket))
try:
while True:
while True:
res = stockfish.read_line()
if res:
await websocket.send_text(f"{res}")
else:
break
await asyncio.sleep(0.1)
finally:
user_playing['total'] -= 1
user_playing['rodent'] -= 1
@app.websocket("/patricia-{elo}")
async def websocket_endpoint(websocket: WebSocket, elo: str):
await websocket.accept()
user_playing['total'] += 1
user_playing['patricia'] += 1
stockfish = EngineChess([f"./engines/Patricia/patricia"])
await asyncio.sleep(0.5)
stockfish.put(f"setoption name UCI_Elo value {elo}")
async def read_from_socket(websocket: WebSocket):
async for data in websocket.iter_text():
print(f"Patricia Client: {data}")
stockfish.put(data)
asyncio.create_task(read_from_socket(websocket))
try:
while True:
while True:
res = stockfish.read_line()
if res:
await websocket.send_text(f"{res}")
else:
break
await asyncio.sleep(0.1)
finally:
user_playing['total'] -= 1
user_playing['patricia'] -= 1
@app.get("/")
async def root(request: Request):
html_content = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Users playing now</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #1f1f1f;
color: #f0f0f0;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
text-align: center;
}
.container {
background-color: #2d2d2d;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
max-width: 400px;
width: 100%;
margin: 20px;
}
h1 {
color: #f0f0f0;
font-size: 2.5rem;
margin-bottom: 20px;
}
.stat {
margin-bottom: 10px;
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border-bottom: 1px solid #444;
}
.stat:last-child {
border-bottom: none;
}
.stat label {
font-weight: bold;
font-size: 1.2rem;
}
.stat span {
font-size: 1.2rem;
}
</style>
</head>
<body>
<div class="container">
<h1>Currently playing users</h1>
<div class="stat">
<label>Total Users:</label>
<span id="total-users">""" + str(user_playing['total']) + """</span>
</div>
<div class="stat">
<label>Stockfish Users:</label>
<span id="stockfish-users">""" + str(user_playing['stockfish']) + """</span>
</div>
<div class="stat">
<label>Rodent-III Users:</label>
<span id="rodent3-users">""" + str(user_playing['rodent']) + """</span>
</div>
<div class="stat">
<label>Patricia Users:</label>
<span id="patricia-users">""" + str(user_playing['patricia']) + """</span>
</div>
<div class="stat">
<label>Maia Users:</label>
<span id="maia-users">""" + str(user_playing['maia']) + """</span>
</div>
</div>
</body>
</html>
"""
return HTMLResponse(content=html_content) |