updated ui tester
Browse files
app.py
CHANGED
|
@@ -102,22 +102,26 @@ async def generate_endpoint(
|
|
| 102 |
|
| 103 |
@app.post("/ui-tester")
|
| 104 |
async def ui_tester(file: UploadFile = File(...), description: str = Query(...)):
|
| 105 |
-
"""Uses BLIP's native capability to score the match between image and text."""
|
| 106 |
image = Image.open(file.file).convert("RGB")
|
| 107 |
blip_data = MODELS["blip"]
|
| 108 |
|
| 109 |
-
# We use the processor to prepare both image and text for the model
|
| 110 |
inputs = blip_data["processor"](images=image, text=description, return_tensors="pt").to(DEVICE)
|
| 111 |
|
| 112 |
with torch.no_grad():
|
| 113 |
-
# BLIP models have a built-in vision/text matching logic
|
| 114 |
-
# For simple captioning models, we can use the model's loss or log-likelihood
|
| 115 |
outputs = blip_data["model"](**inputs, labels=inputs["input_ids"])
|
| 116 |
-
# We convert the loss to a pseudo-similarity score (lower loss = higher match)
|
| 117 |
loss = outputs.loss.item()
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
return {
|
| 121 |
-
"
|
| 122 |
-
"
|
|
|
|
|
|
|
| 123 |
}
|
|
|
|
| 102 |
|
| 103 |
@app.post("/ui-tester")
|
| 104 |
async def ui_tester(file: UploadFile = File(...), description: str = Query(...)):
|
|
|
|
| 105 |
image = Image.open(file.file).convert("RGB")
|
| 106 |
blip_data = MODELS["blip"]
|
| 107 |
|
|
|
|
| 108 |
inputs = blip_data["processor"](images=image, text=description, return_tensors="pt").to(DEVICE)
|
| 109 |
|
| 110 |
with torch.no_grad():
|
|
|
|
|
|
|
| 111 |
outputs = blip_data["model"](**inputs, labels=inputs["input_ids"])
|
|
|
|
| 112 |
loss = outputs.loss.item()
|
| 113 |
+
|
| 114 |
+
# Scaling the score to make 0.3 look like a "Strong Match"
|
| 115 |
+
# and 0.2 look like a "Poor Match"
|
| 116 |
+
# Using a steep sigmoid or a linear multiplier:
|
| 117 |
+
score = max(0, min(1, (1 / (loss + 1e-6)) * 0.5))
|
| 118 |
+
|
| 119 |
+
# Define thresholds based on your tests
|
| 120 |
+
status = "Match Found" if score > 0.25 else "No Match"
|
| 121 |
|
| 122 |
return {
|
| 123 |
+
"raw_loss": round(loss, 4),
|
| 124 |
+
"confidence_score": round(score, 4),
|
| 125 |
+
"status": status,
|
| 126 |
+
"is_valid": score > 0.25
|
| 127 |
}
|