Syed-Adnan commited on
Commit
67ec601
Β·
verified Β·
1 Parent(s): 512f9fe

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +43 -18
src/streamlit_app.py CHANGED
@@ -1,33 +1,58 @@
1
  import streamlit as st
2
- # ... other imports (wav, tempfile, etc.)
 
3
 
4
- # --- Attempt to import audio libraries and set a flag ---
5
  try:
6
- # This is the line that is currently crashing your app
7
  import sounddevice as sd
 
8
  AUDIO_RECORDER_AVAILABLE = True
9
  except OSError as e:
10
- # If it fails, we catch the error and set a flag
11
- st.warning(f"Audio recording hardware not found or library failed. Recording is disabled. Error: {e}")
12
  AUDIO_RECORDER_AVAILABLE = False
13
  except Exception as e:
14
- st.warning(f"An unexpected error occurred with the audio library. Recording is disabled: {e}")
15
  AUDIO_RECORDER_AVAILABLE = False
16
 
17
- # ... (rest of your script) ...
18
-
19
  # ------------------ STREAMLIT UI ------------------
20
  st.title("πŸ—£οΈ Jugaadu Audio Collector")
 
 
 
 
21
 
22
- # ... (your markdown description) ...
23
-
24
- # --- Conditionally show the recording UI based on the flag ---
25
  if AUDIO_RECORDER_AVAILABLE:
26
- duration = st.slider("πŸŽ™οΈ Select recording duration (seconds)", 5, 300, 30)
27
- if st.button("πŸ”΄ Start Recording", type="primary"):
28
- # ... (all your recording and processing logic) ...
29
- pass # placeholder for your logic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  else:
31
- # This message will be displayed instead of the app crashing
32
- st.error("πŸ”΄ Audio recording is not available on this server environment.")
33
- st.info("This is expected on some cloud platforms. If you need to record, please run the app on your local computer.")
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import tempfile
3
+ import os
4
 
5
+ # --- Try importing sounddevice ---
6
  try:
 
7
  import sounddevice as sd
8
+ from scipy.io.wavfile import write
9
  AUDIO_RECORDER_AVAILABLE = True
10
  except OSError as e:
11
+ st.warning(f"Audio recording library failed (likely missing PortAudio). Recording disabled.\n\nError: {e}")
 
12
  AUDIO_RECORDER_AVAILABLE = False
13
  except Exception as e:
14
+ st.warning(f"Unexpected error loading audio recorder: {e}")
15
  AUDIO_RECORDER_AVAILABLE = False
16
 
 
 
17
  # ------------------ STREAMLIT UI ------------------
18
  st.title("πŸ—£οΈ Jugaadu Audio Collector")
19
+ st.markdown("""
20
+ This tool helps collect local voice inputs to build regional datasets.
21
+ If your system supports it, you can record directly. If not, you can upload `.wav`, `.mp3`, or `.m4a` files instead.
22
+ """)
23
 
24
+ # -------------- RECORD AUDIO ------------------
 
 
25
  if AUDIO_RECORDER_AVAILABLE:
26
+ duration = st.slider("πŸŽ™οΈ Select recording duration (seconds)", 5, 60, 10)
27
+ if st.button("πŸ”΄ Start Recording"):
28
+ try:
29
+ st.info("Recording...")
30
+ fs = 44100 # Sample rate
31
+ recording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
32
+ sd.wait() # Wait until recording is finished
33
+
34
+ # Save to temp file
35
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
36
+ write(tmpfile.name, fs, recording)
37
+ st.success("βœ… Recording complete!")
38
+ st.audio(tmpfile.name, format='audio/wav')
39
+
40
+ # If you want to save/upload, do it here
41
+ st.info(f"Saved to: `{tmpfile.name}`")
42
+ except Exception as e:
43
+ st.error(f"Recording failed: {e}")
44
  else:
45
+ st.error("πŸ”΄ Audio recording is not supported in this environment.")
46
+ st.info("Try running this app on your local computer to enable recording.")
47
+
48
+ # -------------- UPLOAD FALLBACK ------------------
49
+ st.markdown("---")
50
+ st.subheader("πŸ“€ Or Upload Audio")
51
+ uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3", "m4a"])
52
+
53
+ if uploaded_file is not None:
54
+ st.success("βœ… File uploaded successfully!")
55
+ st.audio(uploaded_file, format='audio/wav')
56
+ # You can save to disk or process further here
57
+ # with open("some_path.wav", "wb") as f:
58
+ # f.write(uploaded_file.getbuffer())