Spaces:
Sleeping
Sleeping
MatteoScript
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -20,6 +20,7 @@ import pytesseract
|
|
20 |
from PIL import Image
|
21 |
from youtube_transcript_api import YouTubeTranscriptApi
|
22 |
import webbrowser
|
|
|
23 |
|
24 |
load_dotenv()
|
25 |
URL_APP_SCRIPT = os.getenv('URL_APP_SCRIPT')
|
@@ -45,6 +46,12 @@ option_leggicalendar = {'Leggi Calendar': {'systemRole': 'Tu sei BONSI AI, il mi
|
|
45 |
'tipo': 'CALENDAR',
|
46 |
'RAG': False}
|
47 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
# ----------------------------------------------------------- Interfaccia --------------------------------------------------------------------
|
49 |
st.set_page_config(page_title="Bonsi A.I.", page_icon="🏫")
|
50 |
|
@@ -100,6 +107,9 @@ def init_state() :
|
|
100 |
if "urls" not in st.session_state:
|
101 |
st.session_state.urls = [""] * 5
|
102 |
|
|
|
|
|
|
|
103 |
if "tbs_options" not in st.session_state:
|
104 |
st.session_state.tbs_options = {
|
105 |
"Sempre": "0",
|
@@ -109,7 +119,8 @@ def init_state() :
|
|
109 |
"Ultimo giorno": "qdr:d"
|
110 |
}
|
111 |
|
112 |
-
if not st.session_state.loaded_data:
|
|
|
113 |
place=st.empty()
|
114 |
with place:
|
115 |
with st.status("Caricamento in corso...", expanded=True) as status:
|
@@ -372,11 +383,6 @@ def show_source(links) :
|
|
372 |
reference, testo = link
|
373 |
st.info('##### ' + reference.replace('_', ' ') + '\n\n'+ testo)
|
374 |
|
375 |
-
init_state()
|
376 |
-
sidebar()
|
377 |
-
header()
|
378 |
-
chat_box()
|
379 |
-
|
380 |
def split_text(text, chunk_size):
|
381 |
testo_suddiviso = []
|
382 |
if text == '':
|
@@ -387,44 +393,53 @@ def split_text(text, chunk_size):
|
|
387 |
testo_suddiviso.append(text[i:i+chunk_size])
|
388 |
return testo_suddiviso
|
389 |
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
from PIL import Image
|
21 |
from youtube_transcript_api import YouTubeTranscriptApi
|
22 |
import webbrowser
|
23 |
+
from streamlit_javascript import st_javascript
|
24 |
|
25 |
load_dotenv()
|
26 |
URL_APP_SCRIPT = os.getenv('URL_APP_SCRIPT')
|
|
|
46 |
'tipo': 'CALENDAR',
|
47 |
'RAG': False}
|
48 |
}
|
49 |
+
|
50 |
+
def local_storage_get(key):
|
51 |
+
return st_javascript(f"localStorage.getItem('{key}');")
|
52 |
+
|
53 |
+
def local_storage_set(key, value):
|
54 |
+
return st_javascript(f"localStorage.setItem('{key}', '{value}');")
|
55 |
# ----------------------------------------------------------- Interfaccia --------------------------------------------------------------------
|
56 |
st.set_page_config(page_title="Bonsi A.I.", page_icon="🏫")
|
57 |
|
|
|
107 |
if "urls" not in st.session_state:
|
108 |
st.session_state.urls = [""] * 5
|
109 |
|
110 |
+
if "login_effettuato" not in st.session_state:
|
111 |
+
st.session_state.login_effettuato = False
|
112 |
+
|
113 |
if "tbs_options" not in st.session_state:
|
114 |
st.session_state.tbs_options = {
|
115 |
"Sempre": "0",
|
|
|
119 |
"Ultimo giorno": "qdr:d"
|
120 |
}
|
121 |
|
122 |
+
if not st.session_state.loaded_data and st.session_state.login_effettuato == True:
|
123 |
+
place=st.empty()
|
124 |
place=st.empty()
|
125 |
with place:
|
126 |
with st.status("Caricamento in corso...", expanded=True) as status:
|
|
|
383 |
reference, testo = link
|
384 |
st.info('##### ' + reference.replace('_', ' ') + '\n\n'+ testo)
|
385 |
|
|
|
|
|
|
|
|
|
|
|
386 |
def split_text(text, chunk_size):
|
387 |
testo_suddiviso = []
|
388 |
if text == '':
|
|
|
393 |
testo_suddiviso.append(text[i:i+chunk_size])
|
394 |
return testo_suddiviso
|
395 |
|
396 |
+
init_state()
|
397 |
+
if st.session_state.login_effettuato == False:
|
398 |
+
connetti_google()
|
399 |
+
else:
|
400 |
+
init_state()
|
401 |
+
sidebar()
|
402 |
+
header()
|
403 |
+
chat_box()
|
404 |
+
|
405 |
+
if prompt := st.chat_input("Chatta con BonsiAI..."):
|
406 |
+
prompt_originale = prompt
|
407 |
+
links = inserisci_istruzioni(prompt_originale)
|
408 |
+
st.session_state.instruction+= ' \n\n' + st.session_state.testo_documenti
|
409 |
+
instruction_suddivise = split_text(st.session_state.instruction, st.session_state.split*2000)
|
410 |
+
ruolo_originale = st.session_state.systemRole
|
411 |
+
ruoli_divisi = ruolo_originale.split("&&")
|
412 |
+
parte=1
|
413 |
+
i=1
|
414 |
+
risposta_completa = ''
|
415 |
+
for ruolo_singolo in ruoli_divisi:
|
416 |
+
for instruction_singola in instruction_suddivise:
|
417 |
+
for numgen in range(1, st.session_state.numero_generazioni+1):
|
418 |
+
if i==1:
|
419 |
+
st.chat_message("user").markdown(prompt_originale + (': Parte ' + str(parte) if i > 1 else ''))
|
420 |
+
i+=1
|
421 |
+
prompt = formattaPrompt(prompt_originale, ruolo_singolo, st.session_state.systemStyle, instruction_singola)
|
422 |
+
print('------------------------------------------------------------------------------------')
|
423 |
+
print(prompt)
|
424 |
+
st.session_state.messages.append({"role": "user", "content": prompt_originale})
|
425 |
+
chat_stream = generate_chat_stream(prompt)
|
426 |
+
with st.chat_message("assistant"):
|
427 |
+
placeholder = st.empty()
|
428 |
+
full_response = stream_handler(chat_stream, placeholder)
|
429 |
+
if st.session_state.rag_enabled or st.session_state.cerca_online or st.session_state.selected_option["tipo"]=='EMAIL' or st.session_state.selected_option["tipo"]=='CALENDAR':
|
430 |
+
show_source(links)
|
431 |
+
if st.session_state.options.get(st.session_state.selected_option_key, {})["tipo"]=='DOCUMENTO':
|
432 |
+
with st.expander("Mostra Documento") :
|
433 |
+
st.info('##### ' + st.session_state.selected_documento_key + ' (Parte ' + str(parte) +')'+ '\n\n\n' + instruction_singola)
|
434 |
+
parte+=1
|
435 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
436 |
+
risposta_completa = risposta_completa + '\n' + full_response
|
437 |
+
|
438 |
+
if st.session_state.enable_history:
|
439 |
+
st.session_state.history.append([prompt_originale, full_response])
|
440 |
+
else:
|
441 |
+
st.session_state.history.append(['', ''])
|
442 |
+
st.success('Generazione Completata')
|
443 |
+
payload = {"domanda": prompt_originale, "risposta": risposta_completa}
|
444 |
+
json_payload = json.dumps(payload)
|
445 |
+
response = requests.post(URL_APP_SCRIPT, data=json_payload)
|