Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,15 +7,18 @@ from langchain_community.document_loaders import TextLoader
|
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# 🔹 Inicializa pipeline com modelo leve
|
| 12 |
pipe = pipeline("text2text-generation", model="google/flan-t5-small", max_new_tokens=50)
|
| 13 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 14 |
|
| 15 |
-
# 🔹 Memória de conversa
|
| 16 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 17 |
|
| 18 |
-
# 🔹
|
| 19 |
def carregar_documento_txt(path):
|
| 20 |
loader = TextLoader(path, encoding="latin-1")
|
| 21 |
documentos = loader.load()
|
|
@@ -25,30 +28,57 @@ def carregar_documento_txt(path):
|
|
| 25 |
vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
|
| 26 |
return vectorstore
|
| 27 |
|
| 28 |
-
# 🔹 Carrega documento TXT
|
| 29 |
vectorstore = carregar_documento_txt("meu_documento.txt")
|
| 30 |
|
| 31 |
-
# 🔹
|
| 32 |
chat_chain = ConversationalRetrievalChain.from_llm(
|
| 33 |
llm=llm,
|
| 34 |
retriever=vectorstore.as_retriever(),
|
| 35 |
memory=memory,
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# 🔹
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
return chat_chain.run({"question": pergunta})
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("## 🤖 Chat IA com Documento TXT")
|
| 45 |
|
| 46 |
-
chatbot = gr.Chatbot(label="Chat com Documento")
|
| 47 |
entrada = gr.Textbox(placeholder="Digite sua pergunta e aperte Enter ou clique em Enviar", show_label=False)
|
| 48 |
-
estado_chat = gr.State([])
|
| 49 |
|
| 50 |
def interagir(msg, history):
|
| 51 |
-
resposta =
|
| 52 |
history = history + [(msg, resposta)]
|
| 53 |
return "", history
|
| 54 |
|
|
@@ -56,7 +86,6 @@ with gr.Blocks() as demo:
|
|
| 56 |
enviar_btn = gr.Button("Enviar")
|
| 57 |
enviar_btn.click(interagir, [entrada, estado_chat], [entrada, chatbot])
|
| 58 |
|
| 59 |
-
# 🔹 Lança a interface
|
| 60 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 61 |
|
| 62 |
|
|
|
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
from langchain.agents import Tool, initialize_agent
|
| 11 |
+
from langchain.agents.agent_types import AgentType
|
| 12 |
+
|
| 13 |
|
| 14 |
# 🔹 Inicializa pipeline com modelo leve
|
| 15 |
pipe = pipeline("text2text-generation", model="google/flan-t5-small", max_new_tokens=50)
|
| 16 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 17 |
|
| 18 |
+
# 🔹 Memória de conversa para a cadeia de documentos
|
| 19 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 20 |
|
| 21 |
+
# 🔹 Carregamento e preparação do documento
|
| 22 |
def carregar_documento_txt(path):
|
| 23 |
loader = TextLoader(path, encoding="latin-1")
|
| 24 |
documentos = loader.load()
|
|
|
|
| 28 |
vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
|
| 29 |
return vectorstore
|
| 30 |
|
|
|
|
| 31 |
vectorstore = carregar_documento_txt("meu_documento.txt")
|
| 32 |
|
| 33 |
+
# 🔹 Cadeia de busca com memória
|
| 34 |
chat_chain = ConversationalRetrievalChain.from_llm(
|
| 35 |
llm=llm,
|
| 36 |
retriever=vectorstore.as_retriever(),
|
| 37 |
memory=memory,
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# 🔹 Define ferramentas para o agente
|
| 41 |
+
|
| 42 |
+
# 1. Responder baseado no documento
|
| 43 |
+
def responder_documento(pergunta):
|
| 44 |
return chat_chain.run({"question": pergunta})
|
| 45 |
|
| 46 |
+
# 2. Responder de forma geral (chat simples)
|
| 47 |
+
def responder_geral(pergunta):
|
| 48 |
+
# Apenas usa o LLM para responder livremente
|
| 49 |
+
return llm.run(pergunta)
|
| 50 |
+
|
| 51 |
+
tools = [
|
| 52 |
+
Tool(
|
| 53 |
+
name="Responder com Documento",
|
| 54 |
+
func=responder_documento,
|
| 55 |
+
description="Usa para responder perguntas específicas sobre o documento."
|
| 56 |
+
),
|
| 57 |
+
Tool(
|
| 58 |
+
name="Responder Geral",
|
| 59 |
+
func=responder_geral,
|
| 60 |
+
description="Usa para responder perguntas gerais, saudações ou conversas comuns."
|
| 61 |
+
)
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
# Inicializa agente com as ferramentas acima
|
| 65 |
+
agent = initialize_agent(
|
| 66 |
+
tools=tools,
|
| 67 |
+
llm=llm,
|
| 68 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 69 |
+
verbose=False,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Interface Gradio com histórico
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("## 🤖 Chat IA com Agente e Documento TXT")
|
| 75 |
|
| 76 |
+
chatbot = gr.Chatbot(label="Chat com Agente e Documento")
|
| 77 |
entrada = gr.Textbox(placeholder="Digite sua pergunta e aperte Enter ou clique em Enviar", show_label=False)
|
| 78 |
+
estado_chat = gr.State([])
|
| 79 |
|
| 80 |
def interagir(msg, history):
|
| 81 |
+
resposta = agent.run(msg)
|
| 82 |
history = history + [(msg, resposta)]
|
| 83 |
return "", history
|
| 84 |
|
|
|
|
| 86 |
enviar_btn = gr.Button("Enviar")
|
| 87 |
enviar_btn.click(interagir, [entrada, estado_chat], [entrada, chatbot])
|
| 88 |
|
|
|
|
| 89 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 90 |
|
| 91 |
|