Spaces:
Running
Running
NoeMartinezSanchez commited on
Commit ·
39758ea
1
Parent(s): 43365fd
mejoras chatgpt 2
Browse files- models/gemma_wrapper.py +32 -2
models/gemma_wrapper.py
CHANGED
|
@@ -221,6 +221,7 @@ class GemmaWrapper:
|
|
| 221 |
response = generated_text[len(prompt):].strip()
|
| 222 |
|
| 223 |
response = self._clean_response(response)
|
|
|
|
| 224 |
|
| 225 |
if len(response) < 10:
|
| 226 |
logger.warning(f"Response very short ({len(response)} chars)")
|
|
@@ -264,7 +265,7 @@ question: str,
|
|
| 264 |
return self.generate(
|
| 265 |
prompt=prompt,
|
| 266 |
max_new_tokens=400,
|
| 267 |
-
min_new_tokens=
|
| 268 |
temperature=0.2,
|
| 269 |
top_p=0.85,
|
| 270 |
repetition_penalty=1.15,
|
|
@@ -280,10 +281,13 @@ INSTRUCCIONES IMPORTANTES:
|
|
| 280 |
- NO inventes información
|
| 281 |
- NO uses conocimiento externo
|
| 282 |
- Si la respuesta NO está en el contexto, responde EXACTAMENTE:
|
| 283 |
-
"No encontré información
|
| 284 |
- Resume y redacta con tus propias palabras (no copies literal)
|
| 285 |
- Sé claro, preciso y directo
|
| 286 |
- Responde en máximo 4 líneas
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
TAREA:
|
| 289 |
1. Identifica qué parte del contexto responde la pregunta
|
|
@@ -296,6 +300,10 @@ CONTEXTO:
|
|
| 296 |
PREGUNTA:
|
| 297 |
{question}
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
RESPUESTA:
|
| 300 |
"""
|
| 301 |
|
|
@@ -310,13 +318,35 @@ RESPUESTA:
|
|
| 310 |
return text
|
| 311 |
|
| 312 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
text = re.sub(r'\s+', ' ', text).strip()
|
| 314 |
|
|
|
|
| 315 |
if text and text[0].islower():
|
| 316 |
text = text[0].upper() + text[1:]
|
| 317 |
|
| 318 |
return text
|
| 319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
def _clear_cache(self) -> None:
|
| 321 |
"""Clear Python and PyTorch garbage and cache."""
|
| 322 |
gc.collect()
|
|
|
|
| 221 |
response = generated_text[len(prompt):].strip()
|
| 222 |
|
| 223 |
response = self._clean_response(response)
|
| 224 |
+
response = self.fix_common_errors(response)
|
| 225 |
|
| 226 |
if len(response) < 10:
|
| 227 |
logger.warning(f"Response very short ({len(response)} chars)")
|
|
|
|
| 265 |
return self.generate(
|
| 266 |
prompt=prompt,
|
| 267 |
max_new_tokens=400,
|
| 268 |
+
min_new_tokens=20,
|
| 269 |
temperature=0.2,
|
| 270 |
top_p=0.85,
|
| 271 |
repetition_penalty=1.15,
|
|
|
|
| 281 |
- NO inventes información
|
| 282 |
- NO uses conocimiento externo
|
| 283 |
- Si la respuesta NO está en el contexto, responde EXACTAMENTE:
|
| 284 |
+
"No encontré información sobre eso en los documentos disponibles. ¿Quieres que intente con otra pregunta?"
|
| 285 |
- Resume y redacta con tus propias palabras (no copies literal)
|
| 286 |
- Sé claro, preciso y directo
|
| 287 |
- Responde en máximo 4 líneas
|
| 288 |
+
- Usa español correcto y claro
|
| 289 |
+
- No inventes palabras ni deformes términos
|
| 290 |
+
- Escribe oraciones completas y bien redactadas
|
| 291 |
|
| 292 |
TAREA:
|
| 293 |
1. Identifica qué parte del contexto responde la pregunta
|
|
|
|
| 300 |
PREGUNTA:
|
| 301 |
{question}
|
| 302 |
|
| 303 |
+
FORMATO DE RESPUESTA:
|
| 304 |
+
- Si es una lista, usa viñetas con "-"
|
| 305 |
+
- Si es explicación, usa máximo 3 a 4 oraciones
|
| 306 |
+
|
| 307 |
RESPUESTA:
|
| 308 |
"""
|
| 309 |
|
|
|
|
| 318 |
return text
|
| 319 |
|
| 320 |
import re
|
| 321 |
+
|
| 322 |
+
# eliminar basura al inicio (ej: "Tud", "U", símbolos raros)
|
| 323 |
+
text = re.sub(r'^[^a-zA-ZáéíóúÁÉÍÓÚ¿¡]+', '', text)
|
| 324 |
+
|
| 325 |
+
# normalizar espacios
|
| 326 |
text = re.sub(r'\s+', ' ', text).strip()
|
| 327 |
|
| 328 |
+
# capitalizar primera letra
|
| 329 |
if text and text[0].islower():
|
| 330 |
text = text[0].upper() + text[1:]
|
| 331 |
|
| 332 |
return text
|
| 333 |
|
| 334 |
+
def fix_common_errors(self, text: str) -> str:
|
| 335 |
+
replacements = {
|
| 336 |
+
"constatancia": "constancia",
|
| 337 |
+
"constatancoa": "constancia",
|
| 338 |
+
"secondary": "secundaria",
|
| 339 |
+
"otografía": "fotografía",
|
| 340 |
+
"credenciación": "credencial",
|
| 341 |
+
"cartascompromiso": "carta compromiso",
|
| 342 |
+
"carta compromiso ": "carta compromiso ",
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
for wrong, correct in replacements.items():
|
| 346 |
+
text = text.replace(wrong, correct)
|
| 347 |
+
|
| 348 |
+
return text
|
| 349 |
+
|
| 350 |
def _clear_cache(self) -> None:
|
| 351 |
"""Clear Python and PyTorch garbage and cache."""
|
| 352 |
gc.collect()
|