Spaces:
Sleeping
Sleeping
Update helper.py
Browse files
helper.py
CHANGED
@@ -13,23 +13,23 @@ from sklearn.preprocessing import MinMaxScaler
|
|
13 |
def assign_main_accounts(creators_file, chatter_files):
|
14 |
"""
|
15 |
Process creators and chatter files to assign main accounts to chatters and prepare data for scheduling.
|
|
|
16 |
"""
|
17 |
# Load and process creators data
|
18 |
creators = pd.read_excel(creators_file)
|
19 |
-
creators.columns = creators.columns.str.strip()
|
20 |
|
21 |
-
# Ensure required columns
|
22 |
required_columns = ["Creator", "Total earnings", "Subscription", "Total active fans"]
|
23 |
missing_columns = [col for col in required_columns if col not in creators.columns]
|
24 |
if missing_columns:
|
25 |
raise KeyError(f"Missing required columns in creators file: {missing_columns}")
|
26 |
|
27 |
-
# Normalize and
|
28 |
creators["Total earnings"] = creators["Total earnings"].replace("[\$,]", "", regex=True).astype(float)
|
29 |
creators["Subscription"] = creators["Subscription"].replace("[\$,]", "", regex=True).astype(float)
|
30 |
creators["Total active fans"] = creators["Total active fans"].fillna(0).astype(int)
|
31 |
|
32 |
-
# Scale data for ranking
|
33 |
scaler = MinMaxScaler()
|
34 |
creators[["Earnings_Normalized", "Subscriptions_Normalized"]] = scaler.fit_transform(
|
35 |
creators[["Total earnings", "Subscription"]]
|
@@ -40,38 +40,38 @@ def assign_main_accounts(creators_file, chatter_files):
|
|
40 |
) * creators["Penalty Factor"]
|
41 |
creators["Rank"] = creators["Score"].rank(ascending=False)
|
42 |
|
43 |
-
# Sort creators by rank
|
44 |
creators = creators.sort_values(by="Rank").reset_index(drop=True)
|
45 |
|
46 |
-
# Save processed creator file
|
47 |
-
processed_creator_file =
|
48 |
-
creators[["Creator", "Total active fans"]].rename(
|
49 |
columns={"Total active fans": "ActiveFans"}
|
50 |
-
)
|
51 |
-
print(f"DEBUG: Processed creators file saved to {processed_creator_file}")
|
52 |
|
53 |
# Assign main accounts to chatters
|
|
|
54 |
updated_chatter_files = []
|
55 |
for idx, chatter_file in enumerate(chatter_files):
|
56 |
-
shift_name = ["
|
57 |
chatters = pd.read_excel(chatter_file)
|
58 |
chatters.columns = chatters.columns.str.strip()
|
59 |
|
60 |
if "Final Rating" not in chatters.columns:
|
61 |
raise KeyError(f"'Final Rating' column is missing in {chatter_file}")
|
62 |
|
63 |
-
# Sort chatters by performance
|
64 |
chatters = chatters.sort_values(by="Final Rating", ascending=False).reset_index(drop=True)
|
65 |
-
|
66 |
-
# Match top creators with top chatters
|
67 |
num_chatters = len(chatters)
|
68 |
top_creators = creators.iloc[:num_chatters]
|
69 |
chatters["Main Account"] = top_creators["Creator"].values
|
|
|
70 |
|
71 |
-
# Append updated chatter data
|
72 |
updated_chatter_files.append(chatters)
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
return updated_chatter_files, processed_creator_file
|
75 |
|
76 |
|
77 |
|
|
|
13 |
def assign_main_accounts(creators_file, chatter_files):
|
14 |
"""
|
15 |
Process creators and chatter files to assign main accounts to chatters and prepare data for scheduling.
|
16 |
+
Returns the combined assignments for preview and the processed creator file.
|
17 |
"""
|
18 |
# Load and process creators data
|
19 |
creators = pd.read_excel(creators_file)
|
20 |
+
creators.columns = creators.columns.str.strip()
|
21 |
|
22 |
+
# Ensure required columns
|
23 |
required_columns = ["Creator", "Total earnings", "Subscription", "Total active fans"]
|
24 |
missing_columns = [col for col in required_columns if col not in creators.columns]
|
25 |
if missing_columns:
|
26 |
raise KeyError(f"Missing required columns in creators file: {missing_columns}")
|
27 |
|
28 |
+
# Normalize and rank creators
|
29 |
creators["Total earnings"] = creators["Total earnings"].replace("[\$,]", "", regex=True).astype(float)
|
30 |
creators["Subscription"] = creators["Subscription"].replace("[\$,]", "", regex=True).astype(float)
|
31 |
creators["Total active fans"] = creators["Total active fans"].fillna(0).astype(int)
|
32 |
|
|
|
33 |
scaler = MinMaxScaler()
|
34 |
creators[["Earnings_Normalized", "Subscriptions_Normalized"]] = scaler.fit_transform(
|
35 |
creators[["Total earnings", "Subscription"]]
|
|
|
40 |
) * creators["Penalty Factor"]
|
41 |
creators["Rank"] = creators["Score"].rank(ascending=False)
|
42 |
|
|
|
43 |
creators = creators.sort_values(by="Rank").reset_index(drop=True)
|
44 |
|
45 |
+
# Save processed creator file
|
46 |
+
processed_creator_file = creators[["Creator", "Total active fans"]].rename(
|
|
|
47 |
columns={"Total active fans": "ActiveFans"}
|
48 |
+
)
|
|
|
49 |
|
50 |
# Assign main accounts to chatters
|
51 |
+
combined_assignments = []
|
52 |
updated_chatter_files = []
|
53 |
for idx, chatter_file in enumerate(chatter_files):
|
54 |
+
shift_name = ["Overnight", "Day", "Prime"][idx]
|
55 |
chatters = pd.read_excel(chatter_file)
|
56 |
chatters.columns = chatters.columns.str.strip()
|
57 |
|
58 |
if "Final Rating" not in chatters.columns:
|
59 |
raise KeyError(f"'Final Rating' column is missing in {chatter_file}")
|
60 |
|
|
|
61 |
chatters = chatters.sort_values(by="Final Rating", ascending=False).reset_index(drop=True)
|
|
|
|
|
62 |
num_chatters = len(chatters)
|
63 |
top_creators = creators.iloc[:num_chatters]
|
64 |
chatters["Main Account"] = top_creators["Creator"].values
|
65 |
+
chatters["Shift"] = shift_name
|
66 |
|
|
|
67 |
updated_chatter_files.append(chatters)
|
68 |
+
combined_assignments.append(chatters)
|
69 |
+
|
70 |
+
# Combine all assignments for display
|
71 |
+
combined_assignments_df = pd.concat(combined_assignments, ignore_index=True)
|
72 |
+
|
73 |
+
return updated_chatter_files, processed_creator_file, combined_assignments_df
|
74 |
|
|
|
75 |
|
76 |
|
77 |
|