Em4e commited on
Commit
60dbb1d
·
verified ·
1 Parent(s): 7513fc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -25
app.py CHANGED
@@ -9,26 +9,22 @@ SAMPLE_FILE_URL = (
9
  "resolve/main/sample_keyword_data_cpc.csv"
10
  )
11
 
12
- # --- 1. Data Loading and Preprocessing ---
13
- class DataLoader:
14
- def __init__(self, sample_file_url: str = SAMPLE_FILE_URL):
15
- self.sample_file_url = sample_file_url
16
-
17
- @st.cache_data
18
- def load_csv(_self, uploaded_file_obj: st.runtime.uploaded_file_manager.UploadedFile | None) -> pd.DataFrame | None:
19
- try:
20
- if uploaded_file_obj:
21
- df = pd.read_csv(uploaded_file_obj)
22
- else:
23
- df = pd.read_csv(_self.sample_file_url)
24
- except Exception as e:
25
- st.error(f"Error loading file: {e}")
26
- return None
27
- df.columns = [col.strip().lower() for col in df.columns]
28
- if "cpc" not in df.columns:
29
- st.warning("No `cpc` column found—simulating CPC values between 0.50–3.00 USD (for testing purposes only!)")
30
- df["cpc"] = np.round(np.random.uniform(0.5, 3.0, size=len(df)), 2)
31
- return df
32
 
33
  # --- 2. SEO Forecasting Core ---
34
  class SeoCalculator:
@@ -109,8 +105,7 @@ class SeoCalculator:
109
 
110
  # --- 3. Streamlit App UI ---
111
  class SeoAppUI:
112
- def __init__(self, data_loader: DataLoader, seo_calculator: SeoCalculator):
113
- self.data_loader = data_loader
114
  self.seo_calculator = seo_calculator
115
  self._set_page_config()
116
 
@@ -179,7 +174,7 @@ class SeoAppUI:
179
  st.download_button("📥 Download sample CSV", sample_bytes, file_name="sample_keyword_data_cpc.csv", mime="text/csv", key="download_sample_main")
180
  st.markdown("---")
181
  uploaded_file, target_position, conversion_rate, close_rate, mrr_per_customer, seo_cost, add_spend = self._get_sidebar_inputs()
182
- df = self.data_loader.load_csv(uploaded_file)
183
  if df is not None:
184
  metrics, df_results = self.seo_calculator.calculate_metrics(
185
  df, target_position, conversion_rate, close_rate, mrr_per_customer, seo_cost, add_spend
@@ -191,7 +186,6 @@ class SeoAppUI:
191
 
192
  # --- App Runner ---
193
  if __name__ == "__main__":
194
- data_loader = DataLoader()
195
  seo_calculator = SeoCalculator()
196
- app_ui = SeoAppUI(data_loader, seo_calculator)
197
  app_ui.run()
 
9
  "resolve/main/sample_keyword_data_cpc.csv"
10
  )
11
 
12
+ # --- CACHED CSV LOADER FUNCTION (moved out of class) ---
13
+ @st.cache_data
14
+ def load_csv(uploaded_file_obj: st.runtime.uploaded_file_manager.UploadedFile | None, sample_file_url: str) -> pd.DataFrame | None:
15
+ try:
16
+ if uploaded_file_obj:
17
+ df = pd.read_csv(uploaded_file_obj)
18
+ else:
19
+ df = pd.read_csv(sample_file_url)
20
+ except Exception as e:
21
+ st.error(f"Error loading file: {e}")
22
+ return None
23
+ df.columns = [col.strip().lower() for col in df.columns]
24
+ if "cpc" not in df.columns:
25
+ st.warning("No `cpc` column found—simulating CPC values between 0.50–3.00 USD (for testing purposes only!)")
26
+ df["cpc"] = np.round(np.random.uniform(0.5, 3.0, size=len(df)), 2)
27
+ return df
 
 
 
 
28
 
29
  # --- 2. SEO Forecasting Core ---
30
  class SeoCalculator:
 
105
 
106
  # --- 3. Streamlit App UI ---
107
  class SeoAppUI:
108
+ def __init__(self, seo_calculator: SeoCalculator):
 
109
  self.seo_calculator = seo_calculator
110
  self._set_page_config()
111
 
 
174
  st.download_button("📥 Download sample CSV", sample_bytes, file_name="sample_keyword_data_cpc.csv", mime="text/csv", key="download_sample_main")
175
  st.markdown("---")
176
  uploaded_file, target_position, conversion_rate, close_rate, mrr_per_customer, seo_cost, add_spend = self._get_sidebar_inputs()
177
+ df = load_csv(uploaded_file, SAMPLE_FILE_URL)
178
  if df is not None:
179
  metrics, df_results = self.seo_calculator.calculate_metrics(
180
  df, target_position, conversion_rate, close_rate, mrr_per_customer, seo_cost, add_spend
 
186
 
187
  # --- App Runner ---
188
  if __name__ == "__main__":
 
189
  seo_calculator = SeoCalculator()
190
+ app_ui = SeoAppUI(seo_calculator)
191
  app_ui.run()