File size: 10,728 Bytes
dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 5f3888e 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 10e7f25 dd81747 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | # โโโ flash_attn Mock โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
import sys
import types
import importlib.util
flash_mock = types.ModuleType("flash_attn")
flash_mock.__version__ = "2.0.0"
flash_mock.__spec__ = importlib.util.spec_from_loader("flash_attn", loader=None)
sys.modules["flash_attn"] = flash_mock
sys.modules["flash_attn.flash_attn_interface"] = types.ModuleType("flash_attn.flash_attn_interface")
sys.modules["flash_attn.bert_padding"] = types.ModuleType("flash_attn.bert_padding")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
import io
import time
import httpx
import torch
from PIL import Image
from transformers import (
BlipProcessor, BlipForQuestionAnswering,
AutoProcessor, AutoModelForCausalLM
)
from fastapi import FastAPI, HTTPException, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from contextlib import asynccontextmanager
# โโโ ุงููู
ุงุฐุฌ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
BLIP_MODEL_ID = "Salesforce/blip-vqa-base"
FLORENCE_MODEL_ID = "microsoft/Florence-2-large-ft"
# โโโ ุฃุณุฆูุฉ BLIP โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
QUESTIONS = [
"is there a person in this image?",
"is there a woman in this image?",
"is there a human body part in this image?",
"is there a hand or arm visible?",
"is there a face visible?",
"is there a leg or foot visible?",
"is there a belly or stomach visible?",
]
# โโโ ุณุคุงู Florence โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
FLORENCE_QUESTION = (
"Is there a woman or any part of a woman's body in this image? "
"Answer yes or no only."
)
MODEL_DATA = {}
@asynccontextmanager
async def lifespan(app: FastAPI):
# โโ ุชุญู
ูู BLIP โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print(f"๐ฅ Loading {BLIP_MODEL_ID}...")
start = time.time()
MODEL_DATA["blip_processor"] = BlipProcessor.from_pretrained(BLIP_MODEL_ID)
MODEL_DATA["blip_model"] = BlipForQuestionAnswering.from_pretrained(
BLIP_MODEL_ID, torch_dtype=torch.float32
).eval()
print(f"โ
BLIP ready in {time.time()-start:.1f}s")
# โโ ุชุญู
ูู Florence-2 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
print(f"๐ฅ Loading {FLORENCE_MODEL_ID}...")
start = time.time()
MODEL_DATA["florence_processor"] = AutoProcessor.from_pretrained(
FLORENCE_MODEL_ID, trust_remote_code=True
)
MODEL_DATA["florence_model"] = AutoModelForCausalLM.from_pretrained(
FLORENCE_MODEL_ID,
torch_dtype=torch.float32,
trust_remote_code=True,
attn_implementation="eager"
).eval()
print(f"โ
Florence-2 ready in {time.time()-start:.1f}s")
yield
MODEL_DATA.clear()
app = FastAPI(
title="AI Shield - Dual Model Detection",
description="BLIP + Florence-2-large-ft | Compatible with AI Shield Chrome Extension",
version="6.0.0",
lifespan=lifespan
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# โโโ Schema โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class ImageUrlRequest(BaseModel):
image_url: str
# โโโ ุฏุงูุฉ BLIP โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def run_blip(image: Image.Image) -> dict:
processor = MODEL_DATA["blip_processor"]
model = MODEL_DATA["blip_model"]
yes_answers = {}
no_answers = {}
for question in QUESTIONS:
inputs = processor(image, question, return_tensors="pt")
with torch.no_grad():
out = model.generate(**inputs, max_new_tokens=5)
answer = processor.decode(out[0], skip_special_tokens=True).strip().lower()
if answer == "yes" or answer.startswith("yes"):
yes_answers[question] = answer
else:
no_answers[question] = answer
return {"yes": yes_answers, "no": no_answers}
# โโโ ุฏุงูุฉ Florence-2 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def run_florence(image: Image.Image) -> dict:
processor = MODEL_DATA["florence_processor"]
model = MODEL_DATA["florence_model"]
task = "<VQA>"
prompt = f"{task}{FLORENCE_QUESTION}"
inputs = processor(text=prompt, images=image, return_tensors="pt")
start = time.time()
with torch.no_grad():
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=10,
do_sample=False
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
parsed = processor.post_process_generation(
generated_text, task=task,
image_size=(image.width, image.height)
)
answer = parsed.get(task, "").strip().lower()
elapsed = round(time.time() - start, 2)
if answer == "no" or answer.startswith("no"):
return {"decision": "ALLOW", "answer": answer, "elapsed": elapsed}
else:
return {"decision": "BLOCK", "answer": answer, "elapsed": elapsed}
# โโโ ู
ูุทู ุงููุฑุงุฑ ุงูุฑุฆูุณู โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def process_image(image: Image.Image) -> dict:
total_start = time.time()
# โโ ุงูู
ุฑุญูุฉ 1: BLIP โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
blip_start = time.time()
blip_result = run_blip(image)
blip_elapsed = round(time.time() - blip_start, 2)
yes_q = blip_result["yes"]
no_q = blip_result["no"]
# โโโ ุงูุญุงูุฉ 1: BLIP ุงูุชุดู ุงู
ุฑุฃุฉ ู
ุจุงุดุฑุฉ โ BLOCK ููุฑุงู โโโโโโโโโ
WOMAN_QUESTIONS = [
"is there a woman in this image?",
]
woman_detected = any(q in yes_q for q in WOMAN_QUESTIONS)
if woman_detected:
return {
"decision": "BLOCK",
"reason": "blip_detected_woman_directly",
"stage": "blip_only",
"blip_yes": yes_q,
"blip_no": no_q,
"blip_time": blip_elapsed,
"florence_used": False,
"total_time": round(time.time() - total_start, 2),
"status": "success"
}
# โโโ ุงูุญุงูุฉ 2: BLIP ูู
ููุชุดู ุฃู ุฅูุณุงู โ ALLOW ููุฑุงู โโโโโโโโโโ
if not yes_q:
return {
"decision": "ALLOW",
"reason": "blip_no_human_detected",
"stage": "blip_only",
"blip_yes": yes_q,
"blip_no": no_q,
"blip_time": blip_elapsed,
"florence_used": False,
"total_time": round(time.time() - total_start, 2),
"status": "success"
}
# โโโ ุงูุญุงูุฉ 3: BLIP ุงูุชุดู ุฅูุณุงู ููู ููุณ ุงู
ุฑุฃุฉ โ Florence โโโโโ
florence_result = run_florence(image)
final_decision = florence_result["decision"]
reason = "florence_confirmed_woman" if final_decision == "BLOCK" \
else "florence_confirmed_no_woman"
return {
"decision": final_decision,
"reason": reason,
"stage": "blip_then_florence",
"blip_yes": yes_q,
"blip_no": no_q,
"blip_time": blip_elapsed,
"florence_answer": florence_result["answer"],
"florence_time": florence_result["elapsed"],
"florence_used": True,
"total_time": round(time.time() - total_start, 2),
"status": "success"
}
# โโโ Health โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@app.get("/health")
def health():
return {
"status": "ok",
"blip_loaded": "blip_model" in MODEL_DATA,
"florence_loaded": "florence_model" in MODEL_DATA
}
# โโโ Endpoint 1: ู
ู ุฅุถุงูุฉ Chrome โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@app.post("/analyze")
async def analyze_from_url(request: ImageUrlRequest):
try:
async with httpx.AsyncClient(timeout=30) as client:
response = await client.get(request.image_url)
response.raise_for_status()
image_bytes = response.content
except Exception as e:
raise HTTPException(status_code=400, detail=f"ูุดู ุชุญู
ูู ุงูุตูุฑุฉ: {str(e)}")
try:
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
except Exception as e:
raise HTTPException(status_code=400, detail=f"ุฎุทุฃ ูู ูุฑุงุกุฉ ุงูุตูุฑุฉ: {str(e)}")
return process_image(image)
# โโโ Endpoint 2: ุงุฎุชุจุงุฑ ูุฏูู โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@app.post("/analyze-file")
async def analyze_from_file(file: UploadFile = File(...)):
if not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="ุงูู
ูู ููุณ ุตูุฑุฉ")
try:
image = Image.open(io.BytesIO(await file.read())).convert("RGB")
except Exception as e:
raise HTTPException(status_code=400, detail=f"ุฎุทุฃ ูู ูุฑุงุกุฉ ุงูุตูุฑุฉ: {str(e)}")
return process_image(image)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) |