download model

This commit is contained in:
David Brazda
2023-12-06 19:47:03 +01:00
parent 5a5e94eeb5
commit 8abebcc910
4 changed files with 59 additions and 6 deletions

View File

@ -833,6 +833,14 @@ def delete_model(model_name: str):
else:
raise HTTPException(status_code=404, detail="Model not found.")
@app.get("/model/download-model/{model_name}", dependencies=[Depends(api_key_auth)])
def download_model(model_name: str):
model_path = os.path.join(MODEL_DIR, model_name)
if os.path.exists(model_path):
return FileResponse(path=model_path, filename=model_name, media_type='application/octet-stream')
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

@ -793,7 +793,7 @@
<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;">
<form id="upload-form" enctype="multipart/form-data" style="width: 262px;">
<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>

View File

@ -13,7 +13,13 @@ $(document).ready(function() {
} else {
const models = response.models;
models.forEach(function(model) {
$('#model-list').append('<p>' + model + ' <span class="delete-model" data-model="' + model + '">x</span></p>');
$('#model-list').append(`
<p>${model}
<span class="download-model" data-model="${model}">[↓]</span>
<span class="delete-model" data-model="${model}">[x]</span>
</p>
`);
});
}
},
@ -59,6 +65,32 @@ $(document).ready(function() {
});
}
function downloadModel(modelName) {
$.ajax({
url: '/model/download-model/' + modelName,
type: 'GET',
beforeSend: function (xhr) {
xhr.setRequestHeader('X-API-Key', API_KEY);
},
success: function(data, status, xhr) {
// Get a URL for the blob to download
var blob = new Blob([data], { type: xhr.getResponseHeader('Content-Type') });
var downloadUrl = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = downloadUrl;
a.download = modelName;
document.body.appendChild(a);
a.click();
// Clean up
window.URL.revokeObjectURL(downloadUrl);
a.remove();
},
error: function(xhr, status, error) {
alert('Error downloading model: ' + error);
}
});
}
// Fetch models on page load
fetchModels();
@ -87,4 +119,10 @@ $(document).ready(function() {
uploadModel(formData);
});
//Handler to download the model
$('#model-list').on('click', '.download-model', function() {
const modelName = $(this).data('model');
downloadModel(modelName);
});
});

View File

@ -49,17 +49,24 @@
}
.scrollable-div {
width: 229px;
width: 262px;
height: 143px;
overflow-y: auto;
border: 1px solid #585858;
padding: 10px;
}
.delete-model {
color: red;
/* ... existing CSS ... */
.download-model, .delete-model {
cursor: pointer;
margin-left: 10px;
padding: 0 5px;
}
.download-model {
color: green; /* or use an actual icon */
}
.delete-model {
color: red; /* or use an actual icon */
}
.stat_div {