slslslrhfem commited on
Commit ·
e99e064
0
Parent(s):
Clean start: app + 1005_e_4 LFS only
Browse files- .gitattributes +1 -0
- .gitignore +5 -0
- 1005_e_4 +3 -0
- app.py +347 -0
- inference.py +112 -0
- requirements.txt +116 -0
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
1005_e_4 filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
covers80/
|
| 2 |
+
ml_models/
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.pyc
|
| 5 |
+
.env
|
1005_e_4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ff9aab3de0a7c00d5946c33027cc6aa5a8c1267f297b212b7d906304b417f360
|
| 3 |
+
size 17285251
|
app.py
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import librosa
|
| 5 |
+
import numpy as np
|
| 6 |
+
from inference import inference
|
| 7 |
+
from huggingface_hub import snapshot_download
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
token = os.getenv("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
def download_data_from_hub():
|
| 14 |
+
"""
|
| 15 |
+
Download covers80 and ml_models folders from Dataset repository
|
| 16 |
+
"""
|
| 17 |
+
base_dir = Path(".")
|
| 18 |
+
data_repo_id = "nininigold/music-data"
|
| 19 |
+
|
| 20 |
+
folders_to_check = ["covers80", "ml_models"]
|
| 21 |
+
downloaded_folders = {}
|
| 22 |
+
|
| 23 |
+
# 폴더들이 이미 존재하는지 확인
|
| 24 |
+
all_exist = all((base_dir / folder).exists() and any((base_dir / folder).iterdir())
|
| 25 |
+
for folder in folders_to_check)
|
| 26 |
+
|
| 27 |
+
if not all_exist:
|
| 28 |
+
print(f"📥 Downloading data folders from dataset: {data_repo_id}")
|
| 29 |
+
print(f" This includes covers80 and ml_models folders (~17k+ files each)")
|
| 30 |
+
print(f" This may take several minutes...")
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# Dataset 저장소에서 전체 다운로드
|
| 34 |
+
downloaded_path = snapshot_download(
|
| 35 |
+
repo_id=data_repo_id,
|
| 36 |
+
repo_type="dataset",
|
| 37 |
+
local_dir=str(base_dir),
|
| 38 |
+
local_dir_use_symlinks=False,
|
| 39 |
+
token=token,
|
| 40 |
+
ignore_patterns=["*.md", "*.txt", ".gitattributes", "README.md"]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
print(f"✅ Dataset downloaded successfully")
|
| 44 |
+
|
| 45 |
+
# 각 폴더 확인 및 파일 수 카운트
|
| 46 |
+
for folder_name in folders_to_check:
|
| 47 |
+
folder_path = base_dir / folder_name
|
| 48 |
+
if folder_path.exists():
|
| 49 |
+
file_count = len([f for f in folder_path.rglob("*") if f.is_file()])
|
| 50 |
+
print(f" 📁 {folder_name}: {file_count:,} files")
|
| 51 |
+
downloaded_folders[folder_name] = str(folder_path)
|
| 52 |
+
else:
|
| 53 |
+
print(f" ⚠️ {folder_name} folder not found in downloaded data")
|
| 54 |
+
downloaded_folders[folder_name] = None
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"⚠️ Failed to download dataset: {e}")
|
| 58 |
+
print(f" Dataset: {data_repo_id}")
|
| 59 |
+
for folder_name in folders_to_check:
|
| 60 |
+
downloaded_folders[folder_name] = None
|
| 61 |
+
else:
|
| 62 |
+
print(f"✅ Data folders and files already exist locally")
|
| 63 |
+
for folder_name in folders_to_check:
|
| 64 |
+
folder_path = base_dir / folder_name
|
| 65 |
+
if folder_path.exists():
|
| 66 |
+
file_count = len([f for f in folder_path.rglob("*") if f.is_file()])
|
| 67 |
+
print(f" 📁 {folder_name}: {file_count:,} files")
|
| 68 |
+
downloaded_folders[folder_name] = str(folder_path)
|
| 69 |
+
else:
|
| 70 |
+
downloaded_folders[folder_name] = None
|
| 71 |
+
|
| 72 |
+
for file_name in files_to_check:
|
| 73 |
+
file_path = base_dir / file_name
|
| 74 |
+
if file_path.exists():
|
| 75 |
+
file_size = file_path.stat().st_size / (1024*1024) # MB
|
| 76 |
+
print(f" 📄 {file_name}: {file_size:.1f} MB")
|
| 77 |
+
downloaded_folders[file_name] = str(file_path)
|
| 78 |
+
else:
|
| 79 |
+
downloaded_folders[file_name] = None
|
| 80 |
+
|
| 81 |
+
return downloaded_folders
|
| 82 |
+
|
| 83 |
+
@spaces.GPU
|
| 84 |
+
def process_audio_for_matching(audio_file):
|
| 85 |
+
"""
|
| 86 |
+
Process the uploaded audio file and return matching results
|
| 87 |
+
"""
|
| 88 |
+
if audio_file is None:
|
| 89 |
+
return """
|
| 90 |
+
<div style='text-align: center; color: #ff6b6b; padding: 30px; background: #fff5f5; border-radius: 15px; border: 2px dashed #ff6b6b;'>
|
| 91 |
+
<h3>🎵 No Audio File</h3>
|
| 92 |
+
<p>Please upload an audio file to get started!</p>
|
| 93 |
+
</div>
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
# inference 함수 호출
|
| 98 |
+
result = inference(audio_file)
|
| 99 |
+
|
| 100 |
+
# result 구조:
|
| 101 |
+
# {
|
| 102 |
+
# 'matches': [
|
| 103 |
+
# {
|
| 104 |
+
# 'rank': 1,
|
| 105 |
+
# 'score': 0.95,
|
| 106 |
+
# 'song_title': 'Song Name',
|
| 107 |
+
# 'segment_file': 'path/to/segment.wav',
|
| 108 |
+
# 'test_time': 23.5,
|
| 109 |
+
# 'library_time': 45.2,
|
| 110 |
+
# 'confidence': '95.0%',
|
| 111 |
+
# 'time_match': 'Input: 23.5s ↔ Library: 45.2s'
|
| 112 |
+
# }
|
| 113 |
+
# ],
|
| 114 |
+
# 'message': 'success' or error message
|
| 115 |
+
# }
|
| 116 |
+
|
| 117 |
+
if result.get('message') != 'success':
|
| 118 |
+
return f"""
|
| 119 |
+
<div style="text-align: center; padding: 25px; background: #fff3cd; border-radius: 15px; border: 1px solid #ffeaa7; margin: 10px 0;">
|
| 120 |
+
<h3 style="color: #856404; margin-bottom: 15px;">⚠️ No Matches Found</h3>
|
| 121 |
+
<p style="color: #856404; font-size: 1.1em;">{result.get('message', 'Unknown error occurred')}</p>
|
| 122 |
+
<p style="color: #856404; font-size: 0.9em; margin-top: 10px;">Try uploading a clearer audio sample or a different part of the song.</p>
|
| 123 |
+
</div>
|
| 124 |
+
"""
|
| 125 |
+
|
| 126 |
+
matches = result.get('matches', [])
|
| 127 |
+
if not matches:
|
| 128 |
+
return """
|
| 129 |
+
<div style="text-align: center; padding: 25px; background: #fff3cd; border-radius: 15px; border: 1px solid #ffeaa7; margin: 10px 0;">
|
| 130 |
+
<h3 style="color: #856404; margin-bottom: 15px;">🔍 No Matches Found</h3>
|
| 131 |
+
<p style="color: #856404; font-size: 1.1em;">Sorry, we couldn't find any matching songs in our database.</p>
|
| 132 |
+
<p style="color: #856404; font-size: 0.9em; margin-top: 10px;">Try uploading a different audio sample.</p>
|
| 133 |
+
</div>
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
# 매치 결과 HTML 생성
|
| 137 |
+
matches_html = ""
|
| 138 |
+
for match in matches:
|
| 139 |
+
rank = match.get('rank', 0)
|
| 140 |
+
song_title = match.get('song_title', 'Unknown Song')
|
| 141 |
+
confidence = match.get('confidence', '0%')
|
| 142 |
+
time_match = match.get('time_match', 'Unknown')
|
| 143 |
+
test_time = match.get('test_time', 0)
|
| 144 |
+
library_time = match.get('library_time', 0)
|
| 145 |
+
segment_file = match.get('segment_file', '')
|
| 146 |
+
|
| 147 |
+
# 랭킹에 따른 색상 설정
|
| 148 |
+
rank_colors = {1: '#e74c3c', 2: '#f39c12', 3: '#27ae60'}
|
| 149 |
+
rank_color = rank_colors.get(rank, '#7f8c8d')
|
| 150 |
+
|
| 151 |
+
# 세그먼트 파일 정보
|
| 152 |
+
segment_info = f"Found: {segment_file}" if segment_file else "No segment file found"
|
| 153 |
+
|
| 154 |
+
matches_html += f"""
|
| 155 |
+
<div style="background: #ffffff; border-radius: 12px; padding: 20px; margin: 15px 0;
|
| 156 |
+
border-left: 5px solid {rank_color}; box-shadow: 0 3px 10px rgba(0,0,0,0.1);">
|
| 157 |
+
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
|
| 158 |
+
<h3 style="color: #2c3e50; margin: 0; font-size: 1.2em;">
|
| 159 |
+
<span style="background: {rank_color}; color: white; padding: 4px 8px; border-radius: 15px; font-size: 0.8em; margin-right: 10px;">
|
| 160 |
+
#{rank}
|
| 161 |
+
</span>
|
| 162 |
+
{song_title}
|
| 163 |
+
</h3>
|
| 164 |
+
<span style="background: #ecf0f1; color: #2c3e50; padding: 6px 12px; border-radius: 20px; font-weight: 600;">
|
| 165 |
+
{confidence}
|
| 166 |
+
</span>
|
| 167 |
+
</div>
|
| 168 |
+
|
| 169 |
+
<div style="background: #f8f9fa; border-radius: 8px; padding: 12px; margin: 10px 0;">
|
| 170 |
+
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; text-align: center;">
|
| 171 |
+
<div>
|
| 172 |
+
<strong style="color: #3498db;">Your Audio</strong>
|
| 173 |
+
<br><span style="color: #e74c3c; font-size: 1.1em;">{test_time:.1f}s</span>
|
| 174 |
+
</div>
|
| 175 |
+
<div>
|
| 176 |
+
<strong style="color: #3498db;">Matched At</strong>
|
| 177 |
+
<br><span style="color: #27ae60; font-size: 1.1em;">{library_time:.1f}s</span>
|
| 178 |
+
</div>
|
| 179 |
+
</div>
|
| 180 |
+
</div>
|
| 181 |
+
|
| 182 |
+
<div style="font-size: 0.9em; color: #7f8c8d; text-align: center; margin-top: 10px;">
|
| 183 |
+
📁 {segment_info}
|
| 184 |
+
</div>
|
| 185 |
+
</div>
|
| 186 |
+
"""
|
| 187 |
+
|
| 188 |
+
formatted_result = f"""
|
| 189 |
+
<div style="background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%); border-radius: 20px; padding: 30px;
|
| 190 |
+
box-shadow: 0 8px 25px rgba(0,0,0,0.1); border: 1px solid #dee2e6; margin: 10px 0;">
|
| 191 |
+
<div style="text-align: center; margin-bottom: 25px;">
|
| 192 |
+
<h2 style="color: #2c3e50; margin-bottom: 10px; font-size: 1.8em;">🎵 Matching Results</h2>
|
| 193 |
+
<p style="color: #7f8c8d; font-size: 1.1em;">Found {len(matches)} potential matches in our database</p>
|
| 194 |
+
</div>
|
| 195 |
+
|
| 196 |
+
{matches_html}
|
| 197 |
+
|
| 198 |
+
<div style="text-align: center; margin-top: 25px; padding: 15px; background: #e8f5e8; border-radius: 10px;">
|
| 199 |
+
<p style="color: #27ae60; margin: 0; font-size: 0.95em;">
|
| 200 |
+
💡 <strong>How to read results:</strong> The times show where similar segments were found.
|
| 201 |
+
Higher ranked results have better similarity scores.
|
| 202 |
+
</p>
|
| 203 |
+
</div>
|
| 204 |
+
</div>
|
| 205 |
+
"""
|
| 206 |
+
|
| 207 |
+
return formatted_result
|
| 208 |
+
|
| 209 |
+
except Exception as e:
|
| 210 |
+
return f"""
|
| 211 |
+
<div style="text-align: center; padding: 20px; background: #f8d7da; border-radius: 10px; border: 1px solid #f5c6cb; color: #721c24; margin: 10px 0;">
|
| 212 |
+
<h3>❌ Error Processing Audio</h3>
|
| 213 |
+
<p>Error details: {str(e)}</p>
|
| 214 |
+
<p style="font-size: 0.9em; margin-top: 10px;">Please try again with a different audio file.</p>
|
| 215 |
+
</div>
|
| 216 |
+
"""
|
| 217 |
+
|
| 218 |
+
# 깔끔한 CSS 스타일
|
| 219 |
+
custom_css = """
|
| 220 |
+
.gradio-container {
|
| 221 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 222 |
+
min-height: 100vh;
|
| 223 |
+
padding: 20px;
|
| 224 |
+
}
|
| 225 |
+
.main-container {
|
| 226 |
+
background: #ffffff !important;
|
| 227 |
+
border-radius: 20px !important;
|
| 228 |
+
box-shadow: 0 15px 35px rgba(0,0,0,0.1) !important;
|
| 229 |
+
margin: 0 auto !important;
|
| 230 |
+
padding: 40px !important;
|
| 231 |
+
max-width: 900px;
|
| 232 |
+
}
|
| 233 |
+
h1 {
|
| 234 |
+
text-align: center !important;
|
| 235 |
+
font-size: 2.8em !important;
|
| 236 |
+
font-weight: 800 !important;
|
| 237 |
+
margin-bottom: 15px !important;
|
| 238 |
+
background: linear-gradient(135deg, #667eea, #764ba2) !important;
|
| 239 |
+
-webkit-background-clip: text !important;
|
| 240 |
+
-webkit-text-fill-color: transparent !important;
|
| 241 |
+
background-clip: text !important;
|
| 242 |
+
}
|
| 243 |
+
.gradio-markdown p {
|
| 244 |
+
text-align: center !important;
|
| 245 |
+
font-size: 1.1em !important;
|
| 246 |
+
color: #555 !important;
|
| 247 |
+
margin-bottom: 25px !important;
|
| 248 |
+
line-height: 1.6;
|
| 249 |
+
}
|
| 250 |
+
.upload-container {
|
| 251 |
+
background: #f8f9fa !important;
|
| 252 |
+
border-radius: 15px !important;
|
| 253 |
+
padding: 25px !important;
|
| 254 |
+
border: 2px dashed #dee2e6 !important;
|
| 255 |
+
margin-bottom: 25px !important;
|
| 256 |
+
transition: all 0.3s ease !important;
|
| 257 |
+
}
|
| 258 |
+
.upload-container:hover {
|
| 259 |
+
border-color: #667eea !important;
|
| 260 |
+
background: #f1f3ff !important;
|
| 261 |
+
}
|
| 262 |
+
.output-container {
|
| 263 |
+
background: #ffffff !important;
|
| 264 |
+
border-radius: 15px !important;
|
| 265 |
+
padding: 20px !important;
|
| 266 |
+
border: 1px solid #e1e5e9 !important;
|
| 267 |
+
min-height: 200px !important;
|
| 268 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.05) !important;
|
| 269 |
+
}
|
| 270 |
+
.gr-button {
|
| 271 |
+
background: linear-gradient(135deg, #667eea, #764ba2) !important;
|
| 272 |
+
color: #fff !important;
|
| 273 |
+
border: none !important;
|
| 274 |
+
border-radius: 25px !important;
|
| 275 |
+
padding: 12px 30px !important;
|
| 276 |
+
font-weight: 600 !important;
|
| 277 |
+
font-size: 1.1em !important;
|
| 278 |
+
transition: all 0.3s ease !important;
|
| 279 |
+
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
|
| 280 |
+
}
|
| 281 |
+
.gr-button:hover {
|
| 282 |
+
transform: translateY(-2px) !important;
|
| 283 |
+
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6) !important;
|
| 284 |
+
}
|
| 285 |
+
@media (max-width: 768px) {
|
| 286 |
+
h1 { font-size: 2.2em !important; }
|
| 287 |
+
.main-container { margin: 10px !important; padding: 25px !important; }
|
| 288 |
+
.upload-container { padding: 20px !important; }
|
| 289 |
+
}
|
| 290 |
+
"""
|
| 291 |
+
|
| 292 |
+
# 앱 초기화
|
| 293 |
+
print("🚀 Starting Music Similarity Detection App...")
|
| 294 |
+
print("📦 Downloading covers80 and ml_models folders...")
|
| 295 |
+
folders = download_data_from_hub()
|
| 296 |
+
|
| 297 |
+
if folders.get("covers80") and folders.get("ml_models"):
|
| 298 |
+
print("✅ All required data folders are ready")
|
| 299 |
+
elif folders.get("covers80") or folders.get("ml_models"):
|
| 300 |
+
print("⚠️ Some data folders available, app may work with limited functionality")
|
| 301 |
+
else:
|
| 302 |
+
print("⚠️ Warning: Required data folders not available, app may not work properly")
|
| 303 |
+
|
| 304 |
+
# Gradio 인터페이스
|
| 305 |
+
demo = gr.Interface(
|
| 306 |
+
fn=process_audio_for_matching,
|
| 307 |
+
inputs=gr.Audio(
|
| 308 |
+
type="filepath",
|
| 309 |
+
label="🎵 Upload Your Audio File",
|
| 310 |
+
elem_classes=["upload-container"]
|
| 311 |
+
),
|
| 312 |
+
outputs=gr.HTML(
|
| 313 |
+
label="🔍 Similarity Results",
|
| 314 |
+
elem_classes=["output-container"]
|
| 315 |
+
),
|
| 316 |
+
title="🎵 Music Similarity Detector",
|
| 317 |
+
description="""
|
| 318 |
+
<div style="text-align: center; font-size: 1.1em; color: #555; margin: 25px 0; line-height: 1.6;">
|
| 319 |
+
<p><strong>🎯 Upload any audio clip and find similar segments in our music database!</strong></p>
|
| 320 |
+
<p>Our AI analyzes your audio and finds the most similar segments from known songs.</p>
|
| 321 |
+
<p style="font-size: 0.95em; color: #777; margin-top: 15px;">
|
| 322 |
+
📁 Supported formats: MP3, WAV, M4A, FLAC<br>
|
| 323 |
+
⏱️ Processing time: ~15-30 seconds per file<br>
|
| 324 |
+
🎼 Database: covers80 collection with segmented analysis
|
| 325 |
+
</p>
|
| 326 |
+
</div>
|
| 327 |
+
""",
|
| 328 |
+
examples=[],
|
| 329 |
+
css=custom_css,
|
| 330 |
+
theme=gr.themes.Soft(
|
| 331 |
+
primary_hue="blue",
|
| 332 |
+
secondary_hue="purple",
|
| 333 |
+
neutral_hue="gray",
|
| 334 |
+
font=[gr.themes.GoogleFont("Inter"), "Arial", "sans-serif"]
|
| 335 |
+
),
|
| 336 |
+
elem_classes=["main-container"],
|
| 337 |
+
allow_flagging="never"
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
if __name__ == "__main__":
|
| 341 |
+
demo.launch(
|
| 342 |
+
server_name="0.0.0.0",
|
| 343 |
+
server_port=7860,
|
| 344 |
+
show_api=False,
|
| 345 |
+
show_error=True,
|
| 346 |
+
share=False
|
| 347 |
+
)
|
inference.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
from compare import get_one_result
|
| 4 |
+
from segment_transcription import segment_transcription
|
| 5 |
+
|
| 6 |
+
def inference(audio_path):
|
| 7 |
+
segment_datas = segment_transcription(audio_path)
|
| 8 |
+
result = get_one_result(segment_datas)
|
| 9 |
+
final_result = result_formatting(result)
|
| 10 |
+
return final_result
|
| 11 |
+
|
| 12 |
+
def find_closest_segment_file(song_title, target_time):
|
| 13 |
+
"""
|
| 14 |
+
covers80 폴더에서 해당 곡의 가장 가까운 시간대 세그먼트 파일을 찾음
|
| 15 |
+
"""
|
| 16 |
+
# 가능한 패턴들로 검색
|
| 17 |
+
patterns = [
|
| 18 |
+
f"covers80/{song_title}_segments/*.wav",
|
| 19 |
+
f"covers80/*{song_title}*_segments/*.wav",
|
| 20 |
+
f"covers80/{song_title}*/*.wav"
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
segment_files = []
|
| 24 |
+
for pattern in patterns:
|
| 25 |
+
segment_files.extend(glob.glob(pattern))
|
| 26 |
+
|
| 27 |
+
if not segment_files:
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
# 파일명에서 시간 추출하고 target_time과 가장 가까운 것 찾기
|
| 31 |
+
closest_file = None
|
| 32 |
+
min_diff = float('inf')
|
| 33 |
+
|
| 34 |
+
for file_path in segment_files:
|
| 35 |
+
filename = os.path.basename(file_path)
|
| 36 |
+
try:
|
| 37 |
+
# 파일명에서 시간 추출 (예: "53.333.wav" -> 53.333)
|
| 38 |
+
time_str = filename.replace('.wav', '')
|
| 39 |
+
file_time = float(time_str)
|
| 40 |
+
diff = abs(file_time - target_time)
|
| 41 |
+
|
| 42 |
+
if diff < min_diff:
|
| 43 |
+
min_diff = diff
|
| 44 |
+
closest_file = file_path
|
| 45 |
+
except ValueError:
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
return closest_file
|
| 49 |
+
|
| 50 |
+
def result_formatting(result):
|
| 51 |
+
"""
|
| 52 |
+
get_one_result에서 나온 결과를 포맷팅
|
| 53 |
+
result: sorted list of CompareHelper objects
|
| 54 |
+
"""
|
| 55 |
+
if not result or len(result) == 0:
|
| 56 |
+
return {
|
| 57 |
+
'matches': [],
|
| 58 |
+
'message': 'No matches found'
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
# 에러 메시지 체크
|
| 62 |
+
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], str):
|
| 63 |
+
return {
|
| 64 |
+
'matches': [],
|
| 65 |
+
'message': result[0] # "there is no note for this song"
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
# 상위 3개 결과 추출
|
| 69 |
+
top_3_results = []
|
| 70 |
+
for i, compare_helper in enumerate(result[:3]):
|
| 71 |
+
score = compare_helper.data[0] # similarity score
|
| 72 |
+
test_label = compare_helper.data[1] # test song info
|
| 73 |
+
library_label = compare_helper.data[2] # matched song info
|
| 74 |
+
|
| 75 |
+
# 라이브러리 레이블에서 정보 추출
|
| 76 |
+
song_title = library_label.get('title', 'Unknown Song')
|
| 77 |
+
library_time = library_label.get('time', 0) # 매치된 구간의 시간
|
| 78 |
+
|
| 79 |
+
# 테스트 레이블에서 정보 추출
|
| 80 |
+
test_time = test_label.get('time', 0) if test_label else 0 # 입력 곡의 시간
|
| 81 |
+
|
| 82 |
+
# 가장 가까운 세그먼트 파일 찾기
|
| 83 |
+
segment_file = find_closest_segment_file(song_title, library_time)
|
| 84 |
+
|
| 85 |
+
match_info = {
|
| 86 |
+
'rank': i + 1,
|
| 87 |
+
'score': float(score),
|
| 88 |
+
'song_title': song_title,
|
| 89 |
+
'segment_file': segment_file,
|
| 90 |
+
'test_time': float(test_time), # 입력 곡에서 매치된 시간
|
| 91 |
+
'library_time': float(library_time), # 라이브러리 곡에서 매치된 시간
|
| 92 |
+
'confidence': f"{score * 100:.1f}%",
|
| 93 |
+
'time_match': f"Input: {test_time:.1f}s ↔ Library: {library_time:.1f}s"
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
top_3_results.append(match_info)
|
| 97 |
+
|
| 98 |
+
return {
|
| 99 |
+
'matches': top_3_results,
|
| 100 |
+
'message': 'success'
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
result = inference("/home/ubuntu/data/coding/icassp-plagiarism-demo/KEON <3 - I GASLIGHT MYSELF | Udio [The%20Untitled].mp3")
|
| 106 |
+
print("Inference Result:")
|
| 107 |
+
for match in result['matches']:
|
| 108 |
+
print(f"Rank {match['rank']}: {match['song_title']}")
|
| 109 |
+
print(f" Score: {match['confidence']}")
|
| 110 |
+
print(f" Time Match: {match['time_match']}")
|
| 111 |
+
print(f" Segment File: {match['segment_file']}")
|
| 112 |
+
print()
|
requirements.txt
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
absl-py==2.3.1
|
| 2 |
+
antlr4-python3-runtime==4.9.3
|
| 3 |
+
astunparse==1.6.3
|
| 4 |
+
attrs==25.3.0
|
| 5 |
+
audioread==3.0.1
|
| 6 |
+
cachetools==5.5.2
|
| 7 |
+
certifi==2025.8.3
|
| 8 |
+
cffi==1.17.1
|
| 9 |
+
charset-normalizer==3.4.3
|
| 10 |
+
cloudpickle==3.1.1
|
| 11 |
+
Cython==3.1.3
|
| 12 |
+
decorator==5.2.1
|
| 13 |
+
demucs==4.0.1
|
| 14 |
+
dora_search==0.1.12
|
| 15 |
+
einops==0.8.1
|
| 16 |
+
filelock==3.16.1
|
| 17 |
+
flatbuffers==25.2.10
|
| 18 |
+
fsspec==2025.3.0
|
| 19 |
+
gast==0.4.0
|
| 20 |
+
google-auth==2.40.3
|
| 21 |
+
google-auth-oauthlib==1.0.0
|
| 22 |
+
google-pasta==0.2.0
|
| 23 |
+
grpcio==1.70.0
|
| 24 |
+
h5py==3.11.0
|
| 25 |
+
hf-xet==1.1.10
|
| 26 |
+
huggingface-hub==0.34.4
|
| 27 |
+
idna==3.10
|
| 28 |
+
importlib_metadata==8.5.0
|
| 29 |
+
importlib_resources==6.4.5
|
| 30 |
+
Jinja2==3.1.6
|
| 31 |
+
joblib==1.4.2
|
| 32 |
+
jsonpickle==4.1.1
|
| 33 |
+
jsonschema==4.23.0
|
| 34 |
+
jsonschema-specifications==2023.12.1
|
| 35 |
+
julius==0.2.7
|
| 36 |
+
keras==2.13.1
|
| 37 |
+
lameenc==1.8.1
|
| 38 |
+
lazy_loader==0.4
|
| 39 |
+
libclang==18.1.1
|
| 40 |
+
librosa==0.11.0
|
| 41 |
+
llvmlite==0.41.1
|
| 42 |
+
madmom==0.16.1
|
| 43 |
+
Markdown==3.7
|
| 44 |
+
MarkupSafe==2.1.5
|
| 45 |
+
mido==1.3.3
|
| 46 |
+
mir_eval==0.8.2
|
| 47 |
+
mpmath==1.3.0
|
| 48 |
+
msgpack==1.1.1
|
| 49 |
+
networkx==3.1
|
| 50 |
+
numba==0.58.1
|
| 51 |
+
numpy==1.23.5
|
| 52 |
+
nvidia-cublas-cu12==12.1.3.1
|
| 53 |
+
nvidia-cuda-cupti-cu12==12.1.105
|
| 54 |
+
nvidia-cuda-nvrtc-cu12==12.1.105
|
| 55 |
+
nvidia-cuda-runtime-cu12==12.1.105
|
| 56 |
+
nvidia-cudnn-cu12==9.1.0.70
|
| 57 |
+
nvidia-cufft-cu12==11.0.2.54
|
| 58 |
+
nvidia-curand-cu12==10.3.2.106
|
| 59 |
+
nvidia-cusolver-cu12==11.4.5.107
|
| 60 |
+
nvidia-cusparse-cu12==12.1.0.106
|
| 61 |
+
nvidia-nccl-cu12==2.20.5
|
| 62 |
+
nvidia-nvjitlink-cu12==12.9.86
|
| 63 |
+
nvidia-nvtx-cu12==12.1.105
|
| 64 |
+
oauthlib==3.3.1
|
| 65 |
+
omegaconf==2.3.0
|
| 66 |
+
openunmix==1.2.1
|
| 67 |
+
opt_einsum==3.4.0
|
| 68 |
+
packaging==25.0
|
| 69 |
+
pandas==2.0.3
|
| 70 |
+
pillow==10.4.0
|
| 71 |
+
pkgutil_resolve_name==1.3.10
|
| 72 |
+
platformdirs==4.3.6
|
| 73 |
+
pooch==1.8.2
|
| 74 |
+
pretty_midi==0.2.10
|
| 75 |
+
protobuf==4.25.8
|
| 76 |
+
pyasn1==0.6.1
|
| 77 |
+
pyasn1_modules==0.4.2
|
| 78 |
+
pycparser==2.23
|
| 79 |
+
python-dateutil==2.9.0.post0
|
| 80 |
+
pytz==2025.2
|
| 81 |
+
PyYAML==6.0.2
|
| 82 |
+
referencing==0.35.1
|
| 83 |
+
requests==2.32.4
|
| 84 |
+
requests-oauthlib==2.0.0
|
| 85 |
+
retrying==1.4.2
|
| 86 |
+
rpds-py==0.20.1
|
| 87 |
+
rsa==4.9.1
|
| 88 |
+
safetensors==0.5.3
|
| 89 |
+
scikit-learn==1.3.2
|
| 90 |
+
scipy==1.10.1
|
| 91 |
+
six==1.17.0
|
| 92 |
+
soundfile==0.13.1
|
| 93 |
+
soxr==0.3.7
|
| 94 |
+
submitit==1.5.3
|
| 95 |
+
sympy==1.13.3
|
| 96 |
+
tensorboard==2.13.0
|
| 97 |
+
tensorboard-data-server==0.7.2
|
| 98 |
+
tensorflow==2.13.1
|
| 99 |
+
tensorflow-estimator==2.13.0
|
| 100 |
+
tensorflow-io-gcs-filesystem==0.34.0
|
| 101 |
+
termcolor==2.4.0
|
| 102 |
+
threadpoolctl==3.5.0
|
| 103 |
+
timm==1.0.19
|
| 104 |
+
torch==2.4.1
|
| 105 |
+
torchaudio==2.4.1
|
| 106 |
+
torchvision==0.19.1
|
| 107 |
+
tqdm==4.67.1
|
| 108 |
+
treetable==0.2.6
|
| 109 |
+
triton==3.0.0
|
| 110 |
+
typing_extensions==4.13.2
|
| 111 |
+
tzdata==2025.2
|
| 112 |
+
urllib3==2.2.3
|
| 113 |
+
vamp==1.1.0
|
| 114 |
+
Werkzeug==3.0.6
|
| 115 |
+
wrapt==1.17.3
|
| 116 |
+
zipp==3.20.2
|