Spaces:
Sleeping
Sleeping
Download app.py from Prosync/LB_hh_remaining_upload: direct link, hf CLI and curl.
- Browser
- Download file 5.03 kB
-
https://huggingface.co/spaces/Prosync/LB_hh_remaining_upload/resolve/main/app.py
- Command line
-
hf download hf://spaces/Prosync/LB_hh_remaining_upload/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Prosync/LB_hh_remaining_upload/resolve/main/app.py
5.03 kB
| import gradio as gr | |
| import pandas as pd | |
| from io import StringIO | |
| from datetime import datetime | |
| import boto3 | |
| import os | |
| ACCESS = os.environ.get("ACCESS_KEY") | |
| SECRET = os.environ.get("SECRET") | |
| def validar(data): | |
| try: | |
| buffer = StringIO(data) | |
| df = pd.read_csv(buffer, sep="\t", na_values=["null", "NULL", "NaN", ""]) | |
| df_limpio = df.dropna(axis=1, how='all') | |
| if df_limpio.shape[1] < 3: | |
| return None, "⚠️ Error: Debes tener al menos 3 columnas (Activity Type, Spreadsheet Field y una fecha)." | |
| col0, col1, col2 = df_limpio.columns[:3] | |
| if col0.strip() != 'Activity Type': | |
| return None, "⚠️ Error: La primera columna debe llamarse exactamente 'Activity Type'." | |
| if col1.strip() != 'Spreadsheet Field': | |
| return None, "⚠️ Error: La segunda columna debe llamarse exactamente 'Spreadsheet Field'." | |
| if len(df)!=2: | |
| return None, "⚠️ Error: Debes tener exactamente 2 filas de 'Spreadsheet Field'." | |
| df_limpio.fillna(0, inplace=True) | |
| try: | |
| # Identificar las columnas que contienen fechas (todas excepto las dos primeras) | |
| columnas_fecha = df_limpio.columns.difference(['Activity Type', 'Spreadsheet Field']) | |
| # Aplicar conversión solo en columnas de fechas | |
| df_limpio[columnas_fecha] = df_limpio[columnas_fecha].applymap( | |
| lambda x: float(str(x).replace(',', '.')) if isinstance(x, str) and ',' in x else x | |
| ) | |
| datetime.strptime(col2.strip(), "%d-%b-%y") | |
| except ValueError: | |
| return None, "⚠️ Error: La tercera columna debe ser una fecha en formato dd-MMM-yy." | |
| return df_limpio, "✅ ¡Datos válidos! Ya puedes enviarlos al servidor." | |
| except Exception as e: | |
| if str(e) == 'No columns to parse from file': | |
| e = "⚠️ No se encontraron columnas para analizar. Asegúrate copiar la información correcta." | |
| return None, f"❌ Error inesperado: {str(e)}" | |
| def enviar_al_servidor(df, EMPRESA, PROYECTO): | |
| print(EMPRESA, PROYECTO) | |
| # Inicializar cliente DynamoDB | |
| dynamodb = boto3.resource('dynamodb', region_name='us-east-1', | |
| aws_access_key_id=ACCESS, | |
| aws_secret_access_key=SECRET) | |
| # Validar existencia del proyecto | |
| tabla_proyecto = dynamodb.Table('proyecto') | |
| respuesta = tabla_proyecto.get_item(Key={'empresa': EMPRESA, 'proyecto': PROYECTO}) | |
| if 'Item' not in respuesta: | |
| print("❌ El proyecto no existe en la tabla 'proyecto'.") | |
| return "❌ Error: El proyecto no existe en la tabla 'proyecto'." | |
| FINAL = df.groupby('Activity Type')[df.columns[1:]].agg('sum').reset_index() | |
| # Initialize the DynamoDB client | |
| table_name = "cum_budgeted_labor_units" # Replace with your actual table name | |
| table = dynamodb.Table(table_name) | |
| FINAL = FINAL.map(str) | |
| FINAL.rename(columns={'Activity Type':'tipo'}, inplace=True) | |
| for index, row in FINAL.iterrows(): | |
| item = row.to_dict() | |
| item['id_empresa_proyecto'] = EMPRESA+'_'+PROYECTO | |
| response = table.put_item(Item=item) # Insert into DynamoDB | |
| print(f"Inserted row {index}: {response}") | |
| print("Datos enviados al servidor:") | |
| return "✅ Datos enviados al servidor con éxito." | |
| def enviar_al_servidor_botton(df_limpio, EMPRESA, PROYECTO): | |
| EMPRESA = EMPRESA.strip() | |
| PROYECTO = PROYECTO.strip() | |
| if df_limpio is not None: | |
| return enviar_al_servidor(df_limpio, EMPRESA, PROYECTO) | |
| return "❌ No hay datos válidos para enviar." | |
| # Usamos gr.Blocks para mayor control | |
| with gr.Blocks() as demo: | |
| gr.File(value="Layout cum_units.plf", label="📁 Descargar LAYOUT de P6 para extracción de HH", interactive=False) | |
| textbox_input0 = gr.Textbox(label="Escribe el nombre de la empresa en mayúsculas", | |
| lines=1, | |
| placeholder= 'Ejemplo: TECNASIC' | |
| ) | |
| textbox_input1 = gr.Textbox(label="Escribe el nombre del proyecto como se ve en plataforma", | |
| lines=1, | |
| placeholder= 'Ejemplo: OT2936 Lomas Bayas' | |
| ) | |
| textbox_input2 = gr.Textbox( | |
| label="Pega aquí tu tabla de remanentes de la Línea Base", | |
| lines=4, | |
| placeholder="Activity Type\tSpreadsheet Field\t06-May-24\t13-May-24\t20-May-24\nTask Dependent\tCum Earned Value Labor Units\t\t551\t\t10\t\t700\nTask Dependent\tCum Remaining Labor Units\t\t551\t\t10\t\t700" | |
| ) | |
| validar_btn = gr.Button("Validar datos") | |
| df_output = gr.Dataframe(label="Remanentes limpios") | |
| status_box = gr.Textbox(label="Estado de validación", interactive=False) | |
| enviar_btn = gr.Button("Enviar datos al servidor") | |
| validar_btn.click(fn=validar, inputs=textbox_input2, outputs=[df_output, status_box]) | |
| enviar_btn.click(fn=enviar_al_servidor_botton, inputs=[df_output,textbox_input0,textbox_input1], outputs=status_box) | |
| if __name__ == "__main__": | |
| demo.launch() | |