import gradio as gr from llama_index.core import VectorStoreIndex,StorageContext from llama_index.core.memory import ChatMemoryBuffer import re from llama_index.core import get_response_synthesizer from llama_index.core.query_engine import RetrieverQueryEngine # Retrievers from llama_index.core.retrievers import ( VectorIndexRetriever, ) from llama_index.core.chat_engine import ContextChatEngine from llama_index.core.memory import ChatMemoryBuffer from pinecone import Pinecone from llama_index.vector_stores.pinecone import PineconeVectorStore import time from utils import * import spaces import threading import sys import torch from llama_index.core.vector_stores import ( MetadataFilter, MetadataFilters, FilterOperator, FilterCondition, ) head = """ """ css = """ #chatbot { margin-top: 1%; width: 75%; position:relative; height:70%; } #textBox{ width: 75%; position:relative; } .wrapper.svelte-nab2ao p{ font-size: 14px; } #btnClear{ width: 75%; } #buttonChat{ width:50%; position: relative; } #colonnaElementi{ position: absolute; left: 77%; top: 10%; bottom: 10%; /* Adjust this value as necessary */ width: 10%; height: auto; /* Let the height be determined by the top and bottom properties */ max-height: 80%; /* Ensure it does not exceed 80% of the parent container's height */ overflow-y: auto; /* Allow scrolling if content overflows vertically */ overflow-x: hidden; /* Hide horizontal overflow */ word-wrap: break-word; /* Ensure words break to fit within the width */ box-sizing: border-box; /* Include padding and border in the element's total width and height */ } #responseMode{ width: 5%; } .message.user.svelte-gutj6d.message-bubble-border{ padding: 5px; } .message.bot.svelte-gutj6d.message-bubble-border{ padding: 5px; } .icon { cursor: pointer; } /* Style for the hidden text */ .hidden-text { display: none; } .wrap svelte-1sk0pyu{ width: 12% } """ user_message="" current_chat_mode="" current_response_mode="tree_summarize" current_collection="BANDI_SISTEMA_PUGLIA" file_path="" num_responses=0 current_chat_mode="STANDARD" retriever=None token_count_bandi=0 token_count_bandi_sistema_puglia=0 chat_engine_bandi=None chat_engine_bandi_sistema_puglia=None memory_bandi=None memory_bandi_sistema_puglia=None stream_response=None divDocumenti=None llm = None def main(): global llm setGPU() llm = setLLM() Settings.llm = llm Settings.embed_model = "local:google-bert/bert-base-multilingual-cased" embed_model = Settings.embed_model text_qa_template, refine_template = setPromptTemplate() def select_initial_collection(): global current_collection global retriever global index pc = Pinecone(api_key="7e412663-a2dc-44a6-ab57-25dd0bdce226") # connect to index pinecone_index = pc.Index("indexbandisistemapuglia") vector_store = PineconeVectorStore( pinecone_index=pinecone_index, add_sparse_vector=True, ) storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_vector_store( vector_store, storage_context=storage_context ) retriever = VectorIndexRetriever(index=index, similarity_top_k=3, vector_store_query_mode="hybrid", embed_model=embed_model, alpha=0.5) current_collection = "BANDI_SISTEMA_PUGLIA" return "collezione settata" select_initial_collection() def select_collection(evt: gr.SelectData): global current_collection global retriever global chat_engine_bandi global chat_engine_bandi_sistema_puglia global token_count_bandi global token_count_bandi_sistema_puglia global memory_bandi global memory_bandi_sistema_puglia selected_collection = evt.value if(selected_collection != current_collection): if(selected_collection == "BANDI_SISTEMA_PUGLIA"): chat_engine_bandi.reset() chat_engine_bandi_sistema_puglia.reset() memory_bandi_sistema_puglia.reset() memory_bandi.reset() token_count_bandi = 0 token_count_bandi_sistema_puglia = 0 pc = Pinecone(api_key="7e412663-a2dc-44a6-ab57-25dd0bdce226") # connect to index pinecone_index = pc.Index("indexbandisistemapuglia") vector_store = PineconeVectorStore( pinecone_index=pinecone_index, add_sparse_vector=True, ) storage_context = StorageContext.from_defaults(vector_store=vector_store) # load your index from stored vectors index = VectorStoreIndex.from_vector_store( vector_store, storage_context=storage_context ) retriever = VectorIndexRetriever(index=index, similarity_top_k=3, vector_store_query_mode="hybrid", embed_model=embed_model, alpha=0.5) else: chat_engine_bandi.reset() chat_engine_bandi_sistema_puglia.reset() memory_bandi_sistema_puglia.reset() memory_bandi.reset() token_count_bandi = 0 token_count_bandi_sistema_puglia = 0 pc = Pinecone(api_key="7e412663-a2dc-44a6-ab57-25dd0bdce226") # connect to index pinecone_index = pc.Index("indexbandi") vector_store = PineconeVectorStore( pinecone_index=pinecone_index, add_sparse_vector=True, ) storage_context = StorageContext.from_defaults(vector_store=vector_store) # load your index from stored vectors index = VectorStoreIndex.from_vector_store( vector_store, storage_context=storage_context ) retriever = VectorIndexRetriever(index=index, similarity_top_k=3, vector_store_query_mode="hybrid", embed_model=embed_model, alpha=0.4) current_collection = selected_collection return "
"+node.metadata['nome_bando']+"
Nodo 🔍
"+node.text+"
" history[-1][1] = "" for character in stream_response.response_gen: tokens = character.split(" ") num_tokens = len(tokens) token_count_bandi = token_count_bandi + num_tokens print(token_count_bandi) history[-1][1] += html_escape(str(character)) time.sleep(0.05) yield history, responseHTML else: if(token_count_bandi_sistema_puglia >= 1000): print("RESET!!!") token_count_bandi_sistema_puglia = 0 memory_bandi_sistema_puglia.reset() chat_engine_bandi_sistema_puglia.reset() print(chat_engine_bandi_sistema_puglia.chat_history) print(memory_bandi_sistema_puglia) stream_response = None print(userMessage) stream_response = chat_engine_bandi_sistema_puglia.stream_chat(userMessage) print("risposta con chat engine") responseHTML = "" for i, node in enumerate(stream_response.source_nodes): responseHTML += ""+node.metadata['nome_bando']+"
Nodo 🔍
"+node.text+"
" history[-1][1] = "" for character in stream_response.response_gen: tokens = character.split(" ") num_tokens = len(tokens) token_count_bandi_sistema_puglia = token_count_bandi_sistema_puglia + num_tokens print(token_count_bandi_sistema_puglia) history[-1][1] += html_escape(str(character)) time.sleep(0.05) yield history,responseHTML else: print("MODALITA STANDARD") nome_bando = "" userMessage = userMessage.lower() if("diploma professionale" in userMessage): nome_bando += "Scheda Avviso Pubblico Diploma Professionale 2022.pdf," if("red" in userMessage): nome_bando += "Scheda RED 2020.pdf," if("ifts" in userMessage): nome_bando += "Scheda Avviso Pubblico IFTS_2023.pdf," if(("impianti" in userMessage) or ("idrogeno" in userMessage)): nome_bando += "Scheda Avviso PNRR - Impianti idrogeno rinnovabile.pdf," if("laureati" in userMessage): nome_bando += "Scheda Pass Laureati 2023.pdf," if("nidi" in userMessage): nome_bando += "Scheda NIDI - Nuove iniziative d'impresa_ Strumento di ingegneria finanziaria.pdf," if("microprestito" in userMessage): nome_bando += "Scheda MicroPrestito della Regione Puglia - edizione 2021.pdf," if("gol" in userMessage): nome_bando += "Scheda Garanzia di occupabilità dei lavoratori - GOL.pdf," if("edifici pubblici" in userMessage): nome_bando += "Scheda Efficientamento Energetico Edifici Pubblici.pdf," if("innoaid" in userMessage): nome_bando += "Scheda di sintesi Avviso _INNOAID - RIAPERTURA_.pdf," if("tecnonidi" in userMessage): nome_bando += "Scheda Avviso Tecnonidi - Aiuti alle piccole imprese innovative.pdf," if(("bando of" in userMessage) or ("avviso of" in userMessage)): nome_bando += "Scheda Avviso Pubblico OF a_f_ 2023_2024.pdf," if("giardin" in userMessage): nome_bando += "Scheda Avviso Pubblico _Giardiniere d'arte per giardini e parchi storici_.pdf," if("punti cardinali" in userMessage): nome_bando += "Scheda Avviso _Punti Cardinali_ punti di orientamento per la formazione e il lavoro.pdf," if("multimisura POC" in userMessage): nome_bando += "Avviso Multimisura POC.pdf," if("garanzia giovani" in userMessage): nome_bando += "Avviso Multimisura - Garanzia Giovani II Fase.pdf," if("apprendistato professionalizzante" in userMessage): nome_bando += "Apprendistato Professionalizzante.pdf," if(nome_bando!=""): # Rimuovi l'ultima virgola if nome_bando.endswith(","): nome_bando = nome_bando[:-1] # Crea una lista di bandi separati dalla virgola lista_bandi = nome_bando.split(",") # Crea una lista di oggetti MetadataFilter filter_list = [] for bando in lista_bandi: filter_list.append(MetadataFilter(key="nome_bando", value=bando)) #crea una lista di MetadataFilter filters = MetadataFilters( filters=filter_list, condition=FilterCondition.OR, ) retriever = VectorIndexRetriever(index=index, similarity_top_k=3, vector_store_query_mode="hybrid", embed_model=embed_model, alpha=0.5, filters=filters) else: retriever = VectorIndexRetriever(index=index, similarity_top_k=3, vector_store_query_mode="hybrid", embed_model=embed_model, alpha=0.5) if(str(current_response_mode)=="tree_summarize"): # define response synthesizer response_synthesizer = get_response_synthesizer(streaming=True,response_mode="tree_summarize",text_qa_template=text_qa_template) query_engine = None query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=response_synthesizer) stream_response = None print(userMessage) stream_response = query_engine.query(userMessage) print("risposta con query engine") responseHTML = "" for i, node in enumerate(stream_response.source_nodes): responseHTML += ""+node.metadata['nome_bando']+"
Nodo 🔍
"+node.text+"
" history[-1][1] = "" # Misura il tempo di inizio start_time = time.time() for character in stream_response.response_gen: history[-1][1] += html_escape(str(character)) time.sleep(0.05) yield history, responseHTML # Misura il tempo di fine end_time = time.time() # Calcola il tempo di esecuzione execution_time = end_time - start_time print(f"Tempo di esecuzione: {execution_time} secondi") else: # define response synthesizer response_synthesizer = get_response_synthesizer(streaming=True,response_mode="compact",text_qa_template=text_qa_template, refine_template=refine_template) query_engine = None query_engine = RetrieverQueryEngine(retriever=retriever, response_synthesizer=response_synthesizer) stream_response = None print(userMessage) stream_response = query_engine.query(userMessage) print("risposta con query engine") responseHTML = "" for i, node in enumerate(stream_response.source_nodes): responseHTML += ""+node.metadata['nome_bando']+"
Nodo 🔍
"+node.text+"
" history[-1][1] = "" for character in stream_response.response_gen: history[-1][1] += html_escape(str(character)) time.sleep(0.05) yield history, responseHTML torch.cuda.empty_cache() torch.cuda.reset_max_memory_allocated() torch.cuda.reset_max_memory_cached() msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( bot, chatbot, [chatbot, divDocumenti] ) demo.queue() demo.launch(debug=True, share=True) if __name__ == "__main__": main()