Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
# 7-Day Schedule Converter with explicit iteration and header fill
|
5 |
|
6 |
def convert_schedule(file_path, direction):
|
7 |
# 1. Load Excel sheet into DataFrame
|
8 |
-
# Read without header to inspect first two rows for header fill
|
9 |
raw = pd.read_excel(file_path, header=None)
|
10 |
-
# Extract potential header rows (skip first column)
|
11 |
header1 = raw.iloc[0,1:].astype(object)
|
12 |
header2 = raw.iloc[1,1:].astype(object)
|
13 |
|
@@ -16,7 +16,6 @@ def convert_schedule(file_path, direction):
|
|
16 |
days = header2.tolist()
|
17 |
data_start = 2
|
18 |
else:
|
19 |
-
# Forward-fill merged first-row headers
|
20 |
days = []
|
21 |
last = None
|
22 |
for val in header1:
|
@@ -36,13 +35,12 @@ def convert_schedule(file_path, direction):
|
|
36 |
)
|
37 |
df.columns = [str(day) for day in days]
|
38 |
|
39 |
-
# 2. Ensure we have exactly the original
|
40 |
day_cols = list(df.columns)
|
41 |
|
42 |
-
# 3. Build mapping
|
43 |
assignments = {}
|
44 |
if direction == 'A to B':
|
45 |
-
# Rows are Models → output rows are Texters
|
46 |
for model in df.index.astype(str):
|
47 |
for day in day_cols:
|
48 |
cell = df.at[model, day]
|
@@ -52,7 +50,6 @@ def convert_schedule(file_path, direction):
|
|
52 |
continue
|
53 |
assignments.setdefault(texter, {d: [] for d in day_cols})
|
54 |
assignments[texter][day].append(model)
|
55 |
-
# Build output DataFrame
|
56 |
index = sorted(assignments.keys())
|
57 |
result = pd.DataFrame(index=index, columns=day_cols)
|
58 |
for texter, days_map in assignments.items():
|
@@ -60,7 +57,6 @@ def convert_schedule(file_path, direction):
|
|
60 |
models = days_map.get(day, [])
|
61 |
result.at[texter, day] = ', '.join(models) if models else 'OFF'
|
62 |
else:
|
63 |
-
# Rows are Texters → output rows are Models
|
64 |
for texter in df.index.astype(str):
|
65 |
for day in day_cols:
|
66 |
cell = df.at[texter, day]
|
@@ -80,7 +76,12 @@ def convert_schedule(file_path, direction):
|
|
80 |
# 4. Cleanup axis names
|
81 |
result.index.name = None
|
82 |
result.columns.name = None
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
# Gradio UI
|
86 |
def main():
|
@@ -90,10 +91,13 @@ def main():
|
|
90 |
gr.File(label='Upload Weekly Schedule (.xlsx)', file_count='single', type='filepath'),
|
91 |
gr.Radio(['A to B', 'B to A'], label='Convert Direction')
|
92 |
],
|
93 |
-
outputs=
|
|
|
|
|
|
|
94 |
title='7-Day Schedule Converter',
|
95 |
-
description=('Upload a 7-column weekly schedule (Models vs Days) with merged or single headers, '
|
96 |
-
'then flip between Models→Texters or Texters→Models.
|
97 |
allow_flagging='never'
|
98 |
)
|
99 |
iface.launch(server_name='0.0.0.0', server_port=7860)
|
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
+
import uuid
|
4 |
+
import os
|
5 |
|
6 |
# 7-Day Schedule Converter with explicit iteration and header fill
|
7 |
|
8 |
def convert_schedule(file_path, direction):
|
9 |
# 1. Load Excel sheet into DataFrame
|
|
|
10 |
raw = pd.read_excel(file_path, header=None)
|
|
|
11 |
header1 = raw.iloc[0,1:].astype(object)
|
12 |
header2 = raw.iloc[1,1:].astype(object)
|
13 |
|
|
|
16 |
days = header2.tolist()
|
17 |
data_start = 2
|
18 |
else:
|
|
|
19 |
days = []
|
20 |
last = None
|
21 |
for val in header1:
|
|
|
35 |
)
|
36 |
df.columns = [str(day) for day in days]
|
37 |
|
38 |
+
# 2. Ensure we have exactly the original day columns
|
39 |
day_cols = list(df.columns)
|
40 |
|
41 |
+
# 3. Build mapping of assignments
|
42 |
assignments = {}
|
43 |
if direction == 'A to B':
|
|
|
44 |
for model in df.index.astype(str):
|
45 |
for day in day_cols:
|
46 |
cell = df.at[model, day]
|
|
|
50 |
continue
|
51 |
assignments.setdefault(texter, {d: [] for d in day_cols})
|
52 |
assignments[texter][day].append(model)
|
|
|
53 |
index = sorted(assignments.keys())
|
54 |
result = pd.DataFrame(index=index, columns=day_cols)
|
55 |
for texter, days_map in assignments.items():
|
|
|
57 |
models = days_map.get(day, [])
|
58 |
result.at[texter, day] = ', '.join(models) if models else 'OFF'
|
59 |
else:
|
|
|
60 |
for texter in df.index.astype(str):
|
61 |
for day in day_cols:
|
62 |
cell = df.at[texter, day]
|
|
|
76 |
# 4. Cleanup axis names
|
77 |
result.index.name = None
|
78 |
result.columns.name = None
|
79 |
+
|
80 |
+
# 5. Save to Excel for download
|
81 |
+
output_filename = f"converted_{uuid.uuid4().hex}.xlsx"
|
82 |
+
output_path = os.path.join(os.getcwd(), output_filename)
|
83 |
+
result.to_excel(output_path)
|
84 |
+
return result, output_path
|
85 |
|
86 |
# Gradio UI
|
87 |
def main():
|
|
|
91 |
gr.File(label='Upload Weekly Schedule (.xlsx)', file_count='single', type='filepath'),
|
92 |
gr.Radio(['A to B', 'B to A'], label='Convert Direction')
|
93 |
],
|
94 |
+
outputs=[
|
95 |
+
gr.Dataframe(label='Converted Schedule'),
|
96 |
+
gr.File(label='Download Converted Excel')
|
97 |
+
],
|
98 |
title='7-Day Schedule Converter',
|
99 |
+
description=('Upload a 7-column weekly schedule (Models vs Days) with merged or single headers, '
|
100 |
+
'then flip between Models→Texters or Texters→Models. Download the result as .xlsx.'),
|
101 |
allow_flagging='never'
|
102 |
)
|
103 |
iface.launch(server_name='0.0.0.0', server_port=7860)
|