import os import gradio as gr from git import Repo from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext, Settings from llama_index.embeddings.openai import OpenAIEmbedding from llama_index.llms.openai import OpenAI # Configuração inicial GIT_REPO = "https://github.com/luiz-ouroboros/payment_api.git" PROJECT_DIR = "payment_api" STORAGE_DIR = "storage" # Configurar a chave da API da OpenAI os.environ["OPENAI_API_KEY"] = "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" # Baixar o repositório do GitHub (se não existir) if not os.path.exists(PROJECT_DIR): print("Clonando repositório...") Repo.clone_from(GIT_REPO, PROJECT_DIR) # Verificar se o repositório foi clonado corretamente if not os.path.exists(PROJECT_DIR) or not os.listdir(PROJECT_DIR): raise ValueError(f"Erro: O diretório {PROJECT_DIR} está vazio ou não foi clonado corretamente.") # Configurar LlamaIndex corretamente Settings.llm = OpenAI() Settings.embed_model = OpenAIEmbedding() # Lendo os arquivos do projeto documents = SimpleDirectoryReader(PROJECT_DIR).load_data() # Verificar se o diretório de persistência existe if not os.path.exists(STORAGE_DIR): try: print("Criando índice e persistindo dados...") index = VectorStoreIndex.from_documents(documents) index.storage_context.persist(persist_dir=STORAGE_DIR) except Exception as e: print(f"Erro ao criar o índice: {e}") raise else: try: print("Carregando índice existente...") storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR) index = VectorStoreIndex.from_documents(documents, storage_context=storage_context) except Exception as e: print(f"Erro ao carregar o índice: {e}") raise # Função para responder perguntas sobre o projeto def respond(message, history): try: query_engine = index.as_query_engine() response = query_engine.query(message) return response.response except Exception as e: return f"Erro ao processar a pergunta: {e}" # Interface do chatbot no Gradio demo = gr.ChatInterface(respond, title="IA Especializada no Meu Projeto") if __name__ == "__main__": demo.launch()