File size: 11,181 Bytes
3f50570 11f59f6 3f50570 11f59f6 3f50570 be3ce29 3f50570 be3ce29 3f50570 be3ce29 3f50570 be3ce29 3f50570 be3ce29 3f50570 be3ce29 11f59f6 3f50570 be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 3f50570 c4c4ffa be3ce29 3f50570 c4c4ffa 3f50570 be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 3f50570 c4c4ffa 3f50570 be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 d714fce be3ce29 c4c4ffa d714fce c4c4ffa be3ce29 3f50570 11f59f6 3f50570 be3ce29 d714fce be3ce29 3f50570 11f59f6 c4c4ffa be3ce29 c4c4ffa 11f59f6 be3ce29 3f50570 c4c4ffa be3ce29 3f50570 be3ce29 3f50570 be3ce29 11f59f6 c4c4ffa be3ce29 c4c4ffa 11f59f6 be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 c4c4ffa be3ce29 3f50570 be3ce29 11f59f6 c4c4ffa 11f59f6 |
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 333 334 335 |
import os
import torch
import librosa
import numpy as np
import gradio as gr
from sonics import HFAudioClassifier
# Model configurations
MODEL_IDS = {
"SpecTTTra-α (5s)": "awsaf49/sonics-spectttra-alpha-5s",
"SpecTTTra-β (5s)": "awsaf49/sonics-spectttra-beta-5s",
"SpecTTTra-γ (5s)": "awsaf49/sonics-spectttra-gamma-5s",
"SpecTTTra-α (120s)": "awsaf49/sonics-spectttra-alpha-120s",
"SpecTTTra-β (120s)": "awsaf49/sonics-spectttra-beta-120s",
"SpecTTTra-γ (120s)": "awsaf49/sonics-spectttra-gamma-120s",
}
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_cache = {}
def load_model(model_name):
"""Load model if not already cached"""
if model_name not in model_cache:
model_id = MODEL_IDS[model_name]
model = HFAudioClassifier.from_pretrained(model_id)
model = model.to(device)
model.eval()
model_cache[model_name] = model
return model_cache[model_name]
def process_audio(audio_path, model_name):
"""Process audio file and return prediction"""
try:
model = load_model(model_name)
max_time = model.config.audio.max_time
# Load and process audio
audio, sr = librosa.load(audio_path, sr=16000)
chunk_samples = int(max_time * sr)
total_chunks = len(audio) // chunk_samples
middle_chunk_idx = total_chunks // 2
# Extract middle chunk
start = middle_chunk_idx * chunk_samples
end = start + chunk_samples
chunk = audio[start:end]
if len(chunk) < chunk_samples:
chunk = np.pad(chunk, (0, chunk_samples - len(chunk)))
# Get prediction
with torch.no_grad():
chunk = torch.from_numpy(chunk).float().to(device)
pred = model(chunk.unsqueeze(0))
prob = torch.sigmoid(pred).cpu().numpy()[0]
real_prob = 1 - prob
fake_prob = prob
# Return formatted results
return {
"Real": float(real_prob),
"Fake": float(fake_prob)
}
except Exception as e:
return {"Error": str(e)}
def predict(audio_file, model_name):
"""Gradio interface function"""
if audio_file is None:
return {"Message": "Please upload an audio file"}
return process_audio(audio_file, model_name)
# Custom CSS for styling - Dark theme with black background
css = """
:root {
--primary-bg: #000000;
--secondary-bg: #111111;
--panel-bg: #1e1e1e;
--text-color: #ffffff;
--text-secondary: #bbbbbb;
--border-color: #333333;
--analyze-button-color: #ffa500;
--analyze-button-hover: #ff8c00;
--accent-color: #4a78e5;
}
body, .gradio-container {
background-color: var(--primary-bg) !important;
color: var(--text-color) !important;
}
.footer, .header-container, .accordion-content {
background-color: var(--secondary-bg) !important;
color: var(--text-color) !important;
}
/* Headers and text */
h1, h2, h3 {
color: var(--text-color) !important;
}
p {
color: var(--text-secondary) !important;
}
/* Button styling */
button#submit_btn {
background-color: var(--analyze-button-color) !important;
color: white !important;
border: none !important;
font-weight: bold !important;
padding: 10px 20px !important;
font-size: 16px !important;
border-radius: 8px !important;
}
button#submit_btn:hover {
background-color: var(--analyze-button-hover) !important;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5) !important;
transform: translateY(-2px) !important;
transition: all 0.2s ease !important;
}
/* Panel backgrounds */
.gr-panel, .gr-box, .gr-form, .gr-input-label, .gr-input {
background-color: var(--panel-bg) !important;
border: 1px solid var(--border-color) !important;
border-radius: 8px !important;
color: var(--text-color) !important;
}
/* Results panel */
#output {
background-color: var(--panel-bg) !important;
border-radius: 8px !important;
padding: 10px !important;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3) !important;
border: 1px solid var(--border-color) !important;
}
/* Resource links */
.resource-link {
background-color: var(--secondary-bg) !important;
color: var(--accent-color) !important;
border: 1px solid var(--border-color) !important;
padding: 8px 16px !important;
border-radius: 20px !important;
margin: 5px !important;
text-decoration: none !important;
display: inline-block !important;
font-weight: 500 !important;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3) !important;
}
.resource-link:hover {
transform: translateY(-2px) !important;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.4) !important;
transition: all 0.2s ease !important;
background-color: #222222 !important;
}
.header-container {
padding: 20px !important;
border-radius: 10px !important;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3) !important;
margin-bottom: 20px !important;
border: 1px solid var(--border-color) !important;
}
/* Accordion styling */
.gr-accordion {
border: 1px solid var(--border-color) !important;
border-radius: 8px !important;
overflow: hidden !important;
background-color: var(--panel-bg) !important;
}
.gr-accordion-header {
background-color: var(--secondary-bg) !important;
padding: 10px 15px !important;
font-weight: 600 !important;
color: var(--text-color) !important;
}
/* Audio player */
.audio-player {
background-color: var(--panel-bg) !important;
border-radius: 8px !important;
overflow: hidden !important;
}
/* Dropdown & Input fields */
select, input, .gr-dropdown {
background-color: var(--panel-bg) !important;
color: var(--text-color) !important;
border: 1px solid var(--border-color) !important;
}
/* Labels */
label, .gr-label {
color: var(--text-secondary) !important;
}
/* Footer styling */
.footer {
border-top: 1px solid var(--border-color) !important;
margin-top: 30px !important;
padding: 15px !important;
}
"""
# Create Gradio interface
with gr.Blocks(css=css, theme=gr.themes.Default()) as demo:
# Title and Logo
gr.HTML(
"""
<div class="header-container">
<div style="display: flex; justify-content: center; margin-bottom: 20px;">
<img src="https://i.postimg.cc/3Jx3yZ5b/real-vs-fake-sonics-w-logo.jpg"
style="max-width: 150px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.3);">
</div>
<h1 style="text-align: center; font-size: 28px; margin-bottom: 10px; color: #ffffff;">SONICS: Synthetic Or Not - Identifying Counterfeit Songs</h1>
<h3 style="text-align: center; color: #bbbbbb; margin-bottom: 15px;">ICLR 2025 [Poster]</h3>
<p style="text-align: center; font-size: 16px; margin: 0; color: #aaaaaa;">
Detect if a song is real or AI-generated with our state-of-the-art models.
Simply upload an audio file to verify its authenticity!
</p>
</div>
"""
)
# Resource Links - Simplified
gr.HTML(
"""
<div style="display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; margin-bottom: 25px;">
<a href="https://openreview.net/forum?id=PY7KSh29Z8" target="_blank" class="resource-link">
📄 Paper
</a>
<a href="https://huggingface.co/datasets/awsaf49/sonics" target="_blank" class="resource-link">
🎵 Dataset
</a>
<a href="https://huggingface.co/collections/awsaf49/sonics-spectttra-67bb6517b3920fd18e409013" target="_blank" class="resource-link">
🤖 Models
</a>
<a href="https://arxiv.org/abs/2408.14080" target="_blank" class="resource-link">
🔬 ArXiv
</a>
<a href="https://github.com/awsaf49/sonics" target="_blank" class="resource-link">
💻 GitHub
</a>
</div>
"""
)
# Main Interface
with gr.Row(equal_height=True):
with gr.Column():
audio_input = gr.Audio(
label="Upload Audio File",
type="filepath",
elem_id="audio_input",
elem_classes="audio-player"
)
model_dropdown = gr.Dropdown(
choices=list(MODEL_IDS.keys()),
value="SpecTTTra-γ (5s)",
label="Select Model",
elem_id="model_dropdown"
)
submit_btn = gr.Button(
"✨ Analyze Audio",
elem_id="submit_btn"
)
with gr.Column():
# Define output before using it in Examples
output = gr.Label(
label="Analysis Result",
num_top_classes=2,
elem_id="output"
)
with gr.Accordion("How It Works", open=True):
gr.Markdown("""
## The SONICS classifier
The SONICS classifier analyzes your audio to determine if it's an authentic song (human created) or generated by AI. Our models are trained on a diverse dataset of real and AI-generated songs from Suno and Udio.
### Models available:
- **SpecTTTra-α**: Optimized for speed
- **SpecTTTra-β**: Balanced performance
- **SpecTTTra-γ**: Highest accuracy
### Duration variants:
- **5s**: Analyzes a 5-second clip (faster)
- **120s**: Analyzes up to 2 minutes (more accurate)
""")
# Add Examples section after output is defined
with gr.Accordion("Example Audio Files", open=True):
gr.Examples(
examples=[
["demo/real_song.mp3", "SpecTTTra-γ (5s)"],
["demo/fake_song.mp3", "SpecTTTra-γ (5s)"],
],
inputs=[audio_input, model_dropdown],
outputs=[output],
fn=predict,
cache_examples=True,
)
# Footer
gr.HTML(
"""
<div class="footer" style="text-align: center;">
<p style="color: #bbbbbb; font-size: 14px;">SONICS: Synthetic Or Not - Identifying Counterfeit Songs | ICLR 2025</p>
<p style="color: #777777; font-size: 12px;">For research purposes only</p>
</div>
"""
)
# Prediction handling
submit_btn.click(fn=predict, inputs=[audio_input, model_dropdown], outputs=[output])
if __name__ == "__main__":
# Use dark theme as base and then apply custom CSS on top
demo.launch() |