Skip to content

Commit

Permalink
Front update v2 (#10)
Browse files Browse the repository at this point in the history
* Javascript documented in JSDoc format

* Added context close button

* Comments

* Added PDF view window

* Added pdf navigations buttons

* PDF display improve

* Added delay to submitSettingsToServer() function

* v2 applied to arch-ru & seus

small fixes
css cleaning
python cleaning
  • Loading branch information
Binec22 authored Aug 6, 2024
1 parent bc8cd1f commit 4b7c072
Show file tree
Hide file tree
Showing 31 changed files with 831 additions and 2,053 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ data/
.gitignore
node_modules/
*.log
*.pyc
python_script/cleanpdf.py
python_script/docxtopdf.py
*.pyc
45 changes: 22 additions & 23 deletions arch_en_app.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import os
import sys

from flask import Flask, Blueprint, render_template, request, jsonify, send_from_directory, url_for

arch_en = Flask(__name__)
main = Blueprint('main', __name__, static_folder='static', static_url_path='/arch-en/static', template_folder='templates')
from flask import Flask, render_template, request, jsonify, send_from_directory
arch_en_app = Flask(__name__)


sys.path.append(os.path.join(os.path.dirname(__file__), 'python_script'))
from parameters import load_config
global DATA_PATH
load_config('arch_en')
load_config('arch-en')
from parameters import CHROMA_ROOT_PATH, EMBEDDING_MODEL, LLM_MODEL, PROMPT_TEMPLATE, DATA_PATH, REPHRASING_PROMPT, STANDALONE_PROMPT, ROUTER_DECISION_PROMPT
from get_llm_function import get_llm_function
from get_rag_chain import get_rag_chain
from ConversationalRagChain import ConversationalRagChain

@arch_en.route("/")

root = "arch-en"


@arch_en_app.route("/")
def index():
root = "arch-en"
return render_template('arch_en_index.html', root=root)


def init_app():
load_rag()
arch_en.config['UPLOAD_FOLDER'] = DATA_PATH

arch_en_app.config['UPLOAD_FOLDER'] = DATA_PATH

def load_rag(settings = None):

def load_rag(settings = None):
global rag_conv
if settings is None :
rag_conv = ConversationalRagChain.from_llm(
Expand All @@ -45,18 +45,18 @@ def load_rag(settings = None):


# Route to get the document list
@arch_en.route('/documents', methods=['GET'])
@arch_en_app.route('/documents', methods=['GET'])
def list_documents():
files = os.listdir(arch_en.config['UPLOAD_FOLDER'])
documents = [{"name": f, "url": f"/arch-en/files/{f}", "extension":os.path.splitext(f)[1][1:]} for f in files]
files = os.listdir(arch_en_app.config['UPLOAD_FOLDER'])
documents = [{"name": f, "url": f"/{root}/files/{f}", "extension": os.path.splitext(f)[1][1:]} for f in files]
return jsonify(documents)


# Route to get a single document
@arch_en.route('/documents/<document_name>', methods=['GET'])
@arch_en_app.route('/documents/<document_name>', methods=['GET'])
def get_document(document_name):
files = os.listdir(arch_en.config['UPLOAD_FOLDER'])
documents = [{"name": f, "url": f"/arch-en/files/{f}", "extension": os.path.splitext(f)[1][1:]} for f in files]
files = os.listdir(arch_en_app.config['UPLOAD_FOLDER'])
documents = [{"name": f, "url": f"/{root}/files/{f}", "extension": os.path.splitext(f)[1][1:]} for f in files]

document = next((doc for doc in documents if doc["name"] == document_name), None)

Expand All @@ -65,14 +65,13 @@ def get_document(document_name):

return jsonify(document)


# Route to show the pdf
@arch_en.route('/files/<filename>', methods=['GET'])
@arch_en_app.route('/files/<filename>', methods=['GET'])
def serve_file(filename):
return send_from_directory(arch_en.config['UPLOAD_FOLDER'], filename)
return send_from_directory(arch_en_app.config['UPLOAD_FOLDER'], filename)


@arch_en.route("/get", methods=["POST"])
@arch_en_app.route("/get", methods=["POST"])
def chat():
data = request.get_json()
msg = data.get("msg", "")
Expand All @@ -93,19 +92,19 @@ def get_Chat_response(query):
return output


@arch_en.route('/update-settings', methods=['POST'])
@arch_en_app.route('/update-settings', methods=['POST'])
def update_settings():
data = request.get_json()
load_rag(settings=data)
return jsonify({'status': 'success', 'message': 'Settings updated successfully'}), 200


@arch_en.route('/clear_chat_history', methods=['POST'])
@arch_en_app.route('/clear_chat_history', methods=['POST'])
def clear_chat_history():
rag_conv.clear_chat_history()
return jsonify({"status": "success"}), 200


if __name__ == '__main__':
init_app()
arch_en.run(port=6668,debug=False)
arch_en_app.run(port=6668,debug=False)
100 changes: 74 additions & 26 deletions arch_ru_app.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,110 @@
import os
import sys

from flask import Flask, render_template, request, jsonify, send_from_directory
arch_ru_app = Flask(__name__)


sys.path.append(os.path.join(os.path.dirname(__file__), 'python_script'))
from parameters import load_config

###################
### LOAD CONFIG ###
###################
load_config('arch_ru')
from parameters import CHROMA_ROOT_PATH, EMBEDDING_MODEL, LLM_MODEL, PROMPT_TEMPLATE, DATA_PATH
global DATA_PATH
load_config('arch-ru')
from parameters import CHROMA_ROOT_PATH, EMBEDDING_MODEL, LLM_MODEL, PROMPT_TEMPLATE, DATA_PATH, REPHRASING_PROMPT, STANDALONE_PROMPT, ROUTER_DECISION_PROMPT
from get_llm_function import get_llm_function
from get_rag_chain import get_rag_chain
from ConversationalRagChain import ConversationalRagChain

rag_conv = ConversationalRagChain.from_llm(
rag_chain=get_rag_chain(),
llm=get_llm_function(LLM_MODEL),
callbacks=None
)
arch_ru_app = Flask(__name__)

arch_ru_app.config['UPLOAD_FOLDER'] = DATA_PATH
root = "arch-ru"


@arch_ru_app.route("/")
def index():
return render_template('arch_ru_index.html', root=root)


def init_app():
load_rag()
arch_ru_app.config['UPLOAD_FOLDER'] = DATA_PATH


def load_rag(settings = None):
global rag_conv
if settings is None :
rag_conv = ConversationalRagChain.from_llm(
rag_chain=get_rag_chain(),
llm=get_llm_function(model_name = LLM_MODEL),
callbacks=None
)
else:
rag_conv = ConversationalRagChain.from_llm(
rag_chain=get_rag_chain(settings),
llm=get_llm_function(model_name = settings["llm_model"]),
callbacks=None
)


# Route to get the document list
@arch_ru_app.route('/documents', methods=['GET'])
def list_documents():
files = os.listdir(arch_ru_app.config['UPLOAD_FOLDER'])
documents = [{"name": f, "url": f"/arch-ru/files/{f}", "extension":os.path.splitext(f)[1][1:]} for f in files]
documents = [{"name": f, "url": f"/{root}/files/{f}", "extension": os.path.splitext(f)[1][1:]} for f in files]
return jsonify(documents)


# Route to get a single document
@arch_ru_app.route('/documents/<document_name>', methods=['GET'])
def get_document(document_name):
files = os.listdir(arch_ru_app.config['UPLOAD_FOLDER'])
documents = [{"name": f, "url": f"/{root}/files/{f}", "extension": os.path.splitext(f)[1][1:]} for f in files]

document = next((doc for doc in documents if doc["name"] == document_name), None)

if document is None:
return jsonify({'error': 'Document not found'}), 404

return jsonify(document)

# Route to show the pdf
@arch_ru_app.route('/files/<filename>', methods=['GET'])
def serve_file(filename):
return send_from_directory(arch_ru_app.config['UPLOAD_FOLDER'], filename)

@arch_ru_app.route("/")
def index():
return render_template('arch_ru_index.html')


@arch_ru_app.route("/get", methods=["POST"])
def chat():
msg = request.form.get("msg","")
input_query = msg
return get_Chat_response(input_query)

data = request.get_json()
msg = data.get("msg", "")
return get_Chat_response(msg)

def get_Chat_response(query):
inputs = {"query": str(query)}
inputs = {
"query": str(query),
"chat_history": []
}
res = rag_conv._call(inputs)

output = jsonify({
'response': res['result'],
'context': res['context'],
'source': res['source']
'metadatas': res['metadatas']
})
return output



@arch_ru_app.route('/update-settings', methods=['POST'])
def update_settings():
data = request.get_json()
load_rag(settings=data)
return jsonify({'status': 'success', 'message': 'Settings updated successfully'}), 200


@arch_ru_app.route('/clear_chat_history', methods=['POST'])
def clear_chat_history():
rag_conv.clear_chat_history()
return jsonify({"status": "success"}), 200


if __name__ == '__main__':
arch_ru_app.run(port=6667,debug=False)
init_app()
arch_ru_app.run(port=6667,debug=False)
34 changes: 19 additions & 15 deletions local_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import sys

from flask import Flask, render_template, request, jsonify, send_from_directory

local_app = Flask(__name__)


sys.path.append(os.path.join(os.path.dirname(__file__), 'python_script'))
from parameters import load_config
global DATA_PATH
Expand All @@ -14,12 +14,21 @@
from get_rag_chain import get_rag_chain
from ConversationalRagChain import ConversationalRagChain


root = ""


@local_app.route("/")
def index():
return render_template('local.html', root=root)


def init_app():
load_rag()
local_app.config['UPLOAD_FOLDER'] = DATA_PATH

def load_rag(settings = None):

def load_rag(settings = None):
global rag_conv
if settings is None :
rag_conv = ConversationalRagChain.from_llm(
Expand All @@ -34,18 +43,20 @@ def load_rag(settings = None):
callbacks=None
)


# Route to get the document list
@local_app.route('/documents', methods=['GET'])
def list_documents():
files = os.listdir(local_app.config['UPLOAD_FOLDER'])
documents = [{"name": f, "url": f"/files/{f}", "extension":os.path.splitext(f)[1][1:]} for f in files]
documents = [{"name": f, "url": f"{root}/files/{f}", "extension":os.path.splitext(f)[1][1:]} for f in files]
return jsonify(documents)


# Route to get a single document
@local_app.route('/documents/<document_name>', methods=['GET'])
def get_document(document_name):
files = os.listdir(local_app.config['UPLOAD_FOLDER'])
documents = [{"name": f, "url": f"/files/{f}", "extension": os.path.splitext(f)[1][1:]} for f in files]
documents = [{"name": f, "url": f"{root}/files/{f}", "extension": os.path.splitext(f)[1][1:]} for f in files]

document = next((doc for doc in documents if doc["name"] == document_name), None)

Expand All @@ -59,47 +70,40 @@ def get_document(document_name):
def serve_file(filename):
return send_from_directory(local_app.config['UPLOAD_FOLDER'], filename)


@local_app.route("/")
def index():
return render_template('local.html')


@local_app.route("/get", methods=["POST"])
def chat():
data = request.get_json()
msg = data.get("msg", "")
return get_Chat_response(msg)

def get_Chat_response(query):
print("query: ",str(query))
inputs = {
"query": str(query),
"chat_history": []
}
res = rag_conv._call(inputs)

print(res['metadatas'])
output = jsonify({
'response': res['result'],
'context': res['context'],
'source': res['source']
'metadatas': res['metadatas']
})
print(res['result'])
return output


@local_app.route('/update-settings', methods=['POST'])
def update_settings():
data = request.get_json()
load_rag(settings=data)
return jsonify({'status': 'success', 'message': 'Settings updated successfully'}), 200



@local_app.route('/clear_chat_history', methods=['POST'])
def clear_chat_history():
rag_conv.clear_chat_history()
return jsonify({"status": "success"}), 200


if __name__ == '__main__':
init_app()
local_app.run(port=5000,debug=True)
Loading

0 comments on commit 4b7c072

Please sign in to comment.