Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,6 +4,7 @@ import base64
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
import logging
|
|
|
|
| 7 |
from PIL import Image, UnidentifiedImageError
|
| 8 |
import torch
|
| 9 |
import asyncio
|
|
@@ -35,7 +36,7 @@ try:
|
|
| 35 |
"weights/icon_caption_florence",
|
| 36 |
torch_dtype=torch.float16,
|
| 37 |
trust_remote_code=True,
|
| 38 |
-
).to(
|
| 39 |
except Exception as e:
|
| 40 |
logger.warning(f"Failed to load caption model on GPU: {e}. Falling back to CPU.")
|
| 41 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -50,7 +51,7 @@ logger.info("Finished loading models!")
|
|
| 50 |
# Initialize FastAPI app
|
| 51 |
app = FastAPI()
|
| 52 |
|
| 53 |
-
MAX_QUEUE_SIZE =
|
| 54 |
request_queue = asyncio.Queue(maxsize=MAX_QUEUE_SIZE)
|
| 55 |
|
| 56 |
# Define response model
|
|
@@ -79,7 +80,7 @@ async def startup_event():
|
|
| 79 |
asyncio.create_task(worker())
|
| 80 |
|
| 81 |
|
| 82 |
-
# Image processing function
|
| 83 |
async def process(image_input: Image.Image, box_threshold: float, iou_threshold: float) -> ProcessResponse:
|
| 84 |
try:
|
| 85 |
# Define save path
|
|
@@ -132,11 +133,18 @@ async def process(image_input: Image.Image, box_threshold: float, iou_threshold:
|
|
| 132 |
# Join parsed content list
|
| 133 |
parsed_content_list_str = "\n".join([str(item) for item in parsed_content_list])
|
| 134 |
|
| 135 |
-
|
| 136 |
image=img_str,
|
| 137 |
parsed_content_list=parsed_content_list_str,
|
| 138 |
label_coordinates=str(label_coordinates),
|
| 139 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
logger.error(f"Error in process function: {e}")
|
| 142 |
raise HTTPException(status_code=500, detail=f"Failed to process the image: {e}")
|
|
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
import logging
|
| 7 |
+
import gc # Import garbage collector
|
| 8 |
from PIL import Image, UnidentifiedImageError
|
| 9 |
import torch
|
| 10 |
import asyncio
|
|
|
|
| 36 |
"weights/icon_caption_florence",
|
| 37 |
torch_dtype=torch.float16,
|
| 38 |
trust_remote_code=True,
|
| 39 |
+
).to(device)
|
| 40 |
except Exception as e:
|
| 41 |
logger.warning(f"Failed to load caption model on GPU: {e}. Falling back to CPU.")
|
| 42 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 51 |
# Initialize FastAPI app
|
| 52 |
app = FastAPI()
|
| 53 |
|
| 54 |
+
MAX_QUEUE_SIZE = 10 # Set a reasonable limit based on your system capacity
|
| 55 |
request_queue = asyncio.Queue(maxsize=MAX_QUEUE_SIZE)
|
| 56 |
|
| 57 |
# Define response model
|
|
|
|
| 80 |
asyncio.create_task(worker())
|
| 81 |
|
| 82 |
|
| 83 |
+
# Image processing function with memory cleanup
|
| 84 |
async def process(image_input: Image.Image, box_threshold: float, iou_threshold: float) -> ProcessResponse:
|
| 85 |
try:
|
| 86 |
# Define save path
|
|
|
|
| 133 |
# Join parsed content list
|
| 134 |
parsed_content_list_str = "\n".join([str(item) for item in parsed_content_list])
|
| 135 |
|
| 136 |
+
response = ProcessResponse(
|
| 137 |
image=img_str,
|
| 138 |
parsed_content_list=parsed_content_list_str,
|
| 139 |
label_coordinates=str(label_coordinates),
|
| 140 |
)
|
| 141 |
+
|
| 142 |
+
# **Memory Cleanup**
|
| 143 |
+
del image_input, text, ocr_bbox, dino_labled_img, label_coordinates, parsed_content_list
|
| 144 |
+
torch.cuda.empty_cache() # Free GPU memory
|
| 145 |
+
gc.collect() # Free CPU memory
|
| 146 |
+
|
| 147 |
+
return response
|
| 148 |
except Exception as e:
|
| 149 |
logger.error(f"Error in process function: {e}")
|
| 150 |
raise HTTPException(status_code=500, detail=f"Failed to process the image: {e}")
|