alter this to have another process name than python.exe 'uvr.py' import os import json…
- User
alter this to have another process name than python.exe
'uvr.py' import os import json import shutil from fastapi import FastAPI, UploadFile, File, Form, HTTPException from fastapi.responses import FileResponse from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from typing import Optional import uvicorn import torch
import logging
from audio_separator.separator import Separator
---- Constants setup (same as your Gradio defaults) ----
now_dir = os.getcwd() config_file = os.path.join(now_dir, "assets", "config.json") models_dir = "./models" out_dir = "./outputs" output_format_choices = [ 'wav','flac','mp3','ogg','opus','m4a','aiff','ac3' ]
device = "cuda" if torch.cuda.is_available() else "cpu" use_autocast = device == "cuda"
app = FastAPI(title="UVR5 VR Arch Separation API")
app.add_middleware( CORSMiddleware, allow_origins=[""], allow_credentials=True, allow_methods=[""], allow_headers=["*"], )
Ensure output directory exists
os.makedirs(out_dir, exist_ok=True)
Pydantic model for JSON request (multipart preferred for file upload)
class SeparateRequest(BaseModel): model: str output_format: str window_size: int aggression: int tta: bool post_process: bool post_process_threshold: float high_end_process: bool batch_size: int normalization_threshold: float amplification_threshold: float single_stem: Optional[str] = ""
Utility function to validate model path
def get_model_path(model_name: str): path = os.path.join(models_dir, model_name) if not os.path.isfile(path): raise HTTPException(status_code=404, detail=f"Model '{model_name}' not found.") return path
@app.post("/separate/") async def separate_audio( audio: UploadFile = File(...), model: str = Form(...), output_format: str = Form(...), window_size: int = Form(...), aggression: int = Form(...), tta: bool = Form(...), post_process: bool = Form(...), post_process_threshold: float = Form(...), high_end_process: bool = Form(...), batch_size: int = Form(...), normalization_threshold: float = Form(...), amplification_threshold: float = Form(...), single_stem: str = Form("") ): """ Endpoint to perform audio separation.
Return value: JSON with paths to separated stems files. """ # Validate output format if output_format not in output_format_choices: raise HTTPException(status_code=400, detail=f"Unsupported output format '{output_format}'.") # Validate model model_path = get_model_path(model) # Save uploaded audio file temporarily audio_temp_path = os.path.join(out_dir, f"input_{audio.filename}") try: with open(audio_temp_path, "wb") as f: f.write(await audio.read()) except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to save input audio: {e}") try: separator = Separator( log_level=logging.WARNING, model_file_dir=models_dir, output_dir=out_dir, output_format=output_format, use_autocast=use_autocast, normalization_threshold=normalization_threshold, amplification_threshold=amplification_threshold, output_single_stem=single_stem, vr_params={ "batch_size": batch_size, "window_size": window_size, "aggression": aggression, "enable_tta": tta, "enable_post_process": post_process, "post_process_threshold": post_process_threshold, "high_end_process": high_end_process, } ) separator.load_model(model_filename=model) separation = separator.separate(audio_temp_path) if len(separation) == 0: raise RuntimeError("Separation returned empty output list.") # Compose response with file URLs or file paths # serve files directly by endpoint (or just return path) # Here, we just return file paths (relative to out_dir) stems_response = {} # If output_single_stem is set, separator returns one stem, else list if single_stem.strip(): # Return a single stem as "stem1" stem_path = separation[0] stem_filename = os.path.basename(stem_path) stems_response["stem1"] = f"/files/{stem_filename}" else: # Normally two stems for idx, path in enumerate(separation[:2], 1): stem_filename = os.path.basename(path) stems_response[f"stem{idx}"] = f"/files/{stem_filename}" except Exception as e: raise HTTPException(status_code=500, detail=f"Separation failed: {str(e)}") finally: # Cleanup uploaded file if os.path.exists(audio_temp_path): try: os.remove(audio_temp_path) except: pass return {"result": stems_response} - Assistant
- User
- Assistant
The script you posted is a FastAPI app that uses
transformerspipeline to provide a/transcribeendpoint for automatic speech recognition.To run this script correctly, you need to:
- Save the script to a file, say
app.py. - Run it with
uvicornwhich is the standard ASGI server for FastAPI.
Step 1: Save the script as
app.py(e.g., in your current directory)
Step 2: Run using uvicorn
If you have Python and uvicorn installed, run this command in your terminal:
uvicorn app:app --host 0.0.0.0 --port 8000 --reload - Save the script to a file, say
- User
its script name is ucs btw
- Assistant
Thanks for the info!
If your script is named
ucs.py, then the command to run it with uvicorn is:uvicorn ucs:app --host 0.0.0.0 --port 8000 --reloadExplanation:
ucsis your filename without.pyappis the FastAPI app object insideucs.py
Make sure you run this command from the directory where
ucs.pyis located.
Full example:
uvicorn ucs:app --host 0.0.0.0 --port 8000 --reloadThis will start your FastAPI server serving the endpoints defined in
ucs.py(your posted script).
Prerequisites (if needed):
pip install fastapi uvicorn transformers torch soundfile
Feel free to ask if you want help with any setup details!
- User
my code points to http://localhost:21421/transcribe should we change the port u put 8080?
- Assistant
You mentioned your code points to
http://localhost:21421/transcribe, so to keep that consistent, you should run your FastAPI app on port 21421.The port doesn't have to be 8000 or 8080 — it can be any free port you want.
So, your
uvicornrun command should be:uvicorn ucs:app --host 0.0.0.0 --port 21421 --reload- This starts the server on port 21421.
- Then your endpoint will be available at
http://localhost:21421/transcribe.
Summary
Use this command to keep your API accessible at expected URL:
uvicorn ucs:app --host 0.0.0.0 --port 21421 --reload
Let me know if you want to run it on a different host/IP or need help with deployment!