Updated app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,95 @@
|
|
| 1 |
-
# STEP 1: MUST BE THE FIRST MACHINE LEARNING IMPORT
|
| 2 |
-
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
-
import pandas as pd
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
from app.db.repo import init_db
|
| 8 |
-
from app.ui.dashboard import
|
|
|
|
| 9 |
from app.controller.session_loader import load_google_ads_data
|
| 10 |
-
|
| 11 |
from app.ads1.ads_analyst import run_ads_analyst_card
|
| 12 |
from app.ads1.budget_optimizer import run_budget_optimizer_card
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
dfs = load_google_ads_data()
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
]
|
| 23 |
-
return filtered
|
| 24 |
|
| 25 |
-
# STEP 2: DECORATE THE GPU-INTENSIVE FUNCTIONS
|
| 26 |
-
@spaces.GPU()
|
| 27 |
def run_ads_card(state):
|
| 28 |
if not state:
|
| 29 |
-
return "β οΈ
|
| 30 |
-
return run_ads_analyst_card(state["
|
| 31 |
|
| 32 |
-
@spaces.GPU()
|
| 33 |
def run_budget_card(state):
|
| 34 |
if not state:
|
| 35 |
-
return "β οΈ
|
| 36 |
-
return run_budget_optimizer_card(state["
|
| 37 |
|
| 38 |
-
def campaign_row_selected(evt: gr.SelectData):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"dfs": dfs
|
| 50 |
-
},
|
| 51 |
-
f"## π Selected Campaign: {campaign_name}"
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
# GRADIO APP
|
| 55 |
-
with gr.Blocks(title="Ads Assistant") as demo:
|
| 56 |
campaign_state = gr.State()
|
| 57 |
-
gr.
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
with gr.Tab("Dashboard"):
|
| 61 |
-
campaign_table = build_dashboard()
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
inputs=campaign_state,
|
| 75 |
-
outputs=output
|
| 76 |
-
)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
)
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
#
|
|
|
|
|
|
|
| 85 |
campaign_table.select(
|
| 86 |
fn=campaign_row_selected,
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
|
|
|
| 3 |
from app.db.repo import init_db
|
| 4 |
+
from app.ui.dashboard import build_dashboard, get_dashboard_data
|
| 5 |
+
from app.controller.campaign_controller import on_campaign_select
|
| 6 |
from app.controller.session_loader import load_google_ads_data
|
|
|
|
| 7 |
from app.ads1.ads_analyst import run_ads_analyst_card
|
| 8 |
from app.ads1.budget_optimizer import run_budget_optimizer_card
|
| 9 |
|
| 10 |
+
def startup():
|
| 11 |
+
try:
|
| 12 |
+
init_db()
|
| 13 |
+
print("β
DB initialized successfully")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print("β οΈ DB init failed:", e)
|
| 16 |
|
| 17 |
+
startup()
|
| 18 |
+
|
| 19 |
+
# UI Optimization: Fetch data AFTER UI elements are drawn
|
| 20 |
+
def initial_data_load():
|
| 21 |
+
print("π App loaded. Population of background states initiated...")
|
| 22 |
dfs = load_google_ads_data()
|
| 23 |
+
# Unpack dashboard metrics to fill the UI immediately
|
| 24 |
+
spend, leads, cpl, count, formatted_df = get_dashboard_data()
|
| 25 |
+
return dfs, formatted_df, spend, leads, cpl, count
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
| 27 |
def run_ads_card(state):
|
| 28 |
if not state:
|
| 29 |
+
return "β οΈ Select a campaign from the Dashboard tab first."
|
| 30 |
+
return run_ads_analyst_card(state["full_dfs"])
|
| 31 |
|
|
|
|
| 32 |
def run_budget_card(state):
|
| 33 |
if not state:
|
| 34 |
+
return "β οΈ Select a campaign from the Dashboard tab first."
|
| 35 |
+
return run_budget_optimizer_card(state["full_dfs"])
|
| 36 |
|
| 37 |
+
def campaign_row_selected(evt: gr.SelectData, df, full_state):
|
| 38 |
+
if df.empty or full_state is None:
|
| 39 |
+
return gr.State(), "β οΈ Data state is missing. Please click Refresh."
|
| 40 |
+
row_index = evt.index[0]
|
| 41 |
+
campaign_name = df.iloc[row_index]["Campaign"]
|
| 42 |
+
campaign_state = on_campaign_select(full_state, campaign_name)
|
| 43 |
+
return campaign_state, f"## π Selected Campaign: {campaign_name}"
|
| 44 |
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
# Initialize components empty; populated safely via demo.load
|
| 47 |
+
full_state = gr.State()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
campaign_state = gr.State()
|
| 49 |
+
df_state = gr.State()
|
| 50 |
|
| 51 |
+
gr.Markdown("# π― Ads Dashboard")
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
with gr.Tab("Dashboard"):
|
| 54 |
+
# We modify build_dashboard to expose metric components for automated hydration
|
| 55 |
+
gr.Markdown("## π Campaign Dashboard")
|
| 56 |
+
with gr.Row():
|
| 57 |
+
total_spend = gr.Number(label="Total Spend")
|
| 58 |
+
total_leads = gr.Number(label="Total Leads")
|
| 59 |
+
average_cpl = gr.Number(label="Average CPL")
|
| 60 |
+
active_campaigns = gr.Number(label="Active Campaigns")
|
| 61 |
|
| 62 |
+
campaign_table = gr.Dataframe(label="Campaign Performance", interactive=True)
|
| 63 |
+
refresh_btn = gr.Button("π Force Refresh Data")
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
with gr.Tab("Analysis"):
|
| 66 |
+
selected = gr.Markdown("π Select a campaign from the Dashboard tab")
|
| 67 |
+
output = gr.Markdown()
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
gr.Button("π Run Ads Analysis").click(run_ads_card, campaign_state, output)
|
| 71 |
+
gr.Button("π° Run Budget Optimization").click(run_budget_card, campaign_state, output)
|
| 72 |
|
| 73 |
+
# Core Event Bindings
|
| 74 |
+
campaign_table.change(fn=lambda x: x, inputs=[campaign_table], outputs=df_state)
|
| 75 |
+
|
| 76 |
campaign_table.select(
|
| 77 |
fn=campaign_row_selected,
|
| 78 |
+
inputs=[df_state, full_state],
|
| 79 |
+
outputs=[campaign_state, selected]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Button manual refresh
|
| 83 |
+
refresh_btn.click(
|
| 84 |
+
fn=initial_data_load,
|
| 85 |
+
outputs=[full_state, campaign_table, total_spend, total_leads, average_cpl, active_campaigns]
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# β‘ MAGIC FIX: App automatically loads data into UI components instantly on launch
|
| 89 |
+
demo.load(
|
| 90 |
+
fn=initial_data_load,
|
| 91 |
+
outputs=[full_state, campaign_table, total_spend, total_leads, average_cpl, active_campaigns]
|
| 92 |
)
|
| 93 |
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|