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

scope3data / scope3ai-py / 12660867782

07 Jan 2025 11:11PM UTC coverage: 94.25% (+13.7%) from 80.557%
12660867782

Pull #55

github

5c14aa
tito
feat: add more tests
Pull Request #55: feat(openai): support for image generation

32 of 32 new or added lines in 2 files covered. (100.0%)

28 existing lines in 5 files now uncovered.

1672 of 1774 relevant lines covered (94.25%)

3.77 hits per line

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

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

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

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

16
PROVIDER = "huggingface_hub"
4✔
17

18

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

23

24
def huggingface_translation_wrapper_non_stream(
4✔
25
    wrapped: Callable, instance: InferenceClient, args: Any, kwargs: Any
26
) -> TranslationOutput:
27
    http_response: Response | None = None
4✔
28
    with requests_response_capture() as responses:
4✔
29
        response = wrapped(*args, **kwargs)
4✔
30
        http_responses = responses.get()
4✔
31
        if len(http_responses) > 0:
4✔
32
            http_response = http_responses[-1]
4✔
33
    model = kwargs.get("model") or instance.get_recommended_model("translation")
4✔
34
    encoder = tiktoken.get_encoding("cl100k_base")
4✔
35
    if len(args) > 0:
4✔
36
        prompt = args[0]
4✔
37
    else:
UNCOV
38
        prompt = kwargs["text"]
×
39
    compute_time = http_response.headers.get("x-compute-time")
4✔
40
    input_tokens = len(encoder.encode(prompt))
4✔
41
    output_tokens = len(encoder.encode(response.translation_text))
4✔
42
    scope3_row = ImpactRow(
4✔
43
        model=Model(id=model),
44
        task=Task.translation,
45
        input_tokens=input_tokens,
46
        output_tokens=output_tokens,  # TODO: How we can calculate the output tokens of a translation?
47
        request_duration_ms=float(compute_time) * 1000,
48
        managed_service_id=PROVIDER,
49
    )
50

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

56

57
async def huggingface_translation_wrapper_async_non_stream(
4✔
58
    wrapped: Callable, instance: AsyncInferenceClient, args: Any, kwargs: Any
59
) -> TranslationOutput:
60
    http_response: ClientResponse | None = None
4✔
61
    with aiohttp_response_capture() as responses:
4✔
62
        response = await 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("translation")
4✔
67
    encoder = tiktoken.get_encoding("cl100k_base")
4✔
68
    if len(args) > 0:
4✔
69
        prompt = args[0]
4✔
70
    else:
UNCOV
71
        prompt = kwargs["text"]
×
72
    compute_time = http_response.headers.get("x-compute-time")
4✔
73
    input_tokens = len(encoder.encode(prompt))
4✔
74
    output_tokens = len(encoder.encode(response.translation_text))
4✔
75
    scope3_row = ImpactRow(
4✔
76
        model=Model(id=model),
77
        task=Task.translation,
78
        input_tokens=input_tokens,
79
        output_tokens=output_tokens,
80
        request_duration_ms=float(compute_time) * 1000,
81
        managed_service_id=PROVIDER,
82
    )
83

84
    scope3_ctx = Scope3AI.get_instance().submit_impact(scope3_row)
4✔
85
    result = TranslationOutput(**asdict(response))
4✔
86
    result.scope3ai = scope3_ctx
4✔
87
    return result
4✔
88

89

90
async def huggingface_text_to_image_wrapper_async(
4✔
91
    wrapped: Callable, instance: AsyncInferenceClient, args: Any, kwargs: Any
92
) -> TranslationOutput:
UNCOV
93
    return huggingface_translation_wrapper_async_non_stream(
×
94
        wrapped, instance, args, kwargs
95
    )
96

97

98
def huggingface_text_to_image_wrapper(
4✔
99
    wrapped: Callable, instance: InferenceClient, args: Any, kwargs: Any
100
) -> TranslationOutput:
UNCOV
101
    return huggingface_translation_wrapper_non_stream(wrapped, instance, args, kwargs)
×
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