Update app.py
Browse files
app.py
CHANGED
|
@@ -1,345 +1,391 @@
|
|
| 1 |
-
# ---------- app.py ----------
|
| 2 |
-
# Dependências:
|
| 3 |
-
# pip install gradio faiss-cpu sentence-transformers openai
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
import pickle
|
| 8 |
-
|
| 9 |
-
import gradio as gr
|
| 10 |
-
import faiss
|
| 11 |
-
from sentence_transformers import SentenceTransformer
|
| 12 |
-
from openai import OpenAI
|
| 13 |
-
|
| 14 |
-
# ========= NVIDIA API =========
|
| 15 |
-
# Em local: defina NV_API_KEY ou NVIDIA_API_KEY no ambiente.
|
| 16 |
-
# Em Hugging Face Spaces: crie um "Repository secret" chamado NVIDIA_API_KEY.
|
| 17 |
-
NV_API_KEY = os.environ.get("NVIDIA_API_KEY") or os.environ.get("NV_API_KEY")
|
| 18 |
-
if not NV_API_KEY:
|
| 19 |
-
raise RuntimeError(
|
| 20 |
-
"A chave da NVIDIA não foi encontrada.\n"
|
| 21 |
-
"Defina um secret chamado 'NVIDIA_API_KEY' (ou NV_API_KEY) com a tua chave da NVIDIA.\n"
|
| 22 |
-
"• Localmente: export NVIDIA_API_KEY='SUA_CHAVE'\n"
|
| 23 |
-
"• Hugging Face Spaces: Settings -> Repository secrets -> Add secret."
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
client = OpenAI(
|
| 27 |
-
base_url="https://integrate.api.nvidia.com/v1",
|
| 28 |
-
api_key=NV_API_KEY,
|
| 29 |
-
)
|
| 30 |
-
CHAT_MODEL = "meta/llama3-8b-instruct"
|
| 31 |
-
|
| 32 |
-
# ========= Configuração do App =========
|
| 33 |
-
APP_TITLE = "EcoLexIA – Assistente Inteligente de Leis Ambientais de Portugal"
|
| 34 |
-
|
| 35 |
-
INTRO = (
|
| 36 |
-
"👋 Bem-vindo ao **EcoLexIA**, o teu assistente jurídico especializado em **direito do ambiente em Portugal**.\n\n"
|
| 37 |
-
"Este sistema utiliza **RAG (Retrieval-Augmented Generation)** para consultar automaticamente os documentos legais "
|
| 38 |
-
"carregados (leis, decretos, regulamentos, pareceres, etc.) e responder às tuas perguntas com base nesses textos."
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
SUGGESTION_QUESTIONS = [
|
| 42 |
-
"Resuma os principais princípios da Lei de Bases do Ambiente.",
|
| 43 |
-
"Quais são as obrigações do Estado em matéria de proteção ambiental?",
|
| 44 |
-
"Explique como funciona a Avaliação de Impacte Ambiental em Portugal.",
|
| 45 |
-
"Que legislação regula a gestão de resíduos urbanos?",
|
| 46 |
-
"Existe enquadramento legal para participação pública em decisões ambientais?",
|
| 47 |
-
"Quais são as regras sobre emissões poluentes na indústria?",
|
| 48 |
-
]
|
| 49 |
-
|
| 50 |
-
SUGGESTIONS_THEMES = {
|
| 51 |
-
"Lei de Bases do Ambiente": [
|
| 52 |
-
"Quais são os princípios fundamentais da Lei de Bases do Ambiente?",
|
| 53 |
-
"Como a Lei de Bases do Ambiente enquadra o desenvolvimento sustentável?",
|
| 54 |
-
],
|
| 55 |
-
"Avaliação de Impacte Ambiental (AIA)": [
|
| 56 |
-
"Explique o que é Avaliação de Impacte Ambiental e quando é obrigatória.",
|
| 57 |
-
"Que entidades estão envolvidas no processo de AIA?",
|
| 58 |
-
],
|
| 59 |
-
"Resíduos & Poluição": [
|
| 60 |
-
"Que legislação trata da gestão de resíduos em Portugal?",
|
| 61 |
-
"Que obrigações têm as empresas relativamente ao controlo de emissões poluentes?",
|
| 62 |
-
],
|
| 63 |
-
"Ordenamento do Território & Conservação": [
|
| 64 |
-
"Como o ordenamento do território se articula com a proteção ambiental?",
|
| 65 |
-
"Que diplomas legais regulam áreas protegidas e conservação da natureza?",
|
| 66 |
-
],
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
# ========= Caminhos do índice =========
|
| 70 |
-
INDEX_FILE = "faiss_index.faiss"
|
| 71 |
-
EMBEDDINGS_FILE = "embeddings.pkl"
|
| 72 |
-
|
| 73 |
-
if not Path(INDEX_FILE).exists() or not Path(EMBEDDINGS_FILE).exists():
|
| 74 |
-
raise FileNotFoundError(
|
| 75 |
-
"❌ Índice não encontrado.\n"
|
| 76 |
-
"Certifique-se de que 'faiss_index.faiss' e 'embeddings.pkl' "
|
| 77 |
-
"foram gerados pelo build_index.py na mesma pasta deste app."
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
index = faiss.read_index(INDEX_FILE)
|
| 81 |
-
with open(EMBEDDINGS_FILE, "rb") as f:
|
| 82 |
-
emb_data = pickle.load(f)
|
| 83 |
-
|
| 84 |
-
texts = emb_data
|
| 85 |
-
metadatas = emb_data
|
| 86 |
-
|
| 87 |
-
# Mesmo modelo de embeddings usado no build_index.py
|
| 88 |
-
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 89 |
-
|
| 90 |
-
# Histórico do chat: lista de (user, assistant)
|
| 91 |
-
dialog_history = []
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
# ========= Recuperação de contexto =========
|
| 95 |
-
def retrieve_context(query: str, k: int = 4) -> str:
|
| 96 |
-
"""Busca k trechos mais relevantes no índice FAISS para a pergunta."""
|
| 97 |
-
if not query or not query.strip():
|
| 98 |
-
return ""
|
| 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 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
.
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
.
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
gr.
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ---------- app.py ----------
|
| 2 |
+
# Dependências:
|
| 3 |
+
# pip install gradio faiss-cpu sentence-transformers openai
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import faiss
|
| 11 |
+
from sentence_transformers import SentenceTransformer
|
| 12 |
+
from openai import OpenAI
|
| 13 |
+
|
| 14 |
+
# ========= NVIDIA API =========
|
| 15 |
+
# Em local: defina NV_API_KEY ou NVIDIA_API_KEY no ambiente.
|
| 16 |
+
# Em Hugging Face Spaces: crie um "Repository secret" chamado NVIDIA_API_KEY.
|
| 17 |
+
NV_API_KEY = os.environ.get("NVIDIA_API_KEY") or os.environ.get("NV_API_KEY")
|
| 18 |
+
if not NV_API_KEY:
|
| 19 |
+
raise RuntimeError(
|
| 20 |
+
"A chave da NVIDIA não foi encontrada.\n"
|
| 21 |
+
"Defina um secret chamado 'NVIDIA_API_KEY' (ou NV_API_KEY) com a tua chave da NVIDIA.\n"
|
| 22 |
+
"• Localmente: export NVIDIA_API_KEY='SUA_CHAVE'\n"
|
| 23 |
+
"• Hugging Face Spaces: Settings -> Repository secrets -> Add secret."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
client = OpenAI(
|
| 27 |
+
base_url="https://integrate.api.nvidia.com/v1",
|
| 28 |
+
api_key=NV_API_KEY,
|
| 29 |
+
)
|
| 30 |
+
CHAT_MODEL = "meta/llama3-8b-instruct"
|
| 31 |
+
|
| 32 |
+
# ========= Configuração do App =========
|
| 33 |
+
APP_TITLE = "EcoLexIA – Assistente Inteligente de Leis Ambientais de Portugal"
|
| 34 |
+
|
| 35 |
+
INTRO = (
|
| 36 |
+
"👋 Bem-vindo ao **EcoLexIA**, o teu assistente jurídico especializado em **direito do ambiente em Portugal**.\n\n"
|
| 37 |
+
"Este sistema utiliza **RAG (Retrieval-Augmented Generation)** para consultar automaticamente os documentos legais "
|
| 38 |
+
"carregados (leis, decretos, regulamentos, pareceres, etc.) e responder às tuas perguntas com base nesses textos."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
SUGGESTION_QUESTIONS = [
|
| 42 |
+
"Resuma os principais princípios da Lei de Bases do Ambiente.",
|
| 43 |
+
"Quais são as obrigações do Estado em matéria de proteção ambiental?",
|
| 44 |
+
"Explique como funciona a Avaliação de Impacte Ambiental em Portugal.",
|
| 45 |
+
"Que legislação regula a gestão de resíduos urbanos?",
|
| 46 |
+
"Existe enquadramento legal para participação pública em decisões ambientais?",
|
| 47 |
+
"Quais são as regras sobre emissões poluentes na indústria?",
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
SUGGESTIONS_THEMES = {
|
| 51 |
+
"Lei de Bases do Ambiente": [
|
| 52 |
+
"Quais são os princípios fundamentais da Lei de Bases do Ambiente?",
|
| 53 |
+
"Como a Lei de Bases do Ambiente enquadra o desenvolvimento sustentável?",
|
| 54 |
+
],
|
| 55 |
+
"Avaliação de Impacte Ambiental (AIA)": [
|
| 56 |
+
"Explique o que é Avaliação de Impacte Ambiental e quando é obrigatória.",
|
| 57 |
+
"Que entidades estão envolvidas no processo de AIA?",
|
| 58 |
+
],
|
| 59 |
+
"Resíduos & Poluição": [
|
| 60 |
+
"Que legislação trata da gestão de resíduos em Portugal?",
|
| 61 |
+
"Que obrigações têm as empresas relativamente ao controlo de emissões poluentes?",
|
| 62 |
+
],
|
| 63 |
+
"Ordenamento do Território & Conservação": [
|
| 64 |
+
"Como o ordenamento do território se articula com a proteção ambiental?",
|
| 65 |
+
"Que diplomas legais regulam áreas protegidas e conservação da natureza?",
|
| 66 |
+
],
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
# ========= Caminhos do índice =========
|
| 70 |
+
INDEX_FILE = "faiss_index.faiss"
|
| 71 |
+
EMBEDDINGS_FILE = "embeddings.pkl"
|
| 72 |
+
|
| 73 |
+
if not Path(INDEX_FILE).exists() or not Path(EMBEDDINGS_FILE).exists():
|
| 74 |
+
raise FileNotFoundError(
|
| 75 |
+
"❌ Índice não encontrado.\n"
|
| 76 |
+
"Certifique-se de que 'faiss_index.faiss' e 'embeddings.pkl' "
|
| 77 |
+
"foram gerados pelo build_index.py na mesma pasta deste app."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
index = faiss.read_index(INDEX_FILE)
|
| 81 |
+
with open(EMBEDDINGS_FILE, "rb") as f:
|
| 82 |
+
emb_data = pickle.load(f)
|
| 83 |
+
|
| 84 |
+
texts = emb_data.get("texts", [])
|
| 85 |
+
metadatas = emb_data.get("metadatas", [])
|
| 86 |
+
|
| 87 |
+
# Mesmo modelo de embeddings usado no build_index.py
|
| 88 |
+
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 89 |
+
|
| 90 |
+
# Histórico do chat: lista de (user, assistant)
|
| 91 |
+
dialog_history = []
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# ========= Recuperação de contexto =========
|
| 95 |
+
def retrieve_context(query: str, k: int = 4) -> str:
|
| 96 |
+
"""Busca k trechos mais relevantes no índice FAISS para a pergunta."""
|
| 97 |
+
if not query or not query.strip():
|
| 98 |
+
return ""
|
| 99 |
+
|
| 100 |
+
# Proteções básicas
|
| 101 |
+
if index.ntotal == 0 or not texts:
|
| 102 |
+
return ""
|
| 103 |
+
|
| 104 |
+
q_emb = embedding_model.encode([query], convert_to_numpy=True)
|
| 105 |
+
_, indices = index.search(q_emb, k)
|
| 106 |
+
|
| 107 |
+
parts = []
|
| 108 |
+
for idx in indices[0]:
|
| 109 |
+
if idx < 0 or idx >= len(texts):
|
| 110 |
+
continue
|
| 111 |
+
chunk = texts[idx]
|
| 112 |
+
meta = metadatas[idx] if idx < len(metadatas) else {}
|
| 113 |
+
src = meta.get("source", "documento desconhecido")
|
| 114 |
+
parts.append(f"[Documento: {src}]\n{chunk}")
|
| 115 |
+
|
| 116 |
+
return "\n\n---\n\n".join(parts)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# ========= Streaming da NVIDIA (ROBUSTO) =========
|
| 120 |
+
def nv_stream(messages, temperature: float, top_p: float, max_tokens: int):
|
| 121 |
+
"""
|
| 122 |
+
Stream da resposta da NVIDIA (LLaMA 3) com proteções:
|
| 123 |
+
- chunks podem vir sem `choices` (ou choices vazia)
|
| 124 |
+
- `delta` pode não existir
|
| 125 |
+
- `delta.content` pode ser None
|
| 126 |
+
"""
|
| 127 |
+
reply = ""
|
| 128 |
+
|
| 129 |
+
stream = client.chat.completions.create(
|
| 130 |
+
model=CHAT_MODEL,
|
| 131 |
+
messages=messages,
|
| 132 |
+
temperature=temperature,
|
| 133 |
+
top_p=top_p,
|
| 134 |
+
max_tokens=max_tokens,
|
| 135 |
+
stream=True,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
for chunk in stream:
|
| 139 |
+
choices = getattr(chunk, "choices", None)
|
| 140 |
+
if not choices:
|
| 141 |
+
continue
|
| 142 |
+
|
| 143 |
+
choice0 = choices[0]
|
| 144 |
+
delta = getattr(choice0, "delta", None)
|
| 145 |
+
if not delta:
|
| 146 |
+
continue
|
| 147 |
+
|
| 148 |
+
content = getattr(delta, "content", None)
|
| 149 |
+
if content:
|
| 150 |
+
reply += content
|
| 151 |
+
yield reply
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
# ========= Lógica do chat =========
|
| 155 |
+
def chatbot(user_input: str, temperature: float, top_p: float, max_tokens: int):
|
| 156 |
+
global dialog_history
|
| 157 |
+
|
| 158 |
+
if not user_input or not user_input.strip():
|
| 159 |
+
return dialog_history, ""
|
| 160 |
+
|
| 161 |
+
context = retrieve_context(user_input, k=6)
|
| 162 |
+
|
| 163 |
+
# ✅ Se não houver contexto, não deixa o modelo inventar
|
| 164 |
+
if not context.strip():
|
| 165 |
+
reply_full = (
|
| 166 |
+
"Não encontrei base suficiente nos documentos carregados para responder com segurança.\n\n"
|
| 167 |
+
"Sugestões:\n"
|
| 168 |
+
"• Verifica se os PDFs/leis foram realmente indexados.\n"
|
| 169 |
+
"• Faz uma pergunta mais específica (ex.: diploma/ano/artigo).\n"
|
| 170 |
+
"• Consulta a legislação oficial (Diário da República Eletrónico).\n"
|
| 171 |
+
)
|
| 172 |
+
dialog_history.append((user_input, reply_full))
|
| 173 |
+
return dialog_history, ""
|
| 174 |
+
|
| 175 |
+
system_msg = {
|
| 176 |
+
"role": "system",
|
| 177 |
+
"content": (
|
| 178 |
+
"És um assistente jurídico especializado em direito do ambiente em Portugal. "
|
| 179 |
+
"Responde SEMPRE em português europeu, de forma clara e estruturada.\n\n"
|
| 180 |
+
"Regras:\n"
|
| 181 |
+
"1. Usa apenas o contexto abaixo para responder.\n"
|
| 182 |
+
"2. Se não houver informação suficiente, diz que não encontras base nos documentos e "
|
| 183 |
+
"sugere consultar a legislação oficial.\n"
|
| 184 |
+
"3. Indica o nome do documento (PDF) sempre que fizer sentido.\n\n"
|
| 185 |
+
f"=== CONTEXTO RECUPERADO ===\n{context}\n\n"
|
| 186 |
+
),
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
messages = [system_msg]
|
| 190 |
+
|
| 191 |
+
# Reconstroi histórico (limita para evitar prompt gigante)
|
| 192 |
+
MAX_TURNS = 8 # últimas 8 interações
|
| 193 |
+
for u, a in dialog_history[-MAX_TURNS:]:
|
| 194 |
+
messages.append({"role": "user", "content": u})
|
| 195 |
+
messages.append({"role": "assistant", "content": a})
|
| 196 |
+
|
| 197 |
+
messages.append({"role": "user", "content": user_input})
|
| 198 |
+
|
| 199 |
+
reply_full = ""
|
| 200 |
+
try:
|
| 201 |
+
for partial in nv_stream(messages, temperature, top_p, max_tokens):
|
| 202 |
+
reply_full = partial
|
| 203 |
+
|
| 204 |
+
# ✅ Se o stream não devolveu conteúdo, evita choices[0] e afins
|
| 205 |
+
if not reply_full.strip():
|
| 206 |
+
reply_full = (
|
| 207 |
+
"⚠️ A API devolveu uma resposta vazia (sem conteúdo). "
|
| 208 |
+
"Isto pode acontecer por limite de contexto, rate limit, ou erro de input.\n"
|
| 209 |
+
"Tenta reduzir o tamanho dos trechos recuperados (k) ou o histórico."
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
dialog_history.append((user_input, reply_full))
|
| 213 |
+
|
| 214 |
+
except Exception as e:
|
| 215 |
+
reply_full = f"⚠️ Erro na API NVIDIA: {type(e).__name__}: {e}"
|
| 216 |
+
dialog_history.append((user_input, reply_full))
|
| 217 |
+
|
| 218 |
+
return dialog_history, ""
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def clear_history():
|
| 222 |
+
global dialog_history
|
| 223 |
+
dialog_history = []
|
| 224 |
+
return [], ""
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
# ========= CSS simples / layout padrão =========
|
| 228 |
+
custom_css = r"""
|
| 229 |
+
body, .gradio-container {
|
| 230 |
+
background: #ffffff;
|
| 231 |
+
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
/* Header azul */
|
| 235 |
+
#header-box {
|
| 236 |
+
max-width: 1100px;
|
| 237 |
+
margin: 1.5rem auto 1rem auto;
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
.header-card {
|
| 241 |
+
background: linear-gradient(135deg, #0b3c91 0%, #1565c0 40%, #1e88e5 100%);
|
| 242 |
+
border-radius: 16px;
|
| 243 |
+
padding: 1.4rem 1.8rem;
|
| 244 |
+
color: #ffffff;
|
| 245 |
+
box-shadow: 0 14px 30px rgba(15, 23, 42, 0.18);
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
.header-title {
|
| 249 |
+
font-size: 1.6rem;
|
| 250 |
+
font-weight: 700;
|
| 251 |
+
margin: 0;
|
| 252 |
+
color: #ffffff !important;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
.header-subtitle {
|
| 256 |
+
margin: 0.35rem 0 0 0;
|
| 257 |
+
font-size: 0.96rem;
|
| 258 |
+
opacity: 0.95;
|
| 259 |
+
color: #ffffff !important;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
/* Cartões principais */
|
| 263 |
+
.card {
|
| 264 |
+
background: #ffffff;
|
| 265 |
+
border-radius: 16px;
|
| 266 |
+
border: 1px solid #e0e0e0;
|
| 267 |
+
box-shadow: 0 8px 18px rgba(15, 23, 42, 0.06);
|
| 268 |
+
padding: 1rem 1.1rem;
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
/* Chat */
|
| 272 |
+
#chat-window {
|
| 273 |
+
height: 60vh;
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
/* Botões */
|
| 277 |
+
.gr-button-primary {
|
| 278 |
+
background: #1565c0 !important;
|
| 279 |
+
color: #ffffff !important;
|
| 280 |
+
border: none !important;
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
.gr-button-secondary {
|
| 284 |
+
background: #f5f5f5 !important;
|
| 285 |
+
color: #333333 !important;
|
| 286 |
+
border: 1px solid #e0e0e0 !important;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
/* Sugestões */
|
| 290 |
+
.suggestion-btn {
|
| 291 |
+
width: 100%;
|
| 292 |
+
justify-content: flex-start;
|
| 293 |
+
font-size: 0.88rem;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
/* Rodapé */
|
| 297 |
+
.app-footer {
|
| 298 |
+
margin-top: 1rem;
|
| 299 |
+
font-size: 0.8rem;
|
| 300 |
+
text-align: center;
|
| 301 |
+
color: #555555;
|
| 302 |
+
}
|
| 303 |
+
"""
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
# ========= Layout Gradio =========
|
| 307 |
+
with gr.Blocks(title=APP_TITLE, css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 308 |
+
# Header
|
| 309 |
+
with gr.Group(elem_id="header-box"):
|
| 310 |
+
gr.HTML(
|
| 311 |
+
f"""
|
| 312 |
+
<div class="header-card">
|
| 313 |
+
<div class="header-title">{APP_TITLE}</div>
|
| 314 |
+
<div class="header-subtitle">
|
| 315 |
+
Consultor jurídico inteligente com RAG sobre legislação ambiental portuguesa.
|
| 316 |
+
</div>
|
| 317 |
+
</div>
|
| 318 |
+
"""
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
gr.Markdown(INTRO)
|
| 322 |
+
|
| 323 |
+
with gr.Row():
|
| 324 |
+
# Coluna principal (chat)
|
| 325 |
+
with gr.Column(scale=3):
|
| 326 |
+
with gr.Group(elem_classes="card"):
|
| 327 |
+
gr.Markdown("### 💬 Conversa Jurídica")
|
| 328 |
+
chatbot_ui = gr.Chatbot(
|
| 329 |
+
type="tuples",
|
| 330 |
+
elem_id="chat-window",
|
| 331 |
+
label="Chatbot",
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
txt = gr.Textbox(
|
| 335 |
+
placeholder="Escreve aqui a tua pergunta sobre leis do ambiente em Portugal…",
|
| 336 |
+
lines=3,
|
| 337 |
+
show_label=False,
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
with gr.Row():
|
| 341 |
+
btn_send = gr.Button("Enviar", variant="primary")
|
| 342 |
+
btn_clear = gr.Button("Limpar", variant="secondary")
|
| 343 |
+
|
| 344 |
+
with gr.Accordion("Parâmetros avançados", open=False):
|
| 345 |
+
temperature = gr.Slider(0, 1, value=0.5, label="Temperature")
|
| 346 |
+
top_p = gr.Slider(0, 1, value=0.9, label="Top-p")
|
| 347 |
+
max_tokens = gr.Slider(64, 2048, value=512, step=64, label="Max Tokens")
|
| 348 |
+
|
| 349 |
+
btn_send.click(
|
| 350 |
+
chatbot,
|
| 351 |
+
[txt, temperature, top_p, max_tokens],
|
| 352 |
+
[chatbot_ui, txt],
|
| 353 |
+
)
|
| 354 |
+
txt.submit(
|
| 355 |
+
chatbot,
|
| 356 |
+
[txt, temperature, top_p, max_tokens],
|
| 357 |
+
[chatbot_ui, txt],
|
| 358 |
+
)
|
| 359 |
+
btn_clear.click(
|
| 360 |
+
clear_history,
|
| 361 |
+
[],
|
| 362 |
+
[chatbot_ui, txt],
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
# Sidebar
|
| 366 |
+
with gr.Column(scale=2):
|
| 367 |
+
with gr.Group(elem_classes="card"):
|
| 368 |
+
gr.Markdown("### 💡 Sugestões rápidas")
|
| 369 |
+
for q in SUGGESTION_QUESTIONS:
|
| 370 |
+
gr.Button(q, elem_classes="suggestion-btn").click(
|
| 371 |
+
lambda s=q: s, outputs=[txt]
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
gr.Markdown("---")
|
| 375 |
+
gr.Markdown("### 📚 Explorar por tema")
|
| 376 |
+
|
| 377 |
+
for theme, qs in SUGGESTIONS_THEMES.items():
|
| 378 |
+
with gr.Accordion(theme, open=False):
|
| 379 |
+
for q in qs:
|
| 380 |
+
gr.Button(q, elem_classes="suggestion-btn").click(
|
| 381 |
+
lambda s=q: s, outputs=[txt]
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
gr.Markdown(
|
| 385 |
+
'<div class="app-footer">EcoLexIA · Sistema RAG para legislação ambiental em Portugal</div>'
|
| 386 |
+
)
|
| 387 |
+
|
| 388 |
+
# Para Hugging Face Spaces basta que a variável `demo` exista;
|
| 389 |
+
# ainda assim manter o launch permite rodar localmente.
|
| 390 |
+
if __name__ == "__main__":
|
| 391 |
+
demo.launch()
|