Moving to webSocket format for text streaming

This commit is contained in:
saloui 2023-04-27 08:52:00 +02:00 committed by ParisNeo
parent a886789472
commit 2ae9f97eab
5 changed files with 6067 additions and 5 deletions

20
app.py
View File

@ -32,20 +32,26 @@ from flask import (
stream_with_context, stream_with_context,
send_from_directory send_from_directory
) )
from flask_socketio import SocketIO
from pathlib import Path from pathlib import Path
import gc import gc
app = Flask("GPT4All-WebUI", static_url_path="/static", static_folder="static") app = Flask("GPT4All-WebUI", static_url_path="/static", static_folder="static")
socketio = SocketIO(app)
import time import time
from pyGpt4All.config import load_config, save_config from pyGpt4All.config import load_config, save_config
from pyGpt4All.api import GPT4AllAPI from pyGpt4All.api import GPT4AllAPI
import shutil import shutil
import markdown import markdown
class Gpt4AllWebUI(GPT4AllAPI): class Gpt4AllWebUI(GPT4AllAPI):
def __init__(self, _app, config:dict, personality:dict, config_file_path) -> None: def __init__(self, _app, _socketio, config:dict, personality:dict, config_file_path) -> None:
super().__init__(config, personality, config_file_path) super().__init__(config, personality, config_file_path)
self.app = _app self.app = _app
self.cancel_gen = False self.cancel_gen = False
self.socketio = _socketio
self.add_endpoint( self.add_endpoint(
@ -292,6 +298,15 @@ class Gpt4AllWebUI(GPT4AllAPI):
self.cancel_gen = False self.cancel_gen = False
return bot_says return bot_says
# Socket IO stuff
@socketio.on('connected')
def handle_connection(self, data):
self.socketio.emit('message', {'data': 'WebSocket connected!'})
for i in range(10):
socketio.emit('message', {'data': 'Message ' + str(i)})
def generate(self): def generate(self):
if self.current_discussion is None: if self.current_discussion is None:
@ -591,8 +606,7 @@ if __name__ == "__main__":
# executor = ThreadPoolExecutor(max_workers=1) # executor = ThreadPoolExecutor(max_workers=1)
# app.config['executor'] = executor # app.config['executor'] = executor
bot = Gpt4AllWebUI(app, socketio, config, personality, config_file_path)
bot = Gpt4AllWebUI(app, config, personality, config_file_path)
if config["debug"]: if config["debug"]:
app.run(debug=True, host=config["host"], port=config["port"]) app.run(debug=True, host=config["host"], port=config["port"])

View File

@ -1,4 +1,5 @@
flask flask
flask_socketio
nomic nomic
pytest pytest
pyyaml pyyaml

View File

@ -1,8 +1,9 @@
flask flask
flask_socketio
nomic nomic
pytest pytest
pyyaml pyyaml
markdown markdown
pyllamacpp==1.0.6 pyllamacpp==1.0.7
gpt4all-j gpt4all-j
transformers transformers

6046
static/js/socket.io.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -55,7 +55,7 @@
</div> </div>
<footer class="border-t border-accent flex"> <footer class="border-t border-accent flex">
</footer> </footer>
<script src="{{ url_for('static', filename='js/socket.io.js') }}"></script>
<script src="{{ url_for('static', filename='js/chat.js') }}"></script> <script src="{{ url_for('static', filename='js/chat.js') }}"></script>
<script src="{{ url_for('static', filename='js/marked.min.js') }}"></script> <script src="{{ url_for('static', filename='js/marked.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/discussions.js') }}"></script> <script src="{{ url_for('static', filename='js/discussions.js') }}"></script>