Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,748 Bytes
acb0b29 c3c908f e836611 acb0b29 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f e836611 c3c908f 8f94ca9 c3c908f 50d7dc5 c3c908f |
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 |
import spaces
import gradio as gr
import torch
import librosa
import numpy as np
from inference import inference
from huggingface_hub import hf_hub_download
import os
from pathlib import Path
def download_models_from_hub():
"""
Download model checkpoints from Hugging Face Model Hub
"""
model_dir = Path("checkpoints")
model_dir.mkdir(exist_ok=True)
# Original checkpoint filenames on HF Hub
models = {
"main": "EmbeddingModel_MERT_768-epoch=0073-val_loss=0.1058-val_acc=0.9585-val_f1=0.9366-val_precision=0.9936-val_recall=0.8857.ckpt",
"backup": "step=007000-val_loss=0.1831-val_acc=0.9278.ckpt"
}
downloaded_models = {}
for model_name, filename in models.items():
local_path = model_dir / filename
if not local_path.exists():
print(f"π₯ Downloading {model_name} model from Hugging Face Hub...")
model_path = hf_hub_download(
repo_id="mippia/FST-checkpoints",
filename=filename,
local_dir=str(model_dir),
local_dir_use_symlinks=False
)
print(f"β
{model_name} model downloaded successfully!")
downloaded_models[model_name] = str(local_path)
else:
print(f"β
{model_name} model already exists locally")
downloaded_models[model_name] = str(local_path)
return downloaded_models
@spaces.GPU
def detect_ai_audio(audio_file):
"""
Detect whether the uploaded audio file was generated by AI
"""
if audio_file is None:
return """
<div style="text-align: center; padding: 20px; border-radius: 10px; background: linear-gradient(135deg, #ff6b6b22, #ff6b6b11);">
<div style="font-size: 18px; color: #ff6b6b;">β οΈ Please upload an audio file</div>
</div>
"""
try:
result = inference(audio_file)
# Format result with better styling
if "AI" in str(result).upper() or "artificial" in str(result).lower() or "fake" in str(result).lower():
status = "AI Generated"
color = "#ff6b6b"
confidence = "High confidence this audio was generated by AI"
else:
status = "Human Generated"
color = "#51cf66"
confidence = "High confidence this audio was created by humans"
formatted_result = f"""
<div style="text-align: center; padding: 25px; border-radius: 15px; background: linear-gradient(135deg, {color}22, {color}11); border: 2px solid {color}33;">
<div style="font-size: 28px; font-weight: bold; color: {color}; margin-bottom: 10px;">{status}</div>
<div style="font-size: 16px; color: #666; margin-bottom: 8px;">{confidence}</div>
<div style="font-size: 14px; color: #888;">Raw output: {result}</div>
</div>
"""
return formatted_result
except Exception as e:
error_result = f"""
<div style="text-align: center; padding: 20px; border-radius: 10px; background: linear-gradient(135deg, #ff6b6b22, #ff6b6b11);">
<div style="font-size: 20px; font-weight: bold; color: #ff6b6b; margin-bottom: 8px;">Error</div>
<div style="font-size: 14px; color: #666;">Failed to process audio: {str(e)}</div>
</div>
"""
return error_result
# Custom CSS for modern design
custom_css = """
/* Global background gradient */
.gradio-container {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
min-height: 100vh;
}
/* Main container styling */
.main-container {
background: rgba(255, 255, 255, 0.95) !important;
backdrop-filter: blur(10px) !important;
border-radius: 20px !important;
box-shadow: 0 20px 40px rgba(0,0,0,0.1) !important;
margin: 20px !important;
padding: 30px !important;
}
/* Title styling */
h1 {
background: linear-gradient(135deg, #667eea, #764ba2) !important;
-webkit-background-clip: text !important;
-webkit-text-fill-color: transparent !important;
text-align: center !important;
font-size: 3em !important;
font-weight: 800 !important;
margin-bottom: 10px !important;
}
/* Description text */
.gradio-markdown p {
text-align: center !important;
font-size: 1.2em !important;
color: #555 !important;
margin-bottom: 30px !important;
}
/* Audio upload component */
.upload-container {
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
border-radius: 15px !important;
padding: 20px !important;
border: none !important;
box-shadow: 0 10px 30px rgba(240, 147, 251, 0.3) !important;
transition: all 0.3s ease !important;
}
.upload-container:hover {
transform: translateY(-5px) !important;
box-shadow: 0 15px 40px rgba(240, 147, 251, 0.4) !important;
}
/* Output container */
.output-container {
background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%) !important;
border-radius: 15px !important;
padding: 20px !important;
border: none !important;
box-shadow: 0 10px 30px rgba(168, 237, 234, 0.3) !important;
min-height: 150px !important;
}
/* Button styling */
.gr-button {
background: linear-gradient(135deg, #667eea, #764ba2) !important;
border: none !important;
border-radius: 25px !important;
padding: 12px 30px !important;
font-weight: 600 !important;
color: white !important;
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4) !important;
transition: all 0.3s ease !important;
}
.gr-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6) !important;
}
/* Animation */
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.gradio-container > div {
animation: fadeInUp 0.8s ease-out !important;
}
/* Responsive design */
@media (max-width: 768px) {
h1 {
font-size: 2em !important;
}
.main-container {
margin: 10px !important;
padding: 20px !important;
}
}
"""
# Initialize the app
print("π Starting FST AI Audio Detection App...")
print("π¦ Initializing models...")
# Download models at startup
models = download_models_from_hub()
# Check if main model is available
if models.get("main"):
print("β
Main model ready for inference")
else:
print("β οΈ Warning: Main model not available, app may not work properly")
# Create Gradio interface
demo = gr.Interface(
fn=detect_ai_audio,
inputs=gr.Audio(
type="filepath",
label="Upload Audio File",
elem_classes=["upload-container"]
),
outputs=gr.HTML(
label="Detection Result",
elem_classes=["output-container"]
),
title="AI Audio Detector",
description="""
<div style="text-align: center; font-size: 1.2em; color: #555; margin: 20px 0;">
<p><strong>Advanced AI technology</strong> to accurately detect whether uploaded audio was generated by AI!</p>
<p>Supported formats: MP3, WAV, M4A, FLAC and various audio formats</p>
<p>Powered by Fusion Segment Transformer (FST) - ICASSP 2026</p>
</div>
""",
examples=[],
css=custom_css,
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="purple",
neutral_hue="gray",
font=[gr.themes.GoogleFont("Inter"), "Arial", "sans-serif"]
),
elem_classes=["main-container"]
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_api=False,
show_error=True
) |