m4k1-dev
commited on
Commit
·
5ab4772
1
Parent(s):
51ebd86
updates
Browse files- Dockerfile +0 -1
- README.md +4 -3
- app/app.py +27 -32
- model/__init__.py +0 -0
Dockerfile
CHANGED
@@ -10,7 +10,6 @@ RUN groupadd --gid $USER_GID $USERNAME \
|
|
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
|
|
|
10 |
# Copy required files
|
11 |
RUN mkdir -p /modelapi && mkdir -p /home/$USERNAME/.modelapi
|
12 |
COPY app /modelapi/app
|
|
|
13 |
COPY pyproject.toml /modelapi/pyproject.toml
|
14 |
|
15 |
# Setup permissions
|
README.md
CHANGED
@@ -4,10 +4,10 @@ Miners in [Bittensor's](https://bittensor.com/) [SoundsRight Subnet](https://git
|
|
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 |
-
|
8 |
|
9 |
1. `/status/` : Communicates API status
|
10 |
-
2. `/prepare/` : Makes necessary preparations
|
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
|
@@ -18,7 +18,8 @@ To add your own model to this template, there are a few things that a miner must
|
|
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.
|
|
|
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 |
|
|
|
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 |
+
The `main` branch 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 (downloading checkpoints, etc.) and initializes model
|
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
|
|
|
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. If you have directories other than `app` in your repository, be sure to modify the `Dockerfile` accordingly (reference line 12 in the `Dockerfile` for how to do this).
|
22 |
+
6. Cite your sources (if applicable).
|
23 |
|
24 |
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.
|
25 |
|
app/app.py
CHANGED
@@ -35,25 +35,21 @@ class ModelAPI:
|
|
35 |
elif os.path.isdir(file_path):
|
36 |
shutil.rmtree(file_path) # Remove the directory and its contents
|
37 |
except Exception as e:
|
38 |
-
|
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
|
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
|
57 |
"""
|
58 |
Miners should modify this function to fit their fine-tuned models.
|
59 |
|
@@ -61,16 +57,11 @@ class ModelAPI:
|
|
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 |
|
@@ -97,42 +88,46 @@ class ModelAPI:
|
|
97 |
|
98 |
def prepare(self):
|
99 |
try:
|
100 |
-
self.
|
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] = fastapi.File(...)):
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
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
|
115 |
-
|
|
|
116 |
|
117 |
# Append the file name to the list of uploaded files
|
118 |
-
uploaded_files.append(file.filename)
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
-
|
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.
|
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):
|
@@ -149,15 +144,15 @@ class ModelAPI:
|
|
149 |
zip_buffer.seek(0)
|
150 |
|
151 |
# Send the zip file to the client as a downloadable file
|
152 |
-
return fastapi.responses.
|
153 |
-
zip_buffer,
|
154 |
media_type="application/zip",
|
155 |
-
filename=
|
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 |
|
|
|
35 |
elif os.path.isdir(file_path):
|
36 |
shutil.rmtree(file_path) # Remove the directory and its contents
|
37 |
except Exception as e:
|
38 |
+
raise e
|
|
|
|
|
|
|
|
|
39 |
|
40 |
self.app = fastapi.FastAPI()
|
41 |
self._setup_routes()
|
42 |
|
43 |
+
def _prepare(self):
|
44 |
"""Miners should modify this function to fit their fine-tuned models.
|
45 |
|
46 |
This function will make any preparations necessary to initialize the
|
47 |
speech enhancement model (i.e. downloading checkpoint files, etc.)
|
48 |
"""
|
49 |
# Continue from here
|
50 |
+
pass
|
51 |
|
52 |
+
def _enhance(self):
|
53 |
"""
|
54 |
Miners should modify this function to fit their fine-tuned models.
|
55 |
|
|
|
57 |
1. Open each noisy .wav file
|
58 |
2. Enhance the audio with the model
|
59 |
3. Save the enhanced audio in .wav format to MinerAPI.enhanced_audio_path
|
|
|
|
|
|
|
|
|
60 |
"""
|
61 |
|
62 |
# Define file paths for all noisy files to be enhanced
|
63 |
noisy_files = sorted(glob.glob(os.path.join(self.noisy_audio_path, '*.wav')))
|
64 |
for noisy_file in noisy_files:
|
|
|
65 |
# Continue from here
|
66 |
pass
|
67 |
|
|
|
88 |
|
89 |
def prepare(self):
|
90 |
try:
|
91 |
+
self._prepare()
|
92 |
return {'preparations': True}
|
93 |
except:
|
94 |
return fastapi.HTTPException(status_code=500, detail="An error occurred while fetching API status.")
|
95 |
|
96 |
def upload_audio(self, files: List[fastapi.UploadFile] = fastapi.File(...)):
|
97 |
+
|
98 |
+
uploaded_files = []
|
99 |
+
|
100 |
+
for file in files:
|
101 |
+
try:
|
102 |
# Define the path to save the file
|
103 |
+
file_path = os.path.join(self.noisy_audio_path, file.filename)
|
104 |
|
105 |
# Save the uploaded file
|
106 |
+
with open(file_path, "wb") as f:
|
107 |
+
while contents := file.file.read(1024*1024):
|
108 |
+
f.write(contents)
|
109 |
|
110 |
# Append the file name to the list of uploaded files
|
111 |
+
uploaded_files.append(file.filename)
|
112 |
+
|
113 |
+
except:
|
114 |
+
raise fastapi.HTTPException(status_code=500, detail="An error occurred while uploading the noisy files.")
|
115 |
+
finally:
|
116 |
+
file.file.close()
|
117 |
|
118 |
+
return {"uploaded_files": uploaded_files, "status": True}
|
|
|
|
|
|
|
119 |
|
120 |
def enhance_audio(self):
|
121 |
try:
|
122 |
# Enhance audio
|
123 |
+
self._enhance()
|
124 |
# Obtain list of file paths for enhanced audio
|
125 |
wav_files = glob.glob(os.path.join(self.enhanced_audio_path, '*.wav'))
|
126 |
# Extract just the file names
|
127 |
enhanced_files = [os.path.basename(file) for file in wav_files]
|
128 |
return {"status": True}
|
129 |
|
130 |
+
except Exception as e:
|
131 |
raise fastapi.HTTPException(status_code=500, detail="An error occurred while enhancing the noisy files.")
|
132 |
|
133 |
def download_enhanced(self):
|
|
|
144 |
zip_buffer.seek(0)
|
145 |
|
146 |
# Send the zip file to the client as a downloadable file
|
147 |
+
return fastapi.responses.StreamingResponse(
|
148 |
+
iter([zip_buffer.getvalue()]), # Stream the in-memory content
|
149 |
media_type="application/zip",
|
150 |
+
headers={"Content-Disposition": "attachment; filename=enhanced_audio_files.zip"}
|
151 |
)
|
152 |
|
153 |
except Exception as e:
|
154 |
# Log the error if needed, and raise an HTTPException to inform the client
|
155 |
+
raise fastapi.HTTPException(status_code=500, detail=f"An error occurred while creating the download file: {str(e)}")
|
156 |
|
157 |
def run(self):
|
158 |
|
model/__init__.py
DELETED
File without changes
|