working installation script

This commit is contained in:
saloui
2023-05-14 13:33:45 +02:00
parent 8c691b3102
commit 17f87857a8
3 changed files with 32 additions and 19 deletions

View File

@ -19,6 +19,7 @@ import threading
import time import time
import requests import requests
import urllib.request import urllib.request
from tqdm import tqdm
__author__ = "parisneo" __author__ = "parisneo"
__github__ = "https://github.com/nomic-ai/gpt4all-ui" __github__ = "https://github.com/nomic-ai/gpt4all-ui"
@ -317,7 +318,6 @@ class GPT4AllAPI():
if installation_path.exists(): if installation_path.exists():
print("Error: Model already exists") print("Error: Model already exists")
data.installing = False
socketio.emit('install_progress',{'status': 'failed', 'error': 'model already exists'}) socketio.emit('install_progress',{'status': 'failed', 'error': 'model already exists'})
socketio.emit('install_progress',{'status': 'progress', 'progress': progress}) socketio.emit('install_progress',{'status': 'progress', 'progress': progress})
@ -408,25 +408,41 @@ class GPT4AllAPI():
def download_file(self, url, installation_path, callback=None): def download_file(self, url, installation_path, callback=None):
""" """
Downloads a file from a URL and displays the download progress using tqdm. Downloads a file from a URL, reports the download progress using a callback function, and displays a progress bar.
Args: Args:
url (str): The URL of the file to download. url (str): The URL of the file to download.
installation_path (str): The path where the file should be saved.
callback (function, optional): A callback function to be called during the download callback (function, optional): A callback function to be called during the download
with the progress percentage as an argument. Defaults to None. with the progress percentage as an argument. Defaults to None.
""" """
try: try:
def report_hook(count, block_size, total_size): response = requests.get(url, stream=True)
if callback is not None:
percentage = (count * block_size / total_size) * 100
callback(percentage)
urllib.request.urlretrieve(url, installation_path, reporthook=report_hook) # Get the file size from the response headers
total_size = int(response.headers.get('content-length', 0))
with open(installation_path, 'wb') as file:
downloaded_size = 0
with tqdm(total=total_size, unit='B', unit_scale=True, ncols=80) as progress_bar:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
file.write(chunk)
downloaded_size += len(chunk)
if callback is not None:
percentage = (downloaded_size / total_size) * 100
callback(percentage)
progress_bar.update(len(chunk))
if callback is not None: if callback is not None:
callback(100.0) callback(100.0)
except:
print("Couldn't download file") print("File downloaded successfully")
except Exception as e:
print("Couldn't download file:", str(e))
def load_backend(self, backend_path): def load_backend(self, backend_path):

View File

@ -26,9 +26,9 @@
<template v-if="installing"> <template v-if="installing">
<div class="flex items-center space-x-2"> <div class="flex items-center space-x-2">
<div class="h-2 w-20 bg-gray-300 rounded"> <div class="h-2 w-20 bg-gray-300 rounded">
<div :style="{ width: progress + '%' }" class="h-full bg-green-500"></div> <div :style="{ width: progress + '%'}" class="h-full bg-red-500 rounded"></div>
</div> </div>
<span>Installing...</span> <span>Installing...{{ Math.floor(progress) }}%</span>
</div> </div>
</template> </template>
<template v-else-if="uninstalling"> <template v-else-if="uninstalling">
@ -51,10 +51,6 @@
import socket from '@/services/websocket.js' import socket from '@/services/websocket.js'
export default { export default {
props: { props: {
progress: {
type: Number,
default: 0
},
title: String, title: String,
icon: String, icon: String,
path: String, path: String,
@ -67,6 +63,7 @@ export default {
}, },
data() { data() {
return { return {
progress: 0,
installing: false, installing: false,
uninstalling: false uninstalling: false
}; };

View File

@ -360,7 +360,7 @@ export default {
console.log("received something"); console.log("received something");
if (response.status === 'progress') { if (response.status === 'progress') {
console.log(`Progress = ${response.progress}`); console.log(`Progress = ${response.progress}`);
this.progress = response.progress; model_object.progress = response.progress
} else if (response.status === 'succeeded') { } else if (response.status === 'succeeded') {
socket.off('install_progress', progressListener); socket.off('install_progress', progressListener);
// Update the isInstalled property of the corresponding model // Update the isInstalled property of the corresponding model
@ -373,7 +373,7 @@ export default {
// Installation failed or encountered an error // Installation failed or encountered an error
model_object.installing = false; model_object.installing = false;
this.showProgress = false; this.showProgress = false;
console.error('Installation failed:', message.error); console.error('Installation failed:', response.error);
} }
}; };
@ -385,7 +385,7 @@ export default {
console.log("uninstalling model...") console.log("uninstalling model...")
const progressListener = (response) => { const progressListener = (response) => {
if (response.status === 'progress') { if (response.status === 'progress') {
this.progress = message.progress; this.progress = response.progress;
} else if (response.status === 'succeeded') { } else if (response.status === 'succeeded') {
console.log(model_object) console.log(model_object)
// Installation completed // Installation completed