birgermoell commited on
Commit
9b1b90d
·
verified ·
1 Parent(s): ecedb67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -60
app.py CHANGED
@@ -1,61 +1,21 @@
1
- import streamlit as st
2
- import os
3
- import base64
4
- import uuid
5
 
6
- st.title("Record Audio in Browser")
7
-
8
- # JavaScript to record audio
9
- audio_recorder_js = """
10
- <script>
11
- let mediaRecorder;
12
- let audioChunks = [];
13
-
14
- function startRecording() {
15
- navigator.mediaDevices.getUserMedia({ audio: true })
16
- .then(stream => {
17
- mediaRecorder = new MediaRecorder(stream);
18
- mediaRecorder.ondataavailable = event => {
19
- audioChunks.push(event.data);
20
- };
21
- mediaRecorder.onstop = () => {
22
- const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
23
- const reader = new FileReader();
24
- reader.readAsDataURL(audioBlob);
25
- reader.onloadend = () => {
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
+ )