azeus commited on
Commit
7b63dbd
·
1 Parent(s): d57f6a5

Add application file

Browse files
Files changed (1) hide show
  1. app.py +25 -91
app.py CHANGED
@@ -1,10 +1,7 @@
1
  import streamlit as st
2
  import numpy as np
3
  import pandas as pd
4
- import plotly.express as px
5
- from scipy import signal
6
  import io
7
- import soundfile as sf
8
 
9
  # Page config
10
  st.set_page_config(page_title="AI Music Genre Classifier", layout="wide")
@@ -14,101 +11,38 @@ if 'history' not in st.session_state:
14
  st.session_state.history = []
15
 
16
 
17
- def process_audio(audio_bytes):
18
- """Process audio file using scipy instead of librosa"""
19
- try:
20
- # Load audio using soundfile
21
- audio_io = io.BytesIO(audio_bytes)
22
- data, samplerate = sf.read(audio_io)
23
-
24
- # If stereo, convert to mono
25
- if len(data.shape) > 1:
26
- data = np.mean(data, axis=1)
27
-
28
- return data, samplerate
29
- except Exception as e:
30
- st.error(f"Error processing audio: {str(e)}")
31
- return None, None
32
-
33
-
34
- def plot_waveform(data, samplerate):
35
- """Generate waveform visualization"""
36
- time = np.arange(len(data)) / samplerate
37
- fig = px.line(x=time, y=data)
38
- fig.update_layout(
39
- title="Audio Waveform",
40
- xaxis_title="Time (s)",
41
- yaxis_title="Amplitude",
42
- showlegend=False
43
- )
44
- return fig
45
-
46
-
47
- def plot_spectrum(data, samplerate):
48
- """Generate frequency spectrum visualization"""
49
- frequencies, times, spectrogram = signal.spectrogram(data, samplerate)
50
-
51
- fig = px.imshow(
52
- 10 * np.log10(spectrogram + 1e-10), # Convert to dB
53
- aspect='auto',
54
- origin='lower',
55
- labels={'x': 'Time Frame', 'y': 'Frequency Bin'}
56
- )
57
- fig.update_layout(title="Frequency Spectrum")
58
- return fig
59
-
60
-
61
  def main():
62
  st.title("🎵 AI Music Genre Classifier")
63
 
64
- # Sidebar
65
- st.sidebar.header("Controls")
66
- confidence_threshold = st.sidebar.slider("Confidence Threshold", 0.0, 1.0, 0.5)
67
-
68
  # File uploader
69
  audio_file = st.file_uploader("Upload an audio file (MP3, WAV)", type=['mp3', 'wav'])
70
 
71
  if audio_file is not None:
72
- # Read file bytes
73
- audio_bytes = audio_file.read()
74
- data, samplerate = process_audio(audio_bytes)
75
-
76
- if data is not None:
77
- # Create two columns for visualizations
78
- col1, col2 = st.columns(2)
79
-
80
- with col1:
81
- st.plotly_chart(plot_waveform(data, samplerate), use_container_width=True)
82
-
83
- with col2:
84
- st.plotly_chart(plot_spectrum(data, samplerate), use_container_width=True)
85
-
86
- # Simulate genre classification (replace with actual model)
87
- genres = ['Rock', 'Pop', 'Hip Hop', 'Classical', 'Jazz']
88
- predictions = np.random.dirichlet(np.ones(len(genres)))
89
-
90
- # Display predictions
91
- st.header("Genre Predictions:")
92
-
93
- # Create bar chart of predictions
94
- fig = px.bar(
95
- x=genres,
96
- y=predictions,
97
- labels={'x': 'Genre', 'y': 'Confidence'},
98
- title="Genre Confidence Scores"
99
- )
100
- st.plotly_chart(fig, use_container_width=True)
101
-
102
- # Save to history
103
- top_genre = genres[np.argmax(predictions)]
104
- confidence = np.max(predictions)
105
-
106
- if confidence >= confidence_threshold:
107
- st.session_state.history.append({
108
- 'File': audio_file.name,
109
- 'Predicted Genre': top_genre,
110
- 'Confidence': f"{confidence:.2%}"
111
- })
112
 
113
  # History section
114
  if st.session_state.history:
 
1
  import streamlit as st
2
  import numpy as np
3
  import pandas as pd
 
 
4
  import io
 
5
 
6
  # Page config
7
  st.set_page_config(page_title="AI Music Genre Classifier", layout="wide")
 
11
  st.session_state.history = []
12
 
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def main():
15
  st.title("🎵 AI Music Genre Classifier")
16
 
 
 
 
 
17
  # File uploader
18
  audio_file = st.file_uploader("Upload an audio file (MP3, WAV)", type=['mp3', 'wav'])
19
 
20
  if audio_file is not None:
21
+ st.audio(audio_file)
22
+
23
+ # Simulate genre classification
24
+ genres = ['Rock', 'Pop', 'Hip Hop', 'Classical', 'Jazz']
25
+ predictions = np.random.dirichlet(np.ones(len(genres)))
26
+
27
+ # Display predictions
28
+ st.header("Genre Predictions:")
29
+
30
+ # Create simple bar chart using streamlit
31
+ genre_df = pd.DataFrame({
32
+ 'Genre': genres,
33
+ 'Confidence': predictions
34
+ })
35
+ st.bar_chart(genre_df.set_index('Genre'))
36
+
37
+ # Save to history
38
+ top_genre = genres[np.argmax(predictions)]
39
+ confidence = np.max(predictions)
40
+
41
+ st.session_state.history.append({
42
+ 'File': audio_file.name,
43
+ 'Predicted Genre': top_genre,
44
+ 'Confidence': f"{confidence:.2%}"
45
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  # History section
48
  if st.session_state.history: