Skip to content

Commit 6bac2d7

Browse files
committed
server fix
1 parent 3abe436 commit 6bac2d7

File tree

8 files changed

+123
-47
lines changed

8 files changed

+123
-47
lines changed

config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def device_config(self) -> tuple:
105105
/ 1024
106106
/ 1024
107107
/ 1024
108-
+ 0.4
108+
# + 0.4
109109
)
110110
elif self.has_mps():
111111
print("No supported Nvidia GPU found")
@@ -126,7 +126,7 @@ def device_config(self) -> tuple:
126126
x_pad = 3
127127
x_query = 10
128128
x_center = 60
129-
x_max = 65
129+
x_max = 64
130130
else:
131131
# 5G显存配置
132132
x_pad = 1

pages/0_RVC_Server.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@ def stop_server(pid):
1818
if process.is_running(): process.kill()
1919

2020
def start_server(host,port):
21-
pid = SERVERS["RVC"].get("pid")
21+
pid = SERVERS.INFERENCE_PID
2222
if pid_is_active(pid):
2323
process = psutil.Process(pid)
24-
if process.is_running(): return SERVERS["RVC"]["url"]
24+
if process.is_running(): return SERVERS.url
2525

2626
base_url = f"http://{host}:{port}"
2727
cmd = f"python api.py --port={port} --host={host}"
2828
p = subprocess.Popen(cmd, cwd=CWD)
2929

3030
if poll_url(base_url):
31-
SERVERS["RVC"] = {
32-
"url": base_url,
33-
"pid": p.pid
34-
}
31+
SERVERS.RVC_INFERENCE_URL = f"{base_url}/rvc"
32+
print(SERVERS)
33+
SERVERS.UVR_INFERENCE_URL = f"{base_url}/uvr"
34+
print(SERVERS)
35+
SERVERS.DOCS_URL = f"{base_url}/docs"
36+
print(SERVERS)
37+
SERVERS.INFERENCE_PID = p.pid
38+
print(SERVERS)
3539

3640
return base_url
3741

@@ -44,7 +48,7 @@ def initial_state():
4448

4549
if __name__=="__main__":
4650
with SessionStateContext("rvc-api",initial_state()) as state:
47-
pid = SERVERS["RVC"].get("pid")
51+
pid = SERVERS.INFERENCE_PID
4852
is_active = pid_is_active(pid)
4953

5054
with st.form("rvc-api-form"):
@@ -66,4 +70,4 @@ def initial_state():
6670
stop_server(pid)
6771
st.experimental_rerun()
6872

69-
st_iframe(url=f'{SERVERS["RVC"]["url"]}/docs',height=800)
73+
st_iframe(url=SERVERS.DOCS_URL,height=800)

pages/1_Inference.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import streamlit as st
33

4-
from webui import DEVICE_OPTIONS, MENU_ITEMS, SERVERS, config, get_cwd, i18n
4+
from webui import DEVICE_OPTIONS, MENU_ITEMS, config, get_cwd, i18n
55
from webui.api import convert_vocals, get_rvc_models, split_vocals
66
st.set_page_config(layout="centered",menu_items=MENU_ITEMS)
77

@@ -15,7 +15,6 @@
1515
from webui.utils import gc_collect, get_filenames, get_index, get_optimal_torch_device
1616

1717
CWD = get_cwd()
18-
INFERENCE_URL = SERVERS['RVC']['url']
1918

2019
def call_uvr(audio_path,**kwargs):
2120
with st.status(f"splitting vocals... {kwargs}") as status:

pages/3_Text_to_Speech.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_filename(audio_name,model_name):
5151

5252
def one_click_speech(state):
5353
state.tts_audio = generate_speech(state.tts_text,speaker=os.path.basename(state.model_name).split(".")[0],method=state.tts_method, device=state.device)
54-
state.converted_voice = call_rvc(state,state.tts_audio,**(state.tts_options))
54+
state.converted_voice = call_rvc(state.model_name,state.tts_audio,**(state.tts_options))
5555

5656
if __name__=="__main__":
5757
with SessionStateContext("tts",initial_state=init_inference_state()) as state:
@@ -86,10 +86,10 @@ def one_click_speech(state):
8686
options=DEVICE_OPTIONS,horizontal=True,
8787
index=get_index(DEVICE_OPTIONS,state.device))
8888

