11import requests
22from fastapi import FastAPI
3+
34from .log_analyser import analyse_logs
4- from ml_client import call_ml
5- from aggregator import aggregate
6- from decider import decide
5+ from . ml_client import call_ml
6+ from . aggregator import aggregate
7+ from . decider import decide
78from shared .signal import Signal
89
910API_URL = "http://api:8000"
@@ -27,37 +28,47 @@ def run(payload: dict):
2728 record_id = payload .get ("record_id" )
2829 file_path = payload .get ("file_path" )
2930
30- # call ml service
31- ml_result = call_ml (file_path , record_id )
31+ if not record_id or not file_path :
32+ return {"error" : "record_id or file_path missing" }
33+
34+ # 🔹 Step 1 — call ML service
35+ try :
36+ ml_result = call_ml (file_path , record_id )
37+ except Exception as e :
38+ return {"error" : f"ML call failed: { str (e )} " }
3239
33- preprocessing = Signal (** ml_result ["preprocessing" ])
34- detection = Signal (** ml_result ["detection" ])
40+ try :
41+ preprocessing = Signal (** ml_result ["preprocessing" ])
42+ detection = Signal (** ml_result ["detection" ])
43+ except Exception as e :
44+ return {"error" : f"Invalid ML response: { str (e )} " }
3545
36- # analyse logs
46+ # 🔹 Step 2 — analyse logs
3747 log_signal = analyse_logs ()
3848
39- # aggregate all three signals
49+ # 🔹 Step 3 — aggregate signals
4050 aggregated = aggregate (preprocessing , detection , log_signal )
4151
42- # decide verdict
52+ # 🔹 Step 4 — decide verdict
4353 decision = decide (aggregated , record_id )
4454
45- # send verdict back to api
55+ # 🔹 Step 5 — send verdict back to API
4656 try :
4757 requests .post (
4858 f"{ API_URL } /verdict" ,
4959 json = {
5060 "record_id" : record_id ,
51- "verdict" : decision [ "verdict" ] ,
52- "verdict_score" : decision [ "score" ] ,
61+ "verdict" : decision . get ( "verdict" ) ,
62+ "verdict_score" : decision . get ( "score" ) ,
5363 },
5464 timeout = 10 ,
5565 )
5666 except Exception :
67+ # don't crash pipeline if API is temporarily unavailable
5768 pass
5869
5970 return {
6071 "record_id" : record_id ,
6172 "aggregated" : aggregated ,
6273 "decision" : decision ,
63- }
74+ }
0 commit comments