upload download model from gui

This commit is contained in:
David Brazda
2023-12-06 15:23:05 +01:00
parent 01ff23907f
commit 5a5e94eeb5
4 changed files with 93 additions and 4 deletions

View File

@ -35,6 +35,8 @@ from v2realbot.reporting.metricstoolsimage import generate_trading_report_image
from traceback import format_exc
#from v2realbot.reporting.optimizecutoffs import find_optimal_cutoff
import v2realbot.reporting.analyzer as ci
import shutil
from starlette.responses import JSONResponse
#from async io import Queue, QueueEmpty
#
# install()
@ -812,6 +814,25 @@ def list_models():
model_files = os.listdir(models_directory)
return {"models": model_files}
@app.post("/model/upload-model", dependencies=[Depends(api_key_auth)])
def upload_model(file: UploadFile = File(...)):
if not file:
raise HTTPException(status_code=400, detail="No file uploaded.")
file_location = os.path.join(MODEL_DIR, file.filename)
with open(file_location, "wb+") as file_object:
shutil.copyfileobj(file.file, file_object)
return JSONResponse(status_code=200, content={"message": "Model uploaded successfully."})
@app.delete("/model/delete-model/{model_name}", dependencies=[Depends(api_key_auth)])
def delete_model(model_name: str):
model_path = os.path.join(MODEL_DIR, model_name)
if os.path.exists(model_path):
os.remove(model_path)
return {"message": "Model deleted successfully."}
else:
raise HTTPException(status_code=404, detail="Model not found.")
# Thread function to insert data from the queue into the database
def insert_queue2db():
print("starting insert_queue2db thread")

View File

@ -790,8 +790,13 @@
<h4>Model Configuration</h4>
</label>
<div id="MLContainerInner" class="collapse show collapsible-section">
<button id="ml-refresh-button">Refresh Models</button>
<button id="ml-refresh-button" class="btn btn-outline-success btn-sm">Refresh Models</button>
<div id="model-list" class="scrollable-div"></div>
<!-- Upload Form -->
<form id="upload-form" enctype="multipart/form-data" style="width: 229px;">
<input type="file" class="form-control form-control-sm" id="model-file" name="model-file">
<button type="submit" class="btn btn-outline-success btn-sm">Upload Model</button>
</form>
</div>
</div>

View File

@ -13,7 +13,7 @@ $(document).ready(function() {
} else {
const models = response.models;
models.forEach(function(model) {
$('#model-list').append('<p>' + model + '</p>');
$('#model-list').append('<p>' + model + ' <span class="delete-model" data-model="' + model + '">x</span></p>');
});
}
},
@ -23,6 +23,43 @@ $(document).ready(function() {
});
}
function deleteModel(modelName) {
$.ajax({
url: '/model/delete-model/' + modelName,
type: 'DELETE',
beforeSend: function (xhr) {
xhr.setRequestHeader('X-API-Key',
API_KEY); },
success: function(response) {
fetchModels(); // Refresh the list after deletion
},
error: function(xhr, status, error) {
alert('Error deleting model: ' + error);
}
});
}
function uploadModel(formData) {
$.ajax({
url: '/model/upload-model',
type: 'POST',
beforeSend: function (xhr) {
xhr.setRequestHeader('X-API-Key',
API_KEY); },
data: formData,
processData: false,
contentType: false,
success: function(response) {
fetchModels(); // Refresh the list after uploading
alert('Model uploaded successfully');
},
error: function(xhr, status, error) {
alert('Error uploading model: ' + error);
}
});
}
// Fetch models on page load
fetchModels();
@ -30,4 +67,24 @@ $(document).ready(function() {
$('#ml-refresh-button').click(function() {
fetchModels();
});
$('#model-list').on('click', '.delete-model', function() {
const modelName = $(this).data('model');
if (confirm('Are you sure you want to delete ' + modelName + '?')) {
deleteModel(modelName);
}
});
$('#upload-form').submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
if (!$('#model-file')[0].files[0]) {
console.log("prazdne")
alert("No file selected.")
return
}
formData.append('file', $('#model-file')[0].files[0]); // Make sure 'file' matches the FastAPI parameter
uploadModel(formData);
});
});

View File

@ -49,13 +49,19 @@
}
.scrollable-div {
width: 177px;
height: 140px;
width: 229px;
height: 143px;
overflow-y: auto;
border: 1px solid #585858;
padding: 10px;
}
.delete-model {
color: red;
cursor: pointer;
margin-left: 10px;
}
.stat_div {
display: contents;
}