Spaces:
Sleeping
Sleeping
File size: 11,563 Bytes
02c6351 |
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
#!/usr/bin/env python3
"""
Debug version of the web app to isolate the black screen issue
"""
import asyncio
import base64
import io
import json
import logging
import os
import sys
from pathlib import Path
from typing import Dict, List, Optional, Set
import numpy as np
import torch
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
from PIL import Image
# Add src to Python path
src_path = Path(__file__).parent / "src"
sys.path.insert(0, str(src_path))
# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Simple debug app
app = FastAPI(title="Diamond CSGO AI Player - Debug")
class DebugGameEngine:
"""Simple debug version to test image rendering"""
def __init__(self):
self.obs = None
self.frame_count = 0
self.initialized = False
def create_test_observation(self):
"""Create a test observation for debugging"""
# Create a test image with some pattern
height, width = 150, 600
# Create RGB pattern
img_array = np.zeros((height, width, 3), dtype=np.uint8)
# Add some pattern to make it visible
for y in range(height):
for x in range(width):
img_array[y, x, 0] = (x % 256) # Red gradient
img_array[y, x, 1] = (y % 256) # Green gradient
img_array[y, x, 2] = ((x + y) % 256) # Blue pattern
# Convert to torch tensor in the expected format [-1, 1]
tensor = torch.from_numpy(img_array).float().permute(2, 0, 1) # CHW format
tensor = tensor.div(255).mul(2).sub(1) # Convert to [-1, 1] range
tensor = tensor.unsqueeze(0) # Add batch dimension
logger.info(f"Created test observation: {tensor.shape}, range: {tensor.min():.3f} to {tensor.max():.3f}")
return tensor
def obs_to_base64(self, obs: torch.Tensor) -> str:
"""Convert observation tensor to base64 image for web display"""
if obs is None:
logger.warning("Observation is None")
return ""
try:
logger.debug(f"Converting obs: shape={obs.shape}, dtype={obs.dtype}")
# Convert tensor to PIL Image
if obs.ndim == 4 and obs.size(0) == 1:
img_array = obs[0].add(1).div(2).mul(255).byte().permute(1, 2, 0).cpu().numpy()
else:
img_array = obs.add(1).div(2).mul(255).byte().permute(1, 2, 0).cpu().numpy()
logger.debug(f"Image array: shape={img_array.shape}, range={img_array.min()} to {img_array.max()}")
img = Image.fromarray(img_array)
# Resize for web display
img = img.resize((600, 300), Image.BICUBIC)
# Convert to base64
buffer = io.BytesIO()
img.save(buffer, format='PNG')
img_str = base64.b64encode(buffer.getvalue()).decode()
logger.debug(f"Successfully converted to base64, length: {len(img_str)}")
return f"data:image/png;base64,{img_str}"
except Exception as e:
logger.error(f"Error converting observation to base64: {e}")
import traceback
traceback.print_exc()
return ""
async def initialize(self):
"""Initialize with test data"""
logger.info("Initializing debug game engine...")
self.obs = self.create_test_observation()
self.initialized = True
logger.info("Debug game engine initialized successfully!")
return True
# Global debug engine
debug_engine = DebugGameEngine()
connected_clients: Set[WebSocket] = set()
@app.on_event("startup")
async def startup_event():
"""Initialize debug engine"""
success = await debug_engine.initialize()
if success:
# Start a simple game loop
asyncio.create_task(debug_game_loop())
async def debug_game_loop():
"""Simple debug game loop"""
while True:
try:
if debug_engine.initialized and connected_clients:
# Send test frame to all connected clients
frame_data = {
'type': 'frame',
'image': debug_engine.obs_to_base64(debug_engine.obs),
'frame_count': debug_engine.frame_count,
'reward': 0.0,
'info': f"Debug frame {debug_engine.frame_count}"
}
logger.debug(f"Sending frame {debug_engine.frame_count}")
# Send to all connected clients
disconnected = set()
for client in connected_clients.copy():
try:
await client.send_text(json.dumps(frame_data))
except Exception as e:
logger.error(f"Error sending to client: {e}")
disconnected.add(client)
# Remove disconnected clients
connected_clients.difference_update(disconnected)
debug_engine.frame_count += 1
await asyncio.sleep(1.0 / 15) # 15 FPS
except Exception as e:
logger.error(f"Error in debug game loop: {e}")
await asyncio.sleep(0.1)
@app.get("/", response_class=HTMLResponse)
async def get_homepage():
"""Serve debug interface"""
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Diamond CSGO AI - Debug</title>
<style>
body {
margin: 0;
padding: 20px;
background: #1a1a1a;
color: white;
font-family: monospace;
text-align: center;
}
#gameCanvas {
border: 2px solid #00ff00;
background: #000;
margin: 20px auto;
display: block;
}
#status {
margin: 10px;
padding: 10px;
background: #2a2a2a;
border-radius: 4px;
}
.info {
color: #00ff00;
margin: 5px 0;
}
</style>
</head>
<body>
<h1>🔧 Diamond CSGO AI - Debug Mode</h1>
<p>Testing image rendering and WebSocket communication</p>
<canvas id="gameCanvas" width="600" height="300"></canvas>
<div id="status">
<div class="info">Status: <span id="connectionStatus">Connecting...</span></div>
<div class="info">Frame: <span id="frameCount">0</span></div>
<div class="info">Info: <span id="info">Waiting...</span></div>
</div>
<div id="debug">
<h3>Debug Information</h3>
<p id="lastUpdate">No updates yet</p>
<p id="imageInfo">No image data</p>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const statusEl = document.getElementById('connectionStatus');
const frameEl = document.getElementById('frameCount');
const infoEl = document.getElementById('info');
const lastUpdateEl = document.getElementById('lastUpdate');
const imageInfoEl = document.getElementById('imageInfo');
let ws = null;
function connectWebSocket() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${protocol}//${window.location.host}/ws`;
console.log('Connecting to:', wsUrl);
ws = new WebSocket(wsUrl);
ws.onopen = function(event) {
console.log('WebSocket connected');
statusEl.textContent = 'Connected';
statusEl.style.color = '#00ff00';
};
ws.onmessage = function(event) {
console.log('Received message');
const data = JSON.parse(event.data);
if (data.type === 'frame') {
console.log('Received frame:', data.frame_count);
lastUpdateEl.textContent = `Last update: ${new Date().toLocaleTimeString()}`;
// Update frame display
if (data.image && data.image.length > 0) {
console.log('Loading image, length:', data.image.length);
imageInfoEl.textContent = `Image data length: ${data.image.length}`;
const img = new Image();
img.onload = function() {
console.log('Image loaded successfully');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
};
img.onerror = function() {
console.error('Failed to load image');
imageInfoEl.textContent = 'Failed to load image';
};
img.src = data.image;
} else {
console.warn('No image data received');
imageInfoEl.textContent = 'No image data received';
}
frameEl.textContent = data.frame_count;
infoEl.textContent = data.info || 'No info';
}
};
ws.onclose = function(event) {
console.log('WebSocket disconnected');
statusEl.textContent = 'Disconnected';
statusEl.style.color = '#ff0000';
setTimeout(connectWebSocket, 1000);
};
ws.onerror = function(event) {
console.error('WebSocket error:', event);
statusEl.textContent = 'Error';
statusEl.style.color = '#ff0000';
};
}
// Initialize
connectWebSocket();
</script>
</body>
</html>
"""
return html_content
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
"""Handle WebSocket connections"""
await websocket.accept()
connected_clients.add(websocket)
logger.info(f"Client connected. Total clients: {len(connected_clients)}")
try:
while True:
# Just wait for disconnection
await websocket.receive_text()
except WebSocketDisconnect:
connected_clients.discard(websocket)
logger.info(f"Client disconnected. Total clients: {len(connected_clients)}")
except Exception as e:
logger.error(f"WebSocket error: {e}")
connected_clients.discard(websocket)
if __name__ == "__main__":
import uvicorn
uvicorn.run("debug_app:app", host="0.0.0.0", port=7861, reload=False)
|