404Brain-Not-Found-yeah
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import joblib
|
3 |
+
import numpy as np
|
4 |
+
from predict import extract_features
|
5 |
+
import os
|
6 |
+
import tempfile
|
7 |
+
from huggingface_hub import hf_hub_download
|
8 |
+
import logging
|
9 |
+
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(
|
12 |
+
level=logging.INFO,
|
13 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
14 |
+
)
|
15 |
+
logger = logging.getLogger(__name__)
|
16 |
+
|
17 |
+
# Page configuration
|
18 |
+
st.set_page_config(
|
19 |
+
page_title="Healing Music Classifier",
|
20 |
+
page_icon="🎵",
|
21 |
+
layout="centered"
|
22 |
+
)
|
23 |
+
|
24 |
+
@st.cache_resource
|
25 |
+
def load_model():
|
26 |
+
"""Load model from Hugging Face Hub"""
|
27 |
+
try:
|
28 |
+
logger.info("Downloading model from Hugging Face Hub...")
|
29 |
+
model_path = hf_hub_download(
|
30 |
+
repo_id="404Brain-Not-Found-yeah/healing-music-classifier",
|
31 |
+
filename="models/model.joblib"
|
32 |
+
)
|
33 |
+
scaler_path = hf_hub_download(
|
34 |
+
repo_id="404Brain-Not-Found-yeah/healing-music-classifier",
|
35 |
+
filename="models/scaler.joblib"
|
36 |
+
)
|
37 |
+
|
38 |
+
logger.info("Loading model and scaler...")
|
39 |
+
return joblib.load(model_path), joblib.load(scaler_path)
|
40 |
+
except Exception as e:
|
41 |
+
logger.error(f"Error loading model: {str(e)}")
|
42 |
+
return None, None
|
43 |
+
|
44 |
+
def main():
|
45 |
+
st.title("🎵 Healing Music Classifier")
|
46 |
+
st.write("""
|
47 |
+
Upload your music file, and AI will analyze its healing potential!
|
48 |
+
Supports mp3, wav formats.
|
49 |
+
""")
|
50 |
+
|
51 |
+
# Add file upload component
|
52 |
+
uploaded_file = st.file_uploader("Choose an audio file...", type=['mp3', 'wav'])
|
53 |
+
|
54 |
+
if uploaded_file is not None:
|
55 |
+
# Create progress bar
|
56 |
+
progress_bar = st.progress(0)
|
57 |
+
status_text = st.empty()
|
58 |
+
|
59 |
+
try:
|
60 |
+
# Create temporary file
|
61 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
|
62 |
+
# Write uploaded file content
|
63 |
+
tmp_file.write(uploaded_file.getvalue())
|
64 |
+
tmp_file_path = tmp_file.name
|
65 |
+
|
66 |
+
# Update status
|
67 |
+
status_text.text("Analyzing music...")
|
68 |
+
progress_bar.progress(30)
|
69 |
+
|
70 |
+
# Load model
|
71 |
+
model, scaler = load_model()
|
72 |
+
if model is None or scaler is None:
|
73 |
+
st.error("Model loading failed. Please try again later.")
|
74 |
+
return
|
75 |
+
|
76 |
+
progress_bar.progress(50)
|
77 |
+
|
78 |
+
# Extract features
|
79 |
+
features = extract_features(tmp_file_path)
|
80 |
+
if features is None:
|
81 |
+
st.error("Failed to extract audio features. Please ensure the file is a valid audio file.")
|
82 |
+
return
|
83 |
+
|
84 |
+
progress_bar.progress(70)
|
85 |
+
|
86 |
+
# Predict
|
87 |
+
scaled_features = scaler.transform([features])
|
88 |
+
healing_probability = model.predict_proba(scaled_features)[0][1]
|
89 |
+
progress_bar.progress(90)
|
90 |
+
|
91 |
+
# Display results
|
92 |
+
st.subheader("Analysis Results")
|
93 |
+
|
94 |
+
# Create visualization progress bar
|
95 |
+
healing_percentage = healing_probability * 100
|
96 |
+
st.progress(healing_probability)
|
97 |
+
|
98 |
+
# Display percentage
|
99 |
+
st.write(f"Healing Index: {healing_percentage:.1f}%")
|
100 |
+
|
101 |
+
# Provide explanation
|
102 |
+
if healing_percentage >= 75:
|
103 |
+
st.success("This music has strong healing properties! 🌟")
|
104 |
+
elif healing_percentage >= 50:
|
105 |
+
st.info("This music has moderate healing effects. ✨")
|
106 |
+
else:
|
107 |
+
st.warning("This music has limited healing potential. 🎵")
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
st.error(f"An unexpected error occurred: {str(e)}")
|
111 |
+
logger.exception("Unexpected error")
|
112 |
+
|
113 |
+
finally:
|
114 |
+
# Clean up temporary file
|
115 |
+
try:
|
116 |
+
if 'tmp_file_path' in locals() and os.path.exists(tmp_file_path):
|
117 |
+
os.unlink(tmp_file_path)
|
118 |
+
except Exception as e:
|
119 |
+
logger.error(f"Failed to clean up temporary file: {str(e)}")
|
120 |
+
|
121 |
+
# Complete progress bar
|
122 |
+
progress_bar.progress(100)
|
123 |
+
status_text.text("Analysis complete!")
|
124 |
+
|
125 |
+
if __name__ == "__main__":
|
126 |
+
main()
|