Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from langchain_community.llms import
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_community.document_loaders import PyMuPDFLoader
|
|
@@ -16,7 +16,6 @@ DOC_PATH = "meu-documento.pdf"
|
|
| 16 |
MODEL_NAME = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
|
| 17 |
|
| 18 |
# 🔑 Autenticação no Hugging Face Hub (Passo adicionado)
|
| 19 |
-
# Isso garante que o token seja usado para fazer o login antes de carregar o modelo.
|
| 20 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 21 |
if huggingface_token:
|
| 22 |
try:
|
|
@@ -45,18 +44,22 @@ def carregar_documento_pdf(path):
|
|
| 45 |
vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
|
| 46 |
return vectorstore
|
| 47 |
|
| 48 |
-
# ⚙️ Configura o pipeline com o modelo Mistral
|
|
|
|
| 49 |
try:
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 52 |
model=MODEL_NAME,
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
-
|
| 58 |
except Exception as e:
|
| 59 |
-
print(f"Erro ao carregar o modelo
|
| 60 |
llm = None
|
| 61 |
|
| 62 |
# 🧠 Memória da conversação
|
|
@@ -99,28 +102,26 @@ with gr.Blocks() as demo:
|
|
| 99 |
|
| 100 |
def interagir(msg, history):
|
| 101 |
if not chat_chain:
|
| 102 |
-
|
| 103 |
return "", history
|
| 104 |
|
| 105 |
try:
|
| 106 |
resposta_completa = chat_chain.invoke({"question": msg})
|
| 107 |
resposta_texto_bruto = resposta_completa['answer']
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# Busca a string "Resposta:" para extrair apenas a parte da resposta
|
| 111 |
partes = resposta_texto_bruto.split("Resposta:")
|
| 112 |
if len(partes) > 1:
|
| 113 |
-
|
| 114 |
-
resposta_texto = partes[1].strip()
|
| 115 |
else:
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
except Exception as e:
|
| 121 |
resposta_texto = f"Erro ao gerar resposta: {e}"
|
| 122 |
-
|
| 123 |
history.append((msg, resposta_texto))
|
|
|
|
| 124 |
return "", history
|
| 125 |
|
| 126 |
entrada.submit(interagir, [entrada, chatbot], [entrada, chatbot])
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from langchain_community.llms import CTransformers # AQUI está a alteração chave
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_community.document_loaders import PyMuPDFLoader
|
|
|
|
| 16 |
MODEL_NAME = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
|
| 17 |
|
| 18 |
# 🔑 Autenticação no Hugging Face Hub (Passo adicionado)
|
|
|
|
| 19 |
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 20 |
if huggingface_token:
|
| 21 |
try:
|
|
|
|
| 44 |
vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
|
| 45 |
return vectorstore
|
| 46 |
|
| 47 |
+
# ⚙️ Configura o pipeline com o modelo Mistral Quantizado
|
| 48 |
+
llm = None
|
| 49 |
try:
|
| 50 |
+
print("Tentando carregar o modelo quantizado...")
|
| 51 |
+
# Use CTransformers para carregar o modelo GGUF
|
| 52 |
+
llm = CTransformers(
|
| 53 |
model=MODEL_NAME,
|
| 54 |
+
model_type="mistral",
|
| 55 |
+
config={
|
| 56 |
+
"max_new_tokens": 256,
|
| 57 |
+
"temperature": 0.7
|
| 58 |
+
}
|
| 59 |
)
|
| 60 |
+
print("Modelo carregado com sucesso.")
|
| 61 |
except Exception as e:
|
| 62 |
+
print(f"Erro ao carregar o modelo CTransformers: {e}")
|
| 63 |
llm = None
|
| 64 |
|
| 65 |
# 🧠 Memória da conversação
|
|
|
|
| 102 |
|
| 103 |
def interagir(msg, history):
|
| 104 |
if not chat_chain:
|
| 105 |
+
history.append((msg, "Erro: A cadeia de chat não foi inicializada."))
|
| 106 |
return "", history
|
| 107 |
|
| 108 |
try:
|
| 109 |
resposta_completa = chat_chain.invoke({"question": msg})
|
| 110 |
resposta_texto_bruto = resposta_completa['answer']
|
| 111 |
+
|
| 112 |
+
# --- Correção para remover o prompt duplicado ---
|
|
|
|
| 113 |
partes = resposta_texto_bruto.split("Resposta:")
|
| 114 |
if len(partes) > 1:
|
| 115 |
+
resposta_texto = partes[1].strip()
|
|
|
|
| 116 |
else:
|
| 117 |
+
resposta_texto = resposta_texto_bruto
|
| 118 |
+
# --- Fim da correção ---
|
| 119 |
+
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
resposta_texto = f"Erro ao gerar resposta: {e}"
|
| 122 |
+
|
| 123 |
history.append((msg, resposta_texto))
|
| 124 |
+
|
| 125 |
return "", history
|
| 126 |
|
| 127 |
entrada.submit(interagir, [entrada, chatbot], [entrada, chatbot])
|