89-
tts_options = voice_conversion_form(state.tts_options)
89+
state.tts_options = voice_conversion_form(state.tts_options)
9090

9191
if st.form_submit_button(i18n("inference.save.button")):
92-
state.tts_options = tts_options
92+
9393
state.device = device
9494
save_voice_conversion_params("tts",state.tts_options)
9595

@@ -107,7 +107,7 @@ def one_click_speech(state):
107107
col1.audio(state.tts_audio[0],sample_rate=state.tts_audio[1])
108108

109109
if col2.button("Convert Speech"):
110-
state.converted_voice = convert_vocals(state,state.tts_audio,**(state.tts_options))
110+
state.converted_voice = convert_vocals(state.model_name,state.tts_audio,**(state.tts_options))
111111

112112
if state.converted_voice:
113113
col2.audio(state.converted_voice[0],sample_rate=state.converted_voice[1])

server/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from functools import lru_cache
12
from webui.utils import ObjectNamespace
23

3-
STATUS = ObjectNamespace(status="OK",rvc=ObjectNamespace())
4+
@lru_cache
5+
def get_status():
6+
return ObjectNamespace(status="OK",rvc=ObjectNamespace())
7+
8+
STATUS = get_status()

webui/__init__.py

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
from functools import lru_cache
1+
from functools import lru_cache, total_ordering
22
import os
33
import shelve
4+
from contextlib import contextmanager
45
import sys
56
import weakref
67
from config import Config
78
from i18n import I18nAuto
89

