Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,21 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
import base64
|
4 |
-
import uuid
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
const base64AudioMessage = reader.result.split(',')[1];
|
27 |
-
fetch('/save_audio', {
|
28 |
-
method: 'POST',
|
29 |
-
body: JSON.stringify({ audio: base64AudioMessage }),
|
30 |
-
headers: { 'Content-Type': 'application/json' }
|
31 |
-
}).then(response => response.json()).then(data => {
|
32 |
-
console.log(data);
|
33 |
-
});
|
34 |
-
};
|
35 |
-
};
|
36 |
-
mediaRecorder.start();
|
37 |
-
});
|
38 |
-
}
|
39 |
-
|
40 |
-
function stopRecording() {
|
41 |
-
mediaRecorder.stop();
|
42 |
-
}
|
43 |
-
</script>
|
44 |
-
|
45 |
-
<button onclick="startRecording()">Start Recording</button>
|
46 |
-
<button onclick="stopRecording()">Stop Recording</button>
|
47 |
-
"""
|
48 |
-
|
49 |
-
st.components.v1.html(audio_recorder_js)
|
50 |
-
|
51 |
-
# Backend to save audio
|
52 |
-
if "audio_data" not in st.session_state:
|
53 |
-
st.session_state["audio_data"] = None
|
54 |
-
|
55 |
-
if st.session_state["audio_data"]:
|
56 |
-
audio_bytes = base64.b64decode(st.session_state["audio_data"])
|
57 |
-
file_name = f"recording_{uuid.uuid4()}.wav"
|
58 |
-
with open(file_name, "wb") as f:
|
59 |
-
f.write(audio_bytes)
|
60 |
-
st.audio(file_name, format="audio/wav")
|
61 |
-
st.success(f"Audio saved as {file_name}")
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
# Setup model
|
3 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
4 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
5 |
+
model_id = "KBLab/kb-whisper-tiny"
|
6 |
+
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
10 |
+
model_id, torch_dtype=torch_dtype, use_safetensors=True, cache_dir="cache"
|
11 |
+
)
|
12 |
+
model.to(device)
|
13 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
14 |
+
return pipeline(
|
15 |
+
"automatic-speech-recognition",
|
16 |
+
model=model,
|
17 |
+
tokenizer=processor.tokenizer,
|
18 |
+
feature_extractor=processor.feature_extractor,
|
19 |
+
torch_dtype=torch_dtype,
|
20 |
+
device=device,
|
21 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|