Skip to content

Commit

Permalink
Api Update
Browse files Browse the repository at this point in the history
Removed mandatory model loading in api, added endpoint to change model folder
  • Loading branch information
daswer123 committed Aug 26, 2024
1 parent 4329a80 commit 9d6c987
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "rvc-python"
version = "0.1.1"
version = "0.1.3"
authors = [
{ name="daswer123", email="daswerq123@gmail.com" },
]
Expand Down
8 changes: 6 additions & 2 deletions rvc_python/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ def main():
cli_parser.add_argument("-i", "--input", type=str, help="Path to input file")
cli_parser.add_argument("-d", "--dir", type=str, help="Directory path containing audio files")
cli_parser.add_argument("-o", "--output", type=str, default="out.wav", help="Output path for single file, or output directory for multiple files")
cli_parser.add_argument("-mp", "--model", type=str, required=True, help="Path to model file")

# API parser
api_parser = subparsers.add_parser("api", help="Start API server")
api_parser.add_argument("-p", "--port", type=int, default=5050, help="Port number for the API server")
api_parser.add_argument("-l", "--listen", action="store_true", help="Listen to external connections")
api_parser.add_argument("-pm", "--preload-model", type=str, help="Preload model on startup (optional)")

# Common arguments for both CLI and API
for subparser in [cli_parser, api_parser]:
subparser.add_argument("-mp", "--model", type=str, required=True, help="Path to model file")
subparser.add_argument("-md", "--models_dir", type=str, default="rvc_models", help="Directory to store models")
subparser.add_argument("-ip", "--index", type=str, default="", help="Path to index file (optional)")
subparser.add_argument("-de", "--device", type=str, default="cpu:0", help="Device to use (e.g., cpu:0, cuda:0)")
Expand All @@ -41,7 +42,6 @@ def main():

# Initialize RVCInference
rvc = RVCInference(models_dir=args.models_dir, device=args.device)
rvc.load_model(args.model)
rvc.set_params(
f0method=args.method,
f0up_key=args.pitch,
Expand All @@ -54,6 +54,7 @@ def main():

# Handle CLI command
if args.command == "cli":
rvc.load_model(args.model)
if args.input:
# Process single file
rvc.infer_file(args.input, args.output)
Expand All @@ -72,6 +73,9 @@ def main():
app = create_app()
app.state.rvc = rvc

if args.preload_model:
rvc.load_model(args.preload_model)

# Set up server options
host = "0.0.0.0" if args.listen else "127.0.0.1"
print(f"Starting API server on {host}:{args.port}")
Expand Down
15 changes: 15 additions & 0 deletions rvc_python/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ class ConvertAudioRequest(BaseModel):
class SetParamsRequest(BaseModel):
params: dict

class SetModelsDirRequest(BaseModel):
models_dir: str

def setup_routes(app: FastAPI):
@app.post("/convert")
def rvc_convert(request: ConvertAudioRequest):
if not app.state.rvc.current_model:
raise HTTPException(status_code=400, detail="No model loaded. Please load a model first.")

tmp_input = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
tmp_output = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
try:
Expand Down Expand Up @@ -102,6 +108,15 @@ def set_device(request: SetDeviceRequest):
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))

@app.post("/set_models_dir")
def set_models_dir(request: SetModelsDirRequest):
try:
new_models_dir = request.models_dir
app.state.rvc.set_models_dir(new_models_dir)
return JSONResponse(content={"message": f"Models directory set to {new_models_dir}"})
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))

def create_app():
app = FastAPI()

Expand Down
6 changes: 6 additions & 0 deletions rvc_python/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def _load_available_models(self):
}
return models

def set_models_dir(self, new_models_dir):
if not os.path.isdir(new_models_dir):
raise ValueError(f"Directory {new_models_dir} does not exist")
self.models_dir = new_models_dir
self.models = self._load_available_models()

def list_models(self):
"""Returns a list of available models."""
return list(self.models.keys())
Expand Down

0 comments on commit 9d6c987

Please sign in to comment.