Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -49,15 +49,13 @@ def generar_embedding(texto):
|
|
49 |
|
50 |
# LLM para generar respuesta final
|
51 |
llm = pipeline(
|
52 |
-
"text-generation"
|
53 |
-
,
|
54 |
model="meta-llama/Llama-3.1-8B-Instruct",
|
55 |
token=hf_token,
|
56 |
trust_remote_code=True
|
57 |
)
|
58 |
|
59 |
@GPU # DECORADOR para que ZeroGPU ejecute esta función en CPU/GPU remota
|
60 |
-
|
61 |
def responder(pregunta):
|
62 |
if not pregunta:
|
63 |
return "Por favor ingresa una pregunta."
|
@@ -68,20 +66,23 @@ def responder(pregunta):
|
|
68 |
|
69 |
palabras_clave = pregunta.lower().split()
|
70 |
palabras_expandidas = expandir_con_sinonimos(palabras_clave, diccionario_sinonimos)
|
71 |
-
|
72 |
filtrados = [c for c in result_chunks if any(p in c.lower() for p in palabras_expandidas)]
|
73 |
contexto_final = "\n\n".join(filtrados[:15]) if filtrados else "\n\n".join(result_chunks[:15])
|
74 |
|
75 |
prompt = f"""
|
76 |
-
Eres un abogado colombiano especializado en
|
77 |
-
Tu
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
-
|
82 |
-
- Si
|
83 |
-
-
|
84 |
-
-
|
|
|
|
|
|
|
85 |
|
86 |
CONTEXTO LEGAL:
|
87 |
{contexto_final}
|
@@ -92,13 +93,12 @@ PREGUNTA:
|
|
92 |
RESPUESTA:
|
93 |
"""
|
94 |
|
95 |
-
|
96 |
resultado = llm(
|
97 |
prompt,
|
98 |
max_new_tokens=500,
|
99 |
temperature=0.4,
|
100 |
top_p=0.9,
|
101 |
-
repetition_penalty=1.
|
102 |
)[0]["generated_text"]
|
103 |
|
104 |
if "RESPUESTA:" in resultado:
|
|
|
49 |
|
50 |
# LLM para generar respuesta final
|
51 |
llm = pipeline(
|
52 |
+
"text-generation",
|
|
|
53 |
model="meta-llama/Llama-3.1-8B-Instruct",
|
54 |
token=hf_token,
|
55 |
trust_remote_code=True
|
56 |
)
|
57 |
|
58 |
@GPU # DECORADOR para que ZeroGPU ejecute esta función en CPU/GPU remota
|
|
|
59 |
def responder(pregunta):
|
60 |
if not pregunta:
|
61 |
return "Por favor ingresa una pregunta."
|
|
|
66 |
|
67 |
palabras_clave = pregunta.lower().split()
|
68 |
palabras_expandidas = expandir_con_sinonimos(palabras_clave, diccionario_sinonimos)
|
69 |
+
|
70 |
filtrados = [c for c in result_chunks if any(p in c.lower() for p in palabras_expandidas)]
|
71 |
contexto_final = "\n\n".join(filtrados[:15]) if filtrados else "\n\n".join(result_chunks[:15])
|
72 |
|
73 |
prompt = f"""
|
74 |
+
Eres un abogado colombiano especializado en derecho de tránsito, policía y penal.
|
75 |
+
Tu única fuente de información es el siguiente CONTEXTO LEGAL.
|
76 |
+
No tienes permitido inventar artículos, leyes o consecuencias que no estén citadas o explícitas en el contexto.
|
77 |
+
|
78 |
+
INSTRUCCIONES:
|
79 |
+
- Fundamenta tu respuesta exclusivamente en el CONTEXTO LEGAL.
|
80 |
+
- Si mencionas consecuencias, citas o artículos, deben estar explícitamente en el texto.
|
81 |
+
- No incluyas enlaces web, imágenes, disclaimers institucionales ni logos.
|
82 |
+
- No agregues conclusiones si no están fundamentadas.
|
83 |
+
- Usa un lenguaje claro, profesional, breve y entendible para una persona sin conocimientos jurídicos.
|
84 |
+
- Si no encuentras nada útil en el contexto, responde:
|
85 |
+
"No encontré información suficiente en los documentos para responder esta pregunta."
|
86 |
|
87 |
CONTEXTO LEGAL:
|
88 |
{contexto_final}
|
|
|
93 |
RESPUESTA:
|
94 |
"""
|
95 |
|
|
|
96 |
resultado = llm(
|
97 |
prompt,
|
98 |
max_new_tokens=500,
|
99 |
temperature=0.4,
|
100 |
top_p=0.9,
|
101 |
+
repetition_penalty=1.3
|
102 |
)[0]["generated_text"]
|
103 |
|
104 |
if "RESPUESTA:" in resultado:
|