Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,50 +3,70 @@ 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 |
-
from langchain_community.document_loaders import
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
from langchain.memory import ConversationBufferMemory
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
pipe = pipeline(
|
| 13 |
"text2text-generation",
|
| 14 |
-
model="
|
| 15 |
-
max_new_tokens=
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
)
|
| 19 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 20 |
|
| 21 |
-
# Memória
|
| 22 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, k=3)
|
| 23 |
|
| 24 |
-
# Carrega
|
| 25 |
def carregar_documento_pdf(path):
|
| 26 |
-
loader =
|
| 27 |
documentos = loader.load()
|
| 28 |
-
splitter = RecursiveCharacterTextSplitter(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
documentos_divididos = splitter.split_documents(documentos)
|
| 30 |
embeddings = HuggingFaceEmbeddings()
|
| 31 |
vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
|
| 32 |
return vectorstore
|
| 33 |
|
| 34 |
-
# Indexa documento
|
| 35 |
vectorstore = carregar_documento_pdf("meu_documento.pdf")
|
| 36 |
|
| 37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
chat_chain = ConversationalRetrievalChain.from_llm(
|
| 39 |
llm=llm,
|
| 40 |
-
retriever=vectorstore.as_retriever(),
|
| 41 |
memory=memory,
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# Interface Gradio
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
gr.Markdown("##
|
| 47 |
|
| 48 |
-
chatbot = gr.Chatbot(label="Chat com Documento")
|
| 49 |
-
entrada = gr.Textbox(placeholder="Digite sua pergunta e
|
| 50 |
estado_chat = gr.State([])
|
| 51 |
|
| 52 |
def interagir(msg, history):
|
|
@@ -63,3 +83,4 @@ with gr.Blocks() as demo:
|
|
| 63 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 64 |
|
| 65 |
|
|
|
|
|
|
| 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 PyMuPDFLoader
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
from langchain.prompts import PromptTemplate
|
| 11 |
|
| 12 |
+
# ⚙️ Modelo mais robusto para Spaces com GPU (pode mudar para mistralai/Mistral-7B-Instruct ou zephyr-7b-beta)
|
| 13 |
pipe = pipeline(
|
| 14 |
"text2text-generation",
|
| 15 |
+
model="mistralai/Mistral-7B-Instruct",
|
| 16 |
+
max_new_tokens=1024,
|
| 17 |
+
do_sample=True,
|
| 18 |
+
temperature=0.3,
|
| 19 |
+
repetition_penalty=1.1
|
| 20 |
)
|
| 21 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 22 |
|
| 23 |
+
# 🧠 Memória de curto prazo para manter conversas coerentes
|
| 24 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, k=3)
|
| 25 |
|
| 26 |
+
# 📄 Carrega e processa documento PDF fixo
|
| 27 |
def carregar_documento_pdf(path):
|
| 28 |
+
loader = PyMuPDFLoader(path)
|
| 29 |
documentos = loader.load()
|
| 30 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 31 |
+
chunk_size=1000,
|
| 32 |
+
chunk_overlap=200,
|
| 33 |
+
separators=["\n\n", "\n", ".", " "]
|
| 34 |
+
)
|
| 35 |
documentos_divididos = splitter.split_documents(documentos)
|
| 36 |
embeddings = HuggingFaceEmbeddings()
|
| 37 |
vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
|
| 38 |
return vectorstore
|
| 39 |
|
| 40 |
+
# 🧠 Indexa documento uma única vez
|
| 41 |
vectorstore = carregar_documento_pdf("meu_documento.pdf")
|
| 42 |
|
| 43 |
+
# 🧾 Prompt personalizado (importante para coerência com documento)
|
| 44 |
+
custom_prompt = PromptTemplate.from_template("""
|
| 45 |
+
Você é um assistente especializado em responder perguntas com base no conteúdo abaixo.
|
| 46 |
+
|
| 47 |
+
Use **apenas** essas informações. Se não souber, diga "Não sei com base no documento fornecido".
|
| 48 |
+
|
| 49 |
+
Contexto:
|
| 50 |
+
{context}
|
| 51 |
+
|
| 52 |
+
Pergunta:
|
| 53 |
+
{question}
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
# 🔁 Cria a cadeia de RAG com prompt e memória
|
| 57 |
chat_chain = ConversationalRetrievalChain.from_llm(
|
| 58 |
llm=llm,
|
| 59 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 4}),
|
| 60 |
memory=memory,
|
| 61 |
+
combine_docs_chain_kwargs={"prompt": custom_prompt}
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# 💬 Interface Gradio
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
+
gr.Markdown("## 📄💬 Chat com Documento PDF (Mistral-7B + RAG)")
|
| 67 |
|
| 68 |
+
chatbot = gr.Chatbot(label="Chat com Documento PDF")
|
| 69 |
+
entrada = gr.Textbox(placeholder="Digite sua pergunta e pressione Enter", show_label=False)
|
| 70 |
estado_chat = gr.State([])
|
| 71 |
|
| 72 |
def interagir(msg, history):
|
|
|
|
| 83 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
|
| 84 |
|
| 85 |
|
| 86 |
+
|