ps1811 commited on
Commit
5b764dc
·
verified ·
1 Parent(s): 3558379

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -109
app.py CHANGED
@@ -1,112 +1,103 @@
1
- # import gradio as gr
2
- # import pandas as pd
3
- # # from dotenv import load_dotenv
4
- # # load_dotenv()
5
-
6
- # # becoz of deployment on hf space - above 2 lines replaced as below:
7
- # import os
8
-
9
- # from app.db.repo import init_db
10
- # from app.ui.dashboard import load_dashboard, build_dashboard
11
- # from app.controller.session_loader import load_google_ads_data
12
-
13
- # from app.ads1.ads_analyst import run_ads_analyst_card
14
- # from app.ads1.budget_optimizer import run_budget_optimizer_card
15
-
16
-
17
- # init_db()
18
-
19
- # # HELPERS
20
- # def on_campaign_select(campaign_name):
21
- # dfs = load_google_ads_data()
22
-
23
- # filtered = dfs.copy()
24
- # filtered["campaigns"] = dfs["campaigns"][
25
- # dfs["campaigns"]["name"] == campaign_name
26
- # ]
27
- # return filtered
28
-
29
- # def run_ads_card(state):
30
- # if not state:
31
- # return "⚠️ Please select a campaign from the Dashboard first."
32
- # return run_ads_analyst_card(state["dfs"])
33
-
34
- # def run_budget_card(state):
35
- # if not state:
36
- # return "⚠️ Please select a campaign from the Dashboard first."
37
- # return run_budget_optimizer_card(state["dfs"])
38
-
39
- # def campaign_row_selected(evt: gr.SelectData):
40
- # """
41
- # Triggered when user clicks a row in the dashboard table
42
- # """
43
-
44
- # df = load_dashboard()[4] # campaign table returned by load_dashboard()
45
- # campaign_name = df.iloc[evt.index[0]]["Campaign"]
46
- # dfs = on_campaign_select(campaign_name)
47
-
48
- # return (
49
- # {
50
- # "campaign_name": campaign_name,
51
- # "dfs": dfs
52
- # },
53
- # f"## 📊 Selected Campaign: {campaign_name}"
54
- # )
55
-
56
- # # GRADIO APP
57
- # with gr.Blocks(title="Ads Assistant") as demo:
58
-
59
- # campaign_state = gr.State()
60
- # gr.Markdown("# 🎯 Preschool Ads Dashboard")
61
-
62
- # # TAB 1: DASHBOARD
63
- # # -------------------------
64
- # with gr.Tab("Dashboard"):
65
-
66
- # campaign_table = build_dashboard()
67
-
68
- # # TAB 2: CAMPAIGN ANALYSIS
69
- # # -------------------------
70
- # with gr.Tab("Campaign Analysis"):
71
-
72
- # selected_campaign = gr.Markdown(
73
- # "👈 Select a campaign from the Dashboard tab"
74
- # )
75
-
76
- # analyst_btn = gr.Button("Run Ads Analysis")
77
- # budget_btn = gr.Button("Run Budget Optimization")
78
-
79
- # output = gr.Markdown()
80
-
81
- # analyst_btn.click(
82
- # fn=run_ads_card,
83
- # inputs=campaign_state,
84
- # outputs=output
85
- # )
86
-
87
- # budget_btn.click(
88
- # fn=run_budget_card,
89
- # inputs=campaign_state,
90
- # outputs=output
91
- # )
92
-
93
- # # CONNECT TABLE CLICK → STATE
94
- # # -------------------------
95
- # campaign_table.select(
96
- # fn=campaign_row_selected,
97
- # outputs=[
98
- # campaign_state,
99
- # selected_campaign
100
- # ]
101
- # )
102
-
103
- # demo.launch()
104
-
105
  import gradio as gr
106
-
107
- demo = gr.Blocks()
108
-
109
- with demo:
110
- gr.Markdown("Hello")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ # from dotenv import load_dotenv
4
+ # load_dotenv()
5
+
6
+ # becoz of deployment on hf space - above 2 lines replaced as below:
7
+ import os
8
+
9
+ from app.db.repo import init_db
10
+ from app.ui.dashboard import load_dashboard, build_dashboard
11
+ from app.controller.session_loader import load_google_ads_data
12
+
13
+ from app.ads1.ads_analyst import run_ads_analyst_card
14
+ from app.ads1.budget_optimizer import run_budget_optimizer_card
15
+
16
+
17
+ init_db()
18
+
19
+ # HELPERS
20
+ def on_campaign_select(campaign_name):
21
+ dfs = load_google_ads_data()
22
+
23
+ filtered = dfs.copy()
24
+ filtered["campaigns"] = dfs["campaigns"][
25
+ dfs["campaigns"]["name"] == campaign_name
26
+ ]
27
+ return filtered
28
+
29
+ def run_ads_card(state):
30
+ if not state:
31
+ return "⚠️ Please select a campaign from the Dashboard first."
32
+ return run_ads_analyst_card(state["dfs"])
33
+
34
+ def run_budget_card(state):
35
+ if not state:
36
+ return "⚠️ Please select a campaign from the Dashboard first."
37
+ return run_budget_optimizer_card(state["dfs"])
38
+
39
+ def campaign_row_selected(evt: gr.SelectData):
40
+ """
41
+ Triggered when user clicks a row in the dashboard table
42
+ """
43
+
44
+ df = load_dashboard()[4] # campaign table returned by load_dashboard()
45
+ campaign_name = df.iloc[evt.index[0]]["Campaign"]
46
+ dfs = on_campaign_select(campaign_name)
47
+
48
+ return (
49
+ {
50
+ "campaign_name": campaign_name,
51
+ "dfs": dfs
52
+ },
53
+ f"## 📊 Selected Campaign: {campaign_name}"
54
+ )
55
+
56
+ # GRADIO APP
57
+ with gr.Blocks(title="Ads Assistant") as demo:
58
+
59
+ campaign_state = gr.State()
60
+ gr.Markdown("# 🎯 Preschool Ads Dashboard")
61
+
62
+ # TAB 1: DASHBOARD
63
+ # -------------------------
64
+ with gr.Tab("Dashboard"):
65
+
66
+ campaign_table = build_dashboard()
67
+
68
+ # TAB 2: CAMPAIGN ANALYSIS
69
+ # -------------------------
70
+ with gr.Tab("Campaign Analysis"):
71
+
72
+ selected_campaign = gr.Markdown(
73
+ "👈 Select a campaign from the Dashboard tab"
74
+ )
75
+
76
+ analyst_btn = gr.Button("Run Ads Analysis")
77
+ budget_btn = gr.Button("Run Budget Optimization")
78
+
79
+ output = gr.Markdown()
80
+
81
+ analyst_btn.click(
82
+ fn=run_ads_card,
83
+ inputs=campaign_state,
84
+ outputs=output
85
+ )
86
+
87
+ budget_btn.click(
88
+ fn=run_budget_card,
89
+ inputs=campaign_state,
90
+ outputs=output
91
+ )
92
+
93
+ # CONNECT TABLE CLICK → STATE
94
+ # -------------------------
95
+ campaign_table.select(
96
+ fn=campaign_row_selected,
97
+ outputs=[
98
+ campaign_state,
99
+ selected_campaign
100
+ ]
101
+ )
102
 
103
  demo.launch(server_name="0.0.0.0", server_port=7860)