File size: 5,077 Bytes
63efc13
 
 
 
 
3e74548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6ac252
3e74548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c68adb
 
 
 
 
 
 
 
 
 
ee6da54
0c68adb
63efc13
 
 
 
 
 
3e74548
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63efc13
 
0c68adb
63efc13
 
efcc2a1
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
from spleeter import Splitter
import torchaudio
from torchaudio.transforms import Resample
import torch
import gradio as gr
from io import BytesIO
import requests

def yt2audio(search:str,format:str='mp3',audioBitrate='320',videoQuality='720'):
    return requests.get(requests.post('https://cnv.cx/v2/converter', headers={
    'Accept': '*/*',
    'Accept-Language': 'en-GB,en;q=0.9',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    'DNT': '1',
    'Origin': 'https://frame.y2meta-uk.com',
    'Referer': 'https://frame.y2meta-uk.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
    'key': requests.get('https://cnv.cx/v2/sanity/key', headers={
    'Accept': '*/*',
    'Accept-Language': 'en-GB,en;q=0.9',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json',
    'DNT': '1',
    'If-None-Match': requests.get('https://cdn.jsdelivr.net/npm/@iframe-resizer/child', headers={
    'sec-ch-ua-platform': '"Windows"',
    'Referer': 'https://frame.y2meta-uk.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
    'DNT': '1',
    'sec-ch-ua-mobile': '?0',
}).headers['etag'],
    'Origin': 'https://frame.y2meta-uk.com',
    'Referer': 'https://frame.y2meta-uk.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
},verify=False).json()['key'],
    'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}, data={
    'link': 'https://youtu.be/'+requests.get(f'https://wwd.mp3juice.blog/search.php?q={search}').json()['items'][0]['id'],
    'format': format,
    'audioBitrate': audioBitrate,
    'videoQuality': videoQuality,
    'filenameStyle': 'pretty',
    'vCodec': 'h264',
},verify=False).json()['url'], headers={
    'DNT': '1',
    'Referer': 'https://frame.y2meta-uk.com/',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
},verify=False).content


def separate_audio(audio_path:str,inst_no:int,progress=gr.Progress(True)):
    """
    Separate audio into instrument tracks.

    Args:
        audio_path (str): Path to input audio.
        inst_no (int): Number of instruments to separate.

    Returns:
        tuple: Up to 5 MP3 file paths for separated tracks.
    """
    
    model = Splitter(inst_no)
    wav, sr = torchaudio.load(audio_path)
    target_sr = 44100
    if sr != target_sr:
        resampler = Resample(sr, target_sr)
        wav = resampler(wav)
        sr = target_sr
    with torch.inference_mode():
        results = model.forward(wav)
    for i in results:
        torchaudio.save(f"{i}.mp3", results[i], sr)
    return tuple([i+".mp3" for i in results] + [None for _ in range(5-len(results))])

def separate_audio_by_search(query:str,inst_no:int,progress=gr.Progress(True)):
    """
    Searches YouTube for the given text query, downloads the audio,
    and separates it into multiple instruments (e.g., vocals, drums, bass, etc.).

    Args:
        query (str): Text query to search on YouTube.
        inst_no (int): Number of instruments to separate.

    Returns:
        tuple: Up to 5 MP3 file paths for separated tracks.
    """
    
    model = Splitter(inst_no)
    wav, sr = torchaudio.load(BytesIO(yt2audio(query)),format="mp3")
    target_sr = 44100
    if sr != target_sr:
        resampler = Resample(sr, target_sr)
        wav = resampler(wav)
        sr = target_sr
    with torch.inference_mode():
        results = model.forward(wav)
    for i in results:
        torchaudio.save(f"{i}.mp3", results[i], sr)
    return tuple([i+".mp3" for i in results] + [None for _ in range(5-len(results))])

gr.TabbedInterface([gr.Interface(separate_audio_by_search, [gr.Textbox(),gr.Dropdown([2,4,5])], [gr.Audio(type="filepath"), gr.Audio(type="filepath"),gr.Audio(type="filepath"),gr.Audio(type="filepath"),gr.Audio(type="filepath")]),gr.Interface(separate_audio, [gr.Audio(type="filepath"),gr.Dropdown([2,4,5])], [gr.Audio(type="filepath"), gr.Audio(type="filepath"),gr.Audio(type="filepath"),gr.Audio(type="filepath"),gr.Audio(type="filepath")])],["YT2STEM","Audio2STEM"]).launch(mcp_server=True)