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

scope3data / scope3ai-py / 12681022279

08 Jan 2025 11:43PM UTC coverage: 95.11% (+14.6%) from 80.557%
12681022279

Pull #56

github

da957e
kevdevg
fix: add try catch to pillow images
Pull Request #56: feat(huggingface): add support for image-to-image/ text-to-speech

155 of 169 new or added lines in 7 files covered. (91.72%)

26 existing lines in 4 files now uncovered.

1770 of 1861 relevant lines covered (95.11%)

3.8 hits per line

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

94.44
/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

20

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

25

26
def _hugging_face_translation_wrapper(
4✔
27
    timer_start: Any,
28
    model: Any,
29
    response: Any,
30
    http_response: Union[ClientResponse, Response],
31
    args: Any,
32
    kwargs: Any,
33
) -> TranslationOutput:
34
    encoder = tiktoken.get_encoding("cl100k_base")
4✔
35
    input_tokens = None
4✔
36
    if http_response:
4✔
37
        compute_time = http_response.headers.get("x-compute-time")
4✔
38
        input_tokens = http_response.headers.get("x-compute-characters")
4✔
39
    else:
NEW
40
        compute_time = time.perf_counter() - timer_start
×
41
    if not input_tokens:
4✔
NEW
42
        prompt = args[0] if len(args) > 0 else kwargs.get("text", "")
×
NEW
43
        input_tokens = len(encoder.encode(prompt)) if prompt != "" else 0
×
44
    output_tokens = len(encoder.encode(response.translation_text))
4✔
45
    scope3_row = ImpactRow(
4✔
46
        model=Model(id=model),
47
        task=Task.translation,
48
        input_tokens=input_tokens,
49
        output_tokens=output_tokens,
50
        request_duration_ms=float(compute_time) * 1000,
51
        managed_service_id=PROVIDER,
52
    )
53

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

59

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

75

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