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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import csv
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
# 🚨 IMPORTANTE: Esta es la URL de publicación en formato CSV.
|
| 8 |
+
# Reemplázala si has publicado la hoja en un lugar diferente.
|
| 9 |
+
SHEETS_CSV_URL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSEUDzCWFHUWvHM4d8aTpPv7MJ_yHZV-mr7V4yL5kAs5ngwqzyNqXTlOej-W-6agz8CfbES_lbeowqT/pub?gid=0&single=true&output=csv"
|
| 10 |
+
|
| 11 |
+
def limpiar_cedula(cedula):
|
| 12 |
+
"""Limpia la cédula: quita comas, puntos, espacios, y la convierte a cadena."""
|
| 13 |
+
if not cedula:
|
| 14 |
+
return ""
|
| 15 |
+
# Aseguramos que es una cadena para poder usar filter
|
| 16 |
+
cedula_str = str(cedula)
|
| 17 |
+
# Filtra solo los dígitos
|
| 18 |
+
cedula_limpia = ''.join(filter(str.isdigit, cedula_str))
|
| 19 |
+
return cedula_limpia
|
| 20 |
+
|
| 21 |
+
def buscar_cliente(cedula):
|
| 22 |
+
"""Busca al cliente en la hoja de Google Sheets usando la cédula."""
|
| 23 |
+
if not cedula or not cedula.strip():
|
| 24 |
+
return "<p style='text-align:center; font-size:1.2em;'>**💫 Vas a llegar a la comisión de tus sueños. Piensa en tus metas.**</p>"
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
# 1. Descargar el contenido de la hoja de cálculo
|
| 28 |
+
response = requests.get(SHEETS_CSV_URL, timeout=15)
|
| 29 |
+
|
| 30 |
+
# 2. Manejo de errores de conexión/servidor
|
| 31 |
+
if response.status_code != 200:
|
| 32 |
+
return f"""
|
| 33 |
+
<div style='
|
| 34 |
+
background: #f8d7da;
|
| 35 |
+
border-left: 4px solid #dc3545;
|
| 36 |
+
padding: 20px;
|
| 37 |
+
border-radius: 5px;
|
| 38 |
+
text-align: center;
|
| 39 |
+
'>
|
| 40 |
+
<h3>⚠️ ERROR DE CONEXIÓN</h3>
|
| 41 |
+
<p>No se pudo descargar la hoja. Código de estado: **{response.status_code}**.</p>
|
| 42 |
+
<p>Verifique que la URL de publicación sea correcta y que Sheets esté disponible.</p>
|
| 43 |
+
</div>
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
# 3. Preparar el lector CSV
|
| 47 |
+
content = response.content.decode('utf-8')
|
| 48 |
+
csv_reader = csv.DictReader(io.StringIO(content))
|
| 49 |
+
|
| 50 |
+
cedula_limpia = limpiar_cedula(cedula)
|
| 51 |
+
|
| 52 |
+
# 4. Buscar la cédula en los datos (compara con cualquier columna)
|
| 53 |
+
for fila in csv_reader:
|
| 54 |
+
for valor in fila.values():
|
| 55 |
+
if valor and cedula_limpia == limpiar_cedula(valor):
|
| 56 |
+
# 5. Cliente encontrado: Retorna el HTML con los datos
|
| 57 |
+
return f"""
|
| 58 |
+
<div style='
|
| 59 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 60 |
+
color: white;
|
| 61 |
+
padding: 15px;
|
| 62 |
+
border-radius: 10px;
|
| 63 |
+
margin-bottom: 15px;
|
| 64 |
+
'>
|
| 65 |
+
<h3>✅ CLIENTE ENCONTRADO</h3>
|
| 66 |
+
</div>
|
| 67 |
+
<div style='
|
| 68 |
+
background: #f8f9fa;
|
| 69 |
+
border-left: 4px solid #667eea;
|
| 70 |
+
padding: 15px;
|
| 71 |
+
border-radius: 5px;
|
| 72 |
+
font-family: Arial, sans-serif;
|
| 73 |
+
'>
|
| 74 |
+
<table style="width: 100%; border-collapse: collapse;">
|
| 75 |
+
<tr><td style="padding: 8px; border-bottom: 1px solid #eee; width: 30%;"><strong>👤 Nombre:</strong></td><td style="padding: 8px; border-bottom: 1px solid #eee;">{fila.get('Nombre', 'N/A')}</td></tr>
|
| 76 |
+
<tr><td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>📋 Cédula:</strong></td><td style="padding: 8px; border-bottom: 1px solid #eee;">{fila.get('Cédula', 'N/A')}</td></tr>
|
| 77 |
+
<tr><td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>📊 Etapa actual:</strong></td><td style="padding: 8px; border-bottom: 1px solid #eee;">{fila.get('Etapa actual', 'N/A')}</td></tr>
|
| 78 |
+
<tr><td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>🏦 Pagaduría:</strong></td><td style="padding: 8px; border-bottom: 1px solid #eee;">{fila.get('Pagaduría', 'N/A')}</td></tr>
|
| 79 |
+
<tr><td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>💰 Financiera:</strong></td><td style="padding: 8px; border-bottom: 1px solid #eee;">{fila.get('Financiera', 'N/A')}</td></tr>
|
| 80 |
+
<tr><td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>💵 Valor:</strong></td><td style="padding: 8px; border-bottom: 1px solid #eee;">{fila.get('Valor', 'N/A')}</td></tr>
|
| 81 |
+
<tr><td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>👨💼 Analista:</strong></td><td style="padding: 8px; border-bottom: 1px solid #eee;">{fila.get('Analista', 'N/A')}</td></tr>
|
| 82 |
+
<tr><td style="padding: 8px; border-bottom: 1px solid #eee;"><strong>💬 Observaciones:</strong></td><td style="padding: 8px; border-bottom: 1px solid #eee;">{fila.get('Observaciones', 'N/A')}</td></tr>
|
| 83 |
+
<tr><td style="padding: 8px;"><strong>🕐 Actualizado:</strong></td><td style="padding: 8px;">{fila.get('FechaActualización', 'N/A')}</td></tr>
|
| 84 |
+
</table>
|
| 85 |
+
</div>
|
| 86 |
+
<div style='
|
| 87 |
+
background: #e8f5e8;
|
| 88 |
+
padding: 10px;
|
| 89 |
+
border-radius: 5px;
|
| 90 |
+
margin-top: 10px;
|
| 91 |
+
font-size: 12px;
|
| 92 |
+
color: #2d5016;
|
| 93 |
+
'>
|
| 94 |
+
🕐 Consulta: {datetime.now().strftime("%Y-%m-%d %H:%M")}
|
| 95 |
+
</div>
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
# 6. Cliente NO encontrado: Retorna el HTML de alerta
|
| 99 |
+
return f"""
|
| 100 |
+
<div style='
|
| 101 |
+
background: #fff3cd;
|
| 102 |
+
border-left: 4px solid #ffc107;
|
| 103 |
+
padding: 20px;
|
| 104 |
+
border-radius: 5px;
|
| 105 |
+
text-align: center;
|
| 106 |
+
'>
|
| 107 |
+
<h3>❌ CLIENTE NO ENCONTRADO</h3>
|
| 108 |
+
<p>La cédula **{cedula}** no aparece en los registros.</p>
|
| 109 |
+
<p>Verifique el número o contacte al administrador.</p>
|
| 110 |
+
</div>
|
| 111 |
+
"""
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
# 7. Error general (ej: error de red, problema de parsing)
|
| 115 |
+
error_msg = str(e)[:100] # Limitar el mensaje de error por seguridad
|
| 116 |
+
return f"""
|
| 117 |
+
<div style='
|
| 118 |
+
background: #f8d7da;
|
| 119 |
+
border-left: 4px solid #dc3545;
|
| 120 |
+
padding: 20px;
|
| 121 |
+
border-radius: 5px;
|
| 122 |
+
text-align: center;
|
| 123 |
+
'>
|
| 124 |
+
<h3>⚠️ ERROR INESPERADO</h3>
|
| 125 |
+
<p>Ocurrió un problema técnico. Por favor, inténtelo de nuevo.</p>
|
| 126 |
+
<p><small>Detalle: {error_msg}...</small></p>
|
| 127 |
+
</div>
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
# Interfaz Gradio
|
| 131 |
+
with gr.Blocks(
|
| 132 |
+
theme="soft",
|
| 133 |
+
css="""
|
| 134 |
+
.gradio-container { max-width: 800px; margin: 0 auto; }
|
| 135 |
+
.header {
|
| 136 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 137 |
+
color: white;
|
| 138 |
+
padding: 25px;
|
| 139 |
+
border-radius: 10px;
|
| 140 |
+
text-align: center;
|
| 141 |
+
margin-bottom: 20px;
|
| 142 |
+
}
|
| 143 |
+
"""
|
| 144 |
+
) as demo:
|
| 145 |
+
|
| 146 |
+
gr.Markdown("""
|
| 147 |
+
<div class="header">
|
| 148 |
+
<h1>🔍 CONSULTA DE ESTADO</h1>
|
| 149 |
+
<p><strong>Vas a llegar a la comisión de tus sueños. Piensa en tus metas. 💫</strong></p>
|
| 150 |
+
</div>
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
with gr.Row():
|
| 154 |
+
cedula_input = gr.Textbox(
|
| 155 |
+
label="**INGRESE LA CÉDULA DEL CLIENTE**",
|
| 156 |
+
placeholder="Ej: 1000000001, 444343, 8568...",
|
| 157 |
+
max_lines=1,
|
| 158 |
+
elem_id="cedula_input"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
buscar_btn = gr.Button("🔍 CONSULTAR ESTADO", variant="primary", size="lg")
|
| 162 |
+
|
| 163 |
+
gr.Markdown("---")
|
| 164 |
+
|
| 165 |
+
resultado = gr.HTML(
|
| 166 |
+
value="""
|
| 167 |
+
<div style='
|
| 168 |
+
background: #f8f9fa;
|
| 169 |
+
padding: 20px;
|
| 170 |
+
border-radius: 5px;
|
| 171 |
+
text-align: center;
|
| 172 |
+
border: 2px dashed #ddd;
|
| 173 |
+
'>
|
| 174 |
+
<h3>👋 BIENVENIDO/A</h3>
|
| 175 |
+
<p>Ingrese una cédula para consultar el estado del crédito.</p>
|
| 176 |
+
</div>
|
| 177 |
+
"""
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Conecta la función tanto al botón como al presionar Enter
|
| 181 |
+
buscar_btn.click(buscar_cliente, inputs=cedula_input, outputs=resultado)
|
| 182 |
+
cedula_input.submit(buscar_cliente, inputs=cedula_input, outputs=resultado)
|
| 183 |
+
|
| 184 |
+
if __name__ == "__main__":
|
| 185 |
+
demo.launch()
|