File size: 9,168 Bytes
3085c15
 
 
 
 
 
 
efdd97c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3085c15
 
 
 
efdd97c
3085c15
efdd97c
 
 
3085c15
 
 
efdd97c
 
3085c15
 
efdd97c
3085c15
 
efdd97c
3085c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efdd97c
3085c15
 
 
efdd97c
3085c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efdd97c
 
 
 
 
 
 
 
 
3085c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efdd97c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3085c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efdd97c
 
3085c15
efdd97c
3085c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efdd97c
3085c15
 
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
import os
import torch
import librosa
import numpy as np
import gradio as gr
from sonics import HFAudioClassifier

# Restructured model configurations for separate selectors
MODEL_TYPES = ["SpecTTTra-α", "SpecTTTra-β", "SpecTTTra-γ"]
DURATIONS = ["5s", "120s"]

# Mapping for model IDs
def get_model_id(model_type, duration):
    model_map = {
        "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",
    }
    key = f"{model_type}-{duration}"
    return model_map[key]

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_cache = {}

def load_model(model_type, duration):
    """Load model if not already cached"""
    model_key = f"{model_type}-{duration}"
    if model_key not in model_cache:
        model_id = get_model_id(model_type, duration)
        model = HFAudioClassifier.from_pretrained(model_id)
        model = model.to(device)
        model.eval()
        model_cache[model_key] = model
    return model_cache[model_key]


def process_audio(audio_path, model_type, duration):
    """Process audio file and return prediction"""
    try:
        model = load_model(model_type, duration)
        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_type, duration):
    """Gradio interface function"""
    if audio_file is None:
        return {"Message": "Please upload an audio file"}
    return process_audio(audio_file, model_type, duration)


# Updated CSS with better color scheme for resource links
css = """
/* Custom CSS that works with Ocean theme */
.sonics-header {
    text-align: center;
    padding: 20px;
    margin-bottom: 20px;
    border-radius: 10px;
}

.sonics-logo {
    max-width: 150px;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}

.sonics-title {
    font-size: 28px;
    margin-bottom: 10px;
}

.sonics-subtitle {
    margin-bottom: 15px;
}

.sonics-description {
    font-size: 16px;
    margin: 0;
}

/* Resource links styling */
.resource-links {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 25px;
}

.resource-link {
    background-color: #222222;
    color: #4aedd6;
    border: 1px solid #333333;
    padding: 8px 16px;
    border-radius: 20px;
    margin: 5px;
    text-decoration: none;
    display: inline-block;
    font-weight: 500;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    transition: all 0.2s ease;
}

.resource-link:hover {
    background-color: #333333;
    transform: translateY(-2px);
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.4);
    transition: all 0.2s ease;
}

.resource-link-icon {
    margin-right: 5px;
}

/* Footer styling */
.sonics-footer {
    text-align: center;
    margin-top: 30px;
    padding: 15px;
}

/* Selectors wrapper for side-by-side appearance */
.selectors-wrapper {
    display: flex;
    gap: 10px;
}
.selectors-wrapper > div {
    flex: 1;
}
"""

# Create Gradio interface
with gr.Blocks(css=css, theme=gr.themes.Ocean()) as demo:
    # Title and Logo
    gr.HTML(
        """
        <div class="sonics-header">
            <div style="display: flex; justify-content: center; margin-bottom: 20px;">
                <img src="https://i.postimg.cc/3Jx3yZ5b/real-vs-fake-sonics-w-logo.jpg" class="sonics-logo">
            </div>
            <h1 class="sonics-title">SONICS: Synthetic Or Not - Identifying Counterfeit Songs</h1>
            <h3 class="sonics-subtitle">ICLR 2025 [Poster]</h3>
            <p class="sonics-description">
                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 - Updated with custom styling to match screenshot
    gr.HTML(
        """
        <div class="resource-links">
            <a href="https://openreview.net/forum?id=PY7KSh29Z8" target="_blank" class="resource-link">
                <span class="resource-link-icon">📄</span>Paper
            </a>
            <a href="https://huggingface.co/datasets/awsaf49/sonics" target="_blank" class="resource-link">
                <span class="resource-link-icon">🎵</span>Dataset
            </a>
            <a href="https://huggingface.co/collections/awsaf49/sonics-spectttra-67bb6517b3920fd18e409013" target="_blank" class="resource-link">
                <span class="resource-link-icon">🤖</span>Models
            </a>
            <a href="https://arxiv.org/abs/2408.14080" target="_blank" class="resource-link">
                <span class="resource-link-icon">🔬</span>ArXiv
            </a>
            <a href="https://github.com/awsaf49/sonics" target="_blank" class="resource-link">
                <span class="resource-link-icon">💻</span>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"
            )
            
            # Add CSS class to create a wrapper for side-by-side dropdowns
            with gr.Row(elem_classes="selectors-wrapper"):
                model_dropdown = gr.Dropdown(
                    choices=MODEL_TYPES,
                    value="SpecTTTra-γ",
                    label="Select Model",
                    elem_id="model_dropdown"
                )
                
                duration_dropdown = gr.Dropdown(
                    choices=DURATIONS,
                    value="5s",
                    label="Select Duration",
                    elem_id="duration_dropdown"
                )
            
            submit_btn = gr.Button(
                "✨ Analyze Audio",
                elem_id="submit_btn",
                variant="primary"
            )

        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=[
                ["example/real_song.mp3", "SpecTTTra-γ", "5s"],
                ["example/fake_song.mp3", "SpecTTTra-γ", "5s"],
            ],
            inputs=[audio_input, model_dropdown, duration_dropdown],
            outputs=[output],
            fn=predict,
            cache_examples=True,
        )

    # Footer
    gr.HTML(
        """
        <div class="sonics-footer">
            <p>SONICS: Synthetic Or Not - Identifying Counterfeit Songs | ICLR 2025</p>
            <p style="font-size: 12px;">For research purposes only</p>
        </div>
        """
    )

    # Prediction handling
    submit_btn.click(fn=predict, inputs=[audio_input, model_dropdown, duration_dropdown], outputs=[output])

if __name__ == "__main__":
    demo.launch()