import gradio as gr import os import time import instructions from envConfig import envConfiguration from indexer import documentIndexer from conversation import conversationAgent from exporter import exportAgent #****************************************************************************************************************************************************************** chat_types = ["gpt-3.5-turbo", "gpt-3.5-turbo-1106", "gpt-4-1106-preview", "gpt-4-32k"] configuration = envConfiguration() conversation_agent = conversationAgent() counter_text = "Current buffer has {} tokens" #****************************************************************************************************************************************************************** configuration_error = "The settings have not been properly set. Please go to the **Settings** tab and set the proper values." conversation_error = "There is currently no conversation to export." #****************************************************************************************************************************************************************** def saveConfiguration(openai_key, openai_model, openai_temp, pinecone_key, pinecone_env, pinecone_index): configuration.set_config(openai_key, openai_model, openai_temp, pinecone_key, pinecone_env, pinecone_index) gr.Info("Configuration saved successfully") #****************************************************************************************************************************************************************** def generateIndexes(files): if not configuration.is_ready(): gr.Warning(configuration_error) return indexer = documentIndexer(configuration) for file in files: indexer.create_index(file) gr.Info("Documents indexed correctly") #****************************************************************************************************************************************************************** def indexMemory(): if not configuration.is_ready(): gr.Warning(configuration_error) return if not conversation_agent.is_ready(): gr.Info("There is currently no data to index") return indexer = documentIndexer(configuration) memory = conversation_agent.get_memory() indexer.index_memory(memory.buffer_as_str) conversation_agent.clear_memory() gr.Info("Memory indexed correctly") #****************************************************************************************************************************************************************** def clearChatHistory(): if conversation_agent.is_ready(): conversation_agent.clear_memory() return[] #****************************************************************************************************************************************************************** #def clearIndexes(): # indexer = documentIndexer(configuration) # # indexer.clear_indexes # # gr.Info("Document indexes cleared correctly") # # return [] #****************************************************************************************************************************************************************** def exportDocs(template_pdf_files): if not configuration.is_ready(): gr.Warning(configuration_error) return [] if not conversation_agent.is_ready(): gr.Warning(conversation_error) return [] exporter_agent = exportAgent(configuration, conversation_agent.get_memory().copy(deep = True)) generated_files = exporter_agent.do_export(template_pdf_files) return generated_files #****************************************************************************************************************************************************************** def sendPrompt(prompt, history): if not configuration.is_ready(): gr.Warning(configuration_error) return prompt, history else: history = history + [(prompt, None)] return "", history #****************************************************************************************************************************************************************** def receiveResponse(history): if configuration.is_ready(): if not conversation_agent.is_ready(): conversation_agent.create_conversation(configuration) response = conversation_agent.do_conversation(history[-1][0]) history[-1][1] = response return history #****************************************************************************************************************************************************************** def updateTokenCount(): if configuration.is_ready(): if not conversation_agent.is_ready(): conversation_agent.create_conversation(configuration) token_count = conversation_agent.get_tokens() return counter_text.format(token_count) #****************************************************************************************************************************************************************** #Main Form with gr.Blocks() as form: gr.Markdown("