Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,139 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
-
import tempfile
|
4 |
-
import os
|
5 |
-
from openpyxl import load_workbook
|
6 |
-
from openpyxl.styles import Alignment
|
7 |
-
|
8 |
-
def adjust_excel_formatting(file_path):
|
9 |
-
wb = load_workbook(file_path)
|
10 |
-
ws = wb.active
|
11 |
-
for col in ws.columns:
|
12 |
-
max_length = 0
|
13 |
-
col_letter = col[0].column_letter
|
14 |
-
for cell in col:
|
15 |
-
if cell.value:
|
16 |
-
max_length = max(max_length, len(str(cell.value)))
|
17 |
-
cell.alignment = Alignment(wrap_text=True)
|
18 |
-
ws.column_dimensions[col_letter].width = max_length + 2
|
19 |
-
wb.save(file_path)
|
20 |
-
|
21 |
-
def process_file_a_to_b(input_file):
|
22 |
-
try:
|
23 |
-
input_df = pd.read_excel(input_file.name, header=1)
|
24 |
-
date_columns = input_df.columns[1:].tolist()
|
25 |
-
df_long = input_df.melt(
|
26 |
-
id_vars=[input_df.columns[0]],
|
27 |
-
var_name='DATE',
|
28 |
-
value_name='CHATTER'
|
29 |
-
)
|
30 |
-
df_long = df_long[df_long['CHATTER'].notna()]
|
31 |
-
df_long = df_long[df_long['CHATTER'] != '']
|
32 |
-
df_long = df_long[df_long['CHATTER'] != 'OFF']
|
33 |
-
grouped = df_long.groupby(['CHATTER', 'DATE'])[input_df.columns[0]].apply(
|
34 |
-
lambda x: ', '.join(sorted(x))
|
35 |
-
).reset_index()
|
36 |
-
pivoted = grouped.pivot(
|
37 |
-
index='CHATTER',
|
38 |
-
columns='DATE',
|
39 |
-
values=input_df.columns[0]
|
40 |
-
)
|
41 |
-
pivoted = pivoted[date_columns]
|
42 |
-
# Use the order of chatters as they appear in the input file
|
43 |
-
input_order = grouped['CHATTER'].drop_duplicates().tolist()
|
44 |
-
final_df = pivoted.reindex(input_order)
|
45 |
-
final_df = final_df.fillna('OFF')
|
46 |
-
final_df = final_df.reset_index()
|
47 |
-
final_df = final_df.rename(columns={'index': 'CHATTER'})
|
48 |
-
return final_df
|
49 |
-
except Exception as e:
|
50 |
-
return pd.DataFrame({"Error": [str(e)]})
|
51 |
-
|
52 |
-
def process_file_b_to_a(input_file):
|
53 |
-
try:
|
54 |
-
input_df = pd.read_excel(input_file.name, header=0)
|
55 |
-
date_columns = input_df.columns[1:].tolist()
|
56 |
-
df_long = input_df.melt(
|
57 |
-
id_vars=[input_df.columns[0]],
|
58 |
-
var_name='DATE',
|
59 |
-
value_name='MODEL'
|
60 |
-
)
|
61 |
-
df_long = df_long[df_long['MODEL'].notna()]
|
62 |
-
df_long = df_long[df_long['MODEL'] != '']
|
63 |
-
df_long = df_long[df_long['MODEL'] != 'OFF']
|
64 |
-
df_long['MODEL'] = df_long['MODEL'].str.split(', ')
|
65 |
-
df_long = df_long.explode('MODEL')
|
66 |
-
grouped = df_long.groupby(['MODEL', 'DATE'])[input_df.columns[0]].apply(
|
67 |
-
lambda x: ', '.join(sorted(x))
|
68 |
-
).reset_index()
|
69 |
-
pivoted = grouped.pivot(
|
70 |
-
index='MODEL',
|
71 |
-
columns='DATE',
|
72 |
-
values=input_df.columns[0]
|
73 |
-
)
|
74 |
-
pivoted = pivoted[date_columns]
|
75 |
-
# Use the order of models as they appear in the input file
|
76 |
-
input_order = grouped['MODEL'].drop_duplicates().tolist()
|
77 |
-
final_df = pivoted.reindex(input_order)
|
78 |
-
final_df = final_df.fillna('OFF')
|
79 |
-
final_df = final_df.reset_index()
|
80 |
-
final_df = final_df.rename(columns={'index': 'MODEL'})
|
81 |
-
return final_df
|
82 |
-
except Exception as e:
|
83 |
-
return pd.DataFrame({"Error": [str(e)]})
|
84 |
-
|
85 |
-
def convert_schedule(file, direction):
|
86 |
-
if direction == "Format A → Format B":
|
87 |
-
df = process_file_a_to_b(file)
|
88 |
-
else:
|
89 |
-
df = process_file_b_to_a(file)
|
90 |
-
# Save to temp file for download
|
91 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:
|
92 |
-
df.to_excel(tmp.name, index=False)
|
93 |
-
adjust_excel_formatting(tmp.name)
|
94 |
-
tmp.seek(0)
|
95 |
-
data = tmp.read()
|
96 |
-
return df, (tmp.name,)
|
97 |
-
|
98 |
-
def download_file(file_tuple):
|
99 |
-
return file_tuple[0]
|
100 |
-
|
101 |
-
demo = gr.Blocks()
|
102 |
-
with demo:
|
103 |
-
gr.Markdown("# 📅 Schedule Converter")
|
104 |
-
gr.Markdown("Upload your schedule Excel file, select conversion direction, and download the result.")
|
105 |
-
with gr.Row():
|
106 |
-
file = gr.File(label="Upload Schedule File", type="filepath")
|
107 |
-
direction = gr.Dropdown([
|
108 |
-
"Format A → Format B",
|
109 |
-
"Format B → Format A"
|
110 |
-
], value="Format A → Format B", label="Conversion Direction")
|
111 |
-
with gr.Row():
|
112 |
-
process_btn = gr.Button("Process File", variant="primary")
|
113 |
-
reset_btn = gr.Button("Upload New File")
|
114 |
-
output_table = gr.Dataframe(label="Preview", wrap=True)
|
115 |
-
download_button = gr.Button("Download Processed File", visible=False)
|
116 |
-
temp_file_path = gr.State(value=None)
|
117 |
-
def reset_components():
|
118 |
-
return [None, pd.DataFrame(), None, gr.update(visible=False)]
|
119 |
-
def process_and_show(file, direction):
|
120 |
-
df, out_path = convert_schedule(file, direction)
|
121 |
-
if out_path:
|
122 |
-
return df, out_path, gr.update(visible=True)
|
123 |
-
return df, None, gr.update(visible=False)
|
124 |
-
process_btn.click(
|
125 |
-
process_and_show,
|
126 |
-
inputs=[file, direction],
|
127 |
-
outputs=[output_table, temp_file_path, download_button]
|
128 |
-
)
|
129 |
-
reset_btn.click(
|
130 |
-
reset_components,
|
131 |
-
outputs=[file, output_table, temp_file_path, download_button]
|
132 |
-
)
|
133 |
-
download_button.click(
|
134 |
-
download_file,
|
135 |
-
inputs=temp_file_path,
|
136 |
-
outputs=gr.File(label="Processed Schedule")
|
137 |
-
)
|
138 |
-
|
139 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|