Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,26 +9,22 @@ SAMPLE_FILE_URL = (
|
|
| 9 |
"resolve/main/sample_keyword_data_cpc.csv"
|
| 10 |
)
|
| 11 |
|
| 12 |
-
# ---
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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,
|
| 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 =
|
| 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(
|
| 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()
|