• 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

94.74
/scope3ai/tracers/huggingface/speech_to_text.py
1
import time
4✔
2
from dataclasses import dataclass, asdict
4✔
3
from typing import Any, Callable, Optional, Union
4✔
4

5
from aiohttp import ClientResponse
4✔
6
from huggingface_hub import (
4✔
7
    AutomaticSpeechRecognitionOutput as _AutomaticSpeechRecognitionOutput,
8
)
9
from huggingface_hub import InferenceClient  # type: ignore[import-untyped]
4✔
10
from requests import Response
4✔
11

12
from scope3ai.api.types import Scope3AIContext, Model, ImpactRow
4✔
13
from scope3ai.api.typesgen import Task
4✔
14
from scope3ai.constants import PROVIDERS
4✔
15
from scope3ai.lib import Scope3AI
4✔
16
from scope3ai.response_interceptor.requests_interceptor import requests_response_capture
4✔
17

18
PROVIDER = PROVIDERS.HUGGINGFACE_HUB.value
4✔
19
HUGGING_FACE_SPEECH_TO_TEXT_TASK = "automatic-speech-recognition"
4✔
20

21

22
@dataclass
4✔
23
class AutomaticSpeechRecognitionOutput(_AutomaticSpeechRecognitionOutput):
4✔
24
    scope3ai: Optional[Scope3AIContext] = None
4✔
25

26

27
def _hugging_face_automatic_recognition_wrapper(
4✔
28
    timer_start: Any,
29
    model: Any,
30
    response: Any,
31
    http_response: Union[ClientResponse, Response],
32
    args: Any,
33
    kwargs: Any,
34
) -> AutomaticSpeechRecognitionOutput:
35
    if http_response:
4✔
36
        compute_audio_length = http_response.headers.get("x-compute-audio-length")
4✔
37
        compute_time = http_response.headers.get("x-compute-time")
4✔
38
    else:
UNCOV
39
        compute_audio_length = 0
×
UNCOV
40
        compute_time = time.perf_counter() - timer_start
×
41

42
    scope3_row = ImpactRow(
4✔
43
        model=Model(id=model),
44
        task=Task.text_to_speech,
45
        input_audio_seconds=int(float(compute_audio_length)),
46
        request_duration_ms=float(compute_time) * 1000,
47
        managed_service_id=PROVIDER,
48
    )
49

50
    scope3_ctx = Scope3AI.get_instance().submit_impact(scope3_row)
4✔
51
    result = AutomaticSpeechRecognitionOutput(**asdict(response))
4✔
52
    result.scope3ai = scope3_ctx
4✔
53
    return result
4✔
54

55

56
def huggingface_automatic_recognition_output_wrapper(
4✔
57
    wrapped: Callable, instance: InferenceClient, args: Any, kwargs: Any
58
) -> AutomaticSpeechRecognitionOutput:
59
    timer_start = time.perf_counter()
4✔
60
    http_response: Response | None = None
4✔
61
    with requests_response_capture() as responses:
4✔
62
        response = wrapped(*args, **kwargs)
4✔
63
        http_responses = responses.get()
4✔
64
        if len(http_responses) > 0:
4✔
65
            http_response = http_responses[-1]
4✔
66
    model = kwargs.get("model") or instance.get_recommended_model(
4✔
67
        HUGGING_FACE_SPEECH_TO_TEXT_TASK
68
    )
69
    return _hugging_face_automatic_recognition_wrapper(
4✔
70
        timer_start, model, response, http_response, args, kwargs
71
    )
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