Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,23 @@
|
|
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
|
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 |
|
14 |
-
# Decide which header row to use: prefer
|
15 |
if header2.notna().all() and not header2.str.startswith('Unnamed').any():
|
16 |
days = header2.tolist()
|
17 |
data_start = 2
|
18 |
else:
|
|
|
19 |
days = []
|
20 |
last = None
|
21 |
for val in header1:
|
@@ -26,19 +28,19 @@ def convert_schedule(file_path, direction):
|
|
26 |
days.append(last)
|
27 |
data_start = 1
|
28 |
|
29 |
-
# Load actual data with day columns
|
30 |
df = pd.read_excel(
|
31 |
file_path,
|
32 |
header=data_start,
|
33 |
index_col=0,
|
34 |
-
usecols=[0] + list(range(1, len(days)+1))
|
35 |
)
|
36 |
df.columns = [str(day) for day in days]
|
37 |
|
38 |
-
#
|
39 |
day_cols = list(df.columns)
|
40 |
|
41 |
-
#
|
42 |
assignments = {}
|
43 |
if direction == 'A to B':
|
44 |
for model in df.index.astype(str):
|
@@ -50,6 +52,7 @@ def convert_schedule(file_path, direction):
|
|
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():
|
@@ -73,17 +76,19 @@ def convert_schedule(file_path, direction):
|
|
73 |
texters = days_map.get(day, [])
|
74 |
result.at[model, day] = ', '.join(texters) if texters else 'OFF'
|
75 |
|
76 |
-
#
|
77 |
result.index.name = None
|
78 |
result.columns.name = None
|
79 |
|
80 |
-
#
|
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():
|
88 |
iface = gr.Interface(
|
89 |
fn=convert_schedule,
|
@@ -96,8 +101,10 @@ def main():
|
|
96 |
gr.File(label='Download Converted Excel')
|
97 |
],
|
98 |
title='7-Day Schedule Converter',
|
99 |
-
description=(
|
100 |
-
|
|
|
|
|
101 |
allow_flagging='never'
|
102 |
)
|
103 |
iface.launch(server_name='0.0.0.0', server_port=7860)
|
|
|
1 |
import pandas as pd
|
2 |
+
import openpyxl # ensure XLSX engine is available
|
3 |
import gradio as gr
|
4 |
import uuid
|
5 |
import os
|
6 |
|
7 |
+
# 7-Day Schedule Converter with explicit iteration and header fill and download support
|
8 |
|
9 |
def convert_schedule(file_path, direction):
|
10 |
+
# 1. Load Excel sheet without header to inspect real headers
|
11 |
raw = pd.read_excel(file_path, header=None)
|
12 |
header1 = raw.iloc[0,1:].astype(object)
|
13 |
header2 = raw.iloc[1,1:].astype(object)
|
14 |
|
15 |
+
# Decide which header row to use: prefer fully populated second row
|
16 |
if header2.notna().all() and not header2.str.startswith('Unnamed').any():
|
17 |
days = header2.tolist()
|
18 |
data_start = 2
|
19 |
else:
|
20 |
+
# Forward-fill merged first-row headers
|
21 |
days = []
|
22 |
last = None
|
23 |
for val in header1:
|
|
|
28 |
days.append(last)
|
29 |
data_start = 1
|
30 |
|
31 |
+
# 2. Load actual data with resolved day columns
|
32 |
df = pd.read_excel(
|
33 |
file_path,
|
34 |
header=data_start,
|
35 |
index_col=0,
|
36 |
+
usecols=[0] + list(range(1, len(days) + 1))
|
37 |
)
|
38 |
df.columns = [str(day) for day in days]
|
39 |
|
40 |
+
# 3. Retain exactly the original day columns
|
41 |
day_cols = list(df.columns)
|
42 |
|
43 |
+
# 4. Build assignment mapping via explicit iteration
|
44 |
assignments = {}
|
45 |
if direction == 'A to B':
|
46 |
for model in df.index.astype(str):
|
|
|
52 |
continue
|
53 |
assignments.setdefault(texter, {d: [] for d in day_cols})
|
54 |
assignments[texter][day].append(model)
|
55 |
+
# Create result DataFrame
|
56 |
index = sorted(assignments.keys())
|
57 |
result = pd.DataFrame(index=index, columns=day_cols)
|
58 |
for texter, days_map in assignments.items():
|
|
|
76 |
texters = days_map.get(day, [])
|
77 |
result.at[model, day] = ', '.join(texters) if texters else 'OFF'
|
78 |
|
79 |
+
# 5. Cleanup axis names
|
80 |
result.index.name = None
|
81 |
result.columns.name = None
|
82 |
|
83 |
+
# 6. Save to Excel for download, specifying engine
|
84 |
output_filename = f"converted_{uuid.uuid4().hex}.xlsx"
|
85 |
output_path = os.path.join(os.getcwd(), output_filename)
|
86 |
+
result.to_excel(output_path, engine='openpyxl', index=True)
|
87 |
+
|
88 |
+
# Return both DataFrame and download path
|
89 |
return result, output_path
|
90 |
|
91 |
+
# Gradio UI definition
|
92 |
def main():
|
93 |
iface = gr.Interface(
|
94 |
fn=convert_schedule,
|
|
|
101 |
gr.File(label='Download Converted Excel')
|
102 |
],
|
103 |
title='7-Day Schedule Converter',
|
104 |
+
description=(
|
105 |
+
'Upload a 7-column weekly schedule (Models vs Days) with merged or single headers, '
|
106 |
+
'then flip between Models→Texters or Texters→Models. Download the result as .xlsx.'
|
107 |
+
),
|
108 |
allow_flagging='never'
|
109 |
)
|
110 |
iface.launch(server_name='0.0.0.0', server_port=7860)
|