Omarelrayes commited on
Commit
67a7d05
·
verified ·
1 Parent(s): 7739d1f

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +11 -29
app/main.py CHANGED
@@ -8,11 +8,6 @@ from . import configs as config
8
  from .models import UploadResponse, ClassifyRequest, ClassifyResponse, SegmentRequest, SegmentResponse
9
  from .services.predictor import classify_image
10
  from .services.segmenter import segment_image
11
- from .services.routing import (
12
- router as model_router,
13
- resolve_classifier_version,
14
- get_ab_status,
15
- )
16
  from .storage import (
17
  save_image,
18
  get_image_path,
@@ -26,14 +21,11 @@ from .monitoring.metrics import MetricsMiddleware, metrics_endpoint
26
 
27
  app = FastAPI(
28
  title="AI Image Classification API",
29
- version="3.1.0"
30
  )
31
 
32
  app.add_middleware(MetricsMiddleware)
33
 
34
- # ✅ Include the router for model management endpoints
35
- app.include_router(model_router)
36
-
37
  ALLOWED_CONTENT_TYPES = {"image/jpeg", "image/png", "image/jpg"}
38
  MAX_FILE_SIZE = 10 * 1024 * 1024
39
 
@@ -51,8 +43,6 @@ async def models_status():
51
  return {
52
  "classification_loaded": classification_model is not None,
53
  "segmentation_loaded": segmentation_model is not None,
54
- "ab_testing": get_ab_status(), # ✅ استخدام الدالة المستقلة
55
- "loaded_versions": config.get_loaded_versions(),
56
  "storage_paths": {
57
  "images": str(config.IMAGES_DIR),
58
  "segments": str(config.SEGMENTS_DIR),
@@ -60,11 +50,6 @@ async def models_status():
60
  }
61
 
62
 
63
- @app.get("/api/ab-status")
64
- async def ab_testing_status():
65
- return get_ab_status() # ✅ استخدام الدالة المستقلة
66
-
67
-
68
  @app.post("/api/upload", status_code=201, response_model=UploadResponse)
69
  async def upload_image(file: UploadFile = File(...)):
70
  if file.content_type not in ALLOWED_CONTENT_TYPES:
@@ -112,24 +97,20 @@ async def classify(body: ClassifyRequest):
112
  if image_bytes is None:
113
  raise HTTPException(status_code=404, detail="Image not found")
114
 
 
115
  existing = get_classification_result(body.image_id)
116
- if existing and not body.model_version:
117
  return ClassifyResponse(
118
  image_id=body.image_id,
119
  prediction=existing["prediction"],
120
  confidence=existing["confidence"],
121
- model_version=existing["model_version"],
122
  status=existing.get("status", "completed"),
123
  )
124
 
125
- # ✅ استخدام الدالة المستقلة
126
- resolved_uri = resolve_classifier_version(body.model_version)
127
-
128
  try:
129
- prediction, confidence, actual_version = classify_image(
130
- image_bytes,
131
- model_version=resolved_uri,
132
- )
133
  except ValueError as e:
134
  raise HTTPException(status_code=400, detail=str(e))
135
  except Exception as e:
@@ -138,7 +119,7 @@ async def classify(body: ClassifyRequest):
138
  result = {
139
  "prediction": prediction,
140
  "confidence": confidence,
141
- "model_version": actual_version,
142
  "status": "completed",
143
  }
144
  save_classification_result(body.image_id, result)
@@ -159,7 +140,7 @@ async def get_classify_result(image_id: str):
159
  image_id=image_id,
160
  prediction=result["prediction"],
161
  confidence=result["confidence"],
162
- model_version=result["model_version"],
163
  status=result.get("status", "completed"),
164
  )
165
 
@@ -170,6 +151,7 @@ async def segment(body: SegmentRequest):
170
  if image_bytes is None:
171
  raise HTTPException(status_code=404, detail="Image not found")
172
 
 
173
  existing = get_segmentation_result(body.image_id)
174
  if existing:
175
  return SegmentResponse(
@@ -181,6 +163,7 @@ async def segment(body: SegmentRequest):
181
  error=existing.get("error"),
182
  )
183
 
 
184
  seg_result = segment_image(image_bytes)
185
  seg_path = save_segmentation_result(body.image_id, seg_result)
186
 
@@ -217,7 +200,7 @@ async def root():
217
 
218
  return {
219
  "service": "AI Image Classification API",
220
- "version": "3.1.0",
221
  "classification": class_ready,
222
  "segmentation": seg_ready,
223
  "endpoints": {
@@ -226,7 +209,6 @@ async def root():
226
  "segment": "POST /api/segment",
227
  "health": "GET /health",
228
  "metrics": "GET /metrics",
229
- "ab_status": "GET /api/ab-status",
230
  "docs": "/docs",
231
  },
232
  "models_status": "/models/status",
 
8
  from .models import UploadResponse, ClassifyRequest, ClassifyResponse, SegmentRequest, SegmentResponse
9
  from .services.predictor import classify_image
10
  from .services.segmenter import segment_image
 
 
 
 
 
11
  from .storage import (
12
  save_image,
13
  get_image_path,
 
21
 
22
  app = FastAPI(
23
  title="AI Image Classification API",
24
+ version="3.2.0"
25
  )
26
 
27
  app.add_middleware(MetricsMiddleware)
28
 
 
 
 
29
  ALLOWED_CONTENT_TYPES = {"image/jpeg", "image/png", "image/jpg"}
30
  MAX_FILE_SIZE = 10 * 1024 * 1024
31
 
 
43
  return {
44
  "classification_loaded": classification_model is not None,
45
  "segmentation_loaded": segmentation_model is not None,
 
 
46
  "storage_paths": {
47
  "images": str(config.IMAGES_DIR),
48
  "segments": str(config.SEGMENTS_DIR),
 
50
  }
51
 
52
 
 
 
 
 
 
53
  @app.post("/api/upload", status_code=201, response_model=UploadResponse)
54
  async def upload_image(file: UploadFile = File(...)):
55
  if file.content_type not in ALLOWED_CONTENT_TYPES:
 
97
  if image_bytes is None:
98
  raise HTTPException(status_code=404, detail="Image not found")
99
 
100
+ # ✅ Check for cached result
101
  existing = get_classification_result(body.image_id)
102
+ if existing:
103
  return ClassifyResponse(
104
  image_id=body.image_id,
105
  prediction=existing["prediction"],
106
  confidence=existing["confidence"],
107
+ model_version=existing.get("model_version", "hf_savedmodel"),
108
  status=existing.get("status", "completed"),
109
  )
110
 
111
+ # ✅ Classify without model_version
 
 
112
  try:
113
+ prediction, confidence = classify_image(image_bytes)
 
 
 
114
  except ValueError as e:
115
  raise HTTPException(status_code=400, detail=str(e))
116
  except Exception as e:
 
119
  result = {
120
  "prediction": prediction,
121
  "confidence": confidence,
122
+ "model_version": "hf_savedmodel",
123
  "status": "completed",
124
  }
125
  save_classification_result(body.image_id, result)
 
140
  image_id=image_id,
141
  prediction=result["prediction"],
142
  confidence=result["confidence"],
143
+ model_version=result.get("model_version", "hf_savedmodel"),
144
  status=result.get("status", "completed"),
145
  )
146
 
 
151
  if image_bytes is None:
152
  raise HTTPException(status_code=404, detail="Image not found")
153
 
154
+ # ✅ Check for cached result
155
  existing = get_segmentation_result(body.image_id)
156
  if existing:
157
  return SegmentResponse(
 
163
  error=existing.get("error"),
164
  )
165
 
166
+ # ✅ Run segmentation
167
  seg_result = segment_image(image_bytes)
168
  seg_path = save_segmentation_result(body.image_id, seg_result)
169
 
 
200
 
201
  return {
202
  "service": "AI Image Classification API",
203
+ "version": "3.2.0",
204
  "classification": class_ready,
205
  "segmentation": seg_ready,
206
  "endpoints": {
 
209
  "segment": "POST /api/segment",
210
  "health": "GET /health",
211
  "metrics": "GET /metrics",
 
212
  "docs": "/docs",
213
  },
214
  "models_status": "/models/status",