Update app.py
Browse files
app.py
CHANGED
|
@@ -8,63 +8,18 @@ from langchain_community.document_loaders import PyPDFLoader
|
|
| 8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 9 |
|
| 10 |
# ----------------------------
|
| 11 |
-
#
|
| 12 |
# ----------------------------
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
raise RuntimeError(
|
| 16 |
-
"🔒 NV_API_KEY not set. Configure it under Settings → Variables & Secrets."
|
| 17 |
-
)
|
| 18 |
|
| 19 |
-
# NVIDIA-compatible OpenAI client for chat & embeddings
|
| 20 |
-
client = OpenAI(
|
| 21 |
-
base_url="https://integrate.api.nvidia.com/v1",
|
| 22 |
-
api_key=NV_API_KEY
|
| 23 |
-
)
|
| 24 |
-
CHAT_MODEL = "meta/llama3-8b-instruct"
|
| 25 |
-
EMBED_MODEL = "nvidia/embedding-1"
|
| 26 |
-
|
| 27 |
-
# ----------------------------
|
| 28 |
-
# App Configuration
|
| 29 |
-
# ----------------------------
|
| 30 |
-
APP_TITLE = "CVchat – Ronaldo Menezes"
|
| 31 |
-
INTRO = (
|
| 32 |
-
"👋 Olá! Eu sou o CVchat do Ronaldo Menezes.\n"
|
| 33 |
-
"Converse sobre minha experiência, projetos, tecnologias, resultados e muito mais.\n\n"
|
| 34 |
-
"Exemplos de perguntas:\n"
|
| 35 |
-
"• Quem é o Ronaldo Menezes\n"
|
| 36 |
-
"• Resuma sua experiência com Process Mining.\n"
|
| 37 |
-
"• Que linguagens e ferramentas você domina?\n"
|
| 38 |
-
"• Fale de um projeto com financiamento público que você liderou.\n"
|
| 39 |
-
)
|
| 40 |
-
SUGGESTION_QUESTIONS = [
|
| 41 |
-
"Links & exemplos de trabalhos",
|
| 42 |
-
"Quais tecnologias você mais usa?",
|
| 43 |
-
"Resuma sua experiência com Machine Learning.",
|
| 44 |
-
"Artigo sobre Landsat ou Sentinel?",
|
| 45 |
-
"Você já trabalhou com mainframe/COBOL?",
|
| 46 |
-
"Certificações?",
|
| 47 |
-
]
|
| 48 |
-
|
| 49 |
-
# Paths for files generated by build_index.py
|
| 50 |
-
INDEX_FILE = "r_docs.index"
|
| 51 |
-
CHUNKS_FILE = "r_chunks.npy"
|
| 52 |
-
PDF_PATH = "CV-Ronaldo_Menezes_2025_06.pdf"
|
| 53 |
-
|
| 54 |
-
# verify index files exist
|
| 55 |
-
if not Path(INDEX_FILE).exists() or not Path(CHUNKS_FILE).exists():
|
| 56 |
-
raise FileNotFoundError(
|
| 57 |
-
"Index not found. Please run first:\n python build_index.py"
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
# load FAISS index and chunks
|
| 61 |
-
tmp_index = faiss.read_index(INDEX_FILE)
|
| 62 |
-
chunks = np.load(CHUNKS_FILE, allow_pickle=True)
|
| 63 |
-
|
| 64 |
-
# ----------------------------
|
| 65 |
-
# Context retrieval via NVIDIA Embeddings API
|
| 66 |
-
# ----------------------------
|
| 67 |
def retrieve_context(query: str, k: int = 4) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# call NVIDIA embeddings
|
| 69 |
try:
|
| 70 |
resp = client.embeddings.create(
|
|
@@ -183,3 +138,4 @@ with gr.Blocks(title=APP_TITLE, css=custom_css, theme=gr.themes.Base()) as demo:
|
|
| 183 |
if __name__ == "__main__":
|
| 184 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 185 |
|
|
|
|
|
|
| 8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 9 |
|
| 10 |
# ----------------------------
|
| 11 |
+
# Context retrieval using local SentenceTransformer embeddings
|
| 12 |
# ----------------------------
|
| 13 |
+
# Embedding model for context retrieval (local)
|
| 14 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def retrieve_context(query: str, k: int = 4) -> str:
|
| 17 |
+
# encode locally
|
| 18 |
+
q_emb = embedding_model.encode([query], convert_to_numpy=True)
|
| 19 |
+
_, I = index.search(q_emb, k)
|
| 20 |
+
return "
|
| 21 |
+
---
|
| 22 |
+
".join(chunks[i] for i in I[0])(query: str, k: int = 4) -> str:
|
| 23 |
# call NVIDIA embeddings
|
| 24 |
try:
|
| 25 |
resp = client.embeddings.create(
|
|
|
|
| 138 |
if __name__ == "__main__":
|
| 139 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 140 |
|
| 141 |
+
|