Merge pull request #1 from ParisNeo/moving-the-code-to-the-new-python-bindings-code

Moving the code to the new python bindings code
This commit is contained in:
Saifeddine ALOUI 2023-04-06 23:19:21 +02:00 committed by GitHub
commit 74ab83b246
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 172 additions and 83 deletions

9
.gitignore vendored
View File

@ -130,4 +130,13 @@ dmypy.json
# Database
*.db
# Docker files
/data
# models
models/
!models/README.md
# Temporary files
tmp/

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"ros.distro": "noetic"
}

113
app.py
View File

@ -1,5 +1,5 @@
from flask import Flask, jsonify, request, render_template, Response, stream_with_context
from nomic.gpt4all import GPT4All
from pyllamacpp.model import Model
import argparse
import threading
from io import StringIO
@ -148,7 +148,6 @@ class Gpt4AllWebUI():
self.app=app
self.db_path= db_path
self.add_endpoint('/', '', self.index, methods=['GET'])
self.add_endpoint('/stream', 'stream', self.stream, methods=['GET'])
self.add_endpoint('/export', 'export', self.export, methods=['GET'])
self.add_endpoint('/new_discussion', 'new_discussion', self.new_discussion, methods=['GET'])
self.add_endpoint('/bot', 'bot', self.bot, methods=['POST'])
@ -159,12 +158,44 @@ class Gpt4AllWebUI():
self.add_endpoint('/update_message', 'update_message', self.update_message, methods=['GET'])
conditionning_message="""
Instruction: Act as GPT4All. A kind and helpful AI bot built to help users solve problems.
Start by welcoming the user then stop sending text.
GPT4All:"""
self.prepare_query(conditionning_message)
chatbot_bindings.generate(conditionning_message, n_predict=55, new_text_callback=self.new_text_callback, n_threads=8)
print(f"Bot said:{self.bot_says}")
# Chatbot conditionning
# response = self.chatbot_bindings.prompt("This is a discussion between A user and an AI. AI responds to user questions in a helpful manner. AI is not allowed to lie or deceive. AI welcomes the user\n### Response:")
# print(response)
def prepare_query(self, message):
self.bot_says=''
self.full_text=''
self.is_bot_text_started=False
self.current_message = message
def new_text_callback(self, text: str):
print(text, end="")
self.full_text += text
if self.is_bot_text_started:
self.bot_says += text
if self.current_message in self.full_text:
self.is_bot_text_started=True
def new_text_callback_with_yield(self, text: str):
"""
To do , fix the problem with yield to be able to show interactive response as text comes
"""
print(text, end="")
self.full_text += text
if self.is_bot_text_started:
self.bot_says += text
if self.current_message in self.full_text:
self.is_bot_text_started=True
yield text
def add_endpoint(self, endpoint=None, endpoint_name=None, handler=None, methods=['GET'], *args, **kwargs):
self.app.add_url_rule(endpoint, endpoint_name, handler, methods=methods, *args, **kwargs)
@ -184,62 +215,28 @@ class Gpt4AllWebUI():
# Return the formatted message
return message
def stream(self):
def generate():
# Replace this with your text-generating code
for i in range(10):
yield f'This is line {i+1}\n'
time.sleep(1)
return Response(stream_with_context(generate()))
def export(self):
return jsonify(export_to_json(self.db_path))
@stream_with_context
def parse_to_prompt_stream(self, message, message_id):
bot_says = ['']
point = b''
bot = self.chatbot_bindings.bot
bot_says = ''
self.stop=False
# very important. This is the maximum time we wait for the model
wait_val = 15.0 # At the beginning the server may need time to send data. we wait 15s
# send the message to the bot
print(f"Received message : {message}")
bot = self.chatbot_bindings.bot
bot.stdin.write(message.encode('utf-8'))
bot.stdin.write(b"\n")
bot.stdin.flush()
# First we need to send the new message ID to the client
response_id = self.current_discussion.add_message("GPT4All",'') # first the content is empty, but we'll fill it at the end
yield(json.dumps({'type':'input_message_infos','message':message, 'id':message_id, 'response_id':response_id}))
#Now let's wait for the bot to answer
while not self.stop:
readable, _, _ = select.select([bot.stdout], [], [], wait_val)
wait_val = 4.0 # Once started, the process doesn't take that much so we reduce the wait
if bot.stdout in readable:
point += bot.stdout.read(1)
try:
character = point.decode("utf-8")
if character == "\n":
bot_says.append('\n')
yield '\n'
else:
bot_says[-1] += character
yield character
point = b''
self.current_message = "User: "+message+"\nGPT4All:"
self.prepare_query(self.current_message)
chatbot_bindings.generate(self.current_message, n_predict=55, new_text_callback=self.new_text_callback, n_threads=8)
self.current_discussion.update_message(response_id,self.bot_says)
yield self.bot_says
# TODO : change this to use the yield version in order to send text word by word
except UnicodeDecodeError:
if len(point) > 4:
point = b''
else:
self.current_discussion.update_message(response_id,bot_says)
return "\n".join(bot_says)
def bot(self):
@ -335,17 +332,19 @@ if __name__ == '__main__':
args = parser.parse_args()
chatbot_bindings = GPT4All(decoder_config = {
'temp': args.temp,
'n_predict':args.n_predict,
'top_k':args.top_k,
'top_p':args.top_p,
#'color': True,#"## Instruction",
'repeat_penalty': args.repeat_penalty,
'repeat_last_n':args.repeat_last_n,
'ctx_size': args.ctx_size
})
chatbot_bindings.open()
chatbot_bindings = Model(ggml_model='./models/gpt4all-converted.bin', n_ctx=512)
# Old Code
# GPT4All(decoder_config = {
# 'temp': args.temp,
# 'n_predict':args.n_predict,
# 'top_k':args.top_k,
# 'top_p':args.top_p,
# #'color': True,#"## Instruction",
# 'repeat_penalty': args.repeat_penalty,
# 'repeat_last_n':args.repeat_last_n,
# 'ctx_size': args.ctx_size
# })
check_discussion_db(args.db_path)
bot = Gpt4AllWebUI(chatbot_bindings, app, args.db_path)

