Team Ai
Apppublic

IDKHowToCodeFr/tinyml-backend

sourceHugging Faceupdated 11h agoView on Hugging Face
1likes
streamer.py37 linesDownload Raw Back to backend
1import asyncio2from typing import AsyncGenerator3from simulator import PatientDataSimulator4from inference import evaluate5from schemas import PatientData6 7class TelemetryStreamer:8    """9    Deep Module for Telemetry Streaming.10    Encapsulates the simulator lifecycle, ML inference threading, and payload formatting.11    Provides a simple async generator interface.12    """13    def __init__(self, ensemble_model):14        self.ensemble_model = ensemble_model15        self.simulator = PatientDataSimulator()16 17    async def stream(self) -> AsyncGenerator[dict, None]:18        async for state in self.simulator.run():19            data = PatientData(**state)20            21            # Offload ML inference to thread pool to avoid blocking the event loop22            prediction_result = await asyncio.to_thread(evaluate, self.ensemble_model, data)23            24            front_pred = None25            if "error" not in prediction_result:26                front_pred = {27                    "is_at_risk": 1 if prediction_result.get("prediction") else 0,28                    "label": prediction_result.get("prediction_label"),29                    "confidence": prediction_result.get("probability"),30                    "disease_probs": prediction_result.get("disease_probs", {})31                }32            33            yield {34                "sensor_data": data.model_dump(),35                "prediction": front_pred36            }37