Spaces:
Sleeping
Sleeping
MatteoScript
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -74,7 +74,6 @@ option_leggicalendar = {'Leggi Calendar': {'systemRole': 'Tu sei BONSI AI, il mi
|
|
74 |
'tipo': 'CALENDAR',
|
75 |
'RAG': False}
|
76 |
}
|
77 |
-
|
78 |
# ----------------------------------------------------------- Interfaccia --------------------------------------------------------------------
|
79 |
st.set_page_config(page_title="Bonsi A.I.", page_icon="🏫")
|
80 |
|
@@ -118,6 +117,12 @@ def init_state() :
|
|
118 |
if "numero_siti" not in st.session_state:
|
119 |
st.session_state.numero_siti = 3
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
if "numero_generazioni" not in st.session_state:
|
122 |
st.session_state.numero_generazioni = 1
|
123 |
|
@@ -268,12 +273,14 @@ def sidebar():
|
|
268 |
st.session_state.cerca_online = st.toggle("Attivata", value=False)
|
269 |
with st.popover("Siti Specifici", disabled=not st.session_state.cerca_online,use_container_width=True):
|
270 |
st.markdown("#### Inserisci Siti Web ")
|
|
|
271 |
for i in range(5):
|
272 |
-
st.session_state.urls[i]
|
273 |
-
|
|
|
274 |
st.session_state.tbs_value = st.session_state.tbs_options[st.session_state.selected_tbs]
|
275 |
-
st.session_state.numero_siti = st.slider(label="Risultati", min_value = 1, max_value=20, value=3, disabled=(not st.session_state.cerca_online) or (st.session_state.urls[0]!=""))
|
276 |
-
|
277 |
st.markdown("---")
|
278 |
|
279 |
def model_settings():
|
@@ -384,10 +391,14 @@ def get_search_results(query, top_k):
|
|
384 |
results = []
|
385 |
if st.session_state.urls[0] != "":
|
386 |
for i in range(5):
|
387 |
-
|
388 |
-
if
|
389 |
-
|
390 |
-
|
|
|
|
|
|
|
|
|
391 |
for url in search(query, num=top_k, stop=top_k, tbs=st.session_state.tbs_value):
|
392 |
results.append(get_search_results_int(url))
|
393 |
return results
|
@@ -395,10 +406,28 @@ def get_search_results(query, top_k):
|
|
395 |
def gen_online_prompt(prompt, top_k) :
|
396 |
links = []
|
397 |
context = ''
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
402 |
return context, links
|
403 |
|
404 |
def generate_chat_stream(prompt):
|
@@ -429,7 +458,7 @@ def inserisci_istruzioni(prompt_originale):
|
|
429 |
#st.session_state.instruction = instruction_originale + '\n----------------------------------------------\n' + st.session_state.instruction
|
430 |
return links
|
431 |
|
432 |
-
def stream_handler(chat_stream, placeholder)
|
433 |
full_response = ''
|
434 |
for chunk in chat_stream :
|
435 |
if CHAT_BOTS[st.session_state.chat_bot]["model"][:3] == 'gpt':
|
@@ -438,8 +467,10 @@ def stream_handler(chat_stream, placeholder) :
|
|
438 |
else:
|
439 |
if chunk.token.text!='</s>' :
|
440 |
full_response += chunk.token.text
|
441 |
-
placeholder
|
442 |
-
|
|
|
|
|
443 |
return full_response
|
444 |
|
445 |
def show_source(links) :
|
@@ -510,7 +541,7 @@ if st.session_state.login_effettuato or not EFFETTUA_LOGIN_GOOGLE:
|
|
510 |
if CHAT_BOTS[st.session_state.chat_bot]["model"][:3] == 'gpt':
|
511 |
st.error('Inserisci una CHIAVE API valida e controlla il CREDITO residuo: https://platform.openai.com/api-keys')
|
512 |
else:
|
513 |
-
st.error('
|
514 |
|
515 |
if errore_generazione == False:
|
516 |
st.session_state.ultimo_messaggio = full_response
|
|
|
74 |
'tipo': 'CALENDAR',
|
75 |
'RAG': False}
|
76 |
}
|
|
|
77 |
# ----------------------------------------------------------- Interfaccia --------------------------------------------------------------------
|
78 |
st.set_page_config(page_title="Bonsi A.I.", page_icon="🏫")
|
79 |
|
|
|
117 |
if "numero_siti" not in st.session_state:
|
118 |
st.session_state.numero_siti = 3
|
119 |
|
120 |
+
if "suddividi_siti" not in st.session_state:
|
121 |
+
st.session_state.suddividi_siti = 1
|
122 |
+
|
123 |
+
if "cerca_dominio" not in st.session_state:
|
124 |
+
st.session_state.cerca_dominio = False
|
125 |
+
|
126 |
if "numero_generazioni" not in st.session_state:
|
127 |
st.session_state.numero_generazioni = 1
|
128 |
|
|
|
273 |
st.session_state.cerca_online = st.toggle("Attivata", value=False)
|
274 |
with st.popover("Siti Specifici", disabled=not st.session_state.cerca_online,use_container_width=True):
|
275 |
st.markdown("#### Inserisci Siti Web ")
|
276 |
+
st.session_state.cerca_dominio = st.toggle("Cerca nel dominio", value=False, help="Se disattivo, cerca SOLO all'interno dell'url per esempio se digito www.ansa.it, cerca solo nella HOME PAGE di ansa, altrimenti cerca all'interno di tutto il DOMINIO ansa.it")
|
277 |
for i in range(5):
|
278 |
+
if (i<2) or ((i>1) and (st.session_state.urls[i-2] != "")):
|
279 |
+
st.session_state.urls[i] = st.text_input(f"URL Sito {i+1}", placeholder='Sito Web...', help='è possibile specificare anche il link di un video Youtube, in tal caso verrà restituita la trascrizione del video')
|
280 |
+
st.session_state.selected_tbs = st.selectbox("Periodo:", list(st.session_state.tbs_options.keys()), disabled=(not st.session_state.cerca_online) or (st.session_state.urls[0]!="" and not st.session_state.cerca_dominio))
|
281 |
st.session_state.tbs_value = st.session_state.tbs_options[st.session_state.selected_tbs]
|
282 |
+
st.session_state.numero_siti = st.slider(label="Risultati", min_value = 1, max_value=20, value=3, disabled=(not st.session_state.cerca_online) or (st.session_state.urls[0]!="" and not st.session_state.cerca_dominio))
|
283 |
+
st.session_state.suddividi_siti = st.slider(label="Scomponi Ricerca", min_value = 1, max_value=10, value=1, disabled=(not st.session_state.cerca_online) or (st.session_state.urls[0]!="" and not st.session_state.cerca_dominio))
|
284 |
st.markdown("---")
|
285 |
|
286 |
def model_settings():
|
|
|
391 |
results = []
|
392 |
if st.session_state.urls[0] != "":
|
393 |
for i in range(5):
|
394 |
+
dominio = st.session_state.urls[i]
|
395 |
+
if dominio != "":
|
396 |
+
if st.session_state.cerca_dominio:
|
397 |
+
for url in search(query + ' site:' + dominio, num=top_k, stop=top_k, tbs=st.session_state.tbs_value):
|
398 |
+
results.append(get_search_results_int(url))
|
399 |
+
else:
|
400 |
+
results.append(get_search_results_int(dominio))
|
401 |
+
else:
|
402 |
for url in search(query, num=top_k, stop=top_k, tbs=st.session_state.tbs_value):
|
403 |
results.append(get_search_results_int(url))
|
404 |
return results
|
|
|
406 |
def gen_online_prompt(prompt, top_k) :
|
407 |
links = []
|
408 |
context = ''
|
409 |
+
if st.session_state.suddividi_siti > 1:
|
410 |
+
prompt_suddivisione = f'''Genera un elenco PUNTATO di {st.session_state.suddividi_siti} PAROLE CHIAVE in ITALIANO ottimizzate per il WEB e l'indicizzazione SEO.
|
411 |
+
Attenzione, restituisci solo le KEYWORD e non scrivere nulla prima o dopo!
|
412 |
+
Genera le PAROLE CHIAVE a partire da questo argomento, mi raccomando devono essere un elenco PUNTATO in ITALIANO: "{prompt}" '''
|
413 |
+
chat_stream = generate_chat_stream(prompt_suddivisione)
|
414 |
+
risposta = stream_handler(chat_stream, None)
|
415 |
+
sottoricerche = risposta.strip().split('\n')
|
416 |
+
for sottoricerca in sottoricerche:
|
417 |
+
with st.spinner('Sottoricerca: ' + sottoricerca):
|
418 |
+
results = get_search_results(sottoricerca, top_k)
|
419 |
+
for i, result in enumerate(results, start=1):
|
420 |
+
st.toast(result['title'], icon = '🗞️' )
|
421 |
+
context += result['title'] + '\n' + result['description'] + '\n' + '\n\n' + result['body'].replace('\n','.') + '\n\n------------------------------------------------------------'
|
422 |
+
links.append((str(i) + '. ' + result['title'], result['description'] + '\n\n' + result['url']))
|
423 |
+
else:
|
424 |
+
results = get_search_results(prompt, top_k)
|
425 |
+
for i, result in enumerate(results, start=1):
|
426 |
+
st.toast(result['title'])
|
427 |
+
context += result['title'] + '\n' + result['description'] + '\n' + '\n\n' + result['body'].replace('\n','.') + '\n\n------------------------------------------------------------'
|
428 |
+
links.append((str(i) + '. ' + result['title'], result['description'] + '\n\n' + result['url']))
|
429 |
+
if context == '':
|
430 |
+
context = "Non sono state trovate informazioni sul Web in merito all'argomento specificato. Riprova con un altra ricerca o modifica i parametri"
|
431 |
return context, links
|
432 |
|
433 |
def generate_chat_stream(prompt):
|
|
|
458 |
#st.session_state.instruction = instruction_originale + '\n----------------------------------------------\n' + st.session_state.instruction
|
459 |
return links
|
460 |
|
461 |
+
def stream_handler(chat_stream, placeholder):
|
462 |
full_response = ''
|
463 |
for chunk in chat_stream :
|
464 |
if CHAT_BOTS[st.session_state.chat_bot]["model"][:3] == 'gpt':
|
|
|
467 |
else:
|
468 |
if chunk.token.text!='</s>' :
|
469 |
full_response += chunk.token.text
|
470 |
+
if placeholder:
|
471 |
+
placeholder.markdown(full_response + "▌")
|
472 |
+
if placeholder:
|
473 |
+
placeholder.markdown(full_response)
|
474 |
return full_response
|
475 |
|
476 |
def show_source(links) :
|
|
|
541 |
if CHAT_BOTS[st.session_state.chat_bot]["model"][:3] == 'gpt':
|
542 |
st.error('Inserisci una CHIAVE API valida e controlla il CREDITO residuo: https://platform.openai.com/api-keys')
|
543 |
else:
|
544 |
+
st.error('Errore nella generazione riprova o utilizza un altro modello AI')
|
545 |
|
546 |
if errore_generazione == False:
|
547 |
st.session_state.ultimo_messaggio = full_response
|