Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,45 +33,47 @@ def fix_common_ocr_errors(text):
|
|
| 33 |
return text
|
| 34 |
|
| 35 |
def format_text_to_markdown(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
lines = text.splitlines()
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def flush_buffer():
|
| 41 |
-
if buffer:
|
| 42 |
-
final_lines.append(" ".join(buffer).strip())
|
| 43 |
-
final_lines.append("") # salto de línea para nuevo párrafo
|
| 44 |
-
buffer.clear()
|
| 45 |
-
|
| 46 |
for line in lines:
|
| 47 |
line = line.strip()
|
| 48 |
if not line:
|
| 49 |
-
flush_buffer()
|
| 50 |
continue
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
elif re.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
flush_buffer()
|
| 63 |
-
final_lines.append("### 📝 Descripción del problema")
|
| 64 |
elif re.match(r"^\d+\.", line):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
elif re.match(r"
|
| 68 |
-
|
| 69 |
-
|
| 70 |
else:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def translate_text(text):
|
| 77 |
if len(text.strip()) < 5:
|
|
|
|
| 33 |
return text
|
| 34 |
|
| 35 |
def format_text_to_markdown(text):
|
| 36 |
+
# Corrige errores comunes
|
| 37 |
+
text = fix_common_ocr_errors(text)
|
| 38 |
+
|
| 39 |
+
# Línea por línea
|
| 40 |
lines = text.splitlines()
|
| 41 |
+
markdown_lines = []
|
| 42 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
for line in lines:
|
| 44 |
line = line.strip()
|
| 45 |
if not line:
|
|
|
|
| 46 |
continue
|
| 47 |
+
|
| 48 |
+
# Encabezados conocidos
|
| 49 |
+
if re.search(r"\b(posible causa)\b", line, re.IGNORECASE):
|
| 50 |
+
markdown_lines.append("### 🛑 Posible causa\n")
|
| 51 |
+
continue
|
| 52 |
+
elif re.search(r"\b(posible solución)\b", line, re.IGNORECASE):
|
| 53 |
+
markdown_lines.append("### ✅ Posible solución\n")
|
| 54 |
+
continue
|
| 55 |
+
elif re.search(r"\b(descripción del problema)\b", line, re.IGNORECASE):
|
| 56 |
+
markdown_lines.append("### 📝 Descripción del problema\n")
|
| 57 |
+
continue
|
|
|
|
|
|
|
| 58 |
elif re.match(r"^\d+\.", line):
|
| 59 |
+
markdown_lines.append(f"- {line}")
|
| 60 |
+
continue
|
| 61 |
+
elif re.match(r"^[•*-]", line):
|
| 62 |
+
markdown_lines.append(f"- {line[1:].strip()}")
|
| 63 |
+
continue
|
| 64 |
else:
|
| 65 |
+
# Detecta si la línea es basura (letras sin sentido, símbolos)
|
| 66 |
+
if len(line) < 5:
|
| 67 |
+
continue
|
| 68 |
+
if re.search(r"[^\w\s.,:;¡!¿?\-()]", line):
|
| 69 |
+
symbols = re.findall(r"[^\w\s.,:;¡!¿?\-()]", line)
|
| 70 |
+
if len(symbols) > 3:
|
| 71 |
+
continue # basura
|
| 72 |
+
markdown_lines.append(line)
|
| 73 |
+
|
| 74 |
+
# Une líneas y separa párrafos correctamente
|
| 75 |
+
formatted_text = "\n\n".join(markdown_lines)
|
| 76 |
+
return formatted_text
|
| 77 |
|
| 78 |
def translate_text(text):
|
| 79 |
if len(text.strip()) < 5:
|