• 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.0
/scope3ai/tracers/huggingface/vision/object_detection.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

6
from PIL import Image
4✔
7
from aiohttp import ClientResponse
4✔
8
from huggingface_hub import (
4✔
9
    InferenceClient,
10
    AsyncInferenceClient,
11
    ObjectDetectionOutputElement,
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_OBJECT_DETECTION_TASK = "object-detection"
4✔
24

25

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

31

32
def _hugging_face_object_detection_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
) -> ObjectDetectionOutput:
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
    try:
4✔
46
        image_param = args[0] if len(args) > 0 else kwargs["image"]
4✔
47
        if type(image_param) is str:
4✔
48
            input_image = Image.open(args[0] if len(args) > 0 else kwargs["image"])
4✔
49
        else:
50
            input_image = Image.open(io.BytesIO(image_param))
4✔
51
        input_width, input_height = input_image.size
4✔
52
        input_images = [
4✔
53
            ("{width}x{height}".format(width=input_width, height=input_height))
54
        ]
NEW
55
    except Exception:
×
NEW
56
        pass
×
57
    scope3_row = ImpactRow(
4✔
58
        model=Model(id=model),
59
        input_tokens=input_tokens,
60
        task=Task.object_detection,
61
        request_duration_ms=float(compute_time) * 1000,
62
        managed_service_id=PROVIDER,
63
        input_images=input_images,
64
    )
65

66
    scope3_ctx = Scope3AI.get_instance().submit_impact(scope3_row)
4✔
67
    result = ObjectDetectionOutput()
4✔
68
    result.elements = response
4✔
69
    result.scope3ai = scope3_ctx
4✔
70
    return result
4✔
71

72

73
def huggingface_object_detection_wrapper(
4✔
74
    wrapped: Callable, instance: InferenceClient, args: Any, kwargs: Any
75
) -> ObjectDetectionOutput:
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_OBJECT_DETECTION_TASK
85
    )
86
    return _hugging_face_object_detection_wrapper(
4✔
87
        timer_start, model, response, http_response, args, kwargs
88
    )
89

90

91
async def huggingface_object_detection_wrapper_async(
4✔
92
    wrapped: Callable, instance: AsyncInferenceClient, args: Any, kwargs: Any
93
) -> ObjectDetectionOutput:
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_OBJECT_DETECTION_TASK
103
    )
104
    return _hugging_face_object_detection_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