3morrrrr commited on
Commit
6316636
·
verified ·
1 Parent(s): 0caadca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -55
app.py CHANGED
@@ -54,68 +54,39 @@ def generate_main_accounts(updated_data=None):
54
  return "Missing required files. Please upload all necessary files.", None
55
 
56
  try:
57
- if updated_data is not None:
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()
67
- ]
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
- # Debugging: Verify creators file before processing
79
- print("DEBUG: Original Creators File Columns")
80
- creators_df = pd.read_excel(creators_file)
81
- print(creators_df.columns)
82
-
83
- # Ensure creators file has correct columns
84
- column_mapping = {
85
- "Creator": "Creator",
86
- "Total earnings": "Total earnings",
87
- "Subscription": "Subscription",
88
- "Active Fans": "ActiveFans",
89
- "Total active fans": "ActiveFans",
90
  }
91
- creators_df.rename(columns={k: v for k, v in column_mapping.items() if k in creators_df.columns}, inplace=True)
92
-
93
- # Debugging: Verify creators file after renaming
94
- print("DEBUG: Renamed Creators File Columns")
95
- print(creators_df.columns)
96
-
97
- required_columns = ["Creator", "ActiveFans"]
98
- missing_columns = [col for col in required_columns if col not in creators_df.columns]
99
- if missing_columns:
100
- raise KeyError(f"Missing required columns in creators file: {missing_columns}")
101
 
102
- # Save the processed creators file
103
- creators_df.to_excel(os.path.join(PROCESSED_FOLDER, "creators_file.xlsx"), index=False)
104
- main_assignments = assign_main_accounts(creators_file, chatter_files)
105
- UPDATED_ASSIGNMENTS = main_assignments
106
- save_processed_files(main_assignments, PROCESSED_FOLDER)
107
 
108
- # Combine all shifts into a single DataFrame for preview
109
  preview_data = []
110
- for shift, data in main_assignments.items():
111
- if shift != "creator_names": # Skip creator names
112
- for record in data:
113
- record["Shift"] = shift.capitalize()
114
- preview_data.append(record)
115
 
116
- preview_df = pd.DataFrame(preview_data)
117
  return "Main accounts generated successfully!", preview_df
 
118
  except Exception as e:
 
119
  return f"Error during main account generation: {e}", None
120
 
121
 
@@ -124,6 +95,7 @@ def generate_main_accounts(updated_data=None):
124
 
125
 
126
 
 
127
  # Global variable to store updated assignments from the main account generation
128
  UPDATED_ASSIGNMENTS = {}
129
 
 
54
  return "Missing required files. Please upload all necessary files.", None
55
 
56
  try:
57
+ # Step 1: Assign main accounts
58
+ updated_chatter_files, account_data = assign_main_accounts(creators_file, chatter_files)
59
+ print("DEBUG: Updated Chatter Files and Account Data Successfully Generated")
60
+
61
+ # Save processed files
62
+ UPDATED_ASSIGNMENTS = {
63
+ "chatter_files": updated_chatter_files,
64
+ "account_data": account_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  }
66
+ for idx, chatter_df in enumerate(updated_chatter_files):
67
+ chatter_df.to_excel(
68
+ os.path.join(PROCESSED_FOLDER, f"Updated_{['overnight', 'day', 'prime'][idx]}_file.xlsx"),
69
+ index=False
70
+ )
71
+ account_data.to_excel(os.path.join(PROCESSED_FOLDER, "creators_file.xlsx"), index=False)
 
 
 
 
72
 
73
+ # Save Creator and ActiveFans to a separate file
74
+ creator_output_file = os.path.join(PROCESSED_FOLDER, "Creators_ActiveFans.xlsx")
75
+ account_data.to_excel(creator_output_file, index=False)
76
+ print(f"DEBUG: Saved Creator-ActiveFans data to {creator_output_file}")
 
77
 
78
+ # Step 2: Combine for preview
79
  preview_data = []
80
+ shift_names = ["Overnight", "Day", "Prime"]
81
+ for idx, chatter_df in enumerate(updated_chatter_files):
82
+ chatter_df["Shift"] = shift_names[idx]
83
+ preview_data.append(chatter_df)
 
84
 
85
+ preview_df = pd.concat(preview_data, ignore_index=True)
86
  return "Main accounts generated successfully!", preview_df
87
+
88
  except Exception as e:
89
+ print(f"Error during main account generation: {e}")
90
  return f"Error during main account generation: {e}", None
91
 
92
 
 
95
 
96
 
97
 
98
+
99
  # Global variable to store updated assignments from the main account generation
100
  UPDATED_ASSIGNMENTS = {}
101