Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,85 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
| 4 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
from langchain_community.vectorstores import FAISS
|
| 7 |
from langchain.chains import create_retrieval_chain
|
|
|
|
| 2 |
import os
|
| 3 |
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
| 4 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain.chains import create_retrieval_chain
|
| 8 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 9 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 10 |
+
|
| 11 |
+
DATA_DIR = "Data" if os.path.exists("Data") else "data"
|
| 12 |
+
if not os.path.exists(DATA_DIR):
|
| 13 |
+
os.makedirs(DATA_DIR)
|
| 14 |
+
|
| 15 |
+
print(f"Usando diretorio: {DATA_DIR}")
|
| 16 |
+
print(f"Arquivos: {os.listdir(DATA_DIR) if os.path.exists(DATA_DIR) else 'vazio'}")
|
| 17 |
+
|
| 18 |
+
loader = PyPDFDirectoryLoader(DATA_DIR)
|
| 19 |
+
documents = loader.load()
|
| 20 |
+
print(f"Documentos carregados: {len(documents)}")
|
| 21 |
+
|
| 22 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
|
| 23 |
+
docs = text_splitter.split_documents(documents)
|
| 24 |
+
|
| 25 |
+
model_name = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
| 26 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
| 27 |
+
|
| 28 |
+
if docs:
|
| 29 |
+
db = FAISS.from_documents(docs, embeddings)
|
| 30 |
+
else:
|
| 31 |
+
import faiss
|
| 32 |
+
dimension = 384
|
| 33 |
+
index = faiss.IndexFlatL2(dimension)
|
| 34 |
+
from langchain_community.docstore.in_memory import InMemoryDocstore
|
| 35 |
+
db = FAISS(embedding_function=embeddings, index=index, docstore=InMemoryDocstore(), index_to_docstore_id={})
|
| 36 |
+
|
| 37 |
+
retriever = db.as_retriever(search_kwargs={"k": 3})
|
| 38 |
+
|
| 39 |
+
# Usando HuggingFaceEndpoint (substituto moderno do HuggingFaceHub)
|
| 40 |
+
llm = HuggingFaceEndpoint(
|
| 41 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 42 |
+
task="text-generation",
|
| 43 |
+
huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN"),
|
| 44 |
+
max_new_tokens=512,
|
| 45 |
+
temperature=0.7
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
system_prompt = (
|
| 49 |
+
"Você é o Prometheus, um assistente especializado no DETRAN-RJ. "
|
| 50 |
+
"Use o contexto abaixo para responder às perguntas dos usuários de forma clara e profissional. "
|
| 51 |
+
"Se não souber a resposta com base no contexto, diga que não encontrou a informação específica, "
|
| 52 |
+
"mas tente ajudar com o que for possível.\n\n"
|
| 53 |
+
"{context}"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 57 |
+
("system", system_prompt),
|
| 58 |
+
("human", "{input}"),
|
| 59 |
+
])
|
| 60 |
+
|
| 61 |
+
question_answer_chain = create_stuff_documents_chain(llm, prompt)
|
| 62 |
+
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 63 |
+
|
| 64 |
+
def respond(message, history):
|
| 65 |
+
try:
|
| 66 |
+
response = rag_chain.invoke({"input": message})
|
| 67 |
+
return response["answer"]
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Erro ao processar consulta: {str(e)}"
|
| 70 |
+
|
| 71 |
+
demo = gr.ChatInterface(
|
| 72 |
+
respond,
|
| 73 |
+
title="Prometheus — Agente Especialista DETRAN-RJ",
|
| 74 |
+
description="IA treinada com regulamentações e manuais do DETRAN-RJ para suporte ao cidadão.",
|
| 75 |
+
examples=["Como renovar a CNH?", "Quais os documentos para transferência de veículo?", "O que é o GRT?"]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|
| 80 |
+
import gradio as gr
|
| 81 |
+
import os
|
| 82 |
+
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
| 83 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 84 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 85 |
from langchain_community.vectorstores import FAISS
|
| 86 |
from langchain.chains import create_retrieval_chain
|