File size: 10,647 Bytes
cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 e1040b8 cceaf01 031d9dd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
import gradio as gr
import os
from predict import predict_healing_music
import train_model
import logging
import tempfile
import time
import shutil
import socket
import joblib
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def find_free_port(start_port=7860, max_port=7960):
"""Find a free port in the given range."""
for port in range(start_port, max_port + 1):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind(('', port))
return port
except OSError:
continue
return None
# Ensure model directory exists
model_dir = os.path.join(os.path.dirname(__file__), "models")
os.makedirs(model_dir, exist_ok=True)
# Model file paths
model_path = os.path.join(model_dir, "model.joblib")
scaler_path = os.path.join(model_dir, "scaler.joblib")
# Check if model exists
if not os.path.exists(model_path) or not os.path.exists(scaler_path):
print('First run: Training the model...')
try:
train_model.train_and_evaluate_model()
print('Model training completed!')
except Exception as e:
print(f'Model training failed: {str(e)}')
raise e
def process_audio(audio_path):
"""
Process and analyze the audio file
"""
if audio_path is None:
return None, None, None, "Please upload an audio file"
model_dir = os.path.join(os.path.dirname(__file__), "models")
model_path = os.path.join(model_dir, "model.joblib")
scaler_path = os.path.join(model_dir, "scaler.joblib")
try:
# Load model and scaler
model = joblib.load(model_path)
scaler = joblib.load(scaler_path)
tmp_file = None
try:
# Create temporary file
suffix = os.path.splitext(audio_path)[1]
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
shutil.copy2(audio_path, tmp_file.name)
# Make prediction
healing_probability = predict_healing_music(tmp_file.name)
if healing_probability is not None:
# Calculate percentage
healing_percentage = healing_probability * 100
# Generate description
if healing_percentage >= 75:
description = "This music has strong healing properties! ✨"
color = "#15803d" # Dark green
elif healing_percentage >= 50:
description = "This music has moderate healing effects. 🌟"
color = "#0369a1" # Dark blue
else:
description = "This music has limited healing potential. 🎵"
color = "#b91c1c" # Dark red
return f"{healing_percentage:.1f}%", f'<div style="background-color: {color}; color: white; padding: 1rem; border-radius: 8px; text-align: center;">{description}</div>', None, None
else:
return "Error", "Error analyzing file. Please ensure it's a valid MP3 or WAV file.", None, None
except Exception as e:
logger.error(f"Error during analysis: {str(e)}")
return "Error", f"An unexpected error occurred: {str(e)}", None, None
finally:
# Clean up temporary file
if tmp_file is not None:
try:
tmp_file.close()
os.unlink(tmp_file.name)
except Exception as e:
logger.error(f"Failed to clean up temporary file: {str(e)}")
except Exception as e:
logger.error(f"Error during model loading: {str(e)}")
return "Error", f"An unexpected error occurred: {str(e)}", None, None
def analyze_audio(audio):
"""Analyze the audio file"""
try:
if audio is None:
return [
gr.update(visible=False), # results
gr.update(visible=False), # analyzing
"", # healing_index
"" # result_text
]
# Show analyzing status first
yield [
gr.update(visible=False), # results
gr.update(visible=True), # analyzing
"", # healing_index
"" # result_text
]
# Process audio and get results
index, desc, _, _ = process_audio(audio)
desc_with_hint = f'{desc}<div style="margin-top: 1rem; color: #9ca3af; font-size: 0.9rem;">To analyze another file, please refresh the page</div>'
# Return final results
yield [
gr.update(visible=True), # results
gr.update(visible=False), # analyzing
index, # healing_index
desc_with_hint # result_text
]
except Exception as e:
logger.error(f"Error in analyze_audio: {str(e)}")
yield [
gr.update(visible=True), # results
gr.update(visible=False), # analyzing
"Error", # healing_index
f"An error occurred: {str(e)}" # result_text
]
# Custom CSS styles
custom_css = """
.gradio-container {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
max-width: 800px !important;
margin: auto;
padding: 0 1rem;
background-color: #0f1117;
}
.container {
max-width: 700px;
margin: 0 auto;
padding-top: 2rem;
}
.header {
text-align: center;
margin-bottom: 1.5rem;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.title {
font-size: 2.8rem !important;
font-weight: 800 !important;
color: #ffffff !important;
margin: 0 !important;
line-height: 1.2 !important;
text-align: center !important;
letter-spacing: 0.05em !important;
}
.subtitle {
font-size: 1.4rem !important;
text-align: center;
color: #ffffff !important;
margin-top: 1rem !important;
max-width: 800px;
margin-left: auto;
margin-right: auto;
white-space: nowrap !important;
font-weight: 500 !important;
letter-spacing: 0.02em !important;
}
.upload-box {
background-color: #1f2937;
border-radius: 12px;
padding: 2rem;
margin-bottom: 1rem;
border: 2px dashed #6b7280;
transition: all 0.3s ease;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
.upload-area {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 1rem;
padding: 1.5rem 0;
}
.icon-text-container {
color: #ffffff;
font-size: 1.3rem !important;
font-weight: 600 !important;
letter-spacing: 0.02em !important;
}
.upload-hint {
color: #ffffff !important;
font-size: 1rem !important;
margin-top: 0.5rem !important;
font-style: italic !important;
font-weight: 500 !important;
}
.analyzing-status {
margin: 1rem 0;
background-color: #1f2937;
border-radius: 12px;
padding: 1.5rem;
text-align: center;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
.analyzing-text {
color: #ffffff !important;
font-size: 1.4rem !important;
font-weight: 600 !important;
margin: 0 !important;
letter-spacing: 0.02em !important;
}
.results-container {
background-color: #1f2937;
border-radius: 12px;
padding: 1.5rem;
margin-top: 1rem;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
.result-title {
color: #ffffff !important;
font-size: 1.6rem !important;
font-weight: 700 !important;
margin-bottom: 1rem !important;
letter-spacing: 0.02em !important;
}
.healing-index {
font-size: 3rem !important;
font-weight: 800 !important;
text-align: center;
color: #ffffff !important;
margin: 1rem 0 !important;
letter-spacing: 0.05em !important;
}
.result-text {
color: #ffffff !important;
font-size: 1.2rem !important;
font-weight: 600 !important;
letter-spacing: 0.02em !important;
line-height: 1.5 !important;
padding: 1rem !important;
border-radius: 8px !important;
}
"""
# Create Gradio interface
with gr.Blocks(
title="Healing Music Classifier",
css=custom_css,
theme=gr.themes.Default()
) as demo:
with gr.Column(elem_classes="container"):
with gr.Row(elem_classes="header"):
gr.Markdown("🎵 Healing Music Classifier", elem_classes="title")
gr.Markdown(
"Upload your music file, and our model will analyze its healing potential!",
elem_classes="subtitle"
)
with gr.Column(elem_classes="upload-box"):
with gr.Column(elem_classes="upload-area"):
gr.Markdown("☁️ Drop your audio file here", elem_classes="icon-text-container")
audio_input = gr.Audio(
label="Audio Input",
sources=["upload"],
type="filepath",
elem_classes="audio-input",
interactive=True,
label_visible=False
)
gr.Markdown("Limit 200MB per file • MP3, WAV", elem_classes="upload-hint")
with gr.Column(elem_classes="analyzing-status", visible=False) as analyzing:
gr.Markdown(
"""<div style="display: flex; align-items: center; justify-content: center; gap: 0.5rem;">
<div class="loading-spinner"></div>
<span style="color: #60a5fa;">Analyzing your music...</span>
</div>""",
elem_classes="analyzing-text"
)
with gr.Column(elem_classes="results-container", visible=False) as results:
gr.Markdown("Analysis Results", elem_classes="result-title")
healing_index = gr.Markdown("", elem_classes="healing-index")
result_text = gr.Markdown("", elem_classes="result-text")
# Audio analysis event
audio_input.upload(
fn=analyze_audio,
inputs=[audio_input],
outputs=[
results,
analyzing,
healing_index,
result_text
],
queue=True
)
# Enable queue for the entire app
demo.queue()
# Launch application
if __name__ == "__main__":
demo.launch()
|