Spaces:
Runtime error
Runtime error
| import pandas as pd | |
| import gradio as gr | |
| from helper_funcs import functions, INPUT_FILE_TYPE, OUTPUT_FILE_TYPE, get_classla_stats_df | |
| def run_function(selected_function, file_obj, input_column, output_column, output_type): | |
| if 'json' in file_obj.name.lower(): | |
| df = pd.read_json(file_obj.name) | |
| if any([x in file_obj.name.lower() for x in ['csv', 'txt']]): | |
| df = pd.read_csv(file_obj.name, encoding='utf-8') | |
| output_file = 'result' + output_type | |
| if input_column not in list(df.columns): | |
| raise gr.Error("Input column name: such column does not exist in dataframe!") | |
| funcs = functions[selected_function](df, input_column, output_column, output_file) | |
| return funcs | |
| app = gr.Blocks() | |
| with app: | |
| process_status = gr.State(False) | |
| gr.Markdown( | |
| """ | |
| # Instructions | |
| 1. Upload CSV file to process. | |
| 2. Enter the name of the column containing the values to process. | |
| 3. Enter the name of the column in which to save the output. | |
| 4. Select function to process the data. | |
| 5. Click Process. | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown( | |
| """ | |
| # Input | |
| """ | |
| ) | |
| file_obj = gr.File( | |
| label="Input File", | |
| file_count="single", | |
| file_types=INPUT_FILE_TYPE | |
| ) | |
| input_column = gr.Textbox( | |
| label="Input column name", | |
| info="Please enter the name of the column you want to process (as it appears in your dataset)", | |
| lines=1, | |
| ) | |
| output_column = gr.Textbox( | |
| label="Output column name", | |
| info="Please enter the name of the column you want to save the result to", | |
| lines=1, | |
| ) | |
| selected_function = gr.Dropdown( | |
| list(functions.keys()), | |
| label="Select processing", | |
| info="" | |
| ) | |
| output_type = gr.Dropdown( | |
| list(OUTPUT_FILE_TYPE), | |
| label="Select the output file type", | |
| info="" | |
| ) | |
| with gr.Column(): | |
| gr.Markdown( | |
| """ | |
| # Output | |
| """ | |
| ) | |
| output_dataframe = gr.Dataframe( | |
| label="Output Data" | |
| ) | |
| output_csv = gr.File( | |
| label="Output File", | |
| file_types=OUTPUT_FILE_TYPE | |
| ) | |
| stats_plot = gr.BarPlot( | |
| value = pd.DataFrame(columns=['value', 'count']), | |
| x = 'value', | |
| y = 'count' | |
| ) | |
| process_button = gr.Button("Process") | |
| process_button.click( | |
| run_function, | |
| inputs=[selected_function, file_obj, input_column, output_column, output_type], | |
| outputs=[output_dataframe, output_csv], | |
| ) | |
| strats_button = gr.Button("Get Stats") | |
| strats_button.click(get_classla_stats_df, inputs=None, outputs=stats_plot) | |
| app.launch() | |