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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -61
app.py CHANGED
@@ -1,63 +1,61 @@
1
  import streamlit as st
2
- import torch
3
- import tempfile
4
  import os
5
- from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
6
- from audiorecorder import audiorecorder
7
- from pydub import AudioSegment
8
-
9
- # Setup model
10
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
11
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
12
- model_id = "KBLab/kb-whisper-tiny"
13
-
14
- @st.cache_resource
15
- def load_model():
16
- model = AutoModelForSpeechSeq2Seq.from_pretrained(
17
- model_id, torch_dtype=torch_dtype, use_safetensors=True, cache_dir="cache"
18
- )
19
- model.to(device)
20
- processor = AutoProcessor.from_pretrained(model_id)
21
- return pipeline(
22
- "automatic-speech-recognition",
23
- model=model,
24
- tokenizer=processor.tokenizer,
25
- feature_extractor=processor.feature_extractor,
26
- torch_dtype=torch_dtype,
27
- device=device,
28
- )
29
-
30
- pipe = load_model()
31
-
32
- def transcribe_audio(audio_path):
33
- return pipe(audio_path, chunk_length_s=30, generate_kwargs={"task": "transcribe", "language": "sv"})
34
-
35
- st.title("Speech-to-Text Transcription")
36
-
37
- # Audio recording
38
- st.subheader("Record Audio")
39
- recorded_audio = audiorecorder("Start Recording", "Stop Recording")
40
-
41
- if recorded_audio:
42
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
43
- temp_file.write(recorded_audio.tobytes())
44
- temp_file_path = temp_file.name
45
- st.audio(temp_file_path, format="audio/wav")
46
- result = transcribe_audio(temp_file_path)
47
- st.write("### Transcription:")
48
- st.write(result["text"])
49
- os.remove(temp_file_path)
50
-
51
- # File upload
52
- st.subheader("Upload Audio File")
53
- uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3", "ogg", "flac"])
54
-
55
- if uploaded_file:
56
- with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[-1]) as temp_file:
57
- temp_file.write(uploaded_file.read())
58
- temp_file_path = temp_file.name
59
- st.audio(temp_file_path)
60
- result = transcribe_audio(temp_file_path)
61
- st.write("### Transcription:")
62
- st.write(result["text"])
63
- os.remove(temp_file_path)
 
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}")