Spaces:
Sleeping
Sleeping
update app
Browse files
app.py
CHANGED
|
@@ -1,123 +1,132 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from typing import List
|
| 4 |
-
from llama_index.core import SimpleDirectoryReader, StorageContext, VectorStoreIndex
|
| 5 |
-
from llama_index.core.node_parser import SentenceSplitter
|
| 6 |
-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 7 |
-
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 8 |
-
from llama_index.llms.groq import Groq
|
| 9 |
-
from llama_index.core.memory import ChatSummaryMemoryBuffer
|
| 10 |
-
import chromadb
|
| 11 |
-
from tempfile import TemporaryDirectory
|
| 12 |
-
from PyPDF2 import PdfReader
|
| 13 |
-
from corretor import corrigir_texto # <<< Correção importada aqui
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from typing import List
|
| 4 |
+
from llama_index.core import SimpleDirectoryReader, StorageContext, VectorStoreIndex
|
| 5 |
+
from llama_index.core.node_parser import SentenceSplitter
|
| 6 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 7 |
+
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 8 |
+
from llama_index.llms.groq import Groq
|
| 9 |
+
from llama_index.core.memory import ChatSummaryMemoryBuffer
|
| 10 |
+
import chromadb
|
| 11 |
+
from tempfile import TemporaryDirectory
|
| 12 |
+
from PyPDF2 import PdfReader
|
| 13 |
+
from corretor import corrigir_texto # <<< Correção importada aqui
|
| 14 |
+
import platform
|
| 15 |
+
|
| 16 |
+
# Wrapper de embedding compatível com ChromaDB
|
| 17 |
+
class ChromaEmbeddingWrapper:
|
| 18 |
+
def __init__(self, model_name: str):
|
| 19 |
+
self.model = HuggingFaceEmbedding(model_name=model_name)
|
| 20 |
+
|
| 21 |
+
def __call__(self, input: List[str]) -> List[List[float]]:
|
| 22 |
+
return self.model.embed_documents(input)
|
| 23 |
+
|
| 24 |
+
# Inicializa modelos de embedding
|
| 25 |
+
embed_model = HuggingFaceEmbedding(model_name='intfloat/multilingual-e5-large')
|
| 26 |
+
embed_model_chroma = ChromaEmbeddingWrapper(model_name='intfloat/multilingual-e5-large')
|
| 27 |
+
|
| 28 |
+
# Inicializa ChromaDB
|
| 29 |
+
|
| 30 |
+
# Define caminho seguro dependendo do sistema operacional
|
| 31 |
+
if platform.system() == "Windows":
|
| 32 |
+
chroma_path = "./chroma_db"
|
| 33 |
+
else:
|
| 34 |
+
chroma_path = "/tmp/chroma_db"
|
| 35 |
+
|
| 36 |
+
chroma_client = chromadb.PersistentClient(path=chroma_path)
|
| 37 |
+
|
| 38 |
+
collection_name = 'documentos_bitdoglab'
|
| 39 |
+
chroma_collection = chroma_client.get_or_create_collection(
|
| 40 |
+
name=collection_name,
|
| 41 |
+
embedding_function=embed_model_chroma
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
| 45 |
+
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 46 |
+
|
| 47 |
+
# Inicializa LLM da Groq
|
| 48 |
+
Groq_api = os.environ.get("GROQ_API_KEY")
|
| 49 |
+
llms = Groq(model='llama3-70b-8192', api_key=Groq_api or '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')
|
| 50 |
+
|
| 51 |
+
# Estados globais
|
| 52 |
+
document_index = None
|
| 53 |
+
chat_engine = None
|
| 54 |
+
|
| 55 |
+
# Carregamento único do PDF
|
| 56 |
+
def carregar_pdf_inicial():
|
| 57 |
+
global document_index, chat_engine
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
with TemporaryDirectory() as tmpdir:
|
| 61 |
+
pdf_path = "BitDogLab_info_v2.pdf"
|
| 62 |
+
text = ""
|
| 63 |
+
reader = PdfReader(pdf_path)
|
| 64 |
+
for page in reader.pages:
|
| 65 |
+
text += page.extract_text() or ""
|
| 66 |
+
|
| 67 |
+
with open(os.path.join(tmpdir, "temp.txt"), "w", encoding="utf-8") as f:
|
| 68 |
+
f.write(text)
|
| 69 |
+
|
| 70 |
+
documentos = SimpleDirectoryReader(input_dir=tmpdir)
|
| 71 |
+
docs = documentos.load_data()
|
| 72 |
+
|
| 73 |
+
node_parser = SentenceSplitter(chunk_size=1200,chunk_overlap=150)
|
| 74 |
+
nodes = node_parser.get_nodes_from_documents(docs, show_progress=True)
|
| 75 |
+
|
| 76 |
+
document_index = VectorStoreIndex(nodes, storage_context=storage_context, embed_model=embed_model)
|
| 77 |
+
|
| 78 |
+
memory = ChatSummaryMemoryBuffer(llm=llms, token_limit=256)
|
| 79 |
+
|
| 80 |
+
chat_engine = document_index.as_chat_engine(
|
| 81 |
+
chat_mode='context',
|
| 82 |
+
llm=llms,
|
| 83 |
+
memory=memory,
|
| 84 |
+
system_prompt='''Você é especialista na placa BitDog Lab e sua função é ajudar os usuários nas dúvidas e informações sobre a placa e como criar códigos.'''
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
print("PDF carregado com sucesso.")
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"Erro ao carregar PDF: {e}")
|
| 91 |
+
|
| 92 |
+
# Função de chat com correção de texto
|
| 93 |
+
def converse_com_bot(message, chat_history):
|
| 94 |
+
global chat_engine
|
| 95 |
+
|
| 96 |
+
if chat_engine is None:
|
| 97 |
+
return "Erro: o bot ainda não está pronto.", chat_history
|
| 98 |
+
|
| 99 |
+
response = chat_engine.chat(message)
|
| 100 |
+
resposta_corrigida = corrigir_texto(response.response) # <<< Aplica correção
|
| 101 |
+
|
| 102 |
+
if chat_history is None:
|
| 103 |
+
chat_history = []
|
| 104 |
+
|
| 105 |
+
chat_history.append({"role": "user", "content": message})
|
| 106 |
+
chat_history.append({"role": "assistant", "content": resposta_corrigida})
|
| 107 |
+
|
| 108 |
+
return "", chat_history
|
| 109 |
+
|
| 110 |
+
# Resetar conversa
|
| 111 |
+
def resetar_chat():
|
| 112 |
+
global chat_engine
|
| 113 |
+
if chat_engine:
|
| 114 |
+
chat_engine.reset()
|
| 115 |
+
return []
|
| 116 |
+
|
| 117 |
+
# Carregar PDF na inicialização
|
| 118 |
+
carregar_pdf_inicial()
|
| 119 |
+
|
| 120 |
+
# Interface Gradio
|
| 121 |
+
with gr.Blocks() as app:
|
| 122 |
+
gr.Markdown("# 🤖 Chatbot BitDog Lab - Seu assistente para esclarecer dúvidas")
|
| 123 |
+
|
| 124 |
+
chatbot = gr.Chatbot(label="Conversa", type="messages")
|
| 125 |
+
msg = gr.Textbox(label='Digite a sua mensagem')
|
| 126 |
+
limpar = gr.Button('Limpar')
|
| 127 |
+
|
| 128 |
+
msg.submit(converse_com_bot, [msg, chatbot], [msg, chatbot])
|
| 129 |
+
limpar.click(resetar_chat, None, chatbot, queue=False)
|
| 130 |
+
|
| 131 |
+
#app.launch()
|
| 132 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|