deepgram.clients.common

 1# Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
 2# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
 3# SPDX-License-Identifier: MIT
 4
 5from .v1 import (
 6    DeepgramError,
 7    DeepgramTypeError,
 8    DeepgramApiError,
 9    DeepgramUnknownApiError,
10)
11
12from .v1 import AbstractAsyncRestClient
13from .v1 import AbstractSyncRestClient
14from .v1 import AbstractAsyncWebSocketClient
15from .v1 import AbstractSyncWebSocketClient
16
17from .v1 import (
18    TextSource as TextSourceLatest,
19    BufferSource as BufferSourceLatest,
20    StreamSource as StreamSourceLatest,
21    FileSource as FileSourceLatest,
22    UrlSource as UrlSourceLatest,
23)
24
25# shared
26from .v1 import (
27    BaseResponse as BaseResponseLatest,
28    ModelInfo as ModelInfoLatest,
29    Hit as HitLatest,
30    Search as SearchLatest,
31)
32
33# rest
34from .v1 import (
35    Average as AverageLatest,
36    Intent as IntentLatest,
37    Intents as IntentsLatest,
38    IntentsInfo as IntentsInfoLatest,
39    Segment as SegmentLatest,
40    SentimentInfo as SentimentInfoLatest,
41    Sentiment as SentimentLatest,
42    Sentiments as SentimentsLatest,
43    SummaryInfo as SummaryInfoLatest,
44    Topic as TopicLatest,
45    Topics as TopicsLatest,
46    TopicsInfo as TopicsInfoLatest,
47)
48
49# websocket
50from .v1 import (
51    OpenResponse as OpenResponseLatest,
52    CloseResponse as CloseResponseLatest,
53    ErrorResponse as ErrorResponseLatest,
54    UnhandledResponse as UnhandledResponseLatest,
55)
56
57# export
58UrlSource = UrlSourceLatest
59TextSource = TextSourceLatest
60BufferSource = BufferSourceLatest
61StreamSource = StreamSourceLatest
62FileSource = FileSourceLatest
63
64BaseResponse = BaseResponseLatest
65ModelInfo = ModelInfoLatest
66Hit = HitLatest
67Search = SearchLatest
68
69Average = AverageLatest
70Intent = IntentLatest
71Intents = IntentsLatest
72IntentsInfo = IntentsInfoLatest
73Segment = SegmentLatest
74SentimentInfo = SentimentInfoLatest
75Sentiment = SentimentLatest
76Sentiments = SentimentsLatest
77SummaryInfo = SummaryInfoLatest
78Topic = TopicLatest
79Topics = TopicsLatest
80TopicsInfo = TopicsInfoLatest
81
82OpenResponse = OpenResponseLatest
83CloseResponse = CloseResponseLatest
84ErrorResponse = ErrorResponseLatest
85UnhandledResponse = UnhandledResponseLatest
class UrlSource(typing_extensions.TypedDict):
25class UrlSource(TypedDict):
26    """
27    Represents a data source for specifying the location of a file via a URL.
28
29    This class is used to specify a hosted file URL, typically pointing to an
30    externally hosted file, such as an audio file hosted on a server or the internet.
31
32    Attributes:
33        url (str): The URL pointing to the hosted file.
34    """
35
36    url: str

Represents a data source for specifying the location of a file via a URL.

This class is used to specify a hosted file URL, typically pointing to an externally hosted file, such as an audio file hosted on a server or the internet.

Attributes: url (str): The URL pointing to the hosted file.

url: str
class TextSource(typing_extensions.TypedDict):
53class TextSource(TypedDict):
54    """
55    Represents a data source for reading binary data from a text-like source.
56
57    This class is used to specify a source of text data that can be read from.
58
59    Attributes:
60        text (str): A string for reading text data.
61    """
62
63    text: str

Represents a data source for reading binary data from a text-like source.

This class is used to specify a source of text data that can be read from.

Attributes: text (str): A string for reading text data.

text: str
class BufferSource(typing_extensions.TypedDict):
39class BufferSource(TypedDict):
40    """
41    Represents a data source for handling raw binary data.
42
43    This class is used to specify raw binary data, such as audio data in its
44    binary form, which can be captured from a microphone or generated synthetically.
45
46    Attributes:
47        buffer (bytes): The binary data.
48    """
49
50    buffer: bytes

Represents a data source for handling raw binary data.

