Omarelrayes commited on
Commit
3f3d260
·
verified ·
1 Parent(s): 00e8816

Create routing.py

Browse files
Files changed (1) hide show
  1. app/services/routing.py +86 -0
app/services/routing.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/services/routing.py
2
+ from fastapi import APIRouter
3
+ from typing import Dict, Any, Optional
4
+
5
+ from app.configs import CLASSIFIER_URI, SEGMENTER_URI
6
+
7
+
8
+ router = APIRouter()
9
+
10
+ # =========================
11
+ # Model Registry (for A/B testing)
12
+ # =========================
13
+ # Maps version names to actual model URIs
14
+ MODEL_REGISTRY = {
15
+ "default": CLASSIFIER_URI,
16
+ "v1": CLASSIFIER_URI,
17
+ "hf_savedmodel": "hf_savedmodel",
18
+ # يمكنك إضافة إصدارات أخرى هنا
19
+ # "v2": "hf_savedmodel_v2",
20
+ }
21
+
22
+ # A/B Testing configuration
23
+ AB_TESTING_ENABLED = False
24
+ AB_TESTING_RATIO = 0.0 # 0.0 = no traffic to new model
25
+
26
+
27
+ def get_ab_status() -> Dict[str, Any]:
28
+ """Get current A/B testing status"""
29
+ return {
30
+ "enabled": AB_TESTING_ENABLED,
31
+ "ratio": AB_TESTING_RATIO,
32
+ "default_version": CLASSIFIER_URI,
33
+ "available_versions": list(MODEL_REGISTRY.keys()),
34
+ }
35
+
36
+
37
+ def resolve_classifier_version(version: Optional[str]) -> str:
38
+ """
39
+ Resolve a version name to actual model URI.
40
+
41
+ Args:
42
+ version: Version name (e.g., "v1", "default") or None
43
+
44
+ Returns:
45
+ Actual model URI
46
+ """
47
+ if version is None or version == "":
48
+ return CLASSIFIER_URI
49
+
50
+ resolved = MODEL_REGISTRY.get(version)
51
+ if resolved is None:
52
+ # Fallback to default if version not found
53
+ return CLASSIFIER_URI
54
+
55
+ return resolved
56
+
57
+
58
+ # =========================
59
+ # Optional: A/B Testing Router Endpoints
60
+ # =========================
61
+ @router.get("/api/models/versions")
62
+ async def list_model_versions():
63
+ """List all available model versions"""
64
+ return {
65
+ "versions": list(MODEL_REGISTRY.keys()),
66
+ "default": CLASSIFIER_URI,
67
+ "registry": MODEL_REGISTRY,
68
+ }
69
+
70
+
71
+ @router.post("/api/ab-testing/config")
72
+ async def update_ab_config(enabled: bool = False, ratio: float = 0.0):
73
+ """Update A/B testing configuration (admin only)"""
74
+ global AB_TESTING_ENABLED, AB_TESTING_RATIO
75
+
76
+ if not 0.0 <= ratio <= 1.0:
77
+ return {"error": "Ratio must be between 0.0 and 1.0"}
78
+
79
+ AB_TESTING_ENABLED = enabled
80
+ AB_TESTING_RATIO = ratio
81
+
82
+ return {
83
+ "status": "updated",
84
+ "enabled": AB_TESTING_ENABLED,
85
+ "ratio": AB_TESTING_RATIO,
86
+ }