Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
import re
|
4 |
from datetime import datetime
|
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
|
@@ -23,17 +21,16 @@ PRESTACIONES = [
|
|
23 |
"7. OTROS"
|
24 |
]
|
25 |
|
26 |
-
#
|
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("
|
34 |
))
|
35 |
except:
|
36 |
-
# Crear CSV inicial si no existe
|
37 |
df = pd.DataFrame(columns=[
|
38 |
"Fecha_Registro", "Entidad", "Nombre",
|
39 |
"CURP", "Prestacion", "Quincena"
|
@@ -43,7 +40,7 @@ def cargar_csv():
|
|
43 |
|
44 |
def guardar_csv(df):
|
45 |
df.to_csv(CSV_FILE, index=False)
|
46 |
-
api = HfApi(token=os.getenv("
|
47 |
api.upload_file(
|
48 |
path_or_fileobj=CSV_FILE,
|
49 |
path_in_repo=CSV_FILE,
|
@@ -51,7 +48,6 @@ def guardar_csv(df):
|
|
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"
|
@@ -75,11 +71,72 @@ def guardar_registro(entidad, nombre, curp, prestacion, quincena):
|
|
75 |
except Exception as e:
|
76 |
return f"❌ Error: {str(e)}"
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
submit_btn.click(
|
82 |
-
guardar_registro,
|
83 |
inputs=[entidad, nombre, curp, prestacion, quincena],
|
84 |
outputs=resultado
|
85 |
)
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
import re
|
4 |
from datetime import datetime
|
5 |
from hashlib import sha256
|
6 |
import os
|
|
|
|
|
7 |
from huggingface_hub import HfApi, hf_hub_download
|
8 |
|
9 |
# Configuración
|
|
|
21 |
"7. OTROS"
|
22 |
]
|
23 |
|
24 |
+
# Funciones para CSV
|
25 |
def cargar_csv():
|
26 |
try:
|
27 |
return pd.read_csv(hf_hub_download(
|
28 |
repo_id=REPO_ID,
|
29 |
filename=CSV_FILE,
|
30 |
repo_type="space",
|
31 |
+
token=os.getenv("IMSS")
|
32 |
))
|
33 |
except:
|
|
|
34 |
df = pd.DataFrame(columns=[
|
35 |
"Fecha_Registro", "Entidad", "Nombre",
|
36 |
"CURP", "Prestacion", "Quincena"
|
|
|
40 |
|
41 |
def guardar_csv(df):
|
42 |
df.to_csv(CSV_FILE, index=False)
|
43 |
+
api = HfApi(token=os.getenv("IMSS"))
|
44 |
api.upload_file(
|
45 |
path_or_fileobj=CSV_FILE,
|
46 |
path_in_repo=CSV_FILE,
|
|
|
48 |
repo_type="space"
|
49 |
)
|
50 |
|
|
|
51 |
def guardar_registro(entidad, nombre, curp, prestacion, quincena):
|
52 |
if not all([entidad, nombre, curp, prestacion, quincena]):
|
53 |
return "⚠️ Todos los campos son obligatorios"
|
|
|
71 |
except Exception as e:
|
72 |
return f"❌ Error: {str(e)}"
|
73 |
|
74 |
+
def verificar_credenciales(username, password):
|
75 |
+
return username == "admin" and sha256(password.encode()).hexdigest() == PASSWORD_HASH
|
76 |
+
|
77 |
+
# Interfaz
|
78 |
+
with gr.Blocks(title="Sistema de Captura") as app:
|
79 |
+
logged_in = gr.State(False)
|
80 |
+
|
81 |
+
# Login
|
82 |
+
with gr.Column(visible=True) as login_col:
|
83 |
+
try:
|
84 |
+
img = Image.open("d5bb4c4a-f9e5-4c70-a53c-23b7dc3872a1.jpg")
|
85 |
+
img.thumbnail((200, 200))
|
86 |
+
gr.Image(np.array(img), show_label=False)
|
87 |
+
except:
|
88 |
+
gr.Markdown("")
|
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 |
+
# Captura
|
97 |
+
with gr.Column(visible=False) as captura_col:
|
98 |
+
gr.Markdown("## Captura de Datos")
|
99 |
+
entidad = gr.Textbox(label="Entidad Federativa")
|
100 |
+
nombre = gr.Textbox(label="Nombre completo")
|
101 |
+
curp = gr.Textbox(label="CURP")
|
102 |
+
prestacion = gr.Dropdown(PRESTACIONES, label="Prestación afectada")
|
103 |
+
quincena = gr.Textbox(label="Quincena (AAAA-MM-DD)")
|
104 |
+
submit_btn = gr.Button("Guardar")
|
105 |
+
resultado = gr.Textbox(label="Resultado")
|
106 |
+
logout_btn = gr.Button("Cerrar sesión")
|
107 |
+
|
108 |
+
# Eventos (Dentro del bloque with gr.Blocks())
|
109 |
+
def autenticar(user, pwd):
|
110 |
+
if verificar_credenciales(user, pwd):
|
111 |
+
return {
|
112 |
+
login_col: gr.update(visible=False),
|
113 |
+
captura_col: gr.update(visible=True),
|
114 |
+
login_msg: ""
|
115 |
+
}
|
116 |
+
return {
|
117 |
+
login_msg: "⚠️ Credenciales incorrectas"
|
118 |
+
}
|
119 |
+
|
120 |
+
def cerrar_sesion():
|
121 |
+
return {
|
122 |
+
login_col: gr.update(visible=True),
|
123 |
+
captura_col: gr.update(visible=False),
|
124 |
+
login_msg: ""
|
125 |
+
}
|
126 |
+
|
127 |
+
login_btn.click(
|
128 |
+
autenticar,
|
129 |
+
inputs=[username, password],
|
130 |
+
outputs=[login_col, captura_col, login_msg]
|
131 |
+
)
|
132 |
+
|
133 |
+
logout_btn.click(
|
134 |
+
cerrar_sesion,
|
135 |
+
outputs=[login_col, captura_col, login_msg]
|
136 |
+
)
|
137 |
+
|
138 |
submit_btn.click(
|
139 |
+
guardar_registro,
|
140 |
inputs=[entidad, nombre, curp, prestacion, quincena],
|
141 |
outputs=resultado
|
142 |
)
|