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

scope3data / scope3ai-py / 12777439528

14 Jan 2025 10:21PM UTC coverage: 95.153% (+14.6%) from 80.557%
12777439528

Pull #62

github

3071c5
web-flow
Merge 20548d47d into 42c868843
Pull Request #62: feat: Hugging face - speech to text async fix

14 of 14 new or added lines in 1 file covered. (100.0%)

33 existing lines in 9 files now uncovered.

2022 of 2125 relevant lines covered (95.15%)

3.8 hits per line

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

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

5
import tiktoken
4✔
6
from aiohttp import ClientResponse
4✔
7
from huggingface_hub import InferenceClient, AsyncInferenceClient  # type: ignore[import-untyped]
4✔
8
from huggingface_hub import TranslationOutput as _TranslationOutput
4✔
9
from requests import Response
4✔
10

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

18
PROVIDER = PROVIDERS.HUGGINGFACE_HUB.value
4✔
19
HUGGING_FACE_TRANSLATION_TASK = "translation"
4✔
20

21

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

26

27
def _hugging_face_translation_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
) -> TranslationOutput:
35
    encoder = tiktoken.get_encoding("cl100k_base")
4✔
36
    input_tokens = None
4✔
37
    if http_response:
4✔
38
        compute_time = http_response.headers.get("x-compute-time")
4✔
39
        input_tokens = http_response.headers.get("x-compute-characters")
4✔
40
    else:
UNCOV
41
        compute_time = time.perf_counter() - timer_start
×
42
    if not input_tokens:
4✔
UNCOV
43
        prompt = args[0] if len(args) > 0 else kwargs.get("text", "")
×
UNCOV
44
        input_tokens = len(encoder.encode(prompt)) if prompt != "" else 0
×
45
    output_tokens = len(encoder.encode(response.translation_text))
4✔
46
    scope3_row = ImpactRow(
4✔
47
        model=Model(id=model),
48
        task=Task.translation,
49
        input_tokens=input_tokens,
50
        output_tokens=output_tokens,
51
        request_duration_ms=float(compute_time) * 1000,
52
        managed_service_id=PROVIDER,
53
    )
54

55
    scope3_ctx = Scope3AI.get_instance().submit_impact(scope3_row)
4✔
56
    result = TranslationOutput(**asdict(response))
4✔
57
    result.scope3ai = scope3_ctx
4✔
58
    return result
4✔
59

60

61
async def huggingface_translation_wrapper_async_non_stream(
4✔
62
    wrapped: Callable, instance: AsyncInferenceClient, args: Any, kwargs: Any
63
) -> TranslationOutput:
64
    timer_start = time.perf_counter()
4✔
65
    http_response: ClientResponse | None = None
4✔
66
    with aiohttp_response_capture() as responses:
4✔
67
        response = await wrapped(*args, **kwargs)
4✔
68
        http_responses = responses.get()
4✔
69
        if len(http_responses) > 0:
4✔
70
            http_response = http_responses[-1]
4✔
71
    model = kwargs.get("model") or instance.get_recommended_model(
4✔
72
        HUGGING_FACE_TRANSLATION_TASK
73
    )
74
    return _hugging_face_translation_wrapper(
4✔
75
        timer_start, model, response, http_response, args, kwargs
76
    )
77

78

79
def huggingface_translation_wrapper_non_stream(
4✔
80
    wrapped: Callable, instance: InferenceClient, args: Any, kwargs: Any
81
) -> TranslationOutput:
82
    timer_start = time.perf_counter()
4✔
83
    http_response: Response | None = None
4✔
84
    with requests_response_capture() as responses:
4✔
85
        response = wrapped(*args, **kwargs)
4✔
86
        http_responses = responses.get()
4✔
87
        if len(http_responses) > 0:
4✔
88
            http_response = http_responses[-1]
4✔
89
    model = kwargs.get("model") or instance.get_recommended_model(
4✔
90
        HUGGING_FACE_TRANSLATION_TASK
91
    )
92
    return _hugging_face_translation_wrapper(
4✔
93
        timer_start, model, response, http_response, args, kwargs
94
    )
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