File size: 15,227 Bytes
5c263d5 8f80642 5c263d5 7bac21a 8f80642 5c263d5 15a969b 5c263d5 fef773e 5c263d5 afab4d9 fef773e 5c263d5 fef773e 5c263d5 fef773e 5c263d5 afab4d9 7bac21a fef773e afab4d9 fef773e afab4d9 7bac21a fef773e 7bac21a afab4d9 7bac21a fef773e 7bac21a 5c263d5 afab4d9 7bac21a 5c263d5 fef773e 7bac21a fef773e 7bac21a afab4d9 7bac21a fef773e 7bac21a fef773e afab4d9 7bac21a fef773e 7bac21a afab4d9 7bac21a afab4d9 7bac21a fef773e 7bac21a fef773e afab4d9 fef773e afab4d9 7bac21a 8f80642 15a969b 8f80642 15a969b 8f80642 15a969b 8f80642 15a969b |
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# -------------------------------------------------------------------
# This source file is available under the terms of the
# Pimcore Open Core License (POCL)
# Full copyright and license information is available in
# LICENSE.md which is distributed with this source code.
#
# @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
# @license Pimcore Open Core License (POCL)
# -------------------------------------------------------------------
import torch
from fastapi import FastAPI, Path, Request, File, UploadFile
import logging
import sys
from .translation_task import TranslationTaskService
from .classification import ClassificationTaskService
from .text_to_image import TextToImageTaskService
from .embeddings import ImageEmbeddingTaskService, TextEmbeddingTaskService
app = FastAPI(
title="Pimcore Local Inference Service",
description="This services allows HF inference provider compatible inference to models which are not available at HF inference providers.",
version="1.0.0"
)
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create singleton instances of embedding services to enable model caching across requests
image_embedding_service = ImageEmbeddingTaskService(logger)
text_embedding_service = TextEmbeddingTaskService(logger)
class StreamToLogger(object):
def __init__(self, logger, log_level):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
def write(self, buf):
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())
def flush(self):
pass
sys.stdout = StreamToLogger(logger, logging.INFO)
sys.stderr = StreamToLogger(logger, logging.ERROR)
@app.get("/gpu_check")
async def gpu_check():
""" Check if a GPU is available """
gpu = 'GPU not available'
if torch.cuda.is_available():
gpu = 'GPU is available'
print("GPU is available")
else:
print("GPU is not available")
return {'success': True, 'gpu': gpu}
# =========================
# Translation Task
# =========================
@app.post(
"/translation/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"application/json": {
"example": {
"inputs": "Hello, world! foo bar",
"parameters": {"repetition_penalty": 1.6}
}
}
}
}
}
)
async def translate(
request: Request,
model_name: str = Path(
...,
description="The name of the translation model (e.g. Helsinki-NLP/opus-mt-en-de)",
example="Helsinki-NLP/opus-mt-en-de"
)
):
"""
Execute translation tasks.
Returns:
list: The translation result(s) as returned by the pipeline.
"""
model_name = model_name.rstrip("/")
translationTaskService = TranslationTaskService(logger)
return await translationTaskService.translate(request, model_name)
# =========================
# Zero-Shot Image Classification Task
# =========================
@app.post(
"/zero-shot-image-classification/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"application/json": {
"example": {
"inputs": "base64_encoded_image_string",
"parameters": {"candidate_labels": "green, yellow, blue, white, silver"}
}
}
}
}
}
)
async def zero_shot_image_classification(
request: Request,
model_name: str = Path(
...,
description="The name of the zero-shot classification model (e.g., openai/clip-vit-large-patch14-336)",
example="openai/clip-vit-large-patch14-336"
)
):
"""
Execute zero-shot image classification tasks.
Returns:
list: The classification result(s) as returned by the pipeline.
"""
model_name = model_name.rstrip("/")
zeroShotTask = ClassificationTaskService(logger, 'zero-shot-image-classification')
return await zeroShotTask.classify(request, model_name)
# =========================
# Image Classification Task
# =========================
@app.post(
"/image-classification/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"application/json": {
"example": {
"inputs": "base64_encoded_image_string"
}
}
}
}
}
)
async def image_classification(
request: Request,
model_name: str = Path(
...,
description="The name of the image classification model (e.g., pimcore/car-countries-classification)",
example="pimcore/car-countries-classification"
)
):
"""
Execute image classification tasks.
Returns:
list: The classification result(s) as returned by the pipeline.
"""
model_name = model_name.rstrip("/")
imageTask = ClassificationTaskService(logger, 'image-classification')
return await imageTask.classify(request, model_name)
# =========================
# Zero-Shot Text Classification Task
# =========================
@app.post(
"/zero-shot-text-classification/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"application/json": {
"example": {
"inputs": "text to classify",
"parameters": {"candidate_labels": "green, yellow, blue, white, silver"}
}
}
}
}
}
)
async def zero_shot_text_classification(
request: Request,
model_name: str = Path(
...,
description="The name of the zero-shot text classification model (e.g., facebook/bart-large-mnli)",
example="facebook/bart-large-mnli"
)
):
"""
Execute zero-shot text classification tasks.
Returns:
list: The classification result(s) as returned by the pipeline.
"""
model_name = model_name.rstrip("/")
zeroShotTask = ClassificationTaskService(logger, 'zero-shot-classification')
return await zeroShotTask.classify(request, model_name)
# =========================
# Text Classification Task
# =========================
@app.post(
"/text-classification/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"application/json": {
"example": {
"inputs": "text to classify"
}
}
}
}
}
)
async def text_classification(
request: Request,
model_name: str = Path(
...,
description="The name of the text classification model (e.g., pimcore/car-class-classification)",
example="pimcore/car-class-classification"
)
):
"""
Execute text classification tasks.
Returns:
list: The classification result(s) as returned by the pipeline.
"""
model_name = model_name.rstrip("/")
textTask = ClassificationTaskService(logger, 'text-classification')
return await textTask.classify(request, model_name)
# =========================
# Image to Text Task
# =========================
@app.post(
"/image-to-text/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary",
"description": "Image file to upload"
}
},
"required": ["image"]
}
}
}
}
}
)
async def image_to_text(
request: Request,
model_name: str = Path(
...,
description="The name of the image-to-text (e.g., Salesforce/blip-image-captioning-base)",
example="Salesforce/blip-image-captioning-base"
)
):
"""
Execute image-to-text tasks.
Returns:
list: The generated text as returned by the pipeline.
"""
model_name = model_name.rstrip("/")
imageToTextTask = TextToImageTaskService(logger)
return await imageToTextTask.extract(request, model_name)
# =========================
# Image Embedding Task
# =========================
@app.post(
"/image-embedding/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"application/json": {
"example": {
"inputs": "base64_encoded_image_string"
}
}
}
}
}
)
async def image_embedding(
request: Request,
model_name: str = Path(
...,
description="The name of the image embedding model. Supported models include: google/siglip-so400m-patch14-384, openai/clip-vit-large-patch14, openai/clip-vit-base-patch16, laion/CLIP-ViT-bigG-14-laion2B-39B-b160k, Salesforce/blip-itm-large-flickr",
example="google/siglip-so400m-patch14-384"
)
):
"""
Generate embedding vectors for image data.
The service supports multiple model types including SigLIP, CLIP, and BLIP models.
Returns a dense vector representation of the input image.
Returns:
list: The embedding vector as a list of float values.
"""
model_name = model_name.rstrip("/")
return await image_embedding_service.generate_embedding(request, model_name)
# =========================
# Image Embedding Upload Task (Development/Testing)
# =========================
@app.post(
"/image-embedding-upload/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"image": {
"type": "string",
"format": "binary",
"description": "Image file to upload for embedding generation"
}
},
"required": ["image"]
}
}
}
},
"responses": {
"200": {
"description": "Image embedding vector",
"content": {
"application/json": {
"example": {
"embeddings": [0.1, -0.2, 0.3, "..."]
}
}
}
}
}
}
)
async def image_embedding_upload(
image: UploadFile = File(..., description="Image file to generate embeddings for"),
model_name: str = Path(
...,
description="The name of the image embedding model. Supported models include: google/siglip-so400m-patch14-384, openai/clip-vit-large-patch14, openai/clip-vit-base-patch16, laion/CLIP-ViT-bigG-14-laion2B-39B-b160k, Salesforce/blip-itm-large-flickr",
example="google/siglip-so400m-patch14-384"
)
):
"""
Generate embedding vectors for uploaded image data (Development/Testing endpoint).
This endpoint allows you to upload an image file directly through the Swagger UI
for development and testing purposes. The image is processed and converted to
embedding vectors using the specified model.
Supported formats: JPEG, PNG, GIF, BMP, TIFF
The service supports multiple model types including SigLIP, CLIP, and BLIP models.
Returns a dense vector representation of the uploaded image.
Returns:
dict: The embedding vector as a list of float values.
"""
model_name = model_name.rstrip("/")
return await image_embedding_service.generate_embedding_from_upload(image, model_name)
# =========================
# Text Embedding Task
# =========================
@app.post(
"/text-embedding/{model_name:path}",
openapi_extra={
"requestBody": {
"content": {
"application/json": {
"example": {
"inputs": "text to embed"
}
}
}
}
}
)
async def text_embedding(
request: Request,
model_name: str = Path(
...,
description="The name of the text embedding model. Supported models include: google/siglip-so400m-patch14-384, openai/clip-vit-large-patch14, openai/clip-vit-base-patch16, laion/CLIP-ViT-bigG-14-laion2B-39B-b160k, Salesforce/blip-itm-large-flickr",
example="google/siglip-so400m-patch14-384"
)
):
"""
Generate embedding vectors for text data.
The service supports multiple model types including SigLIP, CLIP, and BLIP models.
Returns a dense vector representation of the input text.
Returns:
list: The embedding vector as a list of float values.
"""
model_name = model_name.rstrip("/")
return await text_embedding_service.generate_embedding(request, model_name)
# =========================
# Embedding Vector Size
# =========================
@app.get(
"/embedding-vector-size/{model_name:path}",
openapi_extra={
"responses": {
"200": {
"description": "Vector size information",
"content": {
"application/json": {
"example": {
"model_name": "google/siglip-so400m-patch14-384",
"vector_size": 1152,
"config_attribute_used": "hidden_size"
}
}
}
}
}
}
)
async def embedding_vector_size(
model_name: str = Path(
...,
description="The name of the embedding model. Supported models include: google/siglip-so400m-patch14-384, openai/clip-vit-large-patch14, openai/clip-vit-base-patch16, laion/CLIP-ViT-bigG-14-laion2B-39B-b160k, Salesforce/blip-itm-large-flickr",
example="google/siglip-so400m-patch14-384"
)
):
"""
Get the vector size of embeddings for a given model.
This endpoint returns the dimensionality of the embedding vectors that the model produces.
Useful for understanding the output format before generating embeddings.
Returns:
dict: Information about the vector size including model name, vector size, and configuration attribute used.
"""
model_name = model_name.rstrip("/")
# We can use either embedding service as they inherit from the same base class
return await image_embedding_service.get_embedding_vector_size(model_name)
|