Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,158 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import io
|
|
|
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
-
from
|
|
|
|
| 6 |
from fastapi.responses import JSONResponse
|
| 7 |
-
from
|
| 8 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
async def describe_image(file: UploadFile = File(...)):
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
with torch.no_grad():
|
| 60 |
generated_ids = model.generate(
|
| 61 |
input_ids=inputs["input_ids"],
|
| 62 |
pixel_values=inputs["pixel_values"],
|
| 63 |
-
max_new_tokens=
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 69 |
-
|
| 70 |
-
# ู
ุนุงูุฌุฉ ุงููุชุงุฆุฌ
|
| 71 |
-
parsed_answer = processor.post_process_generation(
|
| 72 |
-
generated_text,
|
| 73 |
-
task=task_prompt,
|
| 74 |
-
image_size=(image.width, image.height)
|
| 75 |
)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
content={"error": "Processing failed", "details": str(e)}
|
| 87 |
)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
-
import uvicorn
|
| 95 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import types
|
| 3 |
+
import importlib.util
|
| 4 |
+
|
| 5 |
+
# โโโ Mock flash_attn โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 6 |
+
flash_mock = types.ModuleType("flash_attn")
|
| 7 |
+
flash_mock.__version__ = "2.0.0"
|
| 8 |
+
flash_mock.__spec__ = importlib.util.spec_from_loader("flash_attn", loader=None)
|
| 9 |
+
sys.modules["flash_attn"] = flash_mock
|
| 10 |
+
sys.modules["flash_attn.flash_attn_interface"] = types.ModuleType("flash_attn.flash_attn_interface")
|
| 11 |
+
sys.modules["flash_attn.bert_padding"] = types.ModuleType("flash_attn.bert_padding")
|
| 12 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 13 |
+
|
| 14 |
import io
|
| 15 |
+
import base64
|
| 16 |
+
import time
|
| 17 |
+
import torch
|
| 18 |
from PIL import Image
|
| 19 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 20 |
+
from fastapi import FastAPI, HTTPException
|
| 21 |
from fastapi.responses import JSONResponse
|
| 22 |
+
from pydantic import BaseModel
|
| 23 |
+
from contextlib import asynccontextmanager
|
| 24 |
+
import uvicorn
|
| 25 |
+
|
| 26 |
+
# โโโ ุงูู
ูุงู
ุงูู
ุชุงุญุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 27 |
+
AVAILABLE_TASKS = [
|
| 28 |
+
"<CAPTION>",
|
| 29 |
+
"<DETAILED_CAPTION>",
|
| 30 |
+
"<MORE_DETAILED_CAPTION>",
|
| 31 |
+
"<OD>",
|
| 32 |
+
"<OCR>",
|
| 33 |
+
"<OCR_WITH_REGION>",
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
# โโโ ุชุญู
ูู ุงููู
ูุฐุฌ ุนูุฏ ุจุฏุก ุงูุชุทุจูู โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 37 |
+
MODEL = {}
|
| 38 |
|
| 39 |
+
@asynccontextmanager
|
| 40 |
+
async def lifespan(app: FastAPI):
|
| 41 |
+
print("๐ฅ ุฌุงุฑู ุชุญู
ูู Florence-2-large...")
|
| 42 |
+
start = time.time()
|
| 43 |
|
| 44 |
+
processor = AutoProcessor.from_pretrained(
|
| 45 |
+
"microsoft/Florence-2-large",
|
| 46 |
+
trust_remote_code=True
|
| 47 |
+
)
|
| 48 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 49 |
+
"microsoft/Florence-2-large",
|
| 50 |
+
torch_dtype=torch.float32,
|
| 51 |
+
trust_remote_code=True,
|
| 52 |
+
attn_implementation="eager"
|
| 53 |
+
)
|
| 54 |
+
model.eval()
|
| 55 |
|
| 56 |
+
MODEL["processor"] = processor
|
| 57 |
+
MODEL["model"] = model
|
| 58 |
+
MODEL["device"] = "cpu"
|
| 59 |
+
|
| 60 |
+
print(f"โ
ุงููู
ูุฐุฌ ุฌุงูุฒ ูู {time.time()-start:.1f}s")
|
| 61 |
+
yield
|
| 62 |
+
MODEL.clear()
|
| 63 |
+
|
| 64 |
+
# โโโ FastAPI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 65 |
+
app = FastAPI(
|
| 66 |
+
title="Florence-2-large API",
|
| 67 |
+
description="Microsoft Florence-2-large - Image Analysis API",
|
| 68 |
+
version="1.0.0",
|
| 69 |
+
lifespan=lifespan
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# โโโ Schemas โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 73 |
+
class PredictRequest(BaseModel):
|
| 74 |
+
image: str # base64 encoded image
|
| 75 |
+
task: str = "<CAPTION>"
|
| 76 |
+
text_input: str = None
|
| 77 |
|
| 78 |
+
class PredictResponse(BaseModel):
|
| 79 |
+
result: dict
|
| 80 |
+
task: str
|
| 81 |
+
elapsed_seconds: float
|
| 82 |
|
| 83 |
+
# โโโ Endpoints โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 84 |
+
@app.get("/")
|
| 85 |
+
def root():
|
| 86 |
+
return {
|
| 87 |
+
"status": "running",
|
| 88 |
+
"model": "microsoft/Florence-2-large",
|
| 89 |
+
"device": MODEL.get("device", "loading..."),
|
| 90 |
+
"available_tasks": AVAILABLE_TASKS
|
| 91 |
+
}
|
| 92 |
|
| 93 |
+
@app.get("/health")
|
| 94 |
+
def health():
|
| 95 |
+
return {"status": "ok", "model_loaded": "model" in MODEL}
|
| 96 |
|
| 97 |
+
@app.post("/predict", response_model=PredictResponse)
|
| 98 |
+
def predict(request: PredictRequest):
|
| 99 |
+
# ุงูุชุญูู ู
ู ุงูู
ูู
ุฉ
|
| 100 |
+
if request.task not in AVAILABLE_TASKS:
|
| 101 |
+
raise HTTPException(
|
| 102 |
+
status_code=400,
|
| 103 |
+
detail=f"ู
ูู
ุฉ ุบูุฑ ุตุงูุญุฉ. ุงูู
ูุงู
ุงูู
ุชุงุญุฉ: {AVAILABLE_TASKS}"
|
| 104 |
+
)
|
| 105 |
|
| 106 |
+
# ูู ุชุดููุฑ ุงูุตูุฑุฉ
|
|
|
|
| 107 |
try:
|
| 108 |
+
image_bytes = base64.b64decode(request.image)
|
| 109 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 110 |
+
except Exception as e:
|
| 111 |
+
raise HTTPException(status_code=400, detail=f"ุฎุทุฃ ูู ุงูุตูุฑุฉ: {str(e)}")
|
| 112 |
+
|
| 113 |
+
# ุชุญุถูุฑ ุงูู prompt
|
| 114 |
+
prompt = request.task
|
| 115 |
+
if request.text_input:
|
| 116 |
+
prompt += request.text_input
|
| 117 |
+
|
| 118 |
+
# ุงูุงุณุชุฏูุงู
|
| 119 |
+
try:
|
| 120 |
+
processor = MODEL["processor"]
|
| 121 |
+
model = MODEL["model"]
|
| 122 |
+
|
| 123 |
+
inputs = processor(
|
| 124 |
+
text=prompt,
|
| 125 |
+
images=image,
|
| 126 |
+
return_tensors="pt"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
start = time.time()
|
| 130 |
with torch.no_grad():
|
| 131 |
generated_ids = model.generate(
|
| 132 |
input_ids=inputs["input_ids"],
|
| 133 |
pixel_values=inputs["pixel_values"],
|
| 134 |
+
max_new_tokens=512,
|
| 135 |
+
do_sample=False,
|
| 136 |
+
num_beams=1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
)
|
| 138 |
|
| 139 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 140 |
+
parsed = processor.post_process_generation(
|
| 141 |
+
generated_text,
|
| 142 |
+
task=request.task,
|
| 143 |
+
image_size=(image.width, image.height)
|
| 144 |
+
)
|
| 145 |
+
elapsed = time.time() - start
|
| 146 |
|
| 147 |
+
return PredictResponse(
|
| 148 |
+
result=parsed,
|
| 149 |
+
task=request.task,
|
| 150 |
+
elapsed_seconds=round(elapsed, 2)
|
|
|
|
| 151 |
)
|
| 152 |
|
| 153 |
+
except Exception as e:
|
| 154 |
+
raise HTTPException(status_code=500, detail=f"ุฎุทุฃ ูู ุงูุงุณุชุฏูุงู: {str(e)}")
|
| 155 |
+
|
| 156 |
|
| 157 |
if __name__ == "__main__":
|
|
|
|
| 158 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|