Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,3 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""Untitled19.ipynb
|
3 |
-
|
4 |
-
Automatically generated by Colab.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/12FC8IfauEBNv8yXDkOWez5aZ0F6sD5q1
|
8 |
-
"""
|
9 |
-
|
10 |
import gradio as gr
|
11 |
import csv
|
12 |
import re
|
@@ -14,139 +5,83 @@ from datetime import datetime
|
|
14 |
from hashlib import sha256
|
15 |
import os
|
16 |
from PIL import Image
|
17 |
-
import
|
18 |
-
from
|
19 |
|
20 |
# Configuración
|
21 |
CSV_FILE = "registros_incidencias.csv"
|
22 |
-
PASSWORD_HASH = "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"
|
|
|
23 |
|
24 |
PRESTACIONES = [
|
25 |
"1. PROFESIONALIZACIÓN",
|
26 |
"2. ESTIMULOS DE PRODUCTIVIDAD",
|
27 |
"3. UNIFORMES",
|
28 |
"4. DESCUENTOS INJUSTIFICADOS",
|
29 |
-
"5. SEGURO DE VIDA",
|
30 |
"6. AHORRO SOLIDARIO",
|
31 |
"7. OTROS"
|
32 |
]
|
33 |
|
34 |
-
|
35 |
-
def
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
|
|
47 |
def guardar_registro(entidad, nombre, curp, prestacion, quincena):
|
48 |
if not all([entidad, nombre, curp, prestacion, quincena]):
|
49 |
return "⚠️ Todos los campos son obligatorios"
|
50 |
-
|
51 |
-
if not
|
52 |
return "⚠️ CURP inválido"
|
53 |
-
|
54 |
try:
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
except Exception as e:
|
67 |
return f"❌ Error: {str(e)}"
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
# Interfaz principal
|
73 |
-
with gr.Blocks(title="Sistema de Captura") as app:
|
74 |
-
# Estado de la aplicación
|
75 |
-
logged_in = gr.State(False)
|
76 |
-
|
77 |
-
# Interfaz de login
|
78 |
-
with gr.Column(visible=True) as login_col:
|
79 |
-
# Agrega la imagen aquí (puede ser desde URL o archivo local)
|
80 |
-
try:
|
81 |
-
# Ejemplo con imagen desde URL
|
82 |
-
response = requests.open("d5bb4c4a-f9e5-4c70-a53c-23b7dc3872a1.jpg")
|
83 |
-
img = Image.open(BytesIO(response.content))
|
84 |
-
login_img = gr.Image(img, label="", interactive=False, show_label=False)
|
85 |
-
except:
|
86 |
-
# Si falla la carga, puedes usar una imagen local
|
87 |
-
# Coloca tu imagen en el mismo directorio que el script
|
88 |
-
login_img = gr.Image("d5bb4c4a-f9e5-4c70-a53c-23b7dc3872a1.jpg", label="", interactive=False, show_label=False)
|
89 |
-
|
90 |
-
gr.Markdown("# Sistema de Captura de Incidencias")
|
91 |
-
username = gr.Textbox(label="Usuario")
|
92 |
-
password = gr.Textbox(label="Contraseña", type="password")
|
93 |
-
login_btn = gr.Button("Ingresar")
|
94 |
-
login_msg = gr.Textbox(label="Mensaje", interactive=False)
|
95 |
-
|
96 |
-
# Interfaz de captura
|
97 |
-
with gr.Column(visible=False) as captura_col:
|
98 |
-
gr.Markdown("## Captura de Datos")
|
99 |
-
|
100 |
-
with gr.Row():
|
101 |
-
with gr.Column():
|
102 |
-
entidad = gr.Textbox(label="Entidad Federativa")
|
103 |
-
nombre = gr.Textbox(label="Nombre completo")
|
104 |
-
curp = gr.Textbox(label="CURP")
|
105 |
-
prestacion = gr.Dropdown(PRESTACIONES, label="Prestación afectada")
|
106 |
-
quincena = gr.Textbox(label="Quincena (AAAA-MM-DD)")
|
107 |
-
submit_btn = gr.Button("Guardar")
|
108 |
-
|
109 |
-
resultado = gr.Textbox(label="Resultado")
|
110 |
-
|
111 |
-
logout_btn = gr.Button("Cerrar sesión")
|
112 |
-
|
113 |
-
# Funciones de control
|
114 |
-
def autenticar(user, pwd):
|
115 |
-
if verificar_credenciales(user, pwd):
|
116 |
-
return {
|
117 |
-
login_col: gr.update(visible=False),
|
118 |
-
captura_col: gr.update(visible=True),
|
119 |
-
login_msg: ""
|
120 |
-
}
|
121 |
-
return {
|
122 |
-
login_msg: "⚠️ Credenciales incorrectas"
|
123 |
-
}
|
124 |
-
|
125 |
-
def cerrar_sesion():
|
126 |
-
return {
|
127 |
-
login_col: gr.update(visible=True),
|
128 |
-
captura_col: gr.update(visible=False),
|
129 |
-
login_msg: ""
|
130 |
-
}
|
131 |
-
|
132 |
-
# Eventos
|
133 |
-
login_btn.click(
|
134 |
-
autenticar,
|
135 |
-
inputs=[username, password],
|
136 |
-
outputs=[login_col, captura_col, login_msg]
|
137 |
-
)
|
138 |
-
|
139 |
-
logout_btn.click(
|
140 |
-
cerrar_sesion,
|
141 |
-
outputs=[login_col, captura_col, login_msg]
|
142 |
-
)
|
143 |
-
|
144 |
submit_btn.click(
|
145 |
-
guardar_registro,
|
146 |
inputs=[entidad, nombre, curp, prestacion, quincena],
|
147 |
outputs=resultado
|
148 |
)
|
149 |
|
150 |
-
# Inicializar
|
151 |
-
inicializar_csv()
|
152 |
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import csv
|
3 |
import re
|
|
|
5 |
from hashlib import sha256
|
6 |
import os
|
7 |
from PIL import Image
|
8 |
+
import numpy as np
|
9 |
+
from huggingface_hub import HfApi, hf_hub_download
|
10 |
|
11 |
# Configuración
|
12 |
CSV_FILE = "registros_incidencias.csv"
|
13 |
+
PASSWORD_HASH = "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"
|
14 |
+
REPO_ID = "tu_usuario/tu_repositorio" # Cambiar por tu usuario/repo de HF
|
15 |
|
16 |
PRESTACIONES = [
|
17 |
"1. PROFESIONALIZACIÓN",
|
18 |
"2. ESTIMULOS DE PRODUCTIVIDAD",
|
19 |
"3. UNIFORMES",
|
20 |
"4. DESCUENTOS INJUSTIFICADOS",
|
21 |
+
"5. SEGURO DE VIDA",
|
22 |
"6. AHORRO SOLIDARIO",
|
23 |
"7. OTROS"
|
24 |
]
|
25 |
|
26 |
+
# --- Nuevas funciones para persistencia ---
|
27 |
+
def cargar_csv():
|
28 |
+
try:
|
29 |
+
return pd.read_csv(hf_hub_download(
|
30 |
+
repo_id=REPO_ID,
|
31 |
+
filename=CSV_FILE,
|
32 |
+
repo_type="space",
|
33 |
+
token=os.getenv("HF_TOKEN")
|
34 |
+
))
|
35 |
+
except:
|
36 |
+
# Crear CSV inicial si no existe
|
37 |
+
df = pd.DataFrame(columns=[
|
38 |
+
"Fecha_Registro", "Entidad", "Nombre",
|
39 |
+
"CURP", "Prestacion", "Quincena"
|
40 |
+
])
|
41 |
+
guardar_csv(df)
|
42 |
+
return df
|
43 |
+
|
44 |
+
def guardar_csv(df):
|
45 |
+
df.to_csv(CSV_FILE, index=False)
|
46 |
+
api = HfApi(token=os.getenv("HF_TOKEN"))
|
47 |
+
api.upload_file(
|
48 |
+
path_or_fileobj=CSV_FILE,
|
49 |
+
path_in_repo=CSV_FILE,
|
50 |
+
repo_id=REPO_ID,
|
51 |
+
repo_type="space"
|
52 |
+
)
|
53 |
|
54 |
+
# --- Modificación de tu función original ---
|
55 |
def guardar_registro(entidad, nombre, curp, prestacion, quincena):
|
56 |
if not all([entidad, nombre, curp, prestacion, quincena]):
|
57 |
return "⚠️ Todos los campos son obligatorios"
|
58 |
+
|
59 |
+
if not re.match(r'^[A-Z]{4}[0-9]{6}[HM][A-Z]{5}[A-Z0-9]{2}$', curp.upper()):
|
60 |
return "⚠️ CURP inválido"
|
61 |
+
|
62 |
try:
|
63 |
+
df = cargar_csv()
|
64 |
+
nuevo_registro = {
|
65 |
+
"Fecha_Registro": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
66 |
+
"Entidad": entidad.strip().upper(),
|
67 |
+
"Nombre": nombre.strip().title(),
|
68 |
+
"CURP": curp.upper(),
|
69 |
+
"Prestacion": prestacion,
|
70 |
+
"Quincena": quincena
|
71 |
+
}
|
72 |
+
df = pd.concat([df, pd.DataFrame([nuevo_registro])], ignore_index=True)
|
73 |
+
guardar_csv(df)
|
74 |
+
return "✅ Registro guardado permanentemente"
|
75 |
except Exception as e:
|
76 |
return f"❌ Error: {str(e)}"
|
77 |
|
78 |
+
# --- Configuración de Gradio (similar a tu código original) ---
|
79 |
+
with gr.Blocks() as app:
|
80 |
+
# ... (tu interfaz existente) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
submit_btn.click(
|
82 |
+
guardar_registro,
|
83 |
inputs=[entidad, nombre, curp, prestacion, quincena],
|
84 |
outputs=resultado
|
85 |
)
|
86 |
|
|
|
|
|
87 |
app.launch()
|