3morrrrr commited on
Commit
8425b4c
·
verified ·
1 Parent(s): eaa65f8

Update helper.py

Browse files
Files changed (1) hide show
  1. helper.py +59 -43
helper.py CHANGED
@@ -7,49 +7,65 @@ import re
7
  import pandas as pd
8
  from sklearn.preprocessing import MinMaxScaler
9
 
10
- def assign_main_accounts(updated_data=None):
11
- global UPDATED_ASSIGNMENTS
12
-
13
- creators_file = os.path.join(UPLOAD_FOLDER, "creators_file.xlsx")
14
- chatter_files = [
15
- os.path.join(UPLOAD_FOLDER, "overnight_file.xlsx"),
16
- os.path.join(UPLOAD_FOLDER, "day_file.xlsx"),
17
- os.path.join(UPLOAD_FOLDER, "prime_file.xlsx"),
18
- ]
19
-
20
- if not all(os.path.exists(path) for path in [creators_file] + chatter_files):
21
- return "Missing required files. Please upload all necessary files.", None
22
-
23
- try:
24
- # Step 1: Assign main accounts
25
- updated_chatter_files, account_data = assign_main_accounts(creators_file, chatter_files)
26
- print("DEBUG: Updated Chatter Files and Account Data Successfully Generated")
27
-
28
- # Save processed files
29
- UPDATED_ASSIGNMENTS = {
30
- "chatter_files": updated_chatter_files,
31
- "account_data": account_data
32
- }
33
- for idx, chatter_df in enumerate(updated_chatter_files):
34
- chatter_df.to_excel(
35
- os.path.join(PROCESSED_FOLDER, f"Updated_{['overnight', 'day', 'prime'][idx]}_file.xlsx"),
36
- index=False
37
- )
38
- account_data.to_excel(os.path.join(PROCESSED_FOLDER, "creators_file.xlsx"), index=False)
39
-
40
- # Step 2: Combine for preview
41
- preview_data = []
42
- shift_names = ["Overnight", "Day", "Prime"]
43
- for idx, chatter_df in enumerate(updated_chatter_files):
44
- chatter_df["Shift"] = shift_names[idx]
45
- preview_data.append(chatter_df)
46
-
47
- preview_df = pd.concat(preview_data, ignore_index=True)
48
- return "Main accounts generated successfully!", preview_df
49
-
50
- except Exception as e:
51
- print(f"Error during main account generation: {e}")
52
- return f"Error during main account generation: {e}", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
 
55
 
 
7
  import pandas as pd
8
  from sklearn.preprocessing import MinMaxScaler
9
 
10
+ def assign_main_accounts(creators_file, chatter_files):
11
+
12
+ creators = pd.read_excel(creators_file)
13
+ creators.columns = creators.columns.str.strip()
14
+
15
+ column_mapping = {
16
+ "Creator": "Creator",
17
+ "Total earnings": "Total earnings",
18
+ "Subscription": "Subscription",
19
+ "Active Fans": "ActiveFans",
20
+ "Total active fans": "ActiveFans",
21
+ }
22
+ creators.rename(columns={k: v for k, v in column_mapping.items() if k in creators.columns}, inplace=True)
23
+
24
+ required_columns = ["Creator", "Total earnings", "Subscription", "ActiveFans"]
25
+ missing_columns = [col for col in required_columns if col not in creators.columns]
26
+ if missing_columns:
27
+ raise KeyError(f"Missing required columns in creators file: {missing_columns}")
28
+
29
+
30
+ creators["Total earnings"] = creators["Total earnings"].replace("[\$,]", "", regex=True).astype(float)
31
+ creators["Subscription"] = creators["Subscription"].replace("[\$,]", "", regex=True).astype(float)
32
+ creators["ActiveFans"] = pd.to_numeric(creators["ActiveFans"], errors="coerce").fillna(0)
33
+
34
+
35
+ scaler = MinMaxScaler()
36
+ creators[["Earnings_Normalized", "Subscriptions_Normalized"]] = scaler.fit_transform(
37
+ creators[["Total earnings", "Subscription"]]
38
+ 0.7 * creators["Earnings_Normalized"] + 0.3 * creators["Subscriptions_Normalized"]
39
+ ) * creators["Penalty Factor"]
40
+ creators["Rank"] = creators["Score"].rank(ascending=False)
41
+
42
+
43
+ creators = creators.sort_values(by="Rank").reset_index(drop=True)
44
+
45
+
46
+ assignments = {}
47
+ for idx, chatter_file in enumerate(chatter_files):
48
+ shift_name = ["overnight", "day", "prime"][idx]
49
+ if "Final Rating" not in chatters.columns:
50
+ raise KeyError(f"'Final Rating' column is missing in {chatter_file}")
51
+
52
+
53
+ chatters = chatters.sort_values(by="Final Rating", ascending=False).reset_index(drop=True)
54
+
55
+
56
+ num_chatters = len(chatters)
57
+ creators_to_assign = creators.iloc[:num_chatters]
58
+ chatters["Main Account"] = creators_to_assign["Creator"].values
59
+
60
+
61
+ assignments[shift_name] = chatters.to_dict(orient="records")
62
+
63
+ assignments["creator_names"] = creators["Creator"].tolist()
64
+ print("DEBUG: Chatter Data with Main Account Assignments:")
65
+ print(chatters.head())
66
+
67
+
68
+ return assignments
69
 
70
 
71