• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

scope3data / scope3ai-py / 12753874046

13 Jan 2025 06:40PM UTC coverage: 95.076% (+14.5%) from 80.557%
12753874046

Pull #61

github

3a8d3f
kevdevg
fix: vision pillow read bytes
Pull Request #61: feat(Hugging face): Vision methods - image classification / image segmentation / object detection

179 of 189 new or added lines in 5 files covered. (94.71%)

34 existing lines in 9 files now uncovered.

2008 of 2112 relevant lines covered (95.08%)

3.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.08
/scope3ai/tracers/huggingface/vision/image_segmentation.py
1
import io
4✔
2
import time
4✔
3
from dataclasses import dataclass
4✔
4
from typing import Any, Callable, Optional, Union, List
4✔
5
from PIL import Image
4✔
6

7
from aiohttp import ClientResponse
4✔
8
from huggingface_hub import (
4✔
9
    InferenceClient,
10
    AsyncInferenceClient,
11
    ImageSegmentationOutputElement,
12
)  # type: ignore[import-untyped]
13
from requests import Response
4✔
14

15
from scope3ai.api.types import Scope3AIContext, Model, ImpactRow
4✔
16
from scope3ai.api.typesgen import Task
4✔
17
from scope3ai.constants import PROVIDERS
4✔
18
from scope3ai.lib import Scope3AI
4✔
19
from scope3ai.response_interceptor.aiohttp_interceptor import aiohttp_response_capture
4✔
20
from scope3ai.response_interceptor.requests_interceptor import requests_response_capture
4✔
21

22
PROVIDER = PROVIDERS.HUGGINGFACE_HUB.value
4✔
23
HUGGING_FACE_IMAGE_SEGMENTATION_TASK = "image-segmentation"
4✔
24

25

26
@dataclass
4✔
27
class ImageSegmentationOutput:
4✔
28
    elements: List[ImageSegmentationOutputElement] = None
4✔
29
    scope3ai: Optional[Scope3AIContext] = None
4✔
30

31

32
def _hugging_face_image_segmentation_wrapper(
4✔
33
    timer_start: Any,
34
    model: Any,
35
    response: Any,
36
    http_response: Union[ClientResponse, Response],
37
    args: Any,
38
    kwargs: Any,
39
) -> ImageSegmentationOutput:
40
    input_tokens = 0
4✔
41
    if http_response:
4✔
42
        compute_time = http_response.headers.get("x-compute-time")
4✔
43
    else:
NEW
44
        compute_time = time.perf_counter() - timer_start
×
45
    input_images = None
4✔
46
    try:
4✔
47
        image_param = args[0] if len(args) > 0 else kwargs["image"]
4✔
48
        if type(image_param) is str:
4✔
49
            input_image = Image.open(args[0] if len(args) > 0 else kwargs["image"])
4✔
50
        else:
51
            input_image = Image.open(io.BytesIO(image_param))
4✔
52
        input_width, input_height = input_image.size
4✔
53
        input_images = [
4✔
54
            ("{width}x{height}".format(width=input_width, height=input_height))
55
        ]
NEW
56
    except Exception:
×
NEW
57
        pass
×
58
    scope3_row = ImpactRow(
4✔
59
        model=Model(id=model),
60
        input_tokens=input_tokens,
61
        task=Task.image_segmentation,
62
        request_duration_ms=float(compute_time) * 1000,
63
        managed_service_id=PROVIDER,
64
        input_images=input_images,
65
    )
66
    scope3_ctx = Scope3AI.get_instance().submit_impact(scope3_row)
4✔
67
    result = ImageSegmentationOutput()
4✔
68
    result.elements = response
4✔
69
    result.scope3ai = scope3_ctx
4✔
70
    return result
4✔
71

72

73
def huggingface_image_segmentation_wrapper(
4✔
74
    wrapped: Callable, instance: InferenceClient, args: Any, kwargs: Any
75
) -> ImageSegmentationOutput:
76
    timer_start = time.perf_counter()
4✔
77
    http_response: Response | None = None
4✔
78
    with requests_response_capture() as responses:
4✔
79
        response = wrapped(*args, **kwargs)
4✔
80
        http_responses = responses.get()
4✔
81
        if len(http_responses) > 0:
4✔
82
            http_response = http_responses[-1]
4✔
83
    model = kwargs.get("model") or instance.get_recommended_model(
4✔
84
        HUGGING_FACE_IMAGE_SEGMENTATION_TASK
85
    )
86
    return _hugging_face_image_segmentation_wrapper(
4✔
87
        timer_start, model, response, http_response, args, kwargs
88
    )
89

90

91
async def huggingface_image_segmentation_wrapper_async(
4✔
92
    wrapped: Callable, instance: AsyncInferenceClient, args: Any, kwargs: Any
93
) -> ImageSegmentationOutput:
94
    timer_start = time.perf_counter()
4✔
95
    http_response: ClientResponse | None = None
4✔
96
    with aiohttp_response_capture() as responses:
4✔
97
        response = await wrapped(*args, **kwargs)
4✔
98
        http_responses = responses.get()
4✔
99
        if len(http_responses) > 0:
4✔
100
            http_response = http_responses[-1]
4✔
101
    model = kwargs.get("model") or instance.get_recommended_model(
4✔
102
        HUGGING_FACE_IMAGE_SEGMENTATION_TASK
103
    )
104
    return _hugging_face_image_segmentation_wrapper(
4✔
105
        timer_start, model, response, http_response, args, kwargs
106
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc