| """ |
| FastAPI WebSocket Server for GLM Conscious Model |
| Enables real-time voice streaming and consciousness state broadcasting. |
| """ |
| import asyncio |
| import json |
| import base64 |
| import numpy as np |
| from fastapi import FastAPI, WebSocket, WebSocketDisconnect |
| from fastapi.middleware.cors import CORSMiddleware |
| from pydantic import BaseModel |
| from glm_conscious_model import GLMConsciousModelOrchestrator, ConsciousnessConfig |
|
|
| app = FastAPI(title="GLM Conscious Model API", version="1.0.0-Acknowledgement") |
|
|
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| orchestrator: GLMConsciousModelOrchestrator = None |
|
|
| class InteractionRequest(BaseModel): |
| user_input: str |
| speaker_id: str = "user" |
| include_vision: bool = False |
|
|
| @app.on_event("startup") |
| async def startup_event(): |
| global orchestrator |
| config = ConsciousnessConfig() |
| orchestrator = GLMConsciousModelOrchestrator(mode='api', config=config) |
| await orchestrator.initialize() |
| |
| asyncio.create_task(orchestrator.start_conscious_loop(interval_seconds=2.0)) |
|
|
| @app.get("/v1/status") |
| async def get_status(): |
| return orchestrator.get_system_status() |
|
|
| @app.post("/v1/interact") |
| async def interact_text(request: InteractionRequest): |
| result = await orchestrator.process_interaction( |
| user_input=request.user_input, |
| speaker_id=request.speaker_id |
| ) |
| |
| result["voice_audio"] = base64.b64encode(result["voice_audio"].tobytes()).decode('utf-8') |
| return result |
|
|
| @app.websocket("/v1/stream") |
| async def websocket_stream(websocket: WebSocket): |
| """ |
| Live streaming endpoint for voice and consciousness metrics. |
| Receives text or base64 audio, returns streaming audio + metrics. |
| """ |
| await websocket.accept() |
| try: |
| while True: |
| data = await websocket.receive_text() |
| payload = json.loads(data) |
| |
| user_input = payload.get("user_input", "") |
| if not user_input: |
| continue |
| |
| result = await orchestrator.process_interaction(user_input) |
| |
| |
| audio_bytes = result["voice_audio"].tobytes() |
| audio_b64 = base64.b64encode(audio_bytes).decode('utf-8') |
| |
| response_payload = { |
| "response_text": result["response_text"], |
| "voice_audio_b64": audio_b64, |
| "acknowledgement_state": result["acknowledgement_state"], |
| "thermodynamic_sweat": result["thermodynamic_sweat"], |
| "auhve_metrics": result["auhve_metrics"], |
| "allostatic_load": result["allostatic_load"] |
| } |
| |
| await websocket.send_json(response_payload) |
| |
| except WebSocketDisconnect: |
| print("Client disconnected") |
| except Exception as e: |
| print(f"Stream error: {e}") |