File size: 7,602 Bytes
c9b1bae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
import streamlit as st
import requests
from Ressources.Config import URL
from Ressources.Translations import translations
from Services.Service_File import Service_File
def view_chatbot():
chatbot = translations[st.session_state['languages']]['chatbotPage']
view_history()
st.title(chatbot['selectedModel'] + " : " + st.session_state['model_selected'])
if st.session_state['chat_id'] is None :
view_select_model()
view_file_upload()
if st.session_state['chat_id'] is not None :
for message in st.session_state['selected_chat']:
if message['chat_message_is_ia'] == 1:
with st.chat_message("assistant"):
st.write(message['chat_message'])
else:
with st.chat_message("user"):
st.write(message['chat_message'])
view_chat()
def view_select_model():
st.selectbox(
translations[st.session_state['languages']]['chatbotPage']['chooseModel'],
("Bert", "BigBird", "Splinter", "Squeeze"),
index=0,
key="model",
on_change=function_select_model
)
def view_history():
Global = translations[st.session_state['languages']]
function_initialize_history()
col1, col2, col3 = st.sidebar.columns([3, 1, 1])
col1.write(Global['language'])
col2.button('EN ', on_click=function_set_language, args=('en',))
col3.button('FR ', on_click=function_set_language, args=('fr',))
col1, col2, col3 = st.sidebar.columns([3, 1, 3])
col1.write(st.session_state['user_name'].capitalize())
col3.button(translations[st.session_state['languages']]['logout'], on_click=function_logout_user)
st.sidebar.title('')
col1, col2, col3 = st.sidebar.columns([3, 1, 1])
col1.write(Global['home'])
col3.button('β', on_click=function_new_chat)
for option in st.session_state.chat_history.values():
col1, col2, col3 = st.sidebar.columns([3, 1, 1])
col1.write(option['chat_title'], key=f"chat_title_{option['chat_id']}")
col2.button('π', key=f"show_{option['chat_id']}", on_click=function_show_chat, args=(option['chat_id'],))
col3.button('ποΈ', key=f"del_{option['chat_id']}", on_click=function_delete_chat, args=(option['chat_id'],))
def view_chat():
chatbot = translations[st.session_state['languages']]['chatbotPage']
st.chat_input(chatbot['enterQuestion'], key='prompt', on_submit=function_send_message)
def view_file_upload():
file = st.file_uploader('Upload a file', type=['.docx', '.pdf', '.xlsx', '.csv'])
st.write('')
if st.session_state['is_chat_show'] == False:
st.session_state['have_file'] = False
st.session_state['file_content'] = ''
serviceFile = Service_File()
string = ''
if file is not None:
string += serviceFile.file_for_string(file)
st.session_state['have_file'] = True
st.session_state['file_content'] = string
def function_initialize_history():
try:
response = requests.get(URL + "/chatHistory", json={
'user_id': st.session_state['user_id'],
'user_is_connected': st.session_state['user_is_connected'],
})
st.session_state.chat_history = dict(sorted(response.json().items(), key=lambda item: item[1]['chat_date'], reverse=True))
except Exception as e:
st.error(translations[st.session_state['languages']]['generalError'])
def function_send_message():
st.session_state['user_question'] = st.session_state['prompt']
try:
response = requests.post(URL + "/predict", json={
'user_id': st.session_state['user_id'],
'chat_id': st.session_state['chat_id'],
'user_is_connected': st.session_state['user_is_connected'],
'user_question': st.session_state['user_question'],
'file_content': st.session_state['file_content'],
'have_file': st.session_state['have_file'],
'model_selected': st.session_state['model_selected']
})
content = response.json()
if st.session_state['chat_id'] == None:
st.session_state['chat_id'] = int(content['chat_id'])
function_initialize_history()
st.session_state['user_question'] = ''
function_show_chat(int(content['chat_id']))
except Exception as e:
st.error(translations[st.session_state['languages']]['generalError'])
def function_new_chat():
st.session_state['chat_id'] = None
st.session_state['selected_chat'] = None
st.session_state['user_question'] = ''
st.session_state['file_content'] = None
st.session_state['have_file'] = False
st.session_state['is_chat_show'] = False
st.session_state['model_selected'] = "Bert"
def function_show_chat(chat_id):
st.session_state['file_content'] = None
st.session_state['have_file'] = False
try:
st.session_state['chat_id'] = int(chat_id)
chat_message_response = requests.get(URL + "/chatMessage", json={
'user_id': st.session_state['user_id'],
'user_is_connected': st.session_state['user_is_connected'],
'chat_id': chat_id
})
chat_message_content = chat_message_response.json()
chat_reponse = requests.get(URL + "/getChat", json={
'user_id': st.session_state['user_id'],
'user_is_connected': st.session_state['user_is_connected'],
'chat_id': chat_id
})
chat_content = chat_reponse.json()
if chat_content[3] is not None:
st.session_state['have_file'] = True
st.session_state['file_content'] = chat_content[3]
st.session_state['is_chat_show'] = True
st.session_state['selected_chat'] = chat_message_content
st.session_state['model_selected'] = st.session_state['chat_history'][str(chat_id)]['chat_model']
except Exception as e:
st.error(translations[st.session_state['languages']]['generalError'])
def function_delete_chat(chat_id):
try:
requests.post(URL + "/deleteChat", json={
'user_id': st.session_state['user_id'],
'chat_id': chat_id,
'user_is_connected': st.session_state['user_is_connected'],
})
del st.session_state.chat_history[str(chat_id)]
if st.session_state['chat_id'] == chat_id:
st.session_state['model_selected'] = "Bert"
st.session_state['chat_id'] = None
st.session_state['selected_chat'] = None
st.session_state['have_file'] = False
st.session_state['file_content'] = None
st.session_state['is_chat_show'] = False
except Exception as e:
st.error(translations[st.session_state['languages']]['generalError'])
def function_set_language(locale):
st.session_state['languages'] = locale
def function_select_model():
st.session_state['model_selected'] = st.session_state['model']
def function_logout_user():
st.session_state['user_id'] = None
st.session_state['user_name'] = None
st.session_state['user_is_connected'] = False
st.session_state['chat_history'] = {}
st.session_state['selected_chat'] = {}
st.session_state['user_question'] = ''
st.session_state['chat_id'] = None
st.session_state['current_page'] = 'Login'
st.session_state['login_pressed'] = False
st.session_state['signup_pressed'] = False
st.session_state['model_selected'] = "Bert"
st.session_state['file_content'] = None
st.session_state['is_chat_show'] = False
|