This class is used to specify raw binary data, such as audio data in its binary form, which can be captured from a microphone or generated synthetically.

Attributes: buffer (bytes): The binary data.

buffer: bytes
class StreamSource(typing_extensions.TypedDict):
11class StreamSource(TypedDict):
12    """
13    Represents a data source for reading binary data from a stream-like source.
14
15    This class is used to specify a source of binary data that can be read from
16    a stream, such as an audio file in .wav format.
17
18    Attributes:
19        stream (BufferedReader): A BufferedReader object for reading binary data.
20    """
21
22    stream: BufferedReader

Represents a data source for reading binary data from a stream-like source.

This class is used to specify a source of binary data that can be read from a stream, such as an audio file in .wav format.

Attributes: stream (BufferedReader): A BufferedReader object for reading binary data.

stream: _io.BufferedReader
FileSource = typing.Union[TextSource, BufferSource, StreamSource]
@dataclass
class BaseResponse(dataclasses_json.api.DataClassJsonMixin):
16@dataclass
17class BaseResponse(DataClassJsonMixin):
18    """
19    BaseResponse class used to define the common methods and properties for all response classes.
20    """
21
22    def __getitem__(self, key):
23        _dict = self.to_dict()
24        return _dict[key]
25
26    def __setitem__(self, key, val):
27        self.__dict__[key] = val
28
29    def __str__(self) -> str:
30        return self.to_json(indent=4)
31
32    def eval(self, key: str) -> str:
33        """
34        This method is used to evaluate a key in the response object using a dot notation style method.
35        """
36        keys = key.split(".")
37        result: Dict[Any, Any] = self.to_dict()
38        for k in keys:
39            if isinstance(result, dict) and k in result:
40                result = result[k]
41            elif isinstance(result, list) and k.isdigit() and int(k) < len(result):
42                result = result[int(k)]
43            else:
44                return ""
45        return str(result)

BaseResponse class used to define the common methods and properties for all response classes.

def eval(self, key: str) -> str:
32    def eval(self, key: str) -> str:
33        """
34        This method is used to evaluate a key in the response object using a dot notation style method.
35        """
36        keys = key.split(".")
37        result: Dict[Any, Any] = self.to_dict()
38        for k in keys:
39            if isinstance(result, dict) and k in result:
40                result = result[k]
41            elif isinstance(result, list) and k.isdigit() and int(k) < len(result):
42                result = result[int(k)]
43            else:
44                return ""
45        return str(result)

This method is used to evaluate a key in the response object using a dot notation style method.

@dataclass
class ModelInfo(deepgram.clients.common.BaseResponse):
51@dataclass
52class ModelInfo(BaseResponse):
53    """
54    ModelInfo object
55    """
56
57    name: str = ""
58    version: str = ""
59    arch: str = ""

ModelInfo object

ModelInfo(name: str = '', version: str = '', arch: str = '')
name: str = ''
version: str = ''
arch: str = ''
Inherited Members
BaseResponse
eval
@dataclass
class Hit(deepgram.clients.common.BaseResponse):
62@dataclass
63class Hit(BaseResponse):
64    """
65    The hit information for the response.
66    """
67
68    confidence: float = 0
69    start: float = 0
70    end: float = 0
71    snippet: Optional[str] = ""

The hit information for the response.

Hit( confidence: float = 0, start: float = 0, end: float = 0, snippet: Optional[str] = '')
confidence: float = 0
start: float = 0
end: float = 0
snippet: Optional[str] = ''
Inherited Members
BaseResponse
eval
@dataclass
class Average(deepgram.clients.common.BaseResponse):
62@dataclass
63class Average(BaseResponse):
64    """
65    Average
66    """
67
68    sentiment: Sentiment
69    sentiment_score: float = 0
70
71    def __getitem__(self, key):
72        _dict = self.to_dict()
73        if "sentiment" in _dict:
74            _dict["sentiment"] = Sentiment.from_dict(_dict["sentiment"])
75        return _dict[key]

Average

Average( sentiment: Sentiment, sentiment_score: float = 0)
sentiment: Sentiment
sentiment_score: float = 0
Inherited Members
BaseResponse
eval
@dataclass
class Intent(deepgram.clients.common.BaseResponse):
88@dataclass
89class Intent(BaseResponse):
90    """
91    Intent
92    """
93
94    intent: str = ""
95    confidence_score: float = 0

Intent

