m4k1-dev commited on
Commit
4c7a799
·
1 Parent(s): 3c5e053
Files changed (8) hide show
  1. Dockerfile +27 -0
  2. README.md +35 -2
  3. app/__init__.py +1 -0
  4. app/app.py +168 -0
  5. app/run.py +7 -0
  6. docker-compose.yml +14 -0
  7. model/__init__.py +0 -0
  8. pyproject.toml +32 -0
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10.14-bookworm
2
+
3
+ ARG USER_UID=10002
4
+ ARG USER_GID=$USER_UID
5
+ ARG USERNAME=modelapi
6
+
7
+ RUN groupadd --gid $USER_GID $USERNAME \
8
+ && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
9
+
10
+ # Copy required files
11
+ RUN mkdir -p /modelapi && mkdir -p /home/$USERNAME/.modelapi
12
+ COPY app /modelapi/app
13
+ COPY model /modelapi/model
14
+ COPY pyproject.toml /modelapi/pyproject.toml
15
+
16
+ # Setup permissions
17
+ RUN chown -R $USER_UID:$USER_GID /modelapi \
18
+ && chown -R $USER_UID:$USER_GID /home/$USERNAME/.modelapi \
19
+ && chown -R $USER_UID:$USER_GID /home/$USERNAME \
20
+ && chmod -R 755 /home/$USERNAME \
21
+ && chmod -R 755 /modelapi \
22
+ && chmod -R 777 /home/$USERNAME/.modelapi
23
+
24
+ # Change to the user and do subnet installation
25
+ USER $USERNAME
26
+
27
+ RUN /bin/bash -c "python3 -m venv /modelapi/.venv && source /modelapi/.venv/bin/activate && pip3 install -e /modelapi/."
README.md CHANGED
@@ -1,2 +1,35 @@
1
- # SoundsRightModelContainer
2
- Model container template for the SoundsRight subnet.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Container Template for SoundsRight Subnet Miners
2
+
3
+ Miners in [Bittensor's](https://bittensor.com/) [SoundsRight Subnet](https://github.com/synapsec-ai/SoundsRightSubnet) must containerize their models before uploading to HuggingFace. This repo serves as a template.
4
+
5
+ The branches `DENOISING_16000HZ` and `DEREVERBERATION_16000HZ` contain this template fitted with [SGMSE+](https://huggingface.co/sp-uhh/speech-enhancement-sgmse) and are also helpful resources for how to incorporate your model.
6
+
7
+ This repo contains a template for a container that will spin up an API to communicate with the validator. The following entrypoints cannot be altered:
8
+
9
+ 1. `/status/` : Communicates API status
10
+ 2. `/prepare/` : Makes necessary preparations to initialize model (downloading checkpoints, etc.)
11
+ 3. `/upload-audio/` : Upload audio files, save to noisy audio directory
12
+ 4. `/enhance/` : Initialize model, enhance audio files, save to enhanced audio directory
13
+ 5. `/download-enhanced/` : Download enhanced audio files
14
+
15
+ To add your own model to this template, there are a few things that a miner must do:
16
+
17
+ 1. Add the model files under the `model` directory.
18
+ 2. Modify the `modelapi.prepare` method in `app/app.py` with necessary preparations to initialize your model.
19
+ 3. Modify the `modelapi.enhance` method in `app/app.py` with the logic your model uses to enhance audio.
20
+ 4. Update `dependencies` in `pyproject.toml` with the dependencies used by your model.
21
+ 5. Cite your sources (if applicable).
22
+
23
+ For your model to be processed by validators, there are a few formatting requirements. Note that the template already has been formatted to fit these guidelines.
24
+
25
+ 1. API endpoints must as outlined above.
26
+ 2. Port must be 6500.
27
+ 3. There should only be one service in `docker-compose.yml`.
28
+ 4. Container must be configured to run as non-root user.
29
+ 5. Container names cannot be any of the following:
30
+
31
+ - common-validator
32
+ - soundsright-validator-debug-mode
33
+ - soundsright-validator-debug-mode-dev
34
+ - soundsright-validator
35
+ - soundsright-validator-dev
app/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .app import modelapi
app/app.py ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import fastapi
2
+ import shutil
3
+ import os
4
+ import zipfile
5
+ import io
6
+ import uvicorn
7
+ import threading
8
+ import glob
9
+ from typing import List
10
+
11
+ class modelapi:
12
+
13
+ def __init__(self, host, port):
14
+
15
+ self.host = host
16
+ self.port = port
17
+
18
+ self.base_path = os.path.join(os.path.expanduser("~"), ".modelapi")
19
+ self.noisy_audio_path = os.path.join(self.base_path, "noisy_audio")
20
+ self.enhanced_audio_path = os.path.join(self.base_path, "enhanced_audio")
21
+
22
+ # Create directories if they do not exist
23
+ for audio_path in [self.noisy_audio_path, self.enhanced_audio_path]:
24
+ if not os.path.exists(audio_path):
25
+ os.makedirs(audio_path)
26
+
27
+ # Loop through all the files and subdirectories in the directory
28
+ for filename in os.listdir(audio_path):
29
+ file_path = os.path.join(audio_path, filename)
30
+
31
+ # Check if it's a file or directory and remove accordingly
32
+ try:
33
+ if os.path.isfile(file_path) or os.path.islink(file_path):
34
+ os.unlink(file_path) # Remove the file or link
35
+ elif os.path.isdir(file_path):
36
+ shutil.rmtree(file_path) # Remove the directory and its contents
37
+ except Exception as e:
38
+ Utils.subnet_logger(
39
+ severity="ERROR",
40
+ message=f"Failed to delete {file_path}. Reason: {e}",
41
+ log_level=self.log_level
42
+ )
43
+
44
+ self.app = fastapi.FastAPI()
45
+ self._setup_routes()
46
+
47
+ def prepare(self):
48
+ """Miners should modify this function to fit their fine-tuned models.
49
+
50
+ This function will make any preparations necessary to initialize the
51
+ speech enhancement model (i.e. downloading checkpoint files, etc.)
52
+ """
53
+ # Continue from here
54
+ pass
55
+
56
+ def enhance(self):
57
+ """
58
+ Miners should modify this function to fit their fine-tuned models.
59
+
60
+ This function will:
61
+ 1. Open each noisy .wav file
62
+ 2. Enhance the audio with the model
63
+ 3. Save the enhanced audio in .wav format to MinerAPI.enhanced_audio_path
64
+
65
+ Args:
66
+ noisy_dir (str): The path to the directory containing the audio files to be enhanced.
67
+ enhanced_dir (str): The path to the directory where the enhanced audio files will be saved (the filenames must be identical to those in the noisy_dir).
68
+ """
69
+
70
+ # Define file paths for all noisy files to be enhanced
71
+ noisy_files = sorted(glob.glob(os.path.join(self.noisy_audio_path, '*.wav')))
72
+ for noisy_file in noisy_files:
73
+
74
+ # Continue from here
75
+ pass
76
+
77
+ def _setup_routes(self):
78
+ """
79
+ Setup API routes:
80
+
81
+ /status/ : Communicates API status
82
+ /upload-audio/ : Upload audio files, save to noisy audio directory
83
+ /enhance/ : Enhance audio files, save to enhanced audio directory
84
+ /download-enhanced/ : Download enhanced audio files
85
+ """
86
+ self.app.get("/status/")(self.get_status)
87
+ self.app.post("/prepare/")(self.prepare)
88
+ self.app.post("/upload-audio/")(self.upload_audio)
89
+ self.app.post("/enhance/")(self.enhance_audio)
90
+ self.app.get("/download-enhanced/")(self.download_enhanced)
91
+
92
+ def get_status(self):
93
+ try:
94
+ return {"container_running": True}
95
+ except:
96
+ raise fastapi.HTTPException(status_code=500, detail="An error occurred while fetching API status.")
97
+
98
+ def prepare(self):
99
+ try:
100
+ self.prepare()
101
+ return {'preparations': True}
102
+ except:
103
+ return fastapi.HTTPException(status_code=500, detail="An error occurred while fetching API status.")
104
+
105
+ def upload_audio(self, files: List[fastapi.UploadFile] = File(...)):
106
+ try:
107
+ uploaded_files = []
108
+
109
+ for file in files:
110
+ # Define the path to save the file
111
+ file_path = os.path.join(noisy_audio_path, file.filename)
112
+
113
+ # Save the uploaded file
114
+ with open(file_path, "wb") as buffer:
115
+ shutil.copyfileobj(file.file, buffer)
116
+
117
+ # Append the file name to the list of uploaded files
118
+ uploaded_files.append(file.filename)
119
+
120
+ return {"uploaded_files": uploaded_files, "status": True}
121
+
122
+ except:
123
+ raise fastapi.HTTPException(status_code=500, detail="An error occurred while uploading the noisy files.")
124
+
125
+ def enhance_audio(self):
126
+ try:
127
+ # Enhance audio
128
+ self.enhance()
129
+ # Obtain list of file paths for enhanced audio
130
+ wav_files = glob.glob(os.path.join(self.enhanced_audio_path, '*.wav'))
131
+ # Extract just the file names
132
+ enhanced_files = [os.path.basename(file) for file in wav_files]
133
+ return {"status": True}
134
+
135
+ except:
136
+ raise fastapi.HTTPException(status_code=500, detail="An error occurred while enhancing the noisy files.")
137
+
138
+ def download_enhanced(self):
139
+ try:
140
+ # Create an in-memory zip file to hold all the enhanced audio files
141
+ zip_buffer = io.BytesIO()
142
+
143
+ with zipfile.ZipFile(zip_buffer, "w") as zip_file:
144
+ # Add each .wav file in the enhanced_audio_path directory to the zip file
145
+ for wav_file in glob.glob(os.path.join(self.enhanced_audio_path, '*.wav')):
146
+ zip_file.write(wav_file, arcname=os.path.basename(wav_file))
147
+
148
+ # Make sure to seek back to the start of the BytesIO object before sending it
149
+ zip_buffer.seek(0)
150
+
151
+ # Send the zip file to the client as a downloadable file
152
+ return fastapi.responses.FileResponse(
153
+ zip_buffer,
154
+ media_type="application/zip",
155
+ filename="enhanced_audio_files.zip"
156
+ )
157
+
158
+ except Exception as e:
159
+ # Log the error if needed, and raise an HTTPException to inform the client
160
+ raise fastapi.HTTPException(status_code=500, detail="An error occurred while creating the download file.")
161
+
162
+ def run(self):
163
+ threading.Thread(
164
+ target=uvicorn.run,
165
+ args=(self.app,),
166
+ kwargs={"host":self.host, "port":self.port},
167
+ daemon=True,
168
+ ).start()
app/run.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from app import modelapi
2
+
3
+ api = modelapi(
4
+ host = "0.0.0.0",
5
+ port = 6500
6
+ )
7
+ api.run()
docker-compose.yml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ services:
2
+ modelapi:
3
+ build:
4
+ context: .
5
+ container_name: modelapi
6
+ ports:
7
+ - "6500:6500"
8
+ environment:
9
+ USER_UID: 10002
10
+ USER_GID: 10002
11
+ USERNAME: modelapi
12
+ volumes:
13
+ - .:/modelapi
14
+ command: /bin/bash -c "source /modelapi/.venv/bin/activate && python3 /modelapi/app/run.py"
model/__init__.py ADDED
File without changes
pyproject.toml ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [build-system]
2
+ requires = ["setuptools", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "modelapi"
7
+ version = "1.0.0"
8
+ description = "This project implements a container for a fine-tuned audio enhancement model."
9
+ readme = { file = "README.md", content-type = "text/markdown" }
10
+ license = { file = "LICENSE" }
11
+ classifiers = [
12
+ "Development Status :: 3 - Beta",
13
+ "Intended Audience :: Developers",
14
+ "Topic :: Software Development :: Build Tools",
15
+ "License :: OSI Approved :: MIT License",
16
+ "Programming Language :: Python :: 3 :: Only",
17
+ "Programming Language :: Python :: 3.10",
18
+ "Topic :: Scientific/Engineering",
19
+ "Topic :: Scientific/Engineering :: Mathematics",
20
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
21
+ "Topic :: Software Development",
22
+ "Topic :: Software Development :: Libraries",
23
+ "Topic :: Software Development :: Libraries :: Python Modules"
24
+ ]
25
+ requires-python = ">=3.10,<3.12"
26
+
27
+ dependencies = [
28
+ "fastapi==0.115.5", "uvicorn==0.32.0",
29
+ ]
30
+
31
+ [tool.setuptools.packages.find]
32
+ include = ["app","model"]