Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Update test_ai_integration.py
Browse files- test_ai_integration.py +0 -182
test_ai_integration.py
CHANGED
|
@@ -290,186 +290,4 @@ def test_ai_integration():
|
|
| 290 |
|
| 291 |
print("\n--- All AI Integration Tests Completed ---")
|
| 292 |
|
| 293 |
-
from fastapi import FastAPI, UploadFile, File
|
| 294 |
-
from fastapi.responses import JSONResponse
|
| 295 |
-
import uvicorn
|
| 296 |
-
import io
|
| 297 |
-
|
| 298 |
-
# Initialize FastAPI app
|
| 299 |
-
app = FastAPI()
|
| 300 |
-
|
| 301 |
-
# Store initialized components
|
| 302 |
-
gpu_components = None
|
| 303 |
-
|
| 304 |
-
@app.on_event("startup")
|
| 305 |
-
async def startup_event():
|
| 306 |
-
"""Initialize GPU components on server startup"""
|
| 307 |
-
global gpu_components
|
| 308 |
-
gpu_components = test_ai_integration()
|
| 309 |
-
|
| 310 |
-
@app.post("/process_image")
|
| 311 |
-
async def process_image(image: UploadFile = File(...)):
|
| 312 |
-
"""Process an image using the initialized GPU components"""
|
| 313 |
-
try:
|
| 314 |
-
# Read the image
|
| 315 |
-
contents = await image.read()
|
| 316 |
-
img = Image.open(io.BytesIO(contents)).convert('RGB')
|
| 317 |
-
|
| 318 |
-
# Process using existing components
|
| 319 |
-
with websocket_manager() as storage:
|
| 320 |
-
# Convert image to numpy array
|
| 321 |
-
image_array = np.array(img)
|
| 322 |
-
|
| 323 |
-
# Store in WebSocket storage
|
| 324 |
-
storage.store_tensor("input_image", image_array)
|
| 325 |
-
|
| 326 |
-
# Process using first AI accelerator
|
| 327 |
-
result = gpu_components['ai_accelerators'][0].inference(
|
| 328 |
-
gpu_components['model_id'],
|
| 329 |
-
"input_image"
|
| 330 |
-
)
|
| 331 |
-
|
| 332 |
-
return JSONResponse({
|
| 333 |
-
"result": result.tolist() if isinstance(result, np.ndarray) else result,
|
| 334 |
-
"status": "success"
|
| 335 |
-
})
|
| 336 |
-
|
| 337 |
-
except Exception as e:
|
| 338 |
-
return JSONResponse({
|
| 339 |
-
"error": str(e),
|
| 340 |
-
"status": "error"
|
| 341 |
-
}, status_code=500)
|
| 342 |
-
|
| 343 |
-
@app.get("/status")
|
| 344 |
-
async def get_status():
|
| 345 |
-
"""Get the status of the GPU components"""
|
| 346 |
-
if not gpu_components:
|
| 347 |
-
return {"status": "not_initialized"}
|
| 348 |
-
|
| 349 |
-
return {
|
| 350 |
-
"status": "running",
|
| 351 |
-
"num_chips": len(gpu_components['chips']),
|
| 352 |
-
"num_accelerators": len(gpu_components['ai_accelerators']),
|
| 353 |
-
"model_id": gpu_components['model_id']
|
| 354 |
-
}
|
| 355 |
-
|
| 356 |
-
def test_ai_integration():
|
| 357 |
-
"""Original test function modified to return components"""
|
| 358 |
-
print("\n--- Testing WebSocket-Based AI Integration with Zero CPU Usage ---")
|
| 359 |
-
from electron_speed import TARGET_SWITCHES_PER_SEC, TRANSISTORS_ON_CHIP, drift_velocity, speed_of_light_silicon
|
| 360 |
-
|
| 361 |
-
components = {
|
| 362 |
-
'chips': [],
|
| 363 |
-
'ai_accelerators': [],
|
| 364 |
-
'model_id': None
|
| 365 |
-
}
|
| 366 |
-
|
| 367 |
-
# Rest of your original test_ai_integration code here...
|
| 368 |
-
# Store important components in the components dict
|
| 369 |
-
# Replace print statements with logging if needed
|
| 370 |
-
|
| 371 |
-
return components
|
| 372 |
-
|
| 373 |
-
def create_app():
|
| 374 |
-
"""Create and configure the FastAPI application"""
|
| 375 |
-
from fastapi import FastAPI, File, UploadFile
|
| 376 |
-
from fastapi.responses import JSONResponse
|
| 377 |
-
import io
|
| 378 |
-
from PIL import Image
|
| 379 |
-
|
| 380 |
-
app = FastAPI(
|
| 381 |
-
title="AI Integration Test API",
|
| 382 |
-
description="WebSocket-based GPU storage with zero CPU memory usage",
|
| 383 |
-
version="1.0.0"
|
| 384 |
-
)
|
| 385 |
-
|
| 386 |
-
@app.on_event("startup")
|
| 387 |
-
async def startup_event():
|
| 388 |
-
"""Initialize GPU components on server startup"""
|
| 389 |
-
global gpu_components
|
| 390 |
-
gpu_components = test_ai_integration()
|
| 391 |
-
|
| 392 |
-
@app.get("/")
|
| 393 |
-
async def root():
|
| 394 |
-
"""Root endpoint showing system status"""
|
| 395 |
-
from electron_speed import drift_velocity
|
| 396 |
-
return {
|
| 397 |
-
"status": "running",
|
| 398 |
-
"speed": f"{drift_velocity:.2e} m/s",
|
| 399 |
-
"flip_flop_delay": "1.02e-15 s",
|
| 400 |
-
"max_frequency": "9.80e+14 Hz"
|
| 401 |
-
}
|
| 402 |
-
|
| 403 |
-
@app.post("/process")
|
| 404 |
-
async def process_image(image: UploadFile = File(...)):
|
| 405 |
-
"""Process an image using WebSocket-based GPU acceleration"""
|
| 406 |
-
try:
|
| 407 |
-
contents = await image.read()
|
| 408 |
-
img = Image.open(io.BytesIO(contents)).convert('RGB')
|
| 409 |
-
|
| 410 |
-
# Process using initialized GPU components
|
| 411 |
-
with websocket_manager() as storage:
|
| 412 |
-
# Convert and store image
|
| 413 |
-
image_array = np.array(img)
|
| 414 |
-
storage.store_tensor("input_image", image_array)
|
| 415 |
-
|
| 416 |
-
# Run inference
|
| 417 |
-
if gpu_components and gpu_components.get('ai_accelerators'):
|
| 418 |
-
accelerator = gpu_components['ai_accelerators'][0]
|
| 419 |
-
result = accelerator.inference(
|
| 420 |
-
gpu_components['model_id'],
|
| 421 |
-
"input_image"
|
| 422 |
-
)
|
| 423 |
-
|
| 424 |
-
return JSONResponse({
|
| 425 |
-
"status": "success",
|
| 426 |
-
"result": result.tolist() if isinstance(result, np.ndarray) else result,
|
| 427 |
-
"processing_stats": {
|
| 428 |
-
"flip_flop_delay": "1.02e-15 s",
|
| 429 |
-
"max_frequency": "9.80e+14 Hz"
|
| 430 |
-
}
|
| 431 |
-
})
|
| 432 |
-
else:
|
| 433 |
-
raise ValueError("GPU components not initialized")
|
| 434 |
-
|
| 435 |
-
except Exception as e:
|
| 436 |
-
return JSONResponse(
|
| 437 |
-
status_code=500,
|
| 438 |
-
content={
|
| 439 |
-
"status": "error",
|
| 440 |
-
"message": str(e)
|
| 441 |
-
}
|
| 442 |
-
)
|
| 443 |
-
|
| 444 |
-
@app.get("/status")
|
| 445 |
-
async def get_status():
|
| 446 |
-
if not gpu_components:
|
| 447 |
-
return {"status": "not_initialized"}
|
| 448 |
-
return {
|
| 449 |
-
"status": "running",
|
| 450 |
-
"flip_flop_delay": "1.02e-15 s",
|
| 451 |
-
"max_frequency": "9.80e+14 Hz",
|
| 452 |
-
"model_loaded": gpu_components.get('model_id') is not None
|
| 453 |
-
}
|
| 454 |
-
|
| 455 |
-
return app
|
| 456 |
-
|
| 457 |
-
if __name__ == "__main__":
|
| 458 |
-
import logging
|
| 459 |
-
import uvicorn
|
| 460 |
-
from fastapi import FastAPI
|
| 461 |
-
|
| 462 |
-
logging.basicConfig(level=logging.INFO)
|
| 463 |
-
logger = logging.getLogger(__name__)
|
| 464 |
-
|
| 465 |
-
# Create the FastAPI app
|
| 466 |
-
app = create_app()
|
| 467 |
-
|
| 468 |
-
# Run as FastAPI server
|
| 469 |
-
logger.info("Starting AI Integration Test Server...")
|
| 470 |
-
uvicorn.run("test_ai_integration:create_app()",
|
| 471 |
-
host="0.0.0.0",
|
| 472 |
-
port=8000,
|
| 473 |
-
factory=True,
|
| 474 |
-
reload=True)
|
| 475 |
|
|
|
|
| 290 |
|
| 291 |
print("\n--- All AI Integration Tests Completed ---")
|
| 292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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