Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,6 +41,8 @@ def upload_files(creators_file, overnight_file, day_file, prime_file):
|
|
41 |
|
42 |
|
43 |
def generate_main_accounts(updated_data=None):
|
|
|
|
|
44 |
creators_file = os.path.join(UPLOAD_FOLDER, "creators_file.xlsx")
|
45 |
chatter_files = [
|
46 |
os.path.join(UPLOAD_FOLDER, "overnight_file.xlsx"),
|
@@ -56,14 +58,9 @@ def generate_main_accounts(updated_data=None):
|
|
56 |
# Ensure updated_data is in the correct format
|
57 |
if isinstance(updated_data, pd.DataFrame):
|
58 |
updated_data = updated_data.to_dict(orient="records")
|
59 |
-
|
60 |
-
#
|
61 |
-
|
62 |
-
if not isinstance(record, dict):
|
63 |
-
raise ValueError(f"Invalid record in updated data: {record}")
|
64 |
-
|
65 |
-
# Group updated data by shift
|
66 |
-
assignments = {
|
67 |
shift.lower(): [
|
68 |
{key: record[key] for key in record if key != "Shift"} # Remove the Shift column
|
69 |
for record in updated_data if record["Shift"] == shift.capitalize()
|
@@ -71,12 +68,16 @@ def generate_main_accounts(updated_data=None):
|
|
71 |
for shift in ["overnight", "day", "prime"]
|
72 |
}
|
73 |
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
76 |
return "Assignments updated successfully!", pd.DataFrame(updated_data)
|
77 |
|
78 |
# Generate new assignments if no updated data is provided
|
79 |
main_assignments = assign_main_accounts(creators_file, chatter_files)
|
|
|
80 |
save_processed_files(main_assignments, PROCESSED_FOLDER)
|
81 |
|
82 |
# Combine all shifts into a single DataFrame for preview
|
@@ -95,24 +96,47 @@ def generate_main_accounts(updated_data=None):
|
|
95 |
|
96 |
|
97 |
|
98 |
-
def generate_full_schedule():
|
99 |
-
chatter_files = [
|
100 |
-
os.path.join(PROCESSED_FOLDER, "Updated_overnight_file.xlsx"),
|
101 |
-
os.path.join(PROCESSED_FOLDER, "Updated_day_file.xlsx"),
|
102 |
-
os.path.join(PROCESSED_FOLDER, "Updated_prime_file.xlsx"),
|
103 |
-
]
|
104 |
-
creators_file = os.path.join(PROCESSED_FOLDER, "creators_file.xlsx")
|
105 |
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
try:
|
|
|
110 |
full_schedules = generate_schedule(chatter_files, creators_file)
|
111 |
return full_schedules
|
112 |
except Exception as e:
|
113 |
return f"Error generating full schedule: {e}"
|
114 |
|
115 |
|
|
|
116 |
# Gradio Interface
|
117 |
def app():
|
118 |
with gr.Blocks() as interface:
|
|
|
41 |
|
42 |
|
43 |
def generate_main_accounts(updated_data=None):
|
44 |
+
global UPDATED_ASSIGNMENTS
|
45 |
+
|
46 |
creators_file = os.path.join(UPLOAD_FOLDER, "creators_file.xlsx")
|
47 |
chatter_files = [
|
48 |
os.path.join(UPLOAD_FOLDER, "overnight_file.xlsx"),
|
|
|
58 |
# Ensure updated_data is in the correct format
|
59 |
if isinstance(updated_data, pd.DataFrame):
|
60 |
updated_data = updated_data.to_dict(orient="records")
|
61 |
+
|
62 |
+
# Group updated data by shift and save to UPDATED_ASSIGNMENTS
|
63 |
+
UPDATED_ASSIGNMENTS = {
|
|
|
|
|
|
|
|
|
|
|
64 |
shift.lower(): [
|
65 |
{key: record[key] for key in record if key != "Shift"} # Remove the Shift column
|
66 |
for record in updated_data if record["Shift"] == shift.capitalize()
|
|
|
68 |
for shift in ["overnight", "day", "prime"]
|
69 |
}
|
70 |
|
71 |
+
UPDATED_ASSIGNMENTS["creator_names"] = list(
|
72 |
+
set(record["Main Account"] for record in updated_data if "Main Account" in record)
|
73 |
+
)
|
74 |
+
|
75 |
+
save_processed_files(UPDATED_ASSIGNMENTS, PROCESSED_FOLDER)
|
76 |
return "Assignments updated successfully!", pd.DataFrame(updated_data)
|
77 |
|
78 |
# Generate new assignments if no updated data is provided
|
79 |
main_assignments = assign_main_accounts(creators_file, chatter_files)
|
80 |
+
UPDATED_ASSIGNMENTS = main_assignments
|
81 |
save_processed_files(main_assignments, PROCESSED_FOLDER)
|
82 |
|
83 |
# Combine all shifts into a single DataFrame for preview
|
|
|
96 |
|
97 |
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
# Global variable to store updated assignments from the main account generation
|
101 |
+
UPDATED_ASSIGNMENTS = {}
|
102 |
+
|
103 |
+
def generate_full_schedule():
|
104 |
+
global UPDATED_ASSIGNMENTS
|
105 |
+
|
106 |
+
# Check if UPDATED_ASSIGNMENTS is populated
|
107 |
+
if UPDATED_ASSIGNMENTS:
|
108 |
+
# Convert the in-memory assignments to DataFrames
|
109 |
+
chatter_files = []
|
110 |
+
for shift in ["overnight", "day", "prime"]:
|
111 |
+
if shift in UPDATED_ASSIGNMENTS:
|
112 |
+
df = pd.DataFrame(UPDATED_ASSIGNMENTS[shift])
|
113 |
+
chatter_files.append(df)
|
114 |
+
|
115 |
+
creators_file = pd.DataFrame({"Creator": UPDATED_ASSIGNMENTS.get("creator_names", [])})
|
116 |
+
else:
|
117 |
+
# Fall back to reading processed files
|
118 |
+
chatter_files = [
|
119 |
+
pd.read_excel(os.path.join(PROCESSED_FOLDER, "Updated_overnight_file.xlsx")),
|
120 |
+
pd.read_excel(os.path.join(PROCESSED_FOLDER, "Updated_day_file.xlsx")),
|
121 |
+
pd.read_excel(os.path.join(PROCESSED_FOLDER, "Updated_prime_file.xlsx")),
|
122 |
+
]
|
123 |
+
creators_file = pd.read_excel(os.path.join(PROCESSED_FOLDER, "creators_file.xlsx"))
|
124 |
+
|
125 |
+
# Debugging: Ensure chatter files and creators file are populated
|
126 |
+
print("DEBUG: Chatter Files and Creators File for Schedule Generation")
|
127 |
+
for chatter_file in chatter_files:
|
128 |
+
print(chatter_file.head())
|
129 |
+
print(creators_file.head())
|
130 |
|
131 |
try:
|
132 |
+
# Generate schedules using in-memory or processed data
|
133 |
full_schedules = generate_schedule(chatter_files, creators_file)
|
134 |
return full_schedules
|
135 |
except Exception as e:
|
136 |
return f"Error generating full schedule: {e}"
|
137 |
|
138 |
|
139 |
+
|
140 |
# Gradio Interface
|
141 |
def app():
|
142 |
with gr.Blocks() as interface:
|