View File

@ -1,18 +1,54 @@
@echo off
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHH .HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHH. ,HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHH.## HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHH#.HHHHH/*,*,*,*,*,*,*,*,***,*,**#HHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHH.*,,***,***,***,***,***,***,*******HHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHH*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*,,,,,HHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHH.,,,***,***,***,***,***,***,***,***,***,***/HHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHH*,,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*HHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHH#,***,***,***,***,***,***,***,***,***,***,***,**HHHHHHHHHHHHHHHHH
echo HHHHHHHHHH..HHH,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*#HHHHHHHHHHHHHHHH
echo HHHHHHH,,,**,/H*,***,***,***,,,*,***,***,***,**,,,**,***,***,***H,,*,***HHHHHHHH
echo HHHHHH.*,,,*,,,,,*,*,*,***#HHHHH.,,*,*,*,*,**/HHHHH.,*,*,*,*,*,*,*,*****HHHHHHHH
echo HHHHHH.*,***,*,*,***,***,.HHHHHHH/**,***,****HHHHHHH.***,***,***,*******HHHHHHHH
echo HHHHHH.,,,,,,,,,,,,,,,,,,,.HHHHH.,,,,,,,,,,,,.HHHHHH,,,,,,,,,,,,,,,,,***HHHHHHHH
echo HHHHHH.,,,,,,/H,,,**,***,***,,,*,***,***,***,**,,,,*,***,***,***H***,***HHHHHHHH
echo HHHHHHH.,,,,*.H,,,,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,***H*,,,,/HHHHHHHHH
echo HHHHHHHHHHHHHHH*,***,***,**,,***,***,***,***,***,***,***,***,**.HHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHH,,,,,,,,*,,#H#,,,,,*,,,*,,,,,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HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
if not exist "./tmp" mkdir "./tmp"
REM Check if Python is installed
set /p="Checking for python..." <nul
where python >nul 2>&1
if %ERRORLEVEL% neq 0 (
echo Python is not installed. Would you like to install Python? [Y/N]
set /p choice=
if /i "%choice%" equ "Y" (
if %errorlevel% neq 0 (
set /p choice=Python is not installed. Would you like to install Python? [Y/N]
if /i ".choice." equ "Y" (
REM Download Python installer
echo Downloading Python installer...
powershell -Command "Invoke-WebRequest -Uri 'https://www.python.org/ftp/python/3.10.0/python-3.10.0-amd64.exe' -OutFile 'python.exe'"
powershell -Command "Invoke-WebRequest -Uri 'https://www.python.org/ftp/python/3.10.0/python-3.10.0-amd64.exe' -OutFile 'tmp/python.exe'"
REM Install Python
echo Installing Python...
python.exe /quiet /norestart
tmp/python.exe /quiet /norestart
) else (
echo Please install Python and try again.
pause
@ -22,20 +58,20 @@ if %ERRORLEVEL% neq 0 (
echo OK
)
REM Check if pip is installed
set /p="Checking for pip..." <nul
python -m pip >nul 2>&1
if %ERRORLEVEL% neq 0 (
echo Pip is not installed. Would you like to install pip? [Y/N]
set /p choice=
if /i "%choice%" equ "Y" (
if %errorlevel% neq 0 (
set /p choice=Pip is not installed. Would you like to install pip? [Y/N]
if /i ".choice." equ "Y" (
REM Download get-pip.py
echo Downloading get-pip.py...
powershell -Command "Invoke-WebRequest -Uri 'https://bootstrap.pypa.io/get-pip.py' -OutFile 'get-pip.py'"
powershell -Command "Invoke-WebRequest -Uri 'https://bootstrap.pypa.io/get-pip.py' -OutFile 'tmp/get-pip.py'"
REM Install pip
echo Installing pip...
python get-pip.py
) else (
python tmp/get-pip.py
) else .
echo Please install pip and try again.
pause
exit /b 1
@ -47,10 +83,9 @@ if %ERRORLEVEL% neq 0 (
REM Check if venv module is available
set /p="Checking for venv..." <nul
python -c "import venv" >nul 2>&1
if %ERRORLEVEL% neq 0 (
echo venv module is not available. Would you like to upgrade Python to the latest version? [Y/N]
set /p choice=
if /i "%choice%" equ "Y" (
if %errorlevel% neq 0 (
set /p choice=venv module is not available. Would you like to upgrade Python to the latest version? [Y/N]
if /i ".choice." equ "Y" (
REM Upgrade Python
echo Upgrading Python...
python -m pip install --upgrade pip setuptools wheel
@ -67,7 +102,7 @@ if %ERRORLEVEL% neq 0 (
REM Create a new virtual environment
set /p="Creating virtual environment ..." <nul
python -m venv env
if %ERRORLEVEL% neq 0 (
if %errorlevel% neq 0 (
echo Failed to create virtual environment. Please check your Python installation and try again.
pause
exit /b 1
@ -81,16 +116,20 @@ call env\Scripts\activate.bat
echo OK
REM Install the required packages
echo Installing requirements ...
set DS_BUILD_OPS=0
set DS_BUILD_AIO=0
python -m pip install pip --upgrade
python -m pip install -r requirements.txt
if %ERRORLEVEL% neq 0 (
if .ERRORLEVEL. neq 0 (
echo Failed to install required packages. Please check your internet connection and try again.
pause
exit /b 1
)
echo Virtual environment created and packages installed successfully.
echo Downloading latest model
powershell -Command "Invoke-WebRequest -Uri 'https://the-eye.eu/public/AI/models/nomic-ai/gpt4all/gpt4all-lora-quantized-ggml.bin' -OutFile 'models/gpt4all-lora-quantized-ggml.bin'"
echo Cleaning tmp folder
rd /s /q "./tmp"
echo Virtual environment created and packages installed successfully. Run app.py
pause
exit /b 0

39
run.bat Normal file
View File

@ -0,0 +1,39 @@
echo off
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHH .HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHH. ,HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHH.## HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHH#.HHHHH/*,*,*,*,*,*,*,*,***,*,**#HHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHH.*,,***,***,***,***,***,***,*******HHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHH*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*,,,,,HHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHH.,,,***,***,***,***,***,***,***,***,***,***/HHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHH*,,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*HHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHH#,***,***,***,***,***,***,***,***,***,***,***,**HHHHHHHHHHHHHHHHH
echo HHHHHHHHHH..HHH,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*#HHHHHHHHHHHHHHHH
echo HHHHHHH,,,**,/H*,***,***,***,,,*,***,***,***,**,,,**,***,***,***H,,*,***HHHHHHHH
echo HHHHHH.*,,,*,,,,,*,*,*,***#HHHHH.,,*,*,*,*,**/HHHHH.,*,*,*,*,*,*,*,*****HHHHHHHH
echo HHHHHH.*,***,*,*,***,***,.HHHHHHH/**,***,****HHHHHHH.***,***,***,*******HHHHHHHH
echo HHHHHH.,,,,,,,,,,,,,,,,,,,.HHHHH.,,,,,,,,,,,,.HHHHHH,,,,,,,,,,,,,,,,,***HHHHHHHH
echo HHHHHH.,,,,,,/H,,,**,***,***,,,*,***,***,***,**,,,,*,***,***,***H***,***HHHHHHHH
echo HHHHHHH.,,,,*.H,,,,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,***H*,,,,/HHHHHHHHH
echo HHHHHHHHHHHHHHH*,***,***,**,,***,***,***,***,***,***,***,***,**.HHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHH,,,,,,,,*,,#H#,,,,,*,,,*,,,,,,,,*#H*,,,,,,,,,**HHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHH,,*,***,***,**/.HHHHHHHHHHHHH#*,,,*,***,***,*HHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHH,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*HHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHH**,***,***,***,***,***,***,***,***,***,***,*.HHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHH*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*HHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHH**,***,***,*******/..HHHHHHHHH.#/*,*,,,***,***HHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHH*,*,*,******#HHHHHHHHHHHHHHHHHHHHHHHHHHHH./**,,,.HHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHH.,,*,***.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH.*#HHHHHHHHHHHH
echo HHHHHHHHHHHHHHH/,,,*.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHH,,#HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHH.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
env/Scripts/activate.bat
python app.py

View File

@ -6,7 +6,7 @@ if /i "%choice%" equ "Y" (
REM Download Python installer
echo -n
set /p="Removing virtual environment..." <nul
powershell -Command "rm env -y"
powershell -Command "rm env"
echo OK
pause
) else (