aabouzeid commited on
Commit
0a56a24
·
1 Parent(s): 830ca69

Add HuggingFace Inference Endpoint handler

Browse files

Add handler.py with EndpointHandler class that accepts raw audio bytes
and returns depression/anxiety severity scores. Fix torch.load to use
weights_only=False and correct package names in requirements.txt.

Files changed (3) hide show
  1. handler.py +29 -0
  2. pipeline.py +1 -1
  3. requirements.txt +3 -3
handler.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """HuggingFace Inference Endpoint handler for the DAM model."""
2
+
3
+ import io
4
+ from pathlib import Path
5
+
6
+ from pipeline import Pipeline
7
+
8
+
9
+ class EndpointHandler:
10
+ def __init__(self, path=""):
11
+ checkpoint = Path(path) / "dam3.1.ckpt"
12
+ self.pipeline = Pipeline(checkpoint=checkpoint)
13
+
14
+ def __call__(self, data):
15
+ inputs = data.get("inputs")
16
+ if isinstance(inputs, bytes):
17
+ source = io.BytesIO(inputs)
18
+ else:
19
+ raise ValueError("Expected raw audio bytes in data['inputs']")
20
+
21
+ quantized = self.pipeline.run_on_file(source, quantize=True)
22
+ source.seek(0)
23
+ raw = self.pipeline.run_on_file(source, quantize=False)
24
+
25
+ return {
26
+ "depression": quantized["depression"],
27
+ "anxiety": quantized["anxiety"],
28
+ "raw_scores": {k: v.mean().item() for k, v in raw.items()},
29
+ }
pipeline.py CHANGED
@@ -22,7 +22,7 @@ class Pipeline:
22
  self.device = device
23
  self.model = Classifier(**config)
24
  self.preprocessor = Preprocessor(**self.model.preprocessor_config)
25
- state_dict = torch.load(checkpoint, map_location=device)
26
  self.model.load_state_dict(state_dict)
27
  self.model.to(self.device)
28
  self.model.eval()
 
22
  self.device = device
23
  self.model = Classifier(**config)
24
  self.preprocessor = Preprocessor(**self.model.preprocessor_config)
25
+ state_dict = torch.load(checkpoint, map_location=device, weights_only=False)
26
  self.model.load_state_dict(state_dict)
27
  self.model.to(self.device)
28
  self.model.eval()
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
- pytorch~=2.6.0
2
- pysoundfile~=0.13.1
3
- torchaudio~=2.7.0
4
  transformers~=4.52.3
5
  peft~=0.15.2
 
1
+ torch~=2.6.0
2
+ soundfile~=0.13.1
3
+ torchaudio~=2.6.0
4
  transformers~=4.52.3
5
  peft~=0.15.2