Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,26 @@
|
|
| 1 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
| 2 |
from llama_index.llms.openai import OpenAI
|
| 3 |
-
from
|
| 4 |
import gradio as gr
|
| 5 |
import os
|
| 6 |
|
| 7 |
#Configure sua API da OpenAI (ou pode trocar por HuggingFace LLM depois)
|
| 8 |
os.environ["OPENAI_API_KEY"] = "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" # Coloque sua chave da OpenAI aqui
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
for item in dataset:
|
| 17 |
-
if "file" in item and item["file"]["path"].endswith(('.md', '.txt')):
|
| 18 |
-
file_name = item["file"]["path"].split("/")[-1]
|
| 19 |
-
with open(f"docs/{file_name}", "w", encoding="utf-8") as f:
|
| 20 |
-
f.write(item["text"])
|
| 21 |
-
|
| 22 |
-
#Criar o index
|
| 23 |
-
documents = SimpleDirectoryReader('docs').load_data()
|
| 24 |
|
|
|
|
| 25 |
service_context = ServiceContext.from_defaults(
|
| 26 |
llm=OpenAI(model="gpt-3.5-turbo")
|
| 27 |
)
|
| 28 |
-
|
| 29 |
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
|
|
|
|
|
|
| 30 |
query_engine = index.as_query_engine()
|
| 31 |
|
| 32 |
#Função do chatbot
|
|
@@ -34,20 +28,21 @@ def chatbot(input_text):
|
|
| 34 |
response = query_engine.query(input_text)
|
| 35 |
return str(response)
|
| 36 |
|
| 37 |
-
#Interface Gradio
|
| 38 |
interface = gr.Interface(
|
| 39 |
fn=chatbot,
|
| 40 |
-
inputs=gr.Textbox(lines=2, placeholder="Digite sua pergunta sobre o n8n
|
| 41 |
outputs="text",
|
| 42 |
title="🤖 Bot de Dúvidas sobre o n8n",
|
| 43 |
-
description="
|
| 44 |
theme="default",
|
| 45 |
examples=[
|
| 46 |
["Como criar um workflow no n8n?"],
|
| 47 |
["Para que serve o node HTTP Request?"],
|
| 48 |
-
["Quais são os nodes
|
| 49 |
],
|
| 50 |
allow_flagging="never"
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
| 1 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext
|
| 2 |
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 (ou pode trocar por HuggingFace LLM depois)
|
| 8 |
os.environ["OPENAI_API_KEY"] = "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" # Coloque sua chave da OpenAI aqui
|
| 9 |
|
| 10 |
+
#Baixar os arquivos do dataset no Hugging Face
|
| 11 |
+
print("📥 Baixando documentação...")
|
| 12 |
+
docs_dir = snapshot_download(repo_id="Jeice/n8n-docs-v2")
|
| 13 |
|
| 14 |
+
#Carregar os documentos
|
| 15 |
+
documents = SimpleDirectoryReader(docs_dir).load_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
#Criar o índice vetorial
|
| 18 |
service_context = ServiceContext.from_defaults(
|
| 19 |
llm=OpenAI(model="gpt-3.5-turbo")
|
| 20 |
)
|
|
|
|
| 21 |
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
|
| 22 |
+
|
| 23 |
+
#Criar engine de consulta
|
| 24 |
query_engine = index.as_query_engine()
|
| 25 |
|
| 26 |
#Função do chatbot
|
|
|
|
| 28 |
response = query_engine.query(input_text)
|
| 29 |
return str(response)
|
| 30 |
|
| 31 |
+
#Interface Gradio — bonita, limpa e simples
|
| 32 |
interface = gr.Interface(
|
| 33 |
fn=chatbot,
|
| 34 |
+
inputs=gr.Textbox(lines=2, placeholder="Digite sua pergunta sobre o n8n..."),
|
| 35 |
outputs="text",
|
| 36 |
title="🤖 Bot de Dúvidas sobre o n8n",
|
| 37 |
+
description="Este agente responde dúvidas sobre o n8n, baseado na documentação que você subiu para o Hugging Face.",
|
| 38 |
theme="default",
|
| 39 |
examples=[
|
| 40 |
["Como criar um workflow no n8n?"],
|
| 41 |
["Para que serve o node HTTP Request?"],
|
| 42 |
+
["Quais são os nodes para integração com Google Sheets?"],
|
| 43 |
],
|
| 44 |
allow_flagging="never"
|
| 45 |
)
|
| 46 |
|
| 47 |
+
#Rodar o app
|
| 48 |
+
interface.launch()
|