danja commited on
Commit
07f73eb
·
1 Parent(s): 5bd7839
Files changed (6) hide show
  1. Dockerfile +16 -0
  2. _app.py +26 -0
  3. app.py +8 -0
  4. auth.py +12 -0
  5. hf_config.yaml +4 -0
  6. model_handler.py +13 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
_app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from model_handler import MySeparationModel
3
+ import torchaudio
4
+
5
+ app = FastAPI()
6
+ model = MySeparationModel()
7
+
8
+
9
+ @app.post("/separate/")
10
+ async def separate_audio(file: UploadFile = File(...)):
11
+ # Save input
12
+ input_path = "input.wav"
13
+ with open(input_path, "wb") as f:
14
+ f.write(await file.read())
15
+
16
+ # Process
17
+ separated = model.predict(input_path)
18
+
19
+ # Save outputs
20
+ output_paths = []
21
+ for i in range(separated.shape[-1]):
22
+ path = f"output_{i}.wav"
23
+ torchaudio.save(path, separated[..., i].detach().cpu(), 8000)
24
+ output_paths.append(path)
25
+
26
+ return {"outputs": output_paths}
app.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+
3
+ app = FastAPI()
4
+
5
+
6
+ @app.get("/")
7
+ def greet_json():
8
+ return {"Hello": "World!"}
auth.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import login
2
+ import os
3
+ from dotenv import load_dotenv
4
+
5
+ load_dotenv()
6
+
7
+ hf_key = os.getenv('HF_API_KEY')
8
+
9
+ login(token=hf_key) # From Profile → Access Tokens
10
+
11
+ # path = os.environ["PATH"]
12
+ # print(path)
hf_config.yaml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ # hf_config.yaml
2
+ runtime:
3
+ dependencies_file: requirements.txt
4
+ python_version: "3.10"
model_handler.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # model_handler.py
2
+ from speechbrain.inference.separation import SepformerSeparation
3
+
4
+
5
+ class MySeparationModel:
6
+ def __init__(self):
7
+ self.model = SepformerSeparation.from_hparams(
8
+ source="speechbrain/sepformer-wham",
9
+ savedir="pretrained_models/sepformer-wham"
10
+ )
11
+
12
+ def predict(self, audio_path):
13
+ return self.model.separate_file(audio_path)