Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,9 @@ GIT_REPO = "https://github.com/luiz-ouroboros/payment_api.git"
|
|
| 10 |
PROJECT_DIR = "payment_api"
|
| 11 |
STORAGE_DIR = "storage"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
# Baixar o repositório do GitHub (se não existir)
|
| 14 |
if not os.path.exists(PROJECT_DIR):
|
| 15 |
print("Clonando repositório...")
|
|
@@ -28,21 +31,30 @@ documents = SimpleDirectoryReader(PROJECT_DIR).load_data()
|
|
| 28 |
|
| 29 |
# Verificar se o diretório de persistência existe
|
| 30 |
if not os.path.exists(STORAGE_DIR):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
else:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Função para responder perguntas sobre o projeto
|
| 42 |
def respond(message, history):
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Interface do chatbot no Gradio
|
| 48 |
demo = gr.ChatInterface(respond, title="IA Especializada no Meu Projeto")
|
|
|
|
| 10 |
PROJECT_DIR = "payment_api"
|
| 11 |
STORAGE_DIR = "storage"
|
| 12 |
|
| 13 |
+
# Configurar a chave da API da OpenAI
|
| 14 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
| 15 |
+
|
| 16 |
# Baixar o repositório do GitHub (se não existir)
|
| 17 |
if not os.path.exists(PROJECT_DIR):
|
| 18 |
print("Clonando repositório...")
|
|
|
|
| 31 |
|
| 32 |
# Verificar se o diretório de persistência existe
|
| 33 |
if not os.path.exists(STORAGE_DIR):
|
| 34 |
+
try:
|
| 35 |
+
print("Criando índice e persistindo dados...")
|
| 36 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 37 |
+
index.storage_context.persist(persist_dir=STORAGE_DIR)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Erro ao criar o índice: {e}")
|
| 40 |
+
raise
|
| 41 |
else:
|
| 42 |
+
try:
|
| 43 |
+
print("Carregando índice existente...")
|
| 44 |
+
storage_context = StorageContext.from_defaults(persist_dir=STORAGE_DIR)
|
| 45 |
+
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Erro ao carregar o índice: {e}")
|
| 48 |
+
raise
|
| 49 |
|
| 50 |
# Função para responder perguntas sobre o projeto
|
| 51 |
def respond(message, history):
|
| 52 |
+
try:
|
| 53 |
+
query_engine = index.as_query_engine()
|
| 54 |
+
response = query_engine.query(message)
|
| 55 |
+
return response.response
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Erro ao processar a pergunta: {e}"
|
| 58 |
|
| 59 |
# Interface do chatbot no Gradio
|
| 60 |
demo = gr.ChatInterface(respond, title="IA Especializada no Meu Projeto")
|