Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,29 +3,28 @@ from transformers import pipeline
|
|
| 3 |
from langchain_community.llms import HuggingFacePipeline
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
-
|
| 7 |
-
from langchain_community.document_loaders import TextLoader # Importa TextLoader
|
| 8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 9 |
from langchain.chains import ConversationalRetrievalChain
|
| 10 |
from langchain.memory import ConversationBufferMemory
|
| 11 |
|
| 12 |
# 🔹 Inicializa pipeline com modelo leve
|
| 13 |
-
# Sugiro voltar para um modelo não-gated e super leve para depuração inicial,
|
| 14 |
-
# como 'google/flan-t5-small' ou 'TinyLlama/TinyLlama-1.1B-Chat-v1.0'
|
| 15 |
-
# Mantenha o max_new_tokens pequeno para evitar o erro "Too much data" novamente.
|
| 16 |
pipe = pipeline("text2text-generation", model="google/flan-t5-small", max_new_tokens=50)
|
| 17 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 18 |
|
| 19 |
# 🔹 Memória de conversa
|
| 20 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 21 |
|
| 22 |
-
# 🔹 Função de carregamento do documento (
|
| 23 |
def carregar_documento_txt(path):
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
documentos = loader.load()
|
| 27 |
|
| 28 |
-
# O RecursiveCharacterTextSplitter funciona bem para TXT também
|
| 29 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 30 |
documentos_divididos = splitter.split_documents(documentos)
|
| 31 |
|
|
@@ -34,7 +33,6 @@ def carregar_documento_txt(path):
|
|
| 34 |
return vectorstore
|
| 35 |
|
| 36 |
# 🔹 Carrega documento TXT do root
|
| 37 |
-
# ALTEARADO: Chama a nova função com o nome do arquivo TXT
|
| 38 |
vectorstore = carregar_documento_txt("meu_documento.txt")
|
| 39 |
|
| 40 |
# 🔹 Cria cadeia de resposta com memória + busca por similaridade
|
|
@@ -56,7 +54,7 @@ with gr.Blocks() as demo:
|
|
| 56 |
|
| 57 |
def interagir(msg, history):
|
| 58 |
resposta = responder(msg)
|
| 59 |
-
history.append([msg, resposta])
|
| 60 |
return "", history
|
| 61 |
|
| 62 |
entrada.submit(interagir, [entrada, chatbot], [entrada, chatbot])
|
|
|
|
| 3 |
from langchain_community.llms import HuggingFacePipeline
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
+
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 |
+
# 🔹 Função de carregamento do documento (TXT com codificação explícita)
|
| 19 |
def carregar_documento_txt(path):
|
| 20 |
+
# ATENÇÃO: Adicionado o parâmetro 'encoding' ao TextLoader
|
| 21 |
+
# Tente 'latin-1' ou 'iso-8859-1' primeiro, são os mais comuns para português
|
| 22 |
+
loader = TextLoader(path, encoding="latin-1")
|
| 23 |
+
# Se 'latin-1' não funcionar, tente 'cp1252' (Windows-1252)
|
| 24 |
+
# loader = TextLoader(path, encoding="cp1252")
|
| 25 |
+
|
| 26 |
documentos = loader.load()
|
| 27 |
|
|
|
|
| 28 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 29 |
documentos_divididos = splitter.split_documents(documentos)
|
| 30 |
|
|
|
|
| 33 |
return vectorstore
|
| 34 |
|
| 35 |
# 🔹 Carrega documento TXT do root
|
|
|
|
| 36 |
vectorstore = carregar_documento_txt("meu_documento.txt")
|
| 37 |
|
| 38 |
# 🔹 Cria cadeia de resposta com memória + busca por similaridade
|
|
|
|
| 54 |
|
| 55 |
def interagir(msg, history):
|
| 56 |
resposta = responder(msg)
|
| 57 |
+
history.append([msg, resposta])
|
| 58 |
return "", history
|
| 59 |
|
| 60 |
entrada.submit(interagir, [entrada, chatbot], [entrada, chatbot])
|