from fastapi import FastAPI, UploadFile, File from fastapi.responses import HTMLResponse from transformers import AutoProcessor, Florence2ForConditionalGeneration # <--- DIRECT IMPORT from PIL import Image import torch import io app = FastAPI() print("⏳ Initializing Florence-2 (Hardcoded Class Mode)...") # We use the community fork for clean config model_id = "florence-community/Florence-2-large" device = "cpu" try: # 1. Load Processor processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=False) # 2. Load Model using the SPECIFIC CLASS (No "AutoModel" guessing) model = Florence2ForConditionalGeneration.from_pretrained( model_id, trust_remote_code=False, torch_dtype=torch.float32 ).to(device) print("✅ Model Loaded Successfully!") except Exception as e: print(f"❌ Load Error: {e}") model = None processor = None # --- UI --- html_content = """
Advanced OCR & Image Understanding (CPU)