SaniaE commited on
Commit
034e7eb
·
verified ·
1 Parent(s): 5a3fb2d

optimized audit endpoint

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -238,26 +238,44 @@ async def get_vision_saliency(file: UploadFile = File(...)):
238
 
239
  @app.post("/audit")
240
  async def internal_debate_audit(file: UploadFile = File(...), user_prompt: str = Query(...)):
241
- """The CLIP-Powered Jury: Fast deterministic grounding verification track."""
242
  start_time = time.perf_counter()
243
  image_bytes = await file.read()
244
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
245
 
246
- # OPTIMIZATION: Greedy decoding (do_sample=False) + short length constraint
247
- blip_caption = (await asyncio.to_thread(_generate_sync_batch, ["blip"], image, 1.0, 1, 1.0, 25, False))[0]
 
 
 
248
 
249
- # CLIP Scoring
250
  clip_m = MODELS["clip"]["model"]
251
  clip_p = MODELS["clip"]["processor"]
252
 
253
- inputs = clip_p(text=[user_prompt, blip_caption], images=image, return_tensors="pt", padding=True).to(DEVICE)
 
 
254
 
255
  with torch.no_grad():
256
- outputs = clip_m(**inputs)
257
- probs = outputs.logits_per_image.softmax(dim=-1).cpu().numpy()[0]
 
 
 
 
 
 
 
 
 
 
 
 
258
 
259
  u_score, m_score = float(probs[0]), float(probs[1])
260
 
 
261
  if u_score < 0.35:
262
  verdict = "Perspective Divergence: Intent not grounded in image."
263
  elif abs(u_score - m_score) < 0.15:
 
238
 
239
  @app.post("/audit")
240
  async def internal_debate_audit(file: UploadFile = File(...), user_prompt: str = Query(...)):
241
+ """The CLIP-Powered Jury: Highly optimized decoupled feature caching audit pass."""
242
  start_time = time.perf_counter()
243
  image_bytes = await file.read()
244
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
245
 
246
+ # 1. Fast Deterministic Model Perception
247
+ # (Uses our batched/short-constrained utility)
248
+ blip_caption = (await asyncio.to_thread(
249
+ _generate_batched_ensemble, ["blip"], image, 1.0, 1, 1.0, 20
250
+ ))[0]
251
 
252
+ # 2. Optimized Decoupled CLIP Scoring
253
  clip_m = MODELS["clip"]["model"]
254
  clip_p = MODELS["clip"]["processor"]
255
 
256
+ # Process text and image separately to enforce single-pass vision encoding
257
+ image_inputs = clip_p(images=image, return_tensors="pt").to(DEVICE)
258
+ text_inputs = clip_p(text=[user_prompt, blip_caption], return_tensors="pt", padding=True).to(DEVICE)
259
 
260
  with torch.no_grad():
261
+ # Extract and normalize image features (Vision Encoder hits this EXACTLY once)
262
+ image_features = clip_m.get_image_features(**image_inputs)
263
+ image_features = image_features / image_features.norm(dim=-1, keepdim=True)
264
+
265
+ # Extract and normalize text features
266
+ text_features = clip_m.get_text_features(**text_inputs)
267
+ text_features = text_features / text_features.norm(dim=-1, keepdim=True)
268
+
269
+ # Compute cosine similarity and scale with the model's learned temperature parameter
270
+ logit_scale = clip_m.logit_scale.exp()
271
+ logits_per_image = (image_features @ text_features.T) * logit_scale
272
+
273
+ # Apply softmax over text dimensions to get alignment probabilities
274
+ probs = F.softmax(logits_per_image, dim=-1).cpu().numpy()[0]
275
 
276
  u_score, m_score = float(probs[0]), float(probs[1])
277
 
278
+ # 3. Decision Logic
279
  if u_score < 0.35:
280
  verdict = "Perspective Divergence: Intent not grounded in image."
281
  elif abs(u_score - m_score) < 0.15: