|
import os |
|
import torch |
|
from flask import Flask |
|
|
|
FEATURE_WEIGHTS = {"shape": 0.4, "color": 0.5, "texture": 0.1} |
|
FINAL_SCORE_THRESHOLD = 0.5 |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
print("=" * 50) |
|
print("π Initializing application and loading models...") |
|
|
|
device_name = os.environ.get("device", "cpu") |
|
device = torch.device( |
|
'cuda' if 'cuda' in device_name and torch.cuda.is_available() else 'cpu' |
|
) |
|
print(f"π§ Using device: {device}") |
|
|
|
from transformers import ( |
|
AutoProcessor, |
|
AutoModelForZeroShotObjectDetection, |
|
AutoTokenizer, |
|
AutoModel |
|
) |
|
from segment_anything import SamPredictor, sam_model_registry |
|
|
|
print("...Loading Grounding DINO model...") |
|
gnd_model_id = "IDEA-Research/grounding-dino-tiny" |
|
processor_gnd = AutoProcessor.from_pretrained(gnd_model_id) |
|
model_gnd = AutoModelForZeroShotObjectDetection.from_pretrained(gnd_model_id).to(device) |
|
|
|
print("...Loading Segment Anything (SAM) model...") |
|
|
|
sam_checkpoint = "sam_vit_b_01ec64.pth" |
|
sam_model = sam_model_registry["vit_b"](checkpoint=sam_checkpoint).to(device) |
|
predictor = SamPredictor(sam_model) |
|
|
|
print("...Loading BGE model for text embeddings...") |
|
bge_model_id = "BAAI/bge-small-en-v1.5" |
|
tokenizer_text = AutoTokenizer.from_pretrained(bge_model_id) |
|
model_text = AutoModel.from_pretrained(bge_model_id).to(device) |
|
|
|
|
|
models = { |
|
"processor_gnd": processor_gnd, |
|
"model_gnd": model_gnd, |
|
"predictor": predictor, |
|
"tokenizer_text": tokenizer_text, |
|
"model_text": model_text, |
|
"device": device |
|
} |
|
|
|
print("β
All models loaded successfully.") |
|
print("=" * 50) |
|
|
|
|
|
from pipeline import routes |
|
|