Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,75 @@ from llama_index.llms.openai import OpenAI
|
|
| 3 |
from huggingface_hub import snapshot_download
|
| 4 |
import gradio as gr
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# 🔑 Configure sua API da OpenAI (adicione no Hugging Face em Settings > Secrets)
|
| 8 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# 📥 Baixar a documentação do Hugging Face Dataset
|
| 11 |
docs_dir = snapshot_download(
|
| 12 |
repo_id="Jeice/n8n-docs-v2",
|
| 13 |
repo_type="dataset"
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# 🔎 Criar o índice vetorial
|
| 20 |
service_context = ServiceContext.from_defaults(
|
|
@@ -22,21 +79,24 @@ service_context = ServiceContext.from_defaults(
|
|
| 22 |
)
|
| 23 |
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
| 24 |
|
|
|
|
| 25 |
# 🚀 Criar engine de consulta
|
| 26 |
query_engine = index.as_query_engine()
|
| 27 |
|
|
|
|
| 28 |
# 💬 Função do chatbot
|
| 29 |
def chatbot(input_text):
|
| 30 |
response = query_engine.query(input_text)
|
| 31 |
return str(response)
|
| 32 |
|
|
|
|
| 33 |
# 🎨 Interface Gradio — Bonita e Funcional
|
| 34 |
interface = gr.Interface(
|
| 35 |
fn=chatbot,
|
| 36 |
inputs=gr.Textbox(lines=2, placeholder="Digite sua pergunta sobre o n8n..."),
|
| 37 |
outputs="text",
|
| 38 |
title="🤖 Bot de Dúvidas sobre o n8n",
|
| 39 |
-
description="Este agente responde dúvidas sobre o n8n, baseado na documentação oficial.",
|
| 40 |
examples=[
|
| 41 |
["Como criar um workflow no n8n?"],
|
| 42 |
["Para que serve o node HTTP Request?"],
|
|
@@ -46,5 +106,6 @@ interface = gr.Interface(
|
|
| 46 |
allow_flagging="never"
|
| 47 |
)
|
| 48 |
|
|
|
|
| 49 |
# 🚀 Executar
|
| 50 |
interface.launch()
|
|
|
|
| 3 |
from huggingface_hub import snapshot_download
|
| 4 |
import gradio as gr
|
| 5 |
import os
|
| 6 |
+
import yaml
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
|
| 10 |
# 🔑 Configure sua API da OpenAI (adicione no Hugging Face em Settings > Secrets)
|
| 11 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 12 |
|
| 13 |
+
|
| 14 |
+
# 🔥 Função para extrair conteúdo de YAML, JSON, TXT e MD
|
| 15 |
+
def extrair_conteudo_dos_arquivos(pasta):
|
| 16 |
+
texto_final = ""
|
| 17 |
+
|
| 18 |
+
for root, dirs, files in os.walk(pasta):
|
| 19 |
+
for file in files:
|
| 20 |
+
caminho_arquivo = os.path.join(root, file)
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
if file.endswith(('.yml', '.yaml')):
|
| 24 |
+
with open(caminho_arquivo, 'r', encoding='utf-8') as f:
|
| 25 |
+
data = yaml.safe_load(f)
|
| 26 |
+
texto = yaml.dump(data, allow_unicode=True)
|
| 27 |
+
texto_final += f"\n\n### Arquivo: {file}\n{texto}"
|
| 28 |
+
|
| 29 |
+
elif file.endswith('.json'):
|
| 30 |
+
with open(caminho_arquivo, 'r', encoding='utf-8') as f:
|
| 31 |
+
data = json.load(f)
|
| 32 |
+
texto = json.dumps(data, indent=2, ensure_ascii=False)
|
| 33 |
+
texto_final += f"\n\n### Arquivo: {file}\n{texto}"
|
| 34 |
+
|
| 35 |
+
elif file.endswith(('.md', '.txt')):
|
| 36 |
+
with open(caminho_arquivo, 'r', encoding='utf-8') as f:
|
| 37 |
+
texto = f.read()
|
| 38 |
+
texto_final += f"\n\n### Arquivo: {file}\n{texto}"
|
| 39 |
+
|
| 40 |
+
else:
|
| 41 |
+
continue
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Erro ao ler {file}: {e}")
|
| 45 |
+
|
| 46 |
+
return texto_final
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# 🔧 Função para gerar o arquivo documentacao.txt
|
| 50 |
+
def gerar_arquivo_documentacao(pasta_origem, arquivo_destino="documentacao.txt"):
|
| 51 |
+
texto = extrair_conteudo_dos_arquivos(pasta_origem)
|
| 52 |
+
|
| 53 |
+
if texto.strip() == "":
|
| 54 |
+
print("⚠️ Nenhum conteúdo encontrado nos arquivos!")
|
| 55 |
+
else:
|
| 56 |
+
with open(arquivo_destino, 'w', encoding='utf-8') as f:
|
| 57 |
+
f.write(texto)
|
| 58 |
+
print(f"✅ Arquivo {arquivo_destino} gerado com sucesso.")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
# 📥 Baixar a documentação do Hugging Face Dataset
|
| 62 |
docs_dir = snapshot_download(
|
| 63 |
repo_id="Jeice/n8n-docs-v2",
|
| 64 |
repo_type="dataset"
|
| 65 |
)
|
| 66 |
|
| 67 |
+
|
| 68 |
+
# 🔨 Gerar o arquivo de documentação consolidado
|
| 69 |
+
gerar_arquivo_documentacao(docs_dir)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# 📚 Carregar o documento gerado
|
| 73 |
+
documents = SimpleDirectoryReader(input_files=["documentacao.txt"]).load_data()
|
| 74 |
+
|
| 75 |
|
| 76 |
# 🔎 Criar o índice vetorial
|
| 77 |
service_context = ServiceContext.from_defaults(
|
|
|
|
| 79 |
)
|
| 80 |
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
| 81 |
|
| 82 |
+
|
| 83 |
# 🚀 Criar engine de consulta
|
| 84 |
query_engine = index.as_query_engine()
|
| 85 |
|
| 86 |
+
|
| 87 |
# 💬 Função do chatbot
|
| 88 |
def chatbot(input_text):
|
| 89 |
response = query_engine.query(input_text)
|
| 90 |
return str(response)
|
| 91 |
|
| 92 |
+
|
| 93 |
# 🎨 Interface Gradio — Bonita e Funcional
|
| 94 |
interface = gr.Interface(
|
| 95 |
fn=chatbot,
|
| 96 |
inputs=gr.Textbox(lines=2, placeholder="Digite sua pergunta sobre o n8n..."),
|
| 97 |
outputs="text",
|
| 98 |
title="🤖 Bot de Dúvidas sobre o n8n",
|
| 99 |
+
description="Este agente responde dúvidas sobre o n8n, baseado na documentação oficial que você subiu.",
|
| 100 |
examples=[
|
| 101 |
["Como criar um workflow no n8n?"],
|
| 102 |
["Para que serve o node HTTP Request?"],
|
|
|
|
| 106 |
allow_flagging="never"
|
| 107 |
)
|
| 108 |
|
| 109 |
+
|
| 110 |
# 🚀 Executar
|
| 111 |
interface.launch()
|