mirror of
https://github.com/ParisNeo/lollms-webui.git
synced 2025-01-18 02:39:47 +00:00
fixed uploading large files
This commit is contained in:
parent
9f7bb92db2
commit
8e3e26b563
132
api/__init__.py
132
api/__init__.py
@ -575,63 +575,95 @@ class LoLLMsAPPI(LollmsApplication):
|
||||
except Exception as ex:
|
||||
trace_exception(ex)
|
||||
|
||||
@socketio.on('send_file')
|
||||
def send_file(data):
|
||||
ASCIIColors.yellow("Receiving file")
|
||||
|
||||
@socketio.on('send_file_chunk')
|
||||
def send_file_chunk(data):
|
||||
client_id = request.sid
|
||||
self.connections[client_id]["generated_text"] = ""
|
||||
self.connections[client_id]["cancel_generation"] = False
|
||||
filename = data['filename']
|
||||
chunk = data['chunk']
|
||||
offset = data['offset']
|
||||
is_last_chunk = data['isLastChunk']
|
||||
chunk_index = data['chunkIndex']
|
||||
path:Path = self.lollms_paths.personal_uploads_path / self.personality.personality_folder_name
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
file_path = path / data["filename"]
|
||||
# Save the chunk to the server or process it as needed
|
||||
# For example:
|
||||
if chunk_index==0:
|
||||
with open(file_path, 'wb') as file:
|
||||
file.write(chunk)
|
||||
else:
|
||||
with open(file_path, 'ab') as file:
|
||||
file.write(chunk)
|
||||
|
||||
if not self.config.use_files:
|
||||
self.socketio.emit('file_received',
|
||||
{
|
||||
"status":False,
|
||||
"filename":data["filename"],
|
||||
"error":"Couldn't receive file: Verify that file type is compatible with the personality"
|
||||
}, room=client_id
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
self.personality.setCallback(partial(self.process_chunk,client_id = client_id))
|
||||
ASCIIColors.info("Recovering file from front end")
|
||||
|
||||
path:Path = self.lollms_paths.personal_uploads_path / self.personality.personality_folder_name
|
||||
path.mkdir(parents=True, exist_ok=True)
|
||||
file_path = path / data["filename"]
|
||||
File64BitsManager.b642file(data["fileData"],file_path)
|
||||
if is_last_chunk:
|
||||
print('File received and saved successfully')
|
||||
if self.personality.processor:
|
||||
result = self.personality.processor.add_file(file_path, partial(self.process_chunk, client_id=client_id))
|
||||
else:
|
||||
result = self.personality.add_file(file_path, partial(self.process_chunk, client_id=client_id))
|
||||
if result:
|
||||
self.socketio.emit('file_received',
|
||||
{
|
||||
"status":True,
|
||||
"filename":data["filename"],
|
||||
}, room=client_id
|
||||
)
|
||||
else:
|
||||
self.socketio.emit('file_received',
|
||||
{
|
||||
"status":False,
|
||||
"filename":data["filename"],
|
||||
"error":"Couldn't receive file: Verify that file type is compatible with the personality"
|
||||
}, room=client_id
|
||||
)
|
||||
|
||||
except Exception as ex:
|
||||
ASCIIColors.error(ex)
|
||||
trace_exception(ex)
|
||||
self.socketio.emit('file_received',
|
||||
{
|
||||
"status":False,
|
||||
"filename":data["filename"],
|
||||
"error":"Couldn't receive file: "+str(ex)
|
||||
}, room=client_id
|
||||
)
|
||||
self.close_message(client_id)
|
||||
self.socketio.emit('file_received', {'status': True, 'filename': filename})
|
||||
else:
|
||||
# Request the next chunk from the client
|
||||
self.socketio.emit('request_next_chunk', {'offset': offset + len(chunk)})
|
||||
|
||||
# @socketio.on('send_file')
|
||||
# def send_file(data):
|
||||
# ASCIIColors.yellow("Receiving file")
|
||||
|
||||
# client_id = request.sid
|
||||
# self.connections[client_id]["generated_text"] = ""
|
||||
# self.connections[client_id]["cancel_generation"] = False
|
||||
|
||||
# if not self.config.use_files:
|
||||
# self.socketio.emit('file_received',
|
||||
# {
|
||||
# "status":False,
|
||||
# "filename":data["filename"],
|
||||
# "error":"Couldn't receive file: Verify that file type is compatible with the personality"
|
||||
# }, room=client_id
|
||||
# )
|
||||
# return
|
||||
|
||||
# try:
|
||||
# self.personality.setCallback(partial(self.process_chunk,client_id = client_id))
|
||||
# ASCIIColors.info("Recovering file from front end")
|
||||
|
||||
# path:Path = self.lollms_paths.personal_uploads_path / self.personality.personality_folder_name
|
||||
# path.mkdir(parents=True, exist_ok=True)
|
||||
# file_path = path / data["filename"]
|
||||
# File64BitsManager.b642file(data["fileData"],file_path)
|
||||
# if self.personality.processor:
|
||||
# result = self.personality.processor.add_file(file_path, partial(self.process_chunk, client_id=client_id))
|
||||
# else:
|
||||
# result = self.personality.add_file(file_path, partial(self.process_chunk, client_id=client_id))
|
||||
# if result:
|
||||
# self.socketio.emit('file_received',
|
||||
# {
|
||||
# "status":True,
|
||||
# "filename":data["filename"],
|
||||
# }, room=client_id
|
||||
# )
|
||||
# else:
|
||||
# self.socketio.emit('file_received',
|
||||
# {
|
||||
# "status":False,
|
||||
# "filename":data["filename"],
|
||||
# "error":"Couldn't receive file: Verify that file type is compatible with the personality"
|
||||
# }, room=client_id
|
||||
# )
|
||||
|
||||
# except Exception as ex:
|
||||
# ASCIIColors.error(ex)
|
||||
# trace_exception(ex)
|
||||
# self.socketio.emit('file_received',
|
||||
# {
|
||||
# "status":False,
|
||||
# "filename":data["filename"],
|
||||
# "error":"Couldn't receive file: "+str(ex)
|
||||
# }, room=client_id
|
||||
# )
|
||||
# self.close_message(client_id)
|
||||
|
||||
@self.socketio.on('cancel_text_generation')
|
||||
def cancel_text_generation(data):
|
||||
|
7
app.py
7
app.py
@ -135,10 +135,11 @@ log = logging.getLogger('werkzeug')
|
||||
log.setLevel(logging.ERROR)
|
||||
|
||||
app = Flask("Lollms-WebUI", static_url_path="/static", static_folder="static")
|
||||
|
||||
from flask_compress import Compress
|
||||
# async_mode='gevent', ping_timeout=1200, ping_interval=120,
|
||||
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='threading',engineio_options={'websocket_compression': False, 'websocket_ping_interval': 20, 'websocket_ping_timeout': 120, 'websocket_max_queue': 100})
|
||||
|
||||
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='gevent', ping_timeout=1200, ping_interval=120)
|
||||
#socketio = SocketIO(app, cors_allowed_origins="*", async_mode='threading',engineio_options={'websocket_compression': True, 'websocket_ping_interval': 20, 'websocket_ping_timeout': 120, 'websocket_max_queue': 100})
|
||||
compress = Compress(app)
|
||||
app.config['SECRET_KEY'] = 'secret!'
|
||||
# Set the logging level to WARNING or higher
|
||||
logging.getLogger('socketio').setLevel(logging.WARNING)
|
||||
|
1
mPLUG-Owl
Submodule
1
mPLUG-Owl
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 7f1d5f8148a0c9ea224548dfe0a72a823cdd2eae
|
@ -21,6 +21,8 @@ def run_git_pull():
|
||||
submodule_repo = submodule.module()
|
||||
submodule_repo.git.checkout('main')
|
||||
print(f"Checking out main from {submodule}")
|
||||
submodule.update()
|
||||
print(f"Checking out main from {submodule}")
|
||||
|
||||
except Exception as ex:
|
||||
print(f"Couldn't checkout module {submodule}")
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
4
web/dist/index.html
vendored
4
web/dist/index.html
vendored
@ -6,8 +6,8 @@
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>LoLLMS WebUI - Welcome</title>
|
||||
<script type="module" crossorigin src="/assets/index-bebc2e4a.js"></script>
|
||||
<link rel="stylesheet" href="/assets/index-234c3c92.css">
|
||||
<script type="module" crossorigin src="/assets/index-925b269b.js"></script>
|
||||
<link rel="stylesheet" href="/assets/index-9d93b9ab.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
|
@ -552,42 +552,81 @@ export default {
|
||||
this.filesList = []
|
||||
this.isFileSentList = []
|
||||
},
|
||||
send_file(file, next){
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
console.log("Uploading file")
|
||||
// Read the file as a data URL and emit it to the server
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
const data = {
|
||||
filename: file.name,
|
||||
fileData: reader.result,
|
||||
};
|
||||
socket.on('file_received',(resp)=>{
|
||||
if(resp.status){
|
||||
console.log(resp.filename)
|
||||
this.isFileSentList[this.filesList.length-1]=true;
|
||||
console.log(this.isFileSentList)
|
||||
this.onShowToastMessage("File uploaded successfully",4,true);
|
||||
}
|
||||
else{
|
||||
this.onShowToastMessage("Couldn't upload file\n"+resp.error,4,false);
|
||||
try{
|
||||
this.filesList.removeItem(file)
|
||||
}
|
||||
catch{
|
||||
// send_file(file, next){
|
||||
// const formData = new FormData();
|
||||
// formData.append('file', file);
|
||||
// console.log("Uploading file")
|
||||
// // Read the file as a data URL and emit it to the server
|
||||
// const reader = new FileReader();
|
||||
// reader.onload = () => {
|
||||
// const data = {
|
||||
// filename: file.name,
|
||||
// fileData: reader.result,
|
||||
// };
|
||||
// socket.on('file_received',(resp)=>{
|
||||
// if(resp.status){
|
||||
// console.log(resp.filename)
|
||||
// this.isFileSentList[this.filesList.length-1]=true;
|
||||
// console.log(this.isFileSentList)
|
||||
// this.onShowToastMessage("File uploaded successfully",4,true);
|
||||
// }
|
||||
// else{
|
||||
// this.onShowToastMessage("Couldn't upload file\n"+resp.error,4,false);
|
||||
// try{
|
||||
// this.filesList.removeItem(file)
|
||||
// }
|
||||
// catch{
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
}
|
||||
socket.off('file_received')
|
||||
next()
|
||||
// }
|
||||
// socket.off('file_received')
|
||||
// next()
|
||||
|
||||
})
|
||||
socket.emit('send_file', data);
|
||||
// })
|
||||
// console.log("Sending file")
|
||||
// socket.emit('send_file', data);
|
||||
// };
|
||||
// reader.readAsDataURL(file);
|
||||
// },
|
||||
send_file(file, next) {
|
||||
const fileReader = new FileReader();
|
||||
const chunkSize = 24 * 1024; // Chunk size in bytes (e.g., 1MB)
|
||||
let offset = 0;
|
||||
let chunkIndex = 0;
|
||||
fileReader.onloadend = () => {
|
||||
if (fileReader.error) {
|
||||
console.error('Error reading file:', fileReader.error);
|
||||
return;
|
||||
}
|
||||
const chunk = fileReader.result;
|
||||
const isLastChunk = offset + chunk.byteLength >= file.size;
|
||||
socket.emit('send_file_chunk', {
|
||||
filename: file.name,
|
||||
chunk: chunk,
|
||||
offset: offset,
|
||||
isLastChunk: isLastChunk,
|
||||
chunkIndex: chunkIndex
|
||||
});
|
||||
offset += chunk.byteLength;
|
||||
chunkIndex++;
|
||||
if (!isLastChunk) {
|
||||
readNextChunk();
|
||||
} else {
|
||||
console.log('File sent successfully');
|
||||
this.isFileSentList[this.filesList.length-1]=true;
|
||||
console.log(this.isFileSentList)
|
||||
this.onShowToastMessage("File uploaded successfully",4,true);
|
||||
next();
|
||||
}
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
},
|
||||
function readNextChunk() {
|
||||
const blob = file.slice(offset, offset + chunkSize);
|
||||
fileReader.readAsArrayBuffer(blob);
|
||||
}
|
||||
console.log('Uploading file');
|
||||
readNextChunk();
|
||||
},
|
||||
makeAnEmptyMessage() {
|
||||
this.$emit('createEmptyMessage')
|
||||
},
|
||||
|
@ -10,7 +10,7 @@ import io from 'socket.io-client';
|
||||
const URL = process.env.NODE_ENV === "production" ? undefined : (import.meta.env.VITE_LOLLMS_API);
|
||||
const socket = new io(URL,{
|
||||
reconnection: true, // Enable reconnection
|
||||
reconnectionAttempts: 3, // Maximum reconnection attempts
|
||||
reconnectionAttempts: 10, // Maximum reconnection attempts
|
||||
reconnectionDelay: 1000, // Delay between reconnection attempts (in milliseconds)
|
||||
});
|
||||
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit cc80d4f303fc1027b1cbe24b7f6bd8be7e4e7f95
|
||||
Subproject commit 002376c68bb41ac07a363e8d37db557c52e057a1
|
@ -1 +1 @@
|
||||
Subproject commit 7b06d5cbddf4cebaca27aad62f399b5f059fbb84
|
||||
Subproject commit ddd8c60e32cc21d4a6a1eec599b29377fbcc5ed5
|
@ -1 +1 @@
|
||||
Subproject commit 0f23b00853064b6bff8ba30cc78d0d9c4c0070bb
|
||||
Subproject commit d9400f76f34505b1005a1ed0cc8d82d6443b980d
|
Loading…
Reference in New Issue
Block a user