monetjoe commited on
Commit
bbdf556
·
verified ·
1 Parent(s): 8459d67

Rename app.py to index.html

Browse files
Files changed (2) hide show
  1. app.py +0 -205
  2. index.html +18 -0
app.py DELETED
@@ -1,205 +0,0 @@
1
- import os
2
- import torch
3
- import shutil
4
- import librosa
5
- import warnings
6
- import numpy as np
7
- import gradio as gr
8
- import librosa.display
9
- import matplotlib.pyplot as plt
10
- from collections import Counter
11
- from model import EvalNet
12
- from utils import (
13
- get_modelist,
14
- find_files,
15
- embed_img,
16
- _L,
17
- SAMPLE_RATE,
18
- TEMP_DIR,
19
- TRANSLATE,
20
- CLASSES,
21
- EN_US,
22
- )
23
-
24
-
25
- def circular_padding(spec: np.ndarray, end: int):
26
- size = len(spec)
27
- if end <= size:
28
- return spec
29
-
30
- num_padding = end - size
31
- num_repeat = num_padding // size + int(num_padding % size != 0)
32
- padding = np.tile(spec, num_repeat)
33
- return np.concatenate((spec, padding))[:end]
34
-
35
-
36
- def wav2mel(audio_path: str, width=3):
37
- y, sr = librosa.load(audio_path, sr=SAMPLE_RATE)
38
- total_frames = len(y)
39
- if total_frames % (width * sr) != 0:
40
- count = total_frames // (width * sr) + 1
41
- y = circular_padding(y, count * width * sr)
42
-
43
- mel_spec = librosa.feature.melspectrogram(y=y, sr=sr)
44
- log_mel_spec = librosa.power_to_db(mel_spec, ref=np.max)
45
- dur = librosa.get_duration(y=y, sr=sr)
46
- total_frames = log_mel_spec.shape[1]
47
- step = int(width * total_frames / dur)
48
- count = int(total_frames / step)
49
- begin = int(0.5 * (total_frames - count * step))
50
- end = begin + step * count
51
- for i in range(begin, end, step):
52
- librosa.display.specshow(log_mel_spec[:, i : i + step])
53
- plt.axis("off")
54
- plt.savefig(
55
- f"{TEMP_DIR}/{i}.jpg",
56
- bbox_inches="tight",
57
- pad_inches=0.0,
58
- )
59
- plt.close()
60
-
61
-
62
- def wav2cqt(audio_path: str, width=3):
63
- y, sr = librosa.load(audio_path, sr=SAMPLE_RATE)
64
- total_frames = len(y)
65
- if total_frames % (width * sr) != 0:
66
- count = total_frames // (width * sr) + 1
67
- y = circular_padding(y, count * width * sr)
68
-
69
- cqt_spec = librosa.cqt(y=y, sr=sr)
70
- log_cqt_spec = librosa.power_to_db(np.abs(cqt_spec) ** 2, ref=np.max)
71
- dur = librosa.get_duration(y=y, sr=sr)
72
- total_frames = log_cqt_spec.shape[1]
73
- step = int(width * total_frames / dur)
74
- count = int(total_frames / step)
75
- begin = int(0.5 * (total_frames - count * step))
76
- end = begin + step * count
77
- for i in range(begin, end, step):
78
- librosa.display.specshow(log_cqt_spec[:, i : i + step])
79
- plt.axis("off")
80
- plt.savefig(
81
- f"{TEMP_DIR}/{i}.jpg",
82
- bbox_inches="tight",
83
- pad_inches=0.0,
84
- )
85
- plt.close()
86
-
87
-
88
- def wav2chroma(audio_path: str, width=3):
89
- y, sr = librosa.load(audio_path, sr=SAMPLE_RATE)
90
- total_frames = len(y)
91
- if total_frames % (width * sr) != 0:
92
- count = total_frames // (width * sr) + 1
93
- y = circular_padding(y, count * width * sr)
94
-
95
- chroma_spec = librosa.feature.chroma_stft(y=y, sr=sr)
96
- log_chroma_spec = librosa.power_to_db(np.abs(chroma_spec) ** 2, ref=np.max)
97
- dur = librosa.get_duration(y=y, sr=sr)
98
- total_frames = log_chroma_spec.shape[1]
99
- step = int(width * total_frames / dur)
100
- count = int(total_frames / step)
101
- begin = int(0.5 * (total_frames - count * step))
102
- end = begin + step * count
103
- for i in range(begin, end, step):
104
- librosa.display.specshow(log_chroma_spec[:, i : i + step])
105
- plt.axis("off")
106
- plt.savefig(
107
- f"{TEMP_DIR}/{i}.jpg",
108
- bbox_inches="tight",
109
- pad_inches=0.0,
110
- )
111
- plt.close()
112
-
113
-
114
- def most_frequent_value(lst: list):
115
- counter = Counter(lst)
116
- max_count = max(counter.values())
117
- for element, count in counter.items():
118
- if count == max_count:
119
- return element
120
-
121
- return None
122
-
123
-
124
- def infer(wav_path: str, log_name: str, folder_path=TEMP_DIR):
125
- status = "Success"
126
- filename = result = None
127
- try:
128
- if os.path.exists(folder_path):
129
- shutil.rmtree(folder_path)
130
-
131
- if not wav_path:
132
- raise ValueError("请输入音频!")
133
-
134
- spec = log_name.split("_")[-3]
135
- os.makedirs(folder_path, exist_ok=True)
136
- model = EvalNet(log_name, len(TRANSLATE)).model
137
- eval("wav2%s" % spec)(wav_path)
138
- jpgs = find_files(folder_path, ".jpg")
139
- preds = []
140
- for jpg in jpgs:
141
- input = embed_img(jpg)
142
- output: torch.Tensor = model(input)
143
- preds.append(torch.max(output.data, 1)[1])
144
-
145
- pred_id = most_frequent_value(preds)
146
- filename = os.path.basename(wav_path)
147
- result = (
148
- CLASSES[pred_id].capitalize()
149
- if EN_US
150
- else f"{TRANSLATE[CLASSES[pred_id]]} ({CLASSES[pred_id].capitalize()})"
151
- )
152
-
153
- except Exception as e:
154
- status = f"{e}"
155
-
156
- return status, filename, result
157
-
158
-
159
- if __name__ == "__main__":
160
- warnings.filterwarnings("ignore")
161
- models = get_modelist(assign_model="vit_l_16_mel")
162
- examples = []
163
- example_wavs = find_files()
164
- for wav in example_wavs:
165
- examples.append([wav, models[0]])
166
-
167
- with gr.Blocks() as demo:
168
- gr.Interface(
169
- fn=infer,
170
- inputs=[
171
- gr.Audio(label=_L("上传录音"), type="filepath"),
172
- gr.Dropdown(choices=models, label=_L("选择模型"), value=models[0]),
173
- ],
174
- outputs=[
175
- gr.Textbox(label=_L("状态栏"), buttons=["copy"]),
176
- gr.Textbox(label=_L("音频文件名"), buttons=["copy"]),
177
- gr.Textbox(label=_L("古筝演奏技法识别"), buttons=["copy"]),
178
- ],
179
- examples=examples,
180
- cache_examples=False,
181
- flagging_mode="never",
182
- title=_L("建议录音时长保持在 3s 左右"),
183
- )
184
-
185
- gr.Markdown(f"# {_L('引用')}" + """
186
- ```bibtex
187
- @article{Zhou-2025,
188
- author = {Monan Zhou and Shenyang Xu and Zhaorui Liu and Zhaowen Wang and Feng Yu and Wei Li and Baoqiang Han},
189
- title = {CCMusic: An Open and Diverse Database for Chinese Music Information Retrieval Research},
190
- journal = {Transactions of the International Society for Music Information Retrieval},
191
- volume = {8},
192
- number = {1},
193
- pages = {22--38},
194
- month = {Mar},
195
- year = {2025},
196
- url = {https://doi.org/10.5334/tismir.194},
197
- doi = {10.5334/tismir.194}
198
- }
199
- ```""")
200
-
201
- demo.launch(
202
- theme=gr.themes.Ocean(),
203
- css="#gradio-share-link-button-0 { display: none; }",
204
- ssr_mode=False,
205
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
index.html ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <html>
2
+ <head>
3
+ <style>
4
+ html, body {
5
+ margin: 0;
6
+ padding: 0;
7
+ }
8
+ iframe {
9
+ width: 100%;
10
+ height: 100%;
11
+ border: none;
12
+ }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <iframe src="https://ccmusic-database-gz-isotech.ms.show"></iframe>
17
+ </body>
18
+ </html>