10+
@lru_cache
11+
def load_config():
12+
return Config(), I18nAuto()
13+
14+
@lru_cache
15+
def get_cwd():
16+
CWD = os.getcwd()
17+
if CWD not in sys.path:
18+
sys.path.append(CWD)
19+
return CWD
20+
921
MENU_ITEMS = {
1022
"Get help": "https://github.com/SayanoAI/RVC-Studio/discussions",
1123
"Report a Bug": "https://github.com/SayanoAI/RVC-Studio/issues",
@@ -20,6 +32,14 @@
2032
N_THREADS_OPTIONS=[1,2,4,8,12,16]
2133
SR_MAP = {"32k": 32000,"40k": 40000, "48k": 48000}
2234

35+
BASE_DIR = get_cwd()
36+
BASE_MODELS_DIR = os.path.join(BASE_DIR,"models")
37+
SONG_DIR = os.path.join(BASE_DIR,"songs")
38+
BASE_CACHE_DIR = os.path.join(BASE_DIR,".cache")
39+
DATASETS_DIR = os.path.join(BASE_DIR,"datasets")
40+
LOG_DIR = os.path.join(BASE_DIR,"logs")
41+
OUTPUT_DIR = os.path.join(BASE_DIR,"output")
42+
2343
class ObjectNamespace(dict):
2444
def __init__(self,**kwargs): super().__init__(kwargs)
2545
def __missing__(self, name: str): return ObjectNamespace()
@@ -38,45 +58,91 @@ def __setstate__(self, state):
3858
def __getstate__(self): return dict(**self)
3959

4060
class PersistedDict:
41-
def __init__(self,fname,**kwargs):
42-
self.__fname__ = fname
43-
if len(kwargs):
44-
with shelve.open(fname) as shelf:
45-
for k in kwargs:
46-
shelf[k] = kwargs[k]
4761

48-
def __missing__(self, name: str): return print(f"Attribute {name} is missing")
62+
# initialize the class with an optional filename and dict arguments
63+
def __init__(self, filename=None, **data):
64+
# store the filename as an attribute
65+
self.filename = filename
4966

50-
def __getitem__(self, name: str):
51-
if name.startswith("__") and name.endswith("__"): return super().__getattribute__(name)
52-
with shelve.open(self.__fname__) as shelf:
53-
return shelf.get(name,None)
67+
for key, value in data.items():
68+
# recursively convert the values to NestedDict
69+
self.__setattr__(key, value)
70+
71+
# define a context manager to open and close the shelve file
72+
@contextmanager
73+
def open_shelf(self):
74+
# if filename is given, open the shelve file
75+
shelf = shelve.open(self.filename) if self.filename else {}
76+
77+
# yield the shelf as the resource
78+
yield shelf
79+
if hasattr(shelf,"close"):
80+
# close the shelf when exiting the context
81+
shelf.close()
82+
83+
# define a method to get the attribute value given a key
84+
def __getattr__(self, key: str):
85+
is_private = key.startswith("_") and key.endswith("_")
5486

55-
def __setitem__(self, name: str, value):
56-
with shelve.open(self.__fname__) as shelf:
57-
shelf[name] = value
87+
# if the key is filename, set it as an attribute
88+
if key == "filename" or is_private:
89+
if key in self.__dict__: return self.__dict__[key]
90+
else: return None
91+
92+
# use the context manager to open the shelve file
93+
with self.open_shelf() as shelf:
94+
# if the key exists in the shelve file, return the value
95+
# return getattr(shelf, key, None)
96+
if key in shelf:
97+
return shelf[key]
98+
# else, return None
99+
else:
100+
return None
58101

59-
def get(self, name: str, value): return self.__getitem__(name) or value
102+
# define a method to set the attribute value given a key
103+
def __setattr__(self, key, value):
104+
# if the key is filename, set it as an attribute
105+
if key == "filename":
106+
self.__dict__[key] = value
107+
# else, use the context manager to open the shelve file
108+
else:
109+
with self.open_shelf() as shelf:
110+
# store the value in the shelve file
111+
print(f"{key}={value}")
112+
shelf[key] = value
60113

61-
def set(self, name: str, value): return self.__setitem__(name, value)
114+
# define a method to represent the class as a dict
115+
def __repr__(self):
116+
# initialize an empty dict
117+
result = {}
118+
# use the context manager to open the shelve file
119+
with self.open_shelf() as shelf:
120+
# loop through the keys in the shelve file
121+
for key in shelf.keys():
122+
# add the key and value to the result
123+
result[key] = shelf[key]
124+
# return the result
125+
return str(result)
62126

63-
@lru_cache
64-
def load_config():
65-
return Config(), I18nAuto()
127+
def __setitem__(self, key, value): self.__setattr__(key, value)
128+
def __getitem__(self, key): self.__getattr__(key)
129+
def __lt__(self,_): return False
130+
def __eq__(self,other):
131+
if hasattr(other,"filename"): return self.filename==other.filename
132+
else: return False
133+
def __call__(self,*args,**kwargs):
134+
print(f"{args=}, {kwargs=}")
135+
return str(self)
66136

67-
@lru_cache
68-
def get_cwd():
69-
CWD = os.getcwd()
70-
if CWD not in sys.path:
71-
sys.path.append(CWD)
72-
return CWD
73137

74138
@lru_cache
75139
def get_servers():
76-
servers = PersistedDict(os.path.join(get_cwd(),".cache","servers.shelve"))
140+
os.makedirs(BASE_CACHE_DIR,exist_ok=True)
141+
fname = os.path.join(BASE_CACHE_DIR,"servers.shelve")
142+
servers = PersistedDict(fname)
77143
return servers
78144

79145
config, i18n = load_config()
80146
SERVERS = get_servers()
81-
RVC_INFERENCE_URL = f"{SERVERS['RVC']['url']}/rvc"
82-
UVR_INFERENCE_URL = f"{SERVERS['RVC']['url']}/uvr"
147+
RVC_INFERENCE_URL = SERVERS.RVC_INFERENCE_URL
148+
UVR_INFERENCE_URL = SERVERS.UVR_INFERENCE_URL

webui/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from lib.audio import audio2bytes, bytes2audio, load_input_audio
44
from webui import RVC_INFERENCE_URL, UVR_INFERENCE_URL
5+
from webui.utils import gc_collect
56

67
def get_rvc_models():
78
fnames = []
@@ -59,6 +60,7 @@ def split_vocals(audio_path,**args):
5960
return None, None, None
6061

6162
def convert_vocals(model_name,input_audio,**kwargs):
63+
gc_collect()
6264
audio_data = audio2bytes(*input_audio)
6365
body = dict(name=model_name,audio_data=audio_data,**kwargs)
6466

webui/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def pid_is_active(pid: int):
7474
return psutil.pid_exists(pid)
7575
elif platform.system() == "Linux":
7676
os.kill(pid, 0)
77-
except OSError as e:
77+
except Exception as e:
7878
print(e)
7979
return False
8080
else:

0 commit comments

Comments
 (0)