Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from google_function import leggi_gmail
|
|
| 4 |
from google_function import scrivi_bozza_gmail
|
| 5 |
from google_function import leggi_calendario_google
|
| 6 |
from google_function import connetti_google
|
|
|
|
| 7 |
import time
|
| 8 |
import os
|
| 9 |
from dotenv import load_dotenv
|
|
@@ -22,13 +23,15 @@ from PIL import Image
|
|
| 22 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 23 |
import webbrowser
|
| 24 |
from streamlit_javascript import st_javascript
|
|
|
|
| 25 |
|
| 26 |
load_dotenv()
|
| 27 |
URL_APP_SCRIPT = os.getenv('URL_APP_SCRIPT')
|
| 28 |
URL_PROMPT = URL_APP_SCRIPT + '?IdFoglio=1cLw9q70BsPmxMBj9PIzgXtq6sm3X-GVBVnOB5wE8jr8'
|
| 29 |
URL_DOCUMENTI = URL_APP_SCRIPT + '?IdSecondoFoglio=1cLw9q70BsPmxMBj9PIzgXtq6sm3X-GVBVnOB5wE8jr8'
|
| 30 |
SYSTEM_PROMPT = ["Sei BonsiAI e mi aiuterai nelle mie richieste (Parla in ITALIANO)", "Esatto, sono BonsiAI. Di cosa hai bisogno?"]
|
| 31 |
-
CHAT_BOTS = {"Mixtral 8x7B v0.1" :"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
|
|
|
| 32 |
option_personalizzata = {'Personalizzata': {'systemRole': 'Tu sei BONSI AI, il mio assistente personale della scuola superiore del Bonsignori. Aiutami in base alle mie esigenze',
|
| 33 |
'systemStyle': 'Firmati sempre come BONSI AI. (scrivi in italiano)',
|
| 34 |
'instruction': '',
|
|
@@ -48,11 +51,6 @@ option_leggicalendar = {'Leggi Calendar': {'systemRole': 'Tu sei BONSI AI, il mi
|
|
| 48 |
'RAG': False}
|
| 49 |
}
|
| 50 |
|
| 51 |
-
def local_storage_get(key):
|
| 52 |
-
return st_javascript(f"localStorage.getItem('{key}');")
|
| 53 |
-
|
| 54 |
-
def local_storage_set(key, value):
|
| 55 |
-
return st_javascript(f"localStorage.setItem('{key}', '{value}');")
|
| 56 |
# ----------------------------------------------------------- Interfaccia --------------------------------------------------------------------
|
| 57 |
st.set_page_config(page_title="Bonsi A.I.", page_icon="🏫")
|
| 58 |
|
|
@@ -97,8 +95,14 @@ def init_state() :
|
|
| 97 |
st.session_state.numero_generazioni = 1
|
| 98 |
|
| 99 |
if "numero_elementi" not in st.session_state:
|
| 100 |
-
st.session_state.numero_elementi =
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
|
|
|
|
|
|
| 102 |
if "testo_documenti" not in st.session_state:
|
| 103 |
st.session_state.testo_documenti = ''
|
| 104 |
|
|
@@ -113,6 +117,12 @@ def init_state() :
|
|
| 113 |
|
| 114 |
if "login_effettuato" not in st.session_state:
|
| 115 |
st.session_state.login_effettuato = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
if "tbs_options" not in st.session_state:
|
| 118 |
st.session_state.tbs_options = {
|
|
@@ -131,9 +141,14 @@ def init_state() :
|
|
| 131 |
st.write("Inizializzazione Ambiente")
|
| 132 |
time.sleep(1)
|
| 133 |
st.write("Inizializzazione Prompt")
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
st.write("Inizializzazione Documenti")
|
| 136 |
-
documenti =
|
|
|
|
|
|
|
| 137 |
st.session_state.options = {**option_personalizzata, **option_leggiemail, **option_leggicalendar, **options}
|
| 138 |
st.session_state.documenti = documenti
|
| 139 |
st.session_state.loaded_data = True
|
|
@@ -157,6 +172,24 @@ def read_text_from_file(file):
|
|
| 157 |
st.write(f"Non è possibile leggere il testo dal file '{file.name}'.")
|
| 158 |
return text
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
def sidebar():
|
| 161 |
def retrieval_settings() :
|
| 162 |
st.markdown("# Impostazioni Prompt")
|
|
@@ -170,15 +203,18 @@ def sidebar():
|
|
| 170 |
st.session_state.split = st.slider(label="Pagine Suddivisione", min_value=1, max_value=30, value=30, help='Se il documento ha 100 pagine e suddivido per 20 pagine elaborerà la risposta 5 volte. Più alto è il numero e meno volte elaborerà ma la risposta sarà più imprecisa')
|
| 171 |
else:
|
| 172 |
st.session_state.instruction = st.session_state.selected_option.get('instruction', '')
|
| 173 |
-
|
| 174 |
st.session_state.systemRole = st.session_state.selected_option.get('systemRole', '')
|
| 175 |
st.session_state.systemRole = st.text_area("Descrizione", st.session_state.systemRole, help='Ruolo del chatbot e descrizione dell\'azione che deve svolgere')
|
| 176 |
st.session_state.systemStyle = st.session_state.selected_option.get('systemStyle', '')
|
| 177 |
st.session_state.systemStyle = st.text_area("Stile", st.session_state.systemStyle, help='Descrizione dello stile utilizzato per generare il testo')
|
| 178 |
if st.session_state.selected_option["tipo"]=='EMAIL':
|
| 179 |
st.session_state.numero_elementi = st.slider(label="Numero Email", min_value=1, max_value=100, value=10)
|
|
|
|
|
|
|
| 180 |
if st.session_state.selected_option["tipo"]=='CALENDAR':
|
| 181 |
st.session_state.numero_elementi = st.slider(label="Numero Eventi Calendario", min_value=1, max_value=100, value=10)
|
|
|
|
|
|
|
| 182 |
st.session_state.rag_enabled = st.session_state.selected_option.get('tipo', '')=='RAG'
|
| 183 |
if st.session_state.selected_option_key == 'Decreti':
|
| 184 |
st.session_state.top_k = st.slider(label="Documenti da ricercare", min_value=1, max_value=20, value=4, disabled=not st.session_state.rag_enabled)
|
|
@@ -206,16 +242,26 @@ def sidebar():
|
|
| 206 |
st.markdown("---")
|
| 207 |
|
| 208 |
def model_settings():
|
| 209 |
-
st.markdown("#
|
| 210 |
st.session_state.chat_bot = st.sidebar.radio('Modello:', [key for key, value in CHAT_BOTS.items() ])
|
| 211 |
st.session_state.numero_generazioni = st.slider(label="Generazioni", min_value = 1, max_value=10, value=1)
|
| 212 |
st.session_state.enable_history = st.toggle("Storico Messaggi", value=True)
|
| 213 |
st.session_state.temp = st.slider(label="Creatività", min_value=0.0, max_value=1.0, step=0.1, value=0.9)
|
| 214 |
st.session_state.max_tokens = st.slider(label="Lunghezza Output", min_value = 2, max_value=2048, step= 32, value=1024)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
with st.sidebar:
|
| 217 |
retrieval_settings()
|
| 218 |
model_settings()
|
|
|
|
| 219 |
st.markdown("""> **Creato da Matteo Bergamelli **""")
|
| 220 |
|
| 221 |
def audioRec():
|
|
@@ -362,11 +408,11 @@ def inserisci_istruzioni(prompt_originale):
|
|
| 362 |
if st.session_state.selected_option["tipo"]=='EMAIL':
|
| 363 |
with st.spinner("Ricerca nelle Email...."):
|
| 364 |
time.sleep(1)
|
| 365 |
-
st.session_state.instruction, links = leggi_gmail(max_results=st.session_state.numero_elementi)
|
| 366 |
if st.session_state.selected_option["tipo"]=='CALENDAR':
|
| 367 |
with st.spinner("Ricerca nel Calendario...."):
|
| 368 |
time.sleep(1)
|
| 369 |
-
st.session_state.instruction, links = leggi_calendario_google(max_results=st.session_state.numero_elementi)
|
| 370 |
with st.spinner("Generazione in corso...") :
|
| 371 |
time.sleep(1)
|
| 372 |
#st.session_state.instruction = instruction_originale + '\n----------------------------------------------\n' + st.session_state.instruction
|
|
@@ -441,6 +487,8 @@ if st.session_state.login_effettuato == True:
|
|
| 441 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 442 |
risposta_completa = risposta_completa + '\n' + full_response
|
| 443 |
|
|
|
|
|
|
|
| 444 |
if st.session_state.enable_history:
|
| 445 |
st.session_state.history.append([prompt_originale, full_response])
|
| 446 |
else:
|
|
|
|
| 4 |
from google_function import scrivi_bozza_gmail
|
| 5 |
from google_function import leggi_calendario_google
|
| 6 |
from google_function import connetti_google
|
| 7 |
+
from google_function import crea_documento_google
|
| 8 |
import time
|
| 9 |
import os
|
| 10 |
from dotenv import load_dotenv
|
|
|
|
| 23 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 24 |
import webbrowser
|
| 25 |
from streamlit_javascript import st_javascript
|
| 26 |
+
import datetime
|
| 27 |
|
| 28 |
load_dotenv()
|
| 29 |
URL_APP_SCRIPT = os.getenv('URL_APP_SCRIPT')
|
| 30 |
URL_PROMPT = URL_APP_SCRIPT + '?IdFoglio=1cLw9q70BsPmxMBj9PIzgXtq6sm3X-GVBVnOB5wE8jr8'
|
| 31 |
URL_DOCUMENTI = URL_APP_SCRIPT + '?IdSecondoFoglio=1cLw9q70BsPmxMBj9PIzgXtq6sm3X-GVBVnOB5wE8jr8'
|
| 32 |
SYSTEM_PROMPT = ["Sei BonsiAI e mi aiuterai nelle mie richieste (Parla in ITALIANO)", "Esatto, sono BonsiAI. Di cosa hai bisogno?"]
|
| 33 |
+
CHAT_BOTS = {"Mixtral 8x7B v0.1" :"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 34 |
+
"Mistral 7B v0.2" :"mistralai/Mistral-7B-Instruct-v0.2"}
|
| 35 |
option_personalizzata = {'Personalizzata': {'systemRole': 'Tu sei BONSI AI, il mio assistente personale della scuola superiore del Bonsignori. Aiutami in base alle mie esigenze',
|
| 36 |
'systemStyle': 'Firmati sempre come BONSI AI. (scrivi in italiano)',
|
| 37 |
'instruction': '',
|
|
|
|
| 51 |
'RAG': False}
|
| 52 |
}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# ----------------------------------------------------------- Interfaccia --------------------------------------------------------------------
|
| 55 |
st.set_page_config(page_title="Bonsi A.I.", page_icon="🏫")
|
| 56 |
|
|
|
|
| 95 |
st.session_state.numero_generazioni = 1
|
| 96 |
|
| 97 |
if "numero_elementi" not in st.session_state:
|
| 98 |
+
st.session_state.numero_elementi = 5
|
| 99 |
+
|
| 100 |
+
if "data_inizio" not in st.session_state:
|
| 101 |
+
st.session_state.data_inizio = None
|
| 102 |
|
| 103 |
+
if "data_inizio" not in st.session_state:
|
| 104 |
+
st.session_state.data_inizio = None
|
| 105 |
+
|
| 106 |
if "testo_documenti" not in st.session_state:
|
| 107 |
st.session_state.testo_documenti = ''
|
| 108 |
|
|
|
|
| 117 |
|
| 118 |
if "login_effettuato" not in st.session_state:
|
| 119 |
st.session_state.login_effettuato = False
|
| 120 |
+
|
| 121 |
+
if "ultimo_messaggio" not in st.session_state:
|
| 122 |
+
st.session_state.ultimo_messaggio = ""
|
| 123 |
+
|
| 124 |
+
if "tutti_messaggi" not in st.session_state:
|
| 125 |
+
st.session_state.tutti_messaggi = ""
|
| 126 |
|
| 127 |
if "tbs_options" not in st.session_state:
|
| 128 |
st.session_state.tbs_options = {
|
|
|
|
| 141 |
st.write("Inizializzazione Ambiente")
|
| 142 |
time.sleep(1)
|
| 143 |
st.write("Inizializzazione Prompt")
|
| 144 |
+
URL_REDIRECT = os.getenv('URL_REDIRECT')
|
| 145 |
+
options = {}
|
| 146 |
+
if URL_REDIRECT != "http://localhost:8501/":
|
| 147 |
+
options = requests.get(URL_PROMPT).json()
|
| 148 |
st.write("Inizializzazione Documenti")
|
| 149 |
+
documenti = {}
|
| 150 |
+
if URL_REDIRECT != "http://localhost:8501/":
|
| 151 |
+
documenti = requests.get(URL_DOCUMENTI).json()
|
| 152 |
st.session_state.options = {**option_personalizzata, **option_leggiemail, **option_leggicalendar, **options}
|
| 153 |
st.session_state.documenti = documenti
|
| 154 |
st.session_state.loaded_data = True
|
|
|
|
| 172 |
st.write(f"Non è possibile leggere il testo dal file '{file.name}'.")
|
| 173 |
return text
|
| 174 |
|
| 175 |
+
def open_new_tab(url):
|
| 176 |
+
placeholder = st.empty()
|
| 177 |
+
with placeholder:
|
| 178 |
+
placeholder.empty()
|
| 179 |
+
new_tab_js = f'''<script type="text/javascript">window.open("{url}", "_blank");</script>'''
|
| 180 |
+
st.components.v1.html(new_tab_js, height=1)
|
| 181 |
+
time.sleep(0.3)
|
| 182 |
+
placeholder.empty()
|
| 183 |
+
|
| 184 |
+
def esporta_testo(tipo, ultimo_messaggio):
|
| 185 |
+
testo = st.session_state.ultimo_messaggio if ultimo_messaggio else st.session_state.tutti_messaggi
|
| 186 |
+
if tipo == 'Bozza Email':
|
| 187 |
+
url = scrivi_bozza_gmail(testo)
|
| 188 |
+
open_new_tab(url)
|
| 189 |
+
if tipo == 'Google Documenti':
|
| 190 |
+
url = crea_documento_google(testo)
|
| 191 |
+
open_new_tab(url)
|
| 192 |
+
|
| 193 |
def sidebar():
|
| 194 |
def retrieval_settings() :
|
| 195 |
st.markdown("# Impostazioni Prompt")
|
|
|
|
| 203 |
st.session_state.split = st.slider(label="Pagine Suddivisione", min_value=1, max_value=30, value=30, help='Se il documento ha 100 pagine e suddivido per 20 pagine elaborerà la risposta 5 volte. Più alto è il numero e meno volte elaborerà ma la risposta sarà più imprecisa')
|
| 204 |
else:
|
| 205 |
st.session_state.instruction = st.session_state.selected_option.get('instruction', '')
|
|
|
|
| 206 |
st.session_state.systemRole = st.session_state.selected_option.get('systemRole', '')
|
| 207 |
st.session_state.systemRole = st.text_area("Descrizione", st.session_state.systemRole, help='Ruolo del chatbot e descrizione dell\'azione che deve svolgere')
|
| 208 |
st.session_state.systemStyle = st.session_state.selected_option.get('systemStyle', '')
|
| 209 |
st.session_state.systemStyle = st.text_area("Stile", st.session_state.systemStyle, help='Descrizione dello stile utilizzato per generare il testo')
|
| 210 |
if st.session_state.selected_option["tipo"]=='EMAIL':
|
| 211 |
st.session_state.numero_elementi = st.slider(label="Numero Email", min_value=1, max_value=100, value=10)
|
| 212 |
+
st.session_state.data_inizio = st.date_input("Email dal", value=datetime.date.today()-datetime.timedelta(days=7), format='DD/MM/YYYY')
|
| 213 |
+
st.session_state.data_fine = st.date_input("Email al", value=datetime.date.today(), format='DD/MM/YYYY')
|
| 214 |
if st.session_state.selected_option["tipo"]=='CALENDAR':
|
| 215 |
st.session_state.numero_elementi = st.slider(label="Numero Eventi Calendario", min_value=1, max_value=100, value=10)
|
| 216 |
+
st.session_state.data_inizio = st.date_input("Eventi dal", value=datetime.date.today(), format='DD/MM/YYYY')
|
| 217 |
+
st.session_state.data_fine = st.date_input("Eventi al", value=datetime.date.today()+datetime.timedelta(days=7), format='DD/MM/YYYY')
|
| 218 |
st.session_state.rag_enabled = st.session_state.selected_option.get('tipo', '')=='RAG'
|
| 219 |
if st.session_state.selected_option_key == 'Decreti':
|
| 220 |
st.session_state.top_k = st.slider(label="Documenti da ricercare", min_value=1, max_value=20, value=4, disabled=not st.session_state.rag_enabled)
|
|
|
|
| 242 |
st.markdown("---")
|
| 243 |
|
| 244 |
def model_settings():
|
| 245 |
+
st.markdown("# Modello")
|
| 246 |
st.session_state.chat_bot = st.sidebar.radio('Modello:', [key for key, value in CHAT_BOTS.items() ])
|
| 247 |
st.session_state.numero_generazioni = st.slider(label="Generazioni", min_value = 1, max_value=10, value=1)
|
| 248 |
st.session_state.enable_history = st.toggle("Storico Messaggi", value=True)
|
| 249 |
st.session_state.temp = st.slider(label="Creatività", min_value=0.0, max_value=1.0, step=0.1, value=0.9)
|
| 250 |
st.session_state.max_tokens = st.slider(label="Lunghezza Output", min_value = 2, max_value=2048, step= 32, value=1024)
|
| 251 |
+
st.markdown("---")
|
| 252 |
+
|
| 253 |
+
def export_settings():
|
| 254 |
+
st.markdown("# Esportazione")
|
| 255 |
+
st.session_state.export_type = st.selectbox('Tipologia', ('Non Esportare', 'Google Documenti', 'Bozza Email'), help='Seleziona la tipologia di esportazione del testo generato')
|
| 256 |
+
st.session_state.export_all = st.toggle("Considera tutte le chat", value=False)
|
| 257 |
+
if st.button("Esporta", type="primary", use_container_width=True):
|
| 258 |
+
esporta_testo(st.session_state.export_type, st.session_state.export_all)
|
| 259 |
+
st.markdown("---")
|
| 260 |
|
| 261 |
with st.sidebar:
|
| 262 |
retrieval_settings()
|
| 263 |
model_settings()
|
| 264 |
+
export_settings()
|
| 265 |
st.markdown("""> **Creato da Matteo Bergamelli **""")
|
| 266 |
|
| 267 |
def audioRec():
|
|
|
|
| 408 |
if st.session_state.selected_option["tipo"]=='EMAIL':
|
| 409 |
with st.spinner("Ricerca nelle Email...."):
|
| 410 |
time.sleep(1)
|
| 411 |
+
st.session_state.instruction, links = leggi_gmail(max_results=st.session_state.numero_elementi, data_inizio = st.session_state.data_inizio, data_fine = st.session_state.data_fine)
|
| 412 |
if st.session_state.selected_option["tipo"]=='CALENDAR':
|
| 413 |
with st.spinner("Ricerca nel Calendario...."):
|
| 414 |
time.sleep(1)
|
| 415 |
+
st.session_state.instruction, links = leggi_calendario_google(max_results=st.session_state.numero_elementi, data_inizio = st.session_state.data_inizio, data_fine = st.session_state.data_fine)
|
| 416 |
with st.spinner("Generazione in corso...") :
|
| 417 |
time.sleep(1)
|
| 418 |
#st.session_state.instruction = instruction_originale + '\n----------------------------------------------\n' + st.session_state.instruction
|
|
|
|
| 487 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 488 |
risposta_completa = risposta_completa + '\n' + full_response
|
| 489 |
|
| 490 |
+
st.session_state.ultimo_messaggio = full_response
|
| 491 |
+
st.session_state.tutti_messaggi += '\n\n' + full_response
|
| 492 |
if st.session_state.enable_history:
|
| 493 |
st.session_state.history.append([prompt_originale, full_response])
|
| 494 |
else:
|