Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ NV_API_KEY = os.environ.get("NV_API_KEY")
|
|
| 13 |
if not NV_API_KEY:
|
| 14 |
raise RuntimeError(
|
| 15 |
"🔒 NV_API_KEY not set. "
|
| 16 |
-
"
|
| 17 |
)
|
| 18 |
|
| 19 |
# NVIDIA-compatible OpenAI client for chat
|
|
@@ -29,7 +29,7 @@ CHAT_MODEL = "meta/llama3-8b-instruct"
|
|
| 29 |
APP_TITLE = "CVchat – Ronaldo Menezes"
|
| 30 |
INTRO = (
|
| 31 |
"👋 Olá! Eu sou o CVchat do Ronaldo Menezes.\n"
|
| 32 |
-
"Converse sobre minha experiência, projetos, tecnologias
|
| 33 |
"Exemplos de perguntas:\n"
|
| 34 |
"• Quem é o Ronaldo Menezes\n"
|
| 35 |
"• Resuma sua experiência com Process Mining.\n"
|
|
@@ -45,23 +45,23 @@ SUGGESTION_QUESTIONS = [
|
|
| 45 |
"Certificações?",
|
| 46 |
]
|
| 47 |
|
| 48 |
-
# Paths for FAISS
|
| 49 |
INDEX_FILE = "r_docs.index"
|
| 50 |
CHUNKS_FILE = "r_chunks.npy"
|
| 51 |
PDF_PATH = "CV-Ronaldo_Menezes_2025_06.pdf"
|
| 52 |
|
| 53 |
-
#
|
| 54 |
if not Path(INDEX_FILE).exists() or not Path(CHUNKS_FILE).exists():
|
| 55 |
raise FileNotFoundError(
|
| 56 |
"Index not found. Run build_index.py to generate r_docs.index and r_chunks.npy."
|
| 57 |
)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
index = faiss.read_index(INDEX_FILE)
|
| 61 |
chunks = np.load(CHUNKS_FILE, allow_pickle=True)
|
| 62 |
|
| 63 |
# ----------------------------
|
| 64 |
-
#
|
| 65 |
# ----------------------------
|
| 66 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 67 |
|
|
@@ -83,7 +83,7 @@ def chatbot(user_input: str, temperature: float, top_p: float, max_tokens: int):
|
|
| 83 |
if not user_input:
|
| 84 |
return dialog_history, ""
|
| 85 |
|
| 86 |
-
#
|
| 87 |
context = retrieve_context(user_input)
|
| 88 |
system_msg = {
|
| 89 |
"role": "system",
|
|
@@ -94,16 +94,16 @@ def chatbot(user_input: str, temperature: float, top_p: float, max_tokens: int):
|
|
| 94 |
)
|
| 95 |
}
|
| 96 |
|
| 97 |
-
#
|
| 98 |
messages = [system_msg]
|
| 99 |
for u, a in dialog_history:
|
| 100 |
-
messages
|
| 101 |
{"role": "user", "content": u},
|
| 102 |
{"role": "assistant", "content": a}
|
| 103 |
-
]
|
| 104 |
messages.append({"role": "user", "content": user_input})
|
| 105 |
|
| 106 |
-
#
|
| 107 |
assistant_reply = ""
|
| 108 |
try:
|
| 109 |
stream = client.chat.completions.create(
|
|
@@ -121,11 +121,12 @@ def chatbot(user_input: str, temperature: float, top_p: float, max_tokens: int):
|
|
| 121 |
except OpenAIError as e:
|
| 122 |
assistant_reply = f"⚠️ API Error: {e.__class__.__name__}: {e}"
|
| 123 |
|
|
|
|
| 124 |
dialog_history.append((user_input, assistant_reply))
|
| 125 |
return dialog_history, ""
|
| 126 |
|
| 127 |
# ----------------------------
|
| 128 |
-
# Clear
|
| 129 |
# ----------------------------
|
| 130 |
def clear_history():
|
| 131 |
global dialog_history
|
|
@@ -144,44 +145,13 @@ custom_css = r"""
|
|
| 144 |
--radius: 8px;
|
| 145 |
--spacing: 1rem;
|
| 146 |
}
|
| 147 |
-
body {
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
overflow-y: auto;
|
| 155 |
-
padding: var(--spacing);
|
| 156 |
-
border: 1px solid #ddd;
|
| 157 |
-
border-radius: var(--radius);
|
| 158 |
-
}
|
| 159 |
-
#input-area {
|
| 160 |
-
display: flex;
|
| 161 |
-
margin-top: var(--spacing);
|
| 162 |
-
}
|
| 163 |
-
#user-input {
|
| 164 |
-
flex: 1;
|
| 165 |
-
padding: 0.6rem;
|
| 166 |
-
border: 1px solid #ccc;
|
| 167 |
-
border-radius: var(--radius) 0 0 var(--radius);
|
| 168 |
-
}
|
| 169 |
-
#send-button {
|
| 170 |
-
padding: 0 1rem;
|
| 171 |
-
background: var(--primary);
|
| 172 |
-
color: white;
|
| 173 |
-
border: none;
|
| 174 |
-
border-radius: 0 var(--radius) var(--radius) 0;
|
| 175 |
-
cursor: pointer;
|
| 176 |
-
}
|
| 177 |
-
.sidebar {
|
| 178 |
-
background: var(--bg-light);
|
| 179 |
-
padding: var(--spacing);
|
| 180 |
-
border-left: 1px solid #eee;
|
| 181 |
-
}
|
| 182 |
-
.sidebar h3 {
|
| 183 |
-
margin-top: 0;
|
| 184 |
-
}
|
| 185 |
"""
|
| 186 |
|
| 187 |
with gr.Blocks(title=APP_TITLE, css=custom_css, theme=gr.themes.Base()) as demo:
|
|
@@ -189,21 +159,22 @@ with gr.Blocks(title=APP_TITLE, css=custom_css, theme=gr.themes.Base()) as demo:
|
|
| 189 |
gr.Markdown(INTRO)
|
| 190 |
|
| 191 |
with gr.Row():
|
| 192 |
-
#
|
| 193 |
with gr.Column(scale=3):
|
| 194 |
-
chatbot_ui
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
| 204 |
gr.Button("Limpar").click(clear_history, [], [chatbot_ui, txt])
|
| 205 |
|
| 206 |
-
#
|
| 207 |
with gr.Column(scale=1, elem_classes="sidebar"):
|
| 208 |
if Path(PDF_PATH).exists():
|
| 209 |
gr.Markdown(f"[📄 Baixar CV em PDF](/file={PDF_PATH})")
|
|
|
|
| 13 |
if not NV_API_KEY:
|
| 14 |
raise RuntimeError(
|
| 15 |
"🔒 NV_API_KEY not set. "
|
| 16 |
+
"Configure it in Settings → Variables & Secrets."
|
| 17 |
)
|
| 18 |
|
| 19 |
# NVIDIA-compatible OpenAI client for chat
|
|
|
|
| 29 |
APP_TITLE = "CVchat – Ronaldo Menezes"
|
| 30 |
INTRO = (
|
| 31 |
"👋 Olá! Eu sou o CVchat do Ronaldo Menezes.\n"
|
| 32 |
+
"Converse sobre minha experiência, projetos, tecnologias e resultados.\n\n"
|
| 33 |
"Exemplos de perguntas:\n"
|
| 34 |
"• Quem é o Ronaldo Menezes\n"
|
| 35 |
"• Resuma sua experiência com Process Mining.\n"
|
|
|
|
| 45 |
"Certificações?",
|
| 46 |
]
|
| 47 |
|
| 48 |
+
# Paths for FAISS index and chunks
|
| 49 |
INDEX_FILE = "r_docs.index"
|
| 50 |
CHUNKS_FILE = "r_chunks.npy"
|
| 51 |
PDF_PATH = "CV-Ronaldo_Menezes_2025_06.pdf"
|
| 52 |
|
| 53 |
+
# Verify that the index files exist
|
| 54 |
if not Path(INDEX_FILE).exists() or not Path(CHUNKS_FILE).exists():
|
| 55 |
raise FileNotFoundError(
|
| 56 |
"Index not found. Run build_index.py to generate r_docs.index and r_chunks.npy."
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Load FAISS index and chunks
|
| 60 |
index = faiss.read_index(INDEX_FILE)
|
| 61 |
chunks = np.load(CHUNKS_FILE, allow_pickle=True)
|
| 62 |
|
| 63 |
# ----------------------------
|
| 64 |
+
# Context retrieval (local embeddings)
|
| 65 |
# ----------------------------
|
| 66 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 67 |
|
|
|
|
| 83 |
if not user_input:
|
| 84 |
return dialog_history, ""
|
| 85 |
|
| 86 |
+
# Retrieve context
|
| 87 |
context = retrieve_context(user_input)
|
| 88 |
system_msg = {
|
| 89 |
"role": "system",
|
|
|
|
| 94 |
)
|
| 95 |
}
|
| 96 |
|
| 97 |
+
# Build messages list
|
| 98 |
messages = [system_msg]
|
| 99 |
for u, a in dialog_history:
|
| 100 |
+
messages.extend([
|
| 101 |
{"role": "user", "content": u},
|
| 102 |
{"role": "assistant", "content": a}
|
| 103 |
+
])
|
| 104 |
messages.append({"role": "user", "content": user_input})
|
| 105 |
|
| 106 |
+
# Call NVIDIA chat API in streaming mode
|
| 107 |
assistant_reply = ""
|
| 108 |
try:
|
| 109 |
stream = client.chat.completions.create(
|
|
|
|
| 121 |
except OpenAIError as e:
|
| 122 |
assistant_reply = f"⚠️ API Error: {e.__class__.__name__}: {e}"
|
| 123 |
|
| 124 |
+
# Update history and return
|
| 125 |
dialog_history.append((user_input, assistant_reply))
|
| 126 |
return dialog_history, ""
|
| 127 |
|
| 128 |
# ----------------------------
|
| 129 |
+
# Clear history
|
| 130 |
# ----------------------------
|
| 131 |
def clear_history():
|
| 132 |
global dialog_history
|
|
|
|
| 145 |
--radius: 8px;
|
| 146 |
--spacing: 1rem;
|
| 147 |
}
|
| 148 |
+
body { background: var(--bg-light); color: var(--txt-dark); font-family: 'Helvetica Neue', sans-serif; }
|
| 149 |
+
#chat-window { height: 65vh; overflow-y: auto; padding: var(--spacing); border: 1px solid #ddd; border-radius: var(--radius); }
|
| 150 |
+
#input-area { display: flex; margin-top: var(--spacing); }
|
| 151 |
+
#user-input { flex: 1; padding: 0.6rem; border: 1px solid #ccc; border-radius: var(--radius) 0 0 var(--radius); }
|
| 152 |
+
#send-button { padding: 0 1rem; background: var(--primary); color: white; border: none; border-radius: 0 var(--radius) var(--radius) 0; cursor: pointer; }
|
| 153 |
+
.sidebar { background: var(--bg-light); padding: var(--spacing); border-left: 1px solid #eee; }
|
| 154 |
+
.sidebar h3 { margin-top: 0; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
"""
|
| 156 |
|
| 157 |
with gr.Blocks(title=APP_TITLE, css=custom_css, theme=gr.themes.Base()) as demo:
|
|
|
|
| 159 |
gr.Markdown(INTRO)
|
| 160 |
|
| 161 |
with gr.Row():
|
| 162 |
+
# Main chat column
|
| 163 |
with gr.Column(scale=3):
|
| 164 |
+
chatbot_ui = gr.Chatbot(type="tuples", elem_id="chat-window")
|
| 165 |
+
txt = gr.Textbox(placeholder="Digite sua pergunta…", lines=2, elem_id="user-input")
|
| 166 |
+
btn = gr.Button("Enviar", elem_id="send-button")
|
| 167 |
+
# Advanced settings sliders
|
| 168 |
+
temperature = gr.Slider(0, 1, value=0.6, label="Temperature")
|
| 169 |
+
top_p = gr.Slider(0, 1, value=0.95, label="Top-p")
|
| 170 |
+
max_tokens = gr.Slider(64, 2048, value=512, step=64, label="Max Tokens")
|
| 171 |
+
|
| 172 |
+
# Bind events
|
| 173 |
+
btn.click(chatbot, [txt, temperature, top_p, max_tokens], [chatbot_ui, txt])
|
| 174 |
+
txt.submit(chatbot, [txt, temperature, top_p, max_tokens], [chatbot_ui, txt])
|
| 175 |
gr.Button("Limpar").click(clear_history, [], [chatbot_ui, txt])
|
| 176 |
|
| 177 |
+
# Sidebar with PDF & suggestions
|
| 178 |
with gr.Column(scale=1, elem_classes="sidebar"):
|
| 179 |
if Path(PDF_PATH).exists():
|
| 180 |
gr.Markdown(f"[📄 Baixar CV em PDF](/file={PDF_PATH})")
|