jjoyce003 commited on
Commit
c222f61
·
verified ·
1 Parent(s): f84f534

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoProcessor, AutoModel
4
+
5
+ model_name = "MERaLiON/MERaLiON-SpeechEncoder-v1"
6
+
7
+ processor = AutoProcessor.from_pretrained(model_name)
8
+ model = AutoModel.from_pretrained(model_name)
9
+
10
+ def encode_audio(audio):
11
+ # audio = (sample_rate, numpy array)
12
+ sr, data = audio
13
+
14
+ inputs = processor(
15
+ data,
16
+ sampling_rate=sr,
17
+ return_tensors="pt",
18
+ padding=True
19
+ )
20
+
21
+ with torch.no_grad():
22
+ embeddings = model(**inputs).last_hidden_state.mean(dim=1).squeeze().tolist()
23
+
24
+ return {
25
+ "embeddings": embeddings
26
+ }
27
+
28
+ demo = gr.Interface(
29
+ fn=encode_audio,
30
+ inputs=gr.Audio(type="numpy", label="Upload audio"),
31
+ outputs=gr.JSON(label="Embeddings")
32
+ )
33
+
34
+ demo.launch()