Spaces:
Runtime error
Runtime error
Lambang
commited on
Commit
·
f439788
1
Parent(s):
a3de917
ada
Browse files
main.py
CHANGED
@@ -60,11 +60,11 @@ async def root():
|
|
60 |
return {"message": "Hello, World!", "ngrok_url": ngrok_url}
|
61 |
|
62 |
|
63 |
-
|
64 |
-
|
65 |
data_processor = DataProcessing()
|
66 |
data_train_pred = TrainPred()
|
67 |
-
|
68 |
import random
|
69 |
def preprocessing(filepath):
|
70 |
folder_path = './static/temporary'
|
@@ -122,115 +122,3 @@ def get_images():
|
|
122 |
urls.append(url)
|
123 |
bentuk, persentase = data_train_pred.prediction(selected_model)
|
124 |
return {'urls': urls, 'bentuk_wajah':bentuk[0], 'persen':persentase}
|
125 |
-
|
126 |
-
|
127 |
-
## -------------------------------------------------------------------------
|
128 |
-
## API UNTUK MELAKUKAN PROSES TRAINING
|
129 |
-
## -------------------------------------------------------------------------
|
130 |
-
|
131 |
-
# Model pydantic untuk validasi body
|
132 |
-
class TrainingParams(BaseModel):
|
133 |
-
optimizer: str
|
134 |
-
epoch: int
|
135 |
-
batchSize: int
|
136 |
-
|
137 |
-
@app.post('/upload/dataset', tags=["Training"])
|
138 |
-
async def upload_data(dataset: UploadFile):
|
139 |
-
if dataset.filename == '':
|
140 |
-
raise HTTPException(status_code=400, detail='No file selected for uploading')
|
141 |
-
|
142 |
-
# Buat path lengkap untuk menyimpan file
|
143 |
-
file_path = os.path.join(UPLOAD_FOLDER, dataset.filename)
|
144 |
-
|
145 |
-
# Simpan file ke folder yang ditentukan
|
146 |
-
with open(file_path, "wb") as file_object:
|
147 |
-
file_object.write(dataset.file.read())
|
148 |
-
|
149 |
-
# Panggil fungsi untuk mengekstrak file jika perlu
|
150 |
-
FileProcess.extract_zip(file_path)
|
151 |
-
|
152 |
-
return {'message': 'File successfully uploaded'}
|
153 |
-
|
154 |
-
@app.post('/set_params', tags=["Training"])
|
155 |
-
async def set_params(request: Request, params: TrainingParams):
|
156 |
-
global optimizer, epoch, batch_size
|
157 |
-
|
158 |
-
optimizer = params.optimizer
|
159 |
-
epoch = params.epoch
|
160 |
-
batch_size = params.batchSize
|
161 |
-
|
162 |
-
response = {'message': 'Set parameter sukses'}
|
163 |
-
return response
|
164 |
-
|
165 |
-
@app.get('/get_info_data', tags=["Training"])
|
166 |
-
def get_info_prepro():
|
167 |
-
global optimizer, epoch, batch_size
|
168 |
-
training_counts = GetLoadData.get_training_file_counts().json
|
169 |
-
testing_counts = GetLoadData.get_testing_file_counts().json
|
170 |
-
response = {
|
171 |
-
"optimizer": optimizer,
|
172 |
-
"epoch": epoch,
|
173 |
-
"batch_size": batch_size,
|
174 |
-
"training_counts": training_counts,
|
175 |
-
"testing_counts": testing_counts
|
176 |
-
}
|
177 |
-
return response
|
178 |
-
|
179 |
-
@app.get('/get_images_preprocess', tags=["Training"])
|
180 |
-
def get_random_images_crop():
|
181 |
-
images_face_landmark = GetLoadData.get_random_images(tahap="Face Landmark",public_url=public_url)
|
182 |
-
images_face_extraction = GetLoadData.get_random_images(tahap="landmark Extraction", public_url=public_url)
|
183 |
-
|
184 |
-
response = {
|
185 |
-
"face_landmark": images_face_landmark,
|
186 |
-
"landmark_extraction": images_face_extraction
|
187 |
-
}
|
188 |
-
return response
|
189 |
-
|
190 |
-
@app.get('/do_preprocessing', tags=["Training"])
|
191 |
-
async def do_preprocessing():
|
192 |
-
try:
|
193 |
-
data_train_pred.do_pre1(test="")
|
194 |
-
data_train_pred.do_pre2(test="")
|
195 |
-
return {'message': 'Preprocessing sukses'}
|
196 |
-
except Exception as e:
|
197 |
-
# Tangani kesalahan dan kembalikan respons kesalahan
|
198 |
-
error_message = f'Error during preprocessing: {str(e)}'
|
199 |
-
raise HTTPException(status_code=500, detail=error_message)
|
200 |
-
|
201 |
-
@app.get('/do_training', tags=["Training"])
|
202 |
-
def do_training():
|
203 |
-
global epoch
|
204 |
-
folder = ""
|
205 |
-
if (face_landmark_img == True and landmark_extraction_img == True):
|
206 |
-
folder = "Landmark Extraction"
|
207 |
-
elif (face_landmark_img == True and landmark_extraction_img == False):
|
208 |
-
folder = "Face Landmark"
|
209 |
-
# --------------------------------------------------------------
|
210 |
-
train_dataset_path = f"./static/dataset/{folder}/Training/"
|
211 |
-
test_dataset_path = f"./static/dataset/{folder}/Testing/"
|
212 |
-
|
213 |
-
train_image_df, test_image_df = GetLoadData.load_image_dataset(train_dataset_path, test_dataset_path)
|
214 |
-
|
215 |
-
train_gen, test_gen = data_train_pred.data_configuration(train_image_df, test_image_df)
|
216 |
-
model = data_train_pred.model_architecture()
|
217 |
-
|
218 |
-
result = data_train_pred.train_model(model, train_gen, test_gen, epoch)
|
219 |
-
|
220 |
-
# Mengambil nilai akurasi training dan validation dari objek result
|
221 |
-
train_acc = result.history['accuracy'][-1]
|
222 |
-
val_acc = result.history['val_accuracy'][-1]
|
223 |
-
|
224 |
-
# Plot accuracy
|
225 |
-
data_train_pred.plot_accuracy(result=result, epoch=epoch)
|
226 |
-
acc_url = f'{public_url}/static/accuracy_plot.png'
|
227 |
-
|
228 |
-
# Plot loss
|
229 |
-
data_train_pred.plot_loss(result=result, epoch=epoch)
|
230 |
-
loss_url = f'{public_url}/static/loss_plot.png'
|
231 |
-
|
232 |
-
# Confusion Matrix
|
233 |
-
data_train_pred.plot_confusion_matrix(model, test_gen)
|
234 |
-
conf_url = f'{public_url}/static/confusion_matrix.png'
|
235 |
-
|
236 |
-
return jsonify({'train_acc': train_acc, 'val_acc': val_acc, 'plot_acc': acc_url, 'plot_loss':loss_url,'conf':conf_url})
|
|
|
60 |
return {"message": "Hello, World!", "ngrok_url": ngrok_url}
|
61 |
|
62 |
|
63 |
+
#-----------------------------------------------------
|
64 |
+
|
65 |
data_processor = DataProcessing()
|
66 |
data_train_pred = TrainPred()
|
67 |
+
|
68 |
import random
|
69 |
def preprocessing(filepath):
|
70 |
folder_path = './static/temporary'
|
|
|
122 |
urls.append(url)
|
123 |
bentuk, persentase = data_train_pred.prediction(selected_model)
|
124 |
return {'urls': urls, 'bentuk_wajah':bentuk[0], 'persen':persentase}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|