Intent(intent: str = '', confidence_score: float = 0)
intent: str = ''
confidence_score: float = 0
Inherited Members
BaseResponse
eval
@dataclass
class Intents(deepgram.clients.common.BaseResponse):
168@dataclass
169class Intents(BaseResponse):
170    """
171    Intents
172    """
173
174    segments: List[Segment] = field(default_factory=list)
175
176    def __getitem__(self, key):
177        _dict = self.to_dict()
178        if "segments" in _dict:
179            _dict["segments"] = [
180                Segment.from_dict(segments) for segments in _dict["segments"]
181            ]
182        return _dict[key]

Intents

Intents( segments: List[Segment] = <factory>)
segments: List[Segment]
Inherited Members
BaseResponse
eval
@dataclass
class IntentsInfo(deepgram.clients.common.BaseResponse):
18@dataclass
19class IntentsInfo(BaseResponse):
20    """
21    Intents Info
22    """
23
24    model_uuid: str = ""
25    input_tokens: int = 0
26    output_tokens: int = 0

Intents Info

IntentsInfo(model_uuid: str = '', input_tokens: int = 0, output_tokens: int = 0)
model_uuid: str = ''
input_tokens: int = 0
output_tokens: int = 0
Inherited Members
BaseResponse
eval
@dataclass
class Segment(deepgram.clients.common.BaseResponse):
 98@dataclass
 99class Segment(BaseResponse):
100    """
101    Segment
102    """
103
104    text: str = ""
105    start_word: int = 0
106    end_word: int = 0
107    sentiment: Optional[Sentiment] = field(
108        default=None, metadata=dataclass_config(exclude=lambda f: f is None)
109    )
110    sentiment_score: Optional[float] = field(
111        default=None, metadata=dataclass_config(exclude=lambda f: f is None)
112    )
113    intents: Optional[List[Intent]] = field(
114        default=None, metadata=dataclass_config(exclude=lambda f: f is None)
115    )
116    topics: Optional[List[Topic]] = field(
117        default=None, metadata=dataclass_config(exclude=lambda f: f is None)
118    )
119
120    def __getitem__(self, key):
121        _dict = self.to_dict()
122        if "sentiment" in _dict:
123            _dict["sentiment"] = Sentiment.from_dict(_dict["sentiment"])
124        if "intents" in _dict:
125            _dict["intents"] = Intent.from_dict(_dict["intents"])
126        if "topics" in _dict:
127            _dict["topics"] = Topic.from_dict(_dict["topics"])
128        return _dict[key]

Segment

Segment( text: str = '', start_word: int = 0, end_word: int = 0, sentiment: Optional[Sentiment] = None, sentiment_score: Optional[float] = None, intents: Optional[List[Intent]] = None, topics: Optional[List[Topic]] = None)
text: str = ''
start_word: int = 0
end_word: int = 0
sentiment: Optional[Sentiment] = None
sentiment_score: Optional[float] = None
intents: Optional[List[Intent]] = None
topics: Optional[List[Topic]] = None
Inherited Members
BaseResponse
eval
@dataclass
class SentimentInfo(deepgram.clients.common.BaseResponse):
29@dataclass
30class SentimentInfo(BaseResponse):
31    """
32    Sentiment Info
33    """
34
35    model_uuid: str = ""
36    input_tokens: int = 0
37    output_tokens: int = 0

Sentiment Info

SentimentInfo(model_uuid: str = '', input_tokens: int = 0, output_tokens: int = 0)
model_uuid: str = ''
input_tokens: int = 0
output_tokens: int = 0
Inherited Members
BaseResponse
eval
class Sentiment(aenum._enum.StrEnum):
11class Sentiment(StrEnum):
12    """
13    Sentiment values.
14    """
15
16    UNKNOWN: str = ""
17    NEGATIVE: str = "negative"
18    NEUTRAL: str = "neutral"
19    POSITIVE: str = "positive"

Sentiment values.

UNKNOWN: str = <Sentiment.UNKNOWN: ''>
NEGATIVE: str = <Sentiment.NEGATIVE: 'negative'>
NEUTRAL: str = <Sentiment.NEUTRAL: 'neutral'>
POSITIVE: str = <Sentiment.POSITIVE: 'positive'>
@dataclass
class Sentiments(deepgram.clients.common.BaseResponse):
131@dataclass
132class Sentiments(BaseResponse):
133    """
134    Sentiments
135    """
136
137    average: Average
138    segments: List[Segment] = field(default_factory=list)
139
140    def __getitem__(self, key):
141        _dict = self.to_dict()
142        if "segments" in _dict:
143            _dict["segments"] = [
144                Segment.from_dict(segments) for segments in _dict["segments"]
145            ]
146        if "average" in _dict:
147            _dict["average"] = Average.from_dict(_dict["average"])
148        return _dict[key]

