mistermprah commited on
Commit
f9dbd70
1 Parent(s): 5f14ed7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -85
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
3
  from config import MODEL_ID
4
 
5
  # Load the model and pipeline using the model_id variable
@@ -7,68 +8,14 @@ pipe = pipeline("audio-classification", model=MODEL_ID)
7
 
8
  def classify_audio(filepath):
9
  preds = pipe(filepath)
10
- outputs = {"normal": 0.0, "murmur": 0.0, "artifact": 0.0}
11
  for p in preds:
12
- label = p["label"].replace('_', ' ')
13
- if label in outputs:
14
- outputs[label] += p["score"]
15
- else:
16
- outputs["normal"] += p["score"]
17
  return outputs
18
 
19
  # Streamlit app layout
20
  st.title("Heartbeat Sound Classification")
21
 
22
- # Theme selection
23
- theme = st.sidebar.selectbox(
24
- "Select Theme",
25
- ["Light Green", "Light Blue"]
26
- )
27
-
28
- # Add custom CSS for styling based on the selected theme
29
- if theme == "Light Green":
30
- st.markdown(
31
- """
32
- <style>
33
- body, .stApp {
34
- background-color: #e8f5e9; /* Light green background */
35
- }
36
- .stApp {
37
- color: #004d40; /* Dark green text */
38
- }
39
- .stButton > button, .stFileUpload > div {
40
- background-color: #004d40; /* Dark green button and file uploader background */
41
- color: white; /* White text */
42
- }
43
- .stButton > button:hover, .stFileUpload > div:hover {
44
- background-color: #00332c; /* v Darker green on hover */
45
- }
46
- </style>
47
- """,
48
- unsafe_allow_html=True
49
- )
50
- elif theme == "Light Blue":
51
- st.markdown(
52
- """
53
- <style>
54
- body, .stApp {
55
- background-color: #e0f7fa; /* Light blue background */
56
- }
57
- .stApp {
58
- color: #006064; /* Dark blue text */
59
- }
60
- .stButton > button, .stFileUpload > div {
61
- background-color: #006064; /* Dark blue button and file uploader background */
62
- color: white; /* White text */
63
- }
64
- .stButton > button:hover, .stFileUpload > div:hover {
65
- background-color: #004d40; /* Darker blue on hover */
66
- }
67
- </style>
68
- """,
69
- unsafe_allow_html=True
70
- )
71
-
72
  # File uploader for audio files
73
  uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
74
 
@@ -92,36 +39,15 @@ if uploaded_file is not None:
92
  results_str = "\n".join([f"{label}: {score:.2f}" for label, score in results.items()])
93
  results_box.text(results_str)
94
 
95
- # Audio Test Samples for classification
96
- st.write("Audio Test Samples:")
97
-
98
  examples = ['normal.wav', 'murmur.wav', 'extra_systole.wav', 'extra_hystole.wav', 'artifact.wav']
99
-
100
- # Determine the number of columns based on the screen size
101
- is_mobile = st.session_state.get("is_mobile", False)
102
- num_columns = 1 if is_mobile else 3
103
-
104
- # Arrange buttons in the columns
105
- cols = st.columns(num_columns)
106
-
107
- for idx, example in enumerate(examples):
108
- col = cols[idx % num_columns] # Rotate columns for better arrangement
109
- if col.button(example):
110
- col.subheader(f"Sample Audio: {example}")
111
  audio_bytes = open(example, 'rb').read()
112
- col.audio(audio_bytes, format='audio/wav')
113
  results = classify_audio(example)
114
- col.write("Results:")
115
  results_str = "\n".join([f"{label}: {score:.2f}" for label, score in results.items()])
116
- col.text(results_str)
117
-
118
- # JavaScript to detect if the user is on a mobile device
119
- st.markdown(
120
- """
121
- <script>
122
- const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
123
- window.parent.postMessage({type: 'streamlit:storeSessionState', key: 'is_mobile', value: isMobile}, '*');
124
- </script>
125
- """,
126
- unsafe_allow_html=True
127
- )
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ import torchaudio
4
  from config import MODEL_ID
5
 
6
  # Load the model and pipeline using the model_id variable
 
8
 
9
  def classify_audio(filepath):
10
  preds = pipe(filepath)
11
+ outputs = {}
12
  for p in preds:
13
+ outputs[p["label"].replace('_', ' ')] = p["score"]
 
 
 
 
14
  return outputs
15
 
16
  # Streamlit app layout
17
  st.title("Heartbeat Sound Classification")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # File uploader for audio files
20
  uploaded_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
21
 
 
39
  results_str = "\n".join([f"{label}: {score:.2f}" for label, score in results.items()])
40
  results_box.text(results_str)
41
 
42
+ # Sample Audio Files for classification
43
+ st.write("Sample Audio Files:")
 
44
  examples = ['normal.wav', 'murmur.wav', 'extra_systole.wav', 'extra_hystole.wav', 'artifact.wav']
45
+ for example in examples:
46
+ if st.button(example):
47
+ st.subheader(f"Sample Audio: {example}")
 
 
 
 
 
 
 
 
 
48
  audio_bytes = open(example, 'rb').read()
49
+ st.audio(audio_bytes, format='audio/wav')
50
  results = classify_audio(example)
51
+ st.write("Results:")
52
  results_str = "\n".join([f"{label}: {score:.2f}" for label, score in results.items()])
53
+ st.text(results_str)