Spaces:
Sleeping
Sleeping
File size: 8,541 Bytes
95f4d99 cc844d3 95f4d99 d6dda21 95f4d99 cc844d3 95f4d99 cc844d3 95f4d99 5a6c7d5 95f4d99 cc844d3 95f4d99 cc844d3 95f4d99 cc844d3 95f4d99 cc844d3 bc0c9d8 95f4d99 cc844d3 0c504eb 95f4d99 0c504eb 95f4d99 b94a4c6 95f4d99 d6dda21 95f4d99 d6dda21 95f4d99 cc844d3 95f4d99 |
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 |
# # main.py
# from fastapi import FastAPI, File, UploadFile
# from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration
# from transformers.image_utils import load_image
# import torch
# from io import BytesIO
# import os
# from dotenv import load_dotenv
# from PIL import Image
# from huggingface_hub import login
# # Load environment variables
# load_dotenv()
# # Set the cache directory to a writable path
# os.environ["TORCHINDUCTOR_CACHE_DIR"] = "/tmp/torch_inductor_cache"
# token = os.getenv("huggingface_ankit")
# # Login to the Hugging Face Hub
# login(token)
# app = FastAPI()
# model_id = "google/paligemma2-3b-mix-448"
# model = PaliGemmaForConditionalGeneration.from_pretrained(model_id).to('cuda')
# processor = PaliGemmaProcessor.from_pretrained(model_id)
# def predict(image):
# prompt = "<image> ocr"
# model_inputs = processor(text=prompt, images=image, return_tensors="pt").to('cuda')
# input_len = model_inputs["input_ids"].shape[-1]
# with torch.inference_mode():
# generation = model.generate(**model_inputs, max_new_tokens=200)
# torch.cuda.empty_cache()
# decoded = processor.decode(generation[0], skip_special_tokens=True) #[len(prompt):].lstrip("\n")
# return decoded
# @app.post("/extract_text")
# async def extract_text(file: UploadFile = File(...)):
# image = Image.open(BytesIO(await file.read())).convert("RGB") # Ensure it's a valid PIL image
# text = predict(image)
# return {"extracted_text": text}
# @app.post("/batch_extract_text")
# async def batch_extract_text(files: list[UploadFile] = File(...)):
# # if len(files) > 20:
# # return {"error": "A maximum of 20 images can be processed at a time."}
# images = [Image.open(BytesIO(await file.read())).convert("RGB") for file in files]
# prompts = ["OCR"] * len(images)
# model_inputs = processor(text=prompts, images=images, return_tensors="pt").to(torch.bfloat16).to(model.device)
# input_len = model_inputs["input_ids"].shape[-1]
# with torch.inference_mode():
# generations = model.generate(**model_inputs, max_new_tokens=200, do_sample=False)
# torch.cuda.empty_cache()
# extracted_texts = [processor.decode(generations[i], skip_special_tokens=True) for i in range(len(images))]
# return {"extracted_texts": extracted_texts}
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run(app, host="0.0.0.0", port=7860)
from fastapi import FastAPI, File, UploadFile, BackgroundTasks
from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration
import torch
from io import BytesIO
import os
from dotenv import load_dotenv
from PIL import Image
from huggingface_hub import login
import gc
import logging
from typing import List
import time
import numpy as np
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Load environment variables
load_dotenv()
# Set the cache directory to a writable path
os.environ["TORCHINDUCTOR_CACHE_DIR"] = "/tmp/torch_inductor_cache"
token = os.getenv("huggingface_ankit")
# Login to the Hugging Face Hub
login(token)
app = FastAPI()
# Global variables for model and processor
model = None
processor = None
def load_model():
"""Load model and processor when needed"""
global model, processor
if model is None:
model_id = "google/paligemma2-3b-mix-448"
logger.info(f"Loading model {model_id}")
# Load model with memory-efficient settings
model = PaliGemmaForConditionalGeneration.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.bfloat16 # Use lower precision for memory efficiency
)
processor = PaliGemmaProcessor.from_pretrained(model_id)
logger.info("Model loaded successfully")
def clean_memory():
"""Force garbage collection and clear CUDA cache"""
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
# Clear GPU cache
torch.cuda.empty_cache()
logger.info(f"Memory allocated after clearing cache: {torch.cuda.memory_allocated()} bytes")
logger.info("Memory cleaned")
def predict(image):
"""Process a single image"""
load_model() # Ensure model is loaded
# Process input
prompt = "<image> ocr"
model_inputs = processor(text=prompt, images=image, return_tensors="pt")
# Move to appropriate device
model_inputs = {k: v.to(model.device) for k, v in model_inputs.items()}
# Generate with memory optimization
with torch.inference_mode():
generation = model.generate(**model_inputs, max_new_tokens=200)
# Decode output
decoded = processor.decode(generation[0], skip_special_tokens=True)
# Clean up intermediates
del model_inputs, generation
clean_memory()
return decoded
@app.post("/extract_text")
async def extract_text(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
"""Extract text from a single image"""
try:
start_time = time.time()
image = Image.open(BytesIO(await file.read())).convert("RGB")
text = predict(image)
# Schedule cleanup after response
background_tasks.add_task(clean_memory)
logger.info(f"Processing completed in {time.time() - start_time:.2f} seconds")
return {"extracted_text": text}
except Exception as e:
logger.error(f"Error processing image: {str(e)}")
return {"error": str(e)}
@app.post("/batch_extract_text")
async def batch_extract_text(batch_size:int, background_tasks: BackgroundTasks, files: List[UploadFile] = File(...)):
"""Extract text from multiple images with batching"""
try:
start_time = time.time()
# Limit batch size for memory management
max_batch_size = batch_size # Adjust based on your GPU memory
# if len(files) > 32:
# return {"error": "A maximum of 20 images can be processed at a time."}
load_model() # Ensure model is loaded
all_results = []
# Process in smaller batches
for i in range(0, len(files), max_batch_size):
batch_files = files[i:i+max_batch_size]
# Load images
images = []
for file in batch_files:
image_data = await file.read()
img = Image.open(BytesIO(image_data)).convert("RGB")
images.append(img)
# Create batch inputs
prompts = ["<image> ocr"] * len(images)
model_inputs = processor(text=prompts, images=images, return_tensors="pt")
# Move to appropriate device
model_inputs = {k: v.to(model.device) for k, v in model_inputs.items()}
# Generate with memory optimization
with torch.inference_mode():
generations = model.generate(**model_inputs, max_new_tokens=200, do_sample=False)
# Decode outputs
batch_results = [processor.decode(generations[i], skip_special_tokens=True) for i in range(len(images))]
all_results.extend(batch_results)
# Clean up batch resources
del model_inputs, generations, images
clean_memory()
# Schedule cleanup after response
background_tasks.add_task(clean_memory)
logger.info(f"Batch processing completed in {time.time() - start_time:.2f} seconds")
return {"extracted_texts": all_results}
except Exception as e:
logger.error(f"Error in batch processing: {str(e)}")
return {"error": str(e)}
# Health check endpoint
@app.get("/health")
async def health_check():
# Generate a random image (20x40 pixels) with random RGB values
random_data = np.random.randint(0, 256, (20, 40, 3), dtype=np.uint8)
# Create an image from the random data
image = Image.fromarray(random_data)
predict(image)
clean_memory()
return {"status": "healthy"}
# if __name__ == "__main__":
# import uvicorn
# # Start the server with proper worker configuration
# uvicorn.run(
# app,
# host="0.0.0.0",
# port=7860,
# log_level="info",
# workers=1 # Multiple workers can cause GPU memory issues
# ) |