Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,18 +10,24 @@ from langchain.memory import ConversationBufferMemory
|
|
| 10 |
from langchain.agents import Tool, initialize_agent
|
| 11 |
from langchain.agents.agent_types import AgentType
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
pipe = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 16 |
|
| 17 |
-
# Mem贸ria
|
| 18 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
def carregar_documento_txt(path):
|
| 22 |
loader = TextLoader(path, encoding="latin-1")
|
| 23 |
documentos = loader.load()
|
| 24 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=
|
| 25 |
documentos_divididos = splitter.split_documents(documentos)
|
| 26 |
embeddings = HuggingFaceEmbeddings()
|
| 27 |
vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
|
|
@@ -29,7 +35,7 @@ def carregar_documento_txt(path):
|
|
| 29 |
|
| 30 |
vectorstore = carregar_documento_txt("meu_documento.txt")
|
| 31 |
|
| 32 |
-
# Cadeia de
|
| 33 |
chat_chain = ConversationalRetrievalChain.from_llm(
|
| 34 |
llm=llm,
|
| 35 |
retriever=vectorstore.as_retriever(),
|
|
@@ -52,7 +58,7 @@ tools = [
|
|
| 52 |
Tool(
|
| 53 |
name="Responder Geral",
|
| 54 |
func=responder_geral,
|
| 55 |
-
description="Usa para
|
| 56 |
)
|
| 57 |
]
|
| 58 |
|
|
@@ -69,12 +75,17 @@ with gr.Blocks() as demo:
|
|
| 69 |
gr.Markdown("## 馃 Chat IA com Agente e Documento TXT")
|
| 70 |
|
| 71 |
chatbot = gr.Chatbot(label="Chat com Agente e Documento")
|
| 72 |
-
entrada = gr.Textbox(placeholder="Digite sua pergunta e aperte Enter
|
| 73 |
estado_chat = gr.State([])
|
| 74 |
|
| 75 |
def interagir(msg, history):
|
| 76 |
try:
|
| 77 |
resposta = agent.invoke(msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
resposta = f"Erro ao gerar resposta: {e}"
|
| 80 |
history = history + [(msg, resposta)]
|
|
@@ -87,3 +98,4 @@ with gr.Blocks() as demo:
|
|
| 87 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 88 |
|
| 89 |
|
|
|
|
|
|
| 10 |
from langchain.agents import Tool, initialize_agent
|
| 11 |
from langchain.agents.agent_types import AgentType
|
| 12 |
|
| 13 |
+
# Pipeline com limite maior de tokens e truncamento ativado
|
| 14 |
+
pipe = pipeline(
|
| 15 |
+
"text2text-generation",
|
| 16 |
+
model="google/flan-t5-small",
|
| 17 |
+
max_new_tokens=100,
|
| 18 |
+
truncation=True,
|
| 19 |
+
max_length=512,
|
| 20 |
+
)
|
| 21 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 22 |
|
| 23 |
+
# Mem贸ria limitada para 3 turnos
|
| 24 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, k=3)
|
| 25 |
|
| 26 |
+
# Carregamento do documento com chunks menores
|
| 27 |
def carregar_documento_txt(path):
|
| 28 |
loader = TextLoader(path, encoding="latin-1")
|
| 29 |
documentos = loader.load()
|
| 30 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
| 31 |
documentos_divididos = splitter.split_documents(documentos)
|
| 32 |
embeddings = HuggingFaceEmbeddings()
|
| 33 |
vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
|
|
|
|
| 35 |
|
| 36 |
vectorstore = carregar_documento_txt("meu_documento.txt")
|
| 37 |
|
| 38 |
+
# Cadeia de consulta com mem贸ria
|
| 39 |
chat_chain = ConversationalRetrievalChain.from_llm(
|
| 40 |
llm=llm,
|
| 41 |
retriever=vectorstore.as_retriever(),
|
|
|
|
| 58 |
Tool(
|
| 59 |
name="Responder Geral",
|
| 60 |
func=responder_geral,
|
| 61 |
+
description="Usa para perguntas gerais ou bate-papo."
|
| 62 |
)
|
| 63 |
]
|
| 64 |
|
|
|
|
| 75 |
gr.Markdown("## 馃 Chat IA com Agente e Documento TXT")
|
| 76 |
|
| 77 |
chatbot = gr.Chatbot(label="Chat com Agente e Documento")
|
| 78 |
+
entrada = gr.Textbox(placeholder="Digite sua pergunta e aperte Enter", show_label=False)
|
| 79 |
estado_chat = gr.State([])
|
| 80 |
|
| 81 |
def interagir(msg, history):
|
| 82 |
try:
|
| 83 |
resposta = agent.invoke(msg)
|
| 84 |
+
# Garantir resposta string para o chatbot
|
| 85 |
+
if isinstance(resposta, dict):
|
| 86 |
+
resposta = str(resposta)
|
| 87 |
+
elif not isinstance(resposta, str):
|
| 88 |
+
resposta = str(resposta)
|
| 89 |
except Exception as e:
|
| 90 |
resposta = f"Erro ao gerar resposta: {e}"
|
| 91 |
history = history + [(msg, resposta)]
|
|
|
|
| 98 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 99 |
|
| 100 |
|
| 101 |
+
|