Sentiments

Sentiments( average: Average, segments: List[Segment] = <factory>)
average: Average
segments: List[Segment]
Inherited Members
BaseResponse
eval
@dataclass
class SummaryInfo(deepgram.clients.common.BaseResponse):
40@dataclass
41class SummaryInfo(BaseResponse):
42    """
43    Summary Info
44    """
45
46    model_uuid: str = ""
47    input_tokens: int = 0
48    output_tokens: int = 0

Summary Info

SummaryInfo(model_uuid: str = '', input_tokens: int = 0, output_tokens: int = 0)
model_uuid: str = ''
input_tokens: int = 0
output_tokens: int = 0
Inherited Members
BaseResponse
eval
@dataclass
class Topic(deepgram.clients.common.BaseResponse):
78@dataclass
79class Topic(BaseResponse):
80    """
81    Topic
82    """
83
84    topic: str = ""
85    confidence_score: float = 0

Topic

Topic(topic: str = '', confidence_score: float = 0)
topic: str = ''
confidence_score: float = 0
Inherited Members
BaseResponse
eval
@dataclass
class Topics(deepgram.clients.common.BaseResponse):
151@dataclass
152class Topics(BaseResponse):
153    """
154    Topics
155    """
156
157    segments: List[Segment] = field(default_factory=list)
158
159    def __getitem__(self, key):
160        _dict = self.to_dict()
161        if "segments" in _dict:
162            _dict["segments"] = [
163                Segment.from_dict(segments) for segments in _dict["segments"]
164            ]
165        return _dict[key]

Topics

Topics( segments: List[Segment] = <factory>)
segments: List[Segment]
Inherited Members
BaseResponse
eval
@dataclass
class TopicsInfo(deepgram.clients.common.BaseResponse):
51@dataclass
52class TopicsInfo(BaseResponse):
53    """
54    Topics Info
55    """
56
57    model_uuid: str = ""
58    input_tokens: int = 0
59    output_tokens: int = 0

Topics Info

TopicsInfo(model_uuid: str = '', input_tokens: int = 0, output_tokens: int = 0)
model_uuid: str = ''
input_tokens: int = 0
output_tokens: int = 0
Inherited Members
BaseResponse
eval
@dataclass
class OpenResponse(deepgram.clients.common.BaseResponse):
17@dataclass
18class OpenResponse(BaseResponse):
19    """
20    Open Message from the Deepgram Platform
21    """
22
23    type: str = ""

Open Message from the Deepgram Platform

OpenResponse(type: str = '')
type: str = ''
Inherited Members
BaseResponse
eval
@dataclass
class CloseResponse(deepgram.clients.common.BaseResponse):
29@dataclass
30class CloseResponse(BaseResponse):
31    """
32    Close Message from the Deepgram Platform
33    """
34
35    type: str = ""

Close Message from the Deepgram Platform

CloseResponse(type: str = '')
type: str = ''
Inherited Members
BaseResponse
eval
@dataclass
class ErrorResponse(deepgram.clients.common.BaseResponse):
41@dataclass
42class ErrorResponse(BaseResponse):
43    """
44    Error Message from the Deepgram Platform
45    """
46
47    description: str = ""
48    message: str = ""
49    type: str = ""
50    variant: Optional[str] = field(
51        default=None, metadata=dataclass_config(exclude=lambda f: f is None)
52    )

Error Message from the Deepgram Platform

ErrorResponse( description: str = '', message: str = '', type: str = '', variant: Optional[str] = None)
description: str = ''
message: str = ''
type: str = ''
variant: Optional[str] = None
Inherited Members
BaseResponse
eval
@dataclass
class UnhandledResponse(deepgram.clients.common.BaseResponse):
58@dataclass
59class UnhandledResponse(BaseResponse):
60    """
61    Unhandled Message from the Deepgram Platform
62    """
63
64    type: str = ""
65    raw: str = ""

Unhandled Message from the Deepgram Platform

UnhandledResponse(type: str = '', raw: str = '')
type: str = ''
raw: str = ''
Inherited Members
BaseResponse
eval