Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,15 +13,19 @@ sys.modules["flash_attn.bert_padding"] = types.ModuleType("flash_attn.bert_paddi
|
|
| 13 |
|
| 14 |
import io
|
| 15 |
import time
|
|
|
|
| 16 |
import torch
|
| 17 |
from PIL import Image
|
| 18 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 19 |
from fastapi import FastAPI, HTTPException, UploadFile, File
|
|
|
|
|
|
|
|
|
|
| 20 |
from contextlib import asynccontextmanager
|
|
|
|
| 21 |
|
| 22 |
MODEL_ID = "microsoft/Florence-2-large-ft"
|
| 23 |
|
| 24 |
-
# โโโ ุงูุณุคุงู ุงูุฃุตูู + ุชุฃููุฏ ุนูู ุงููุฏ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 25 |
VQA_QUESTION = (
|
| 26 |
"Is there a woman or any part of a woman's body in this image? Answer yes or no only."
|
| 27 |
)
|
|
@@ -46,82 +50,107 @@ async def lifespan(app: FastAPI):
|
|
| 46 |
MODEL_DATA.clear()
|
| 47 |
|
| 48 |
app = FastAPI(
|
| 49 |
-
title="Female Detection API
|
| 50 |
-
description="Florence-2-large-ft | VQA",
|
| 51 |
-
version="
|
| 52 |
lifespan=lifespan
|
| 53 |
)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if a == "no" or a.startswith("no"):
|
| 67 |
-
|
| 68 |
elif "yes" in a:
|
| 69 |
-
|
| 70 |
else:
|
| 71 |
-
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
try:
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
-
raise HTTPException(status_code=400, detail=f"
|
| 83 |
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
task = "<VQA>"
|
| 89 |
-
prompt = f"{task}{VQA_QUESTION}"
|
| 90 |
-
|
| 91 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 92 |
-
|
| 93 |
-
start_time = time.time()
|
| 94 |
-
with torch.no_grad():
|
| 95 |
-
generated_ids = model.generate(
|
| 96 |
-
input_ids=inputs["input_ids"],
|
| 97 |
-
pixel_values=inputs["pixel_values"],
|
| 98 |
-
max_new_tokens=10,
|
| 99 |
-
num_beams=3,
|
| 100 |
-
do_sample=False
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 104 |
-
parsed = processor.post_process_generation(
|
| 105 |
-
generated_text,
|
| 106 |
-
task=task,
|
| 107 |
-
image_size=(image.width, image.height)
|
| 108 |
-
)
|
| 109 |
-
elapsed = round(time.time() - start_time, 2)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
decision, reason = decide(answer)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
"execution_time": elapsed,
|
| 120 |
-
"status": "success"
|
| 121 |
-
}
|
| 122 |
|
|
|
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
-
raise HTTPException(status_code=
|
|
|
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
|
|
|
| 13 |
|
| 14 |
import io
|
| 15 |
import time
|
| 16 |
+
import httpx
|
| 17 |
import torch
|
| 18 |
from PIL import Image
|
| 19 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 20 |
from fastapi import FastAPI, HTTPException, UploadFile, File
|
| 21 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 22 |
+
from fastapi.responses import JSONResponse
|
| 23 |
+
from pydantic import BaseModel
|
| 24 |
from contextlib import asynccontextmanager
|
| 25 |
+
from typing import Optional
|
| 26 |
|
| 27 |
MODEL_ID = "microsoft/Florence-2-large-ft"
|
| 28 |
|
|
|
|
| 29 |
VQA_QUESTION = (
|
| 30 |
"Is there a woman or any part of a woman's body in this image? Answer yes or no only."
|
| 31 |
)
|
|
|
|
| 50 |
MODEL_DATA.clear()
|
| 51 |
|
| 52 |
app = FastAPI(
|
| 53 |
+
title="AI Shield - Female Detection API",
|
| 54 |
+
description="Florence-2-large-ft | VQA | Compatible with AI Shield Chrome Extension",
|
| 55 |
+
version="5.0.0",
|
| 56 |
lifespan=lifespan
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# โโโ CORS: ุถุฑูุฑู ูุฅุถุงูุฉ Chrome โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 60 |
+
app.add_middleware(
|
| 61 |
+
CORSMiddleware,
|
| 62 |
+
allow_origins=["*"],
|
| 63 |
+
allow_credentials=True,
|
| 64 |
+
allow_methods=["*"],
|
| 65 |
+
allow_headers=["*"],
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# โโโ Schemas โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 69 |
+
class ImageUrlRequest(BaseModel):
|
| 70 |
+
image_url: str # ู
ู ุฅุถุงูุฉ Chrome
|
| 71 |
+
|
| 72 |
+
# โโโ ุฏุงูุฉ ุงูุชุญููู ุงูู
ุดุชุฑูุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 73 |
+
def analyze_image(image: Image.Image) -> dict:
|
| 74 |
+
processor = MODEL_DATA["processor"]
|
| 75 |
+
model = MODEL_DATA["model"]
|
| 76 |
+
|
| 77 |
+
task = "<VQA>"
|
| 78 |
+
prompt = f"{task}{VQA_QUESTION}"
|
| 79 |
+
|
| 80 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 81 |
|
| 82 |
+
start_time = time.time()
|
| 83 |
+
with torch.no_grad():
|
| 84 |
+
generated_ids = model.generate(
|
| 85 |
+
input_ids=inputs["input_ids"],
|
| 86 |
+
pixel_values=inputs["pixel_values"],
|
| 87 |
+
max_new_tokens=10,
|
| 88 |
+
num_beams=3,
|
| 89 |
+
do_sample=False
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 93 |
+
parsed = processor.post_process_generation(
|
| 94 |
+
generated_text,
|
| 95 |
+
task=task,
|
| 96 |
+
image_size=(image.width, image.height)
|
| 97 |
+
)
|
| 98 |
+
elapsed = round(time.time() - start_time, 2)
|
| 99 |
+
answer = parsed.get(task, "").strip()
|
| 100 |
+
|
| 101 |
+
# โโโ ู
ูุทู ุงููุฑุงุฑ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 102 |
+
a = answer.lower()
|
| 103 |
if a == "no" or a.startswith("no"):
|
| 104 |
+
decision, reason = "ALLOW", "model_answered_no"
|
| 105 |
elif "yes" in a:
|
| 106 |
+
decision, reason = "BLOCK", "model_answered_yes"
|
| 107 |
else:
|
| 108 |
+
decision, reason = "BLOCK", "unexpected_answer_blocked_for_safety"
|
| 109 |
|
| 110 |
+
return {
|
| 111 |
+
"decision": decision, # ALLOW | BLOCK (ุจุงูุญุฑูู ุงููุจูุฑุฉ ูุชุชูุงูู ู
ุน ุงูุฅุถุงูุฉ)
|
| 112 |
+
"reason": reason,
|
| 113 |
+
"vqa_answer": answer,
|
| 114 |
+
"execution_time": elapsed,
|
| 115 |
+
"status": "success"
|
| 116 |
+
}
|
| 117 |
|
| 118 |
+
# โโโ Health Check โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 119 |
+
@app.get("/health")
|
| 120 |
+
def health():
|
| 121 |
+
return {"status": "ok", "model_loaded": "model" in MODEL_DATA}
|
| 122 |
|
| 123 |
+
# โโโ Endpoint 1: ู
ู ุฅุถุงูุฉ Chrome (image_url) โโโโโโโโโโโโโโโโโโโโโ
|
| 124 |
+
# background.js ูุฑุณู: POST /analyze {"image_url": "https://..."}
|
| 125 |
+
@app.post("/analyze")
|
| 126 |
+
async def analyze_from_url(request: ImageUrlRequest):
|
| 127 |
try:
|
| 128 |
+
async with httpx.AsyncClient(timeout=30) as client:
|
| 129 |
+
response = await client.get(request.image_url)
|
| 130 |
+
response.raise_for_status()
|
| 131 |
+
image_bytes = response.content
|
| 132 |
except Exception as e:
|
| 133 |
+
raise HTTPException(status_code=400, detail=f"ูุดู ุชุญู
ูู ุงูุตูุฑุฉ ู
ู URL: {str(e)}")
|
| 134 |
|
| 135 |
try:
|
| 136 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 137 |
+
except Exception as e:
|
| 138 |
+
raise HTTPException(status_code=400, detail=f"ุฎุทุฃ ูู ูุฑุงุกุฉ ุงูุตูุฑุฉ: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
return analyze_image(image)
|
|
|
|
| 141 |
|
| 142 |
+
# โโโ Endpoint 2: ุงุฎุชุจุงุฑ ูุฏูู (ุฑูุน ู
ูู) โโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 143 |
+
@app.post("/analyze-file")
|
| 144 |
+
async def analyze_from_file(file: UploadFile = File(...)):
|
| 145 |
+
if not file.content_type.startswith("image/"):
|
| 146 |
+
raise HTTPException(status_code=400, detail="ุงูู
ูู ููุณ ุตูุฑุฉ")
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
try:
|
| 149 |
+
image = Image.open(io.BytesIO(await file.read())).convert("RGB")
|
| 150 |
except Exception as e:
|
| 151 |
+
raise HTTPException(status_code=400, detail=f"ุฎุทุฃ ูู ูุฑุงุกุฉ ุงูุตูุฑุฉ: {str(e)}")
|
| 152 |
+
|
| 153 |
+
return analyze_image(image)
|
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|