Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,68 @@ import torchaudio
|
|
| 3 |
from torchaudio.transforms import Resample
|
| 4 |
import torch
|
| 5 |
import gradio as gr
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
Separate audio into instrument tracks.
|
| 9 |
|
|
@@ -22,10 +83,36 @@ def separate(audio_path:str,inst_no:int,progress=gr.Progress(True)):
|
|
| 22 |
resampler = Resample(sr, target_sr)
|
| 23 |
wav = resampler(wav)
|
| 24 |
sr = target_sr
|
| 25 |
-
with torch.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
results = model.forward(wav)
|
| 27 |
for i in results:
|
| 28 |
torchaudio.save(f"{i}.mp3", results[i], sr)
|
| 29 |
return tuple([i+".mp3" for i in results] + [None for _ in range(5-len(results))])
|
| 30 |
|
| 31 |
-
gr.Interface(
|
|
|
|
| 3 |
from torchaudio.transforms import Resample
|
| 4 |
import torch
|
| 5 |
import gradio as gr
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
def yt2audio(search:str,format:str='mp3',audioBitrate='320',videoQuality='720'):
|
| 10 |
+
return requests.get(requests.post('https://cnv.cx/v2/converter', headers={
|
| 11 |
+
'Accept': '*/*',
|
| 12 |
+
'Accept-Language': 'en-GB,en;q=0.9',
|
| 13 |
+
'Connection': 'keep-alive',
|
| 14 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
| 15 |
+
'DNT': '1',
|
| 16 |
+
'Origin': 'https://frame.y2meta-uk.com',
|
| 17 |
+
'Referer': 'https://frame.y2meta-uk.com/',
|
| 18 |
+
'Sec-Fetch-Dest': 'empty',
|
| 19 |
+
'Sec-Fetch-Mode': 'cors',
|
| 20 |
+
'Sec-Fetch-Site': 'cross-site',
|
| 21 |
+
'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',
|
| 22 |
+
'key': requests.get('https://cnv.cx/v2/sanity/key', headers={
|
| 23 |
+
'Accept': '*/*',
|
| 24 |
+
'Accept-Language': 'en-GB,en;q=0.9',
|
| 25 |
+
'Connection': 'keep-alive',
|
| 26 |
+
'Content-Type': 'application/json',
|
| 27 |
+
'DNT': '1',
|
| 28 |
+
'If-None-Match': requests.get('https://cdn.jsdelivr.net/npm/@iframe-resizer/child', headers={
|
| 29 |
+
'sec-ch-ua-platform': '"Windows"',
|
| 30 |
+
'Referer': 'https://frame.y2meta-uk.com/',
|
| 31 |
+
'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',
|
| 32 |
+
'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
|
| 33 |
+
'DNT': '1',
|
| 34 |
+
'sec-ch-ua-mobile': '?0',
|
| 35 |
+
}).headers['etag'],
|
| 36 |
+
'Origin': 'https://frame.y2meta-uk.com',
|
| 37 |
+
'Referer': 'https://frame.y2meta-uk.com/',
|
| 38 |
+
'Sec-Fetch-Dest': 'empty',
|
| 39 |
+
'Sec-Fetch-Mode': 'cors',
|
| 40 |
+
'Sec-Fetch-Site': 'cross-site',
|
| 41 |
+
'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',
|
| 42 |
+
'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
|
| 43 |
+
'sec-ch-ua-mobile': '?0',
|
| 44 |
+
'sec-ch-ua-platform': '"Windows"',
|
| 45 |
+
},verify=False).json()['key'],
|
| 46 |
+
'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
|
| 47 |
+
'sec-ch-ua-mobile': '?0',
|
| 48 |
+
'sec-ch-ua-platform': '"Windows"',
|
| 49 |
+
}, data={
|
| 50 |
+
'link': f'https://youtu.be/{requests.get(f'https://wwd.mp3juice.blog/search.php?q={search}').json()['items'][0]['id']}',
|
| 51 |
+
'format': format,
|
| 52 |
+
'audioBitrate': audioBitrate,
|
| 53 |
+
'videoQuality': videoQuality,
|
| 54 |
+
'filenameStyle': 'pretty',
|
| 55 |
+
'vCodec': 'h264',
|
| 56 |
+
},verify=False).json()['url'], headers={
|
| 57 |
+
'DNT': '1',
|
| 58 |
+
'Referer': 'https://frame.y2meta-uk.com/',
|
| 59 |
+
'Upgrade-Insecure-Requests': '1',
|
| 60 |
+
'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',
|
| 61 |
+
'sec-ch-ua': '"Google Chrome";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
|
| 62 |
+
'sec-ch-ua-mobile': '?0',
|
| 63 |
+
'sec-ch-ua-platform': '"Windows"',
|
| 64 |
+
},verify=False).content
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def separate_audio(audio_path:str,inst_no:int,progress=gr.Progress(True)):
|
| 68 |
"""
|
| 69 |
Separate audio into instrument tracks.
|
| 70 |
|
|
|
|
| 83 |
resampler = Resample(sr, target_sr)
|
| 84 |
wav = resampler(wav)
|
| 85 |
sr = target_sr
|
| 86 |
+
with torch.inference_mode():
|
| 87 |
+
results = model.forward(wav)
|
| 88 |
+
for i in results:
|
| 89 |
+
torchaudio.save(f"{i}.mp3", results[i], sr)
|
| 90 |
+
return tuple([i+".mp3" for i in results] + [None for _ in range(5-len(results))])
|
| 91 |
+
|
| 92 |
+
def separate_audio_by_search(query:str,inst_no:int,progress=gr.Progress(True)):
|
| 93 |
+
"""
|
| 94 |
+
Searches YouTube for the given text query, downloads the audio,
|
| 95 |
+
and separates it into multiple instruments (e.g., vocals, drums, bass, etc.).
|
| 96 |
+
|
| 97 |
+
Args:
|
| 98 |
+
query (str): Text query to search on YouTube.
|
| 99 |
+
inst_no (int): Number of instruments to separate.
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
tuple: Up to 5 MP3 file paths for separated tracks.
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
model = Splitter(inst_no)
|
| 106 |
+
wav, sr = torchaudio.load(BytesIO(yt2audio(query)),format="mp3")
|
| 107 |
+
target_sr = 44100
|
| 108 |
+
if sr != target_sr:
|
| 109 |
+
resampler = Resample(sr, target_sr)
|
| 110 |
+
wav = resampler(wav)
|
| 111 |
+
sr = target_sr
|
| 112 |
+
with torch.inference_mode():
|
| 113 |
results = model.forward(wav)
|
| 114 |
for i in results:
|
| 115 |
torchaudio.save(f"{i}.mp3", results[i], sr)
|
| 116 |
return tuple([i+".mp3" for i in results] + [None for _ in range(5-len(results))])
|
| 117 |
|
| 118 |
+
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)
|