forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhume.py
More file actions
236 lines (200 loc) · 8.29 KB
/
Copy pathhume.py
File metadata and controls
236 lines (200 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os
from typing import Any, Dict, List, Optional, Tuple, cast
import httpx
import logging
logger = logging.getLogger(__name__)
class HumePredictionEmotionResponseModel:
def __init__(
self,
name: str,
score: float,
) -> None:
self.name = name
self.score = score
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "HumePredictionEmotionResponseModel":
# Default to safe values for a malformed entry: a missing/invalid score must stay numeric so
# downstream math in get_top_emotion_names (sum and threshold comparison) does not hit None.
score = data.get("score")
if not isinstance(score, (int, float)) or isinstance(score, bool):
score = 0.0
model = cls(data.get("name") or "", score)
return model
def to_dict(self) -> Dict[str, Any]:
return {
'name': self.name,
'score': self.score,
}
@classmethod
def to_multi_dict(cls, emotions: List["HumePredictionEmotionResponseModel"]) -> List[Dict[str, Any]]:
return [e.to_dict() for e in emotions]
class HumeJobModelPredictionResponseModel:
def __init__(
self,
time: Tuple[float, float],
emotions: Optional[List[HumePredictionEmotionResponseModel]] = None,
) -> None:
# Use a fresh list per instance, never a shared mutable default. from_dict appends to
# self.emotions, so a shared default would leak emotions across parsed callbacks.
self.emotions = emotions if emotions is not None else []
self.time = time
@classmethod
def get_top_emotion_names(
cls,
emotions: Optional[List[HumePredictionEmotionResponseModel]] = None,
k: int = 5,
peak_threshold: float = 0.7,
) -> List[str]:
emotions_dict: Dict[str, float] = {}
for emo in emotions or []:
if emo.name not in emotions_dict:
emotions_dict[emo.name] = emo.score
else:
emotions_dict[emo.name] = emotions_dict[emo.name] + emo.score
n = len(emotions_dict)
emotions_average: Dict[str, float] = {}
for emotion, score in emotions_dict.items():
if score >= peak_threshold:
emotions_average[emotion] = score / n
ascend_sorted_emotion_average = sorted(emotions_average, key=lambda name: emotions_average[name], reverse=True)
k = min(k, len(ascend_sorted_emotion_average))
return ascend_sorted_emotion_average[:k]
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "HumeJobModelPredictionResponseModel":
grouped_prediction_prediction = data
raw_time = data.get("time")
time_data: Dict[str, Any] = cast(Dict[str, Any], raw_time) if isinstance(raw_time, dict) else {}
# Keep the interval numeric so downstream consumers comparing begin/end never hit None.
begin = time_data.get("begin")
end = time_data.get("end")
if not isinstance(begin, (int, float)) or isinstance(begin, bool):
begin = 0.0
if not isinstance(end, (int, float)) or isinstance(end, bool):
end = 0.0
model = cls((begin, end))
raw_emotions = grouped_prediction_prediction.get('emotions')
emotions_list: List[Dict[str, Any]] = (
cast(List[Dict[str, Any]], raw_emotions) if isinstance(raw_emotions, list) else []
)
for emotion in emotions_list:
emo = HumePredictionEmotionResponseModel.from_dict(emotion)
model.emotions.append(emo)
return model
@classmethod
def from_multi_dict(
cls, prediction_model: str, data: Dict[str, Any]
) -> List["HumeJobModelPredictionResponseModel"]:
model: List[HumeJobModelPredictionResponseModel] = []
if "results" not in data or "predictions" not in data["results"]:
return model
for prediction in data["results"]["predictions"]:
# A failed or partial Hume job can omit the requested model, grouped_predictions, or the
# inner predictions list; guard the nested lookups so one malformed prediction yields no
# emotions instead of a KeyError that 500s the whole callback (mirrors the .get(...) style
# used elsewhere in this module).
grouped_predictions = prediction.get('models', {}).get(prediction_model, {}).get('grouped_predictions', [])
for grouped_prediction in grouped_predictions:
for grouped_prediction_prediction in grouped_prediction.get('predictions', []):
model.append(cls.from_dict(grouped_prediction_prediction))
return model
class HumeJobCallbackModel:
def __init__(
self,
job_id: Optional[str],
status: Optional[str],
predictions: Optional[List[HumeJobModelPredictionResponseModel]] = None,
) -> None:
self.job_id = job_id
self.status = status
self.predictions = predictions if predictions is not None else []
@classmethod
def from_dict(cls, prediction_model: str, data: Dict[str, Any]) -> "HumeJobCallbackModel":
# predictions[0] -> results -> predictions
predictions: List[HumeJobModelPredictionResponseModel] = []
if "predictions" in data and len(data["predictions"]) > 0:
predictions = HumeJobModelPredictionResponseModel.from_multi_dict(prediction_model, data["predictions"][0])
model = cls(data.get("job_id"), data.get("status"), predictions)
return model
class HumeJobResponseModel:
def __init__(
self,
id: Optional[str],
) -> None:
self.id = id
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "HumeJobResponseModel":
# Read job_id defensively: this runs on the success (HTTP 200) path from resp.json(),
# and a response missing job_id must not raise KeyError out of the caller while every
# error status is already turned into an error dict. id is Optional[str].
model = cls(data.get("job_id"))
return model
class HumeClient:
"""
Implementation of the Hume APIs.
This abstract class provides a Python interface to all Hume APIs.
"""
def __init__(
self,
api_key: Optional[str],
callback_url: Optional[str],
) -> None:
self.api_key = api_key
self.callback_url = callback_url
def request_user_expression_mersurement(self, urls: List[str]) -> Dict[str, Any]:
err: Optional[Dict[str, Any]] = None
resp: Optional[httpx.Response] = None
# Model
data = {
"models": {"prosody": {"granularity": "utterance"}},
"urls": urls,
"callback_url": self.callback_url,
}
try:
resp = httpx.post(
"https://api.hume.ai/v0/batch/jobs",
json=data,
headers={
'Content-Type': 'application/json',
'Accept': 'application/json; charset=utf-8',
'X-Hume-Api-Key': self.api_key if self.api_key is not None else '',
},
timeout=300.0,
follow_redirects=True,
)
except httpx.TimeoutException:
err = {
"error": {
"message": "Timeout",
},
}
except httpx.TooManyRedirects:
err = {
"error": {
"message": "TooManyRedirects",
},
}
except httpx.RequestError as e:
err = {
"error": {
"message": f"RequestError {e}",
},
}
if err is None and resp is not None and resp.status_code != 200:
resp_text = f"{resp}"
err = {
"error": {
"status": resp.status_code,
"message": resp_text,
},
}
if err is not None:
logger.error(err)
return err
assert resp is not None # err is None implies the try-block assigned resp
return {"result": HumeJobResponseModel.from_dict(resp.json())}
hume_client = HumeClient(
api_key=os.getenv('HUME_API_KEY'),
callback_url=os.getenv('HUME_CALLBACK_URL'),
)
def get_hume() -> HumeClient:
return hume_client