Spaces:
Sleeping
Sleeping
File size: 7,425 Bytes
fc4fcb6 0326f57 d73f855 0326f57 c554237 0326f57 d73f855 c554237 0326f57 c554237 0326f57 a94edc4 0326f57 a94edc4 d73f855 a94edc4 d73f855 a94edc4 d73f855 a94edc4 d73f855 a94edc4 0326f57 a94edc4 d73f855 a94edc4 d73f855 a94edc4 d73f855 0326f57 b3ec40e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
import gradio as gr
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import tempfile
def process_file(file):
df = pd.read_excel(file.name)
df.replace(0, pd.NA, inplace=True)
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
intermediate_file_path = tmp.name
df.to_excel(intermediate_file_path, index=False)
wb = load_workbook(intermediate_file_path)
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
data = ws.values
columns = next(data)[0:]
df = pd.DataFrame(data, columns=columns)
unique_states = df['Name of State'].unique()
for state in unique_states:
state_str = str(state)
state_data = df[df['Name of State'] == state]
unique_channels = state_data['Outlet Channel'].unique()
mode_data = []
for channel in unique_channels:
channel_str = str(channel)
channel_data = state_data[state_data['Outlet Channel'] == channel]
count_series = channel_data.count()
mode_series = channel_data.mode().iloc[0]
numeric_columns = channel_data.select_dtypes(include='number').columns
max_series = channel_data[numeric_columns].max()
min_series = channel_data[numeric_columns].min()
combined_series = pd.concat([
count_series.rename('COUNT'),
mode_series.rename('MODE'),
max_series.rename('MAX'),
min_series.rename('MIN')
], axis=1).T
combined_series.loc[:,'STATE'] = state_str
combined_series.loc[:,'CHANNEL'] = channel_str
mode_data.append(combined_series)
mode_df = pd.concat(mode_data)
new_ws = wb.create_sheet(title=state_str)
for r in dataframe_to_rows(mode_df, index=False, header=True):
new_ws.append(r)
output_file_path = 'state_and_channel_modes.xlsx'
wb.save(output_file_path)
return output_file_path
def process_regional(file):
df = pd.read_excel(file.name)
df.replace(0, pd.NA, inplace=True)
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
intermediate_file_path = tmp.name
df.to_excel(intermediate_file_path, index=False)
wb = load_workbook(intermediate_file_path)
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
data = ws.values
columns = next(data)[0:]
df = pd.DataFrame(data, columns=columns)
unique_states = df['Region'].unique()
for state in unique_states:
state_str = str(state)
state_data = df[df['Region'] == state]
unique_channels = state_data['Outlet Channel'].unique()
mode_data = []
for channel in unique_channels:
channel_str = str(channel)
channel_data = state_data[state_data['Outlet Channel'] == channel]
count_series = channel_data.count()
mode_series = channel_data.mode().iloc[0]
numeric_columns = channel_data.select_dtypes(include='number').columns
max_series = channel_data[numeric_columns].max()
min_series = channel_data[numeric_columns].min()
combined_series = pd.concat([
count_series.rename('COUNT'),
mode_series.rename('MODE'),
max_series.rename('MAX'),
min_series.rename('MIN')
], axis=1).T
combined_series.loc[:,'STATE'] = state_str
combined_series.loc[:,'CHANNEL'] = channel_str
mode_data.append(combined_series)
mode_df = pd.concat(mode_data)
new_ws = wb.create_sheet(title=state_str)
for r in dataframe_to_rows(mode_df, index=False, header=True):
new_ws.append(r)
output_file_path = 'regional.xlsx'
wb.save(output_file_path)
return output_file_path
def process_national(file):
df = pd.read_excel(file.name)
df.replace(0, pd.NA, inplace=True)
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
intermediate_file_path = tmp.name
df.to_excel(intermediate_file_path, index=False)
wb = load_workbook(intermediate_file_path)
for sheet_name in wb.sheetnames:
ws = wb[sheet_name]
data = ws.values
columns = next(data)[0:]
df = pd.DataFrame(data, columns=columns)
unique_states = df['Nation'].unique()
for state in unique_states:
state_str = str(state)
state_data = df[df['Nation'] == state]
unique_channels = state_data['Outlet Channel'].unique()
mode_data = []
for channel in unique_channels:
channel_str = str(channel)
channel_data = state_data[state_data['Outlet Channel'] == channel]
count_series = channel_data.count()
mode_series = channel_data.mode().iloc[0]
numeric_columns = channel_data.select_dtypes(include='number').columns
max_series = channel_data[numeric_columns].max()
min_series = channel_data[numeric_columns].min()
combined_series = pd.concat([
count_series.rename('COUNT'),
mode_series.rename('MODE'),
max_series.rename('MAX'),
min_series.rename('MIN')
], axis=1).T
combined_series.loc[:,'STATE'] = state_str
combined_series.loc[:,'CHANNEL'] = channel_str
mode_data.append(combined_series)
mode_df = pd.concat(mode_data)
new_ws = wb.create_sheet(title=state_str)
for r in dataframe_to_rows(mode_df, index=False, header=True):
new_ws.append(r)
output_file_path = 'national.xlsx'
wb.save(output_file_path)
return output_file_path
iface = gr.Interface(
fn=process_file,
inputs=gr.File(file_types=[".xlsx"]),
outputs=gr.File(),
title="Excel File Processor",
description="Upload an Excel file to process it and generate a new file with state and channel modes."
)
iface2 = gr.Interface(
fn=process_regional,
inputs=gr.File(file_types=[".xlsx"]),
outputs=gr.File(),
title="Excel File Processor",
description="Upload an Excel file to process it and generate a new file with regional and channel modes."
)
iface3 = gr.Interface(
fn=process_national,
inputs=gr.File(file_types=[".xlsx"]),
outputs=gr.File(),
title="Excel File Processor",
description="Upload an Excel file to process it and generate a new file with national and channel modes."
)
gr.TabbedInterface([iface3, iface2, iface], tab_names=['National', 'Region', 'State']).launch(share=True)
|