Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| from scipy.spatial.distance import cdist | |
| # Read the base DataFrame | |
| base_df = pd.read_excel('app_david.xlsx') | |
| selected_column = { | |
| "Proximidade Estabelecimentos": 'v_estab', | |
| "Concentração de Comércio": 'v_ccom', | |
| "Concentração de Linhas de Ônibus": 'v_linhas', | |
| "Proximidade Rua dos Andradas": 'v_ruas' | |
| } | |
| def get_value_at_closest_coordinates(base_df, x_value, y_value, selected_column): | |
| # Convert 'X' and 'Y' columns of the base DataFrame to a 2D array | |
| xy_coords = base_df[['X', 'Y']].values | |
| # Calculate Euclidean distances between the input coordinates and base DataFrame coordinates | |
| distances = cdist([(x_value, y_value)], xy_coords) | |
| # Find the index of the minimum distance | |
| min_distance_index = distances.argmin(axis=1) | |
| # Get the values from the selected column at the index of the minimum distance | |
| values_at_closest_coords = base_df.loc[min_distance_index, selected_column].values | |
| return values_at_closest_coords[0] # Return the value as a scalar, not a single-element array | |
| def aplicar_funcao(planilha, coluna): | |
| # Read the uploaded DataFrame with the new coordinates | |
| df = pd.read_excel(planilha.name) | |
| # Create a new DataFrame for the output | |
| output_df = pd.DataFrame(columns=['X', 'Y', coluna]) | |
| # Calculate the closest values using the base DataFrame for each row | |
| for index, row in df.iterrows(): | |
| selected_value = get_value_at_closest_coordinates(base_df, row['X'], row['Y'], selected_column[coluna]) | |
| output_df.loc[index] = [row['X'], row['Y'], selected_value] | |
| # Save the output DataFrame as an XLS file | |
| output_file = 'output.xlsx' | |
| output_df.to_excel(output_file, index=False) | |
| return output_file | |
| theme=gr.themes.Base(primary_hue="teal",secondary_hue="teal", | |
| neutral_hue="gray") | |
| # Interface do Gradio com output como arquivo XLS | |
| interface = gr.Interface( | |
| fn=aplicar_funcao, | |
| inputs=[ | |
| gr.inputs.File(label="Escolha a planilha XLS ou XLSX", type="file"), | |
| gr.inputs.Dropdown(list(selected_column.keys()), label="Escolha uma variável", default="Proximidade Estabelecimentos") | |
| ], | |
| outputs=gr.outputs.File(label="Baixar planilha XLS"), | |
| live=False, | |
| capture_session=True, | |
| theme=theme, | |
| title="Aplicativo de Atribuição de Variáveis", | |
| description="Faça o upload de uma planilha XLS ou XLSX e escolha uma variável para aplicar nela. A planilha deve conter as colunas X e Y com os valores de coordenadas em WGS 84 (World Geodetic System 1984) - 'EPSG:4326’ - expressos em graus decimais de latitude e longitude. Para um exemplo de estrutura de planilha, você pode baixar <a href='https://huggingface.co/spaces/fschwartzer/App_V_centro/resolve/main/teste.xlsx' download='teste.xlsx'>aqui</a>.") | |
| # Executar o aplicativo Gradio | |
| if __name__ == "__main__": | |
| interface.launch() | |