Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,16 +2,15 @@ import marimo
|
|
2 |
import pandas as pd
|
3 |
import io
|
4 |
import matplotlib.pyplot as plt
|
5 |
-
from script import getSearchResult, getClustersWithGraph, compare_clusters
|
6 |
import os
|
|
|
7 |
|
8 |
app = marimo.App()
|
9 |
|
10 |
-
# 1
|
11 |
@app.cell
|
12 |
-
def
|
13 |
csv_upload = mo.ui.file(label="Upload Keyword CSV (1 column)")
|
14 |
-
# pull secrets from env (set in HF Space Secrets)
|
15 |
api_key = os.environ.get("GOOGLE_API_KEY", "")
|
16 |
cse_id = os.environ.get("GOOGLE_CSE_ID", "")
|
17 |
country = mo.ui.text(label="Country Code (e.g. UK)", value="UK")
|
@@ -22,9 +21,9 @@ def _(mo):
|
|
22 |
run_button = mo.ui.button(label="Run Clustering Comparison")
|
23 |
return csv_upload, api_key, cse_id, country, language, database, serp_table, cluster_table, run_button
|
24 |
|
25 |
-
# 2
|
26 |
@app.cell
|
27 |
-
def
|
28 |
timestamp_options = [
|
29 |
("Latest available (max)", "max"),
|
30 |
("March 2024 core update (2024-04-26 00:00:00.000000)", "2024-04-26 00:00:00.000000"),
|
@@ -38,43 +37,41 @@ def _(mo):
|
|
38 |
timestamp_2 = mo.ui.dropdown(timestamp_options, label="Choose Timestamp 2")
|
39 |
return timestamp_1, timestamp_2
|
40 |
|
41 |
-
# 3
|
42 |
@app.cell
|
43 |
-
def
|
44 |
-
if csv_upload.value:
|
45 |
df_keywords = pd.read_csv(io.BytesIO(csv_upload.value.read()))
|
46 |
keywords = df_keywords.iloc[:, 0].tolist()
|
47 |
-
|
48 |
-
|
49 |
-
return df_keywords, keywords
|
50 |
|
51 |
-
# 4
|
52 |
@app.cell
|
53 |
-
def
|
54 |
if run_button.clicked and keywords:
|
55 |
-
#
|
56 |
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
57 |
fig1, clusters1 = getClustersWithGraph(database, serp_table, timestamp_1.value)
|
58 |
-
|
59 |
-
# 2nd timestamp data fetch + cluster
|
60 |
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
61 |
fig2, clusters2 = getClustersWithGraph(database, serp_table, timestamp_2.value)
|
62 |
-
|
63 |
movement = compare_clusters(clusters1, clusters2)
|
64 |
return fig1, fig2, movement
|
65 |
return None, None, None
|
66 |
|
67 |
-
# 5
|
68 |
@app.cell
|
69 |
-
def
|
70 |
-
if fig1 and fig2:
|
71 |
display(fig1)
|
72 |
display(fig2)
|
73 |
return None
|
74 |
|
75 |
-
# 6
|
76 |
@app.cell
|
77 |
-
def
|
78 |
if movement is not None:
|
79 |
movement.sort_values(by="searchTerms", inplace=True)
|
80 |
movement.reset_index(drop=True, inplace=True)
|
|
|
2 |
import pandas as pd
|
3 |
import io
|
4 |
import matplotlib.pyplot as plt
|
|
|
5 |
import os
|
6 |
+
from script import getSearchResult, getClustersWithGraph, compare_clusters
|
7 |
|
8 |
app = marimo.App()
|
9 |
|
10 |
+
# Cell 1: Upload + Config
|
11 |
@app.cell
|
12 |
+
def cell_config(mo):
|
13 |
csv_upload = mo.ui.file(label="Upload Keyword CSV (1 column)")
|
|
|
14 |
api_key = os.environ.get("GOOGLE_API_KEY", "")
|
15 |
cse_id = os.environ.get("GOOGLE_CSE_ID", "")
|
16 |
country = mo.ui.text(label="Country Code (e.g. UK)", value="UK")
|
|
|
21 |
run_button = mo.ui.button(label="Run Clustering Comparison")
|
22 |
return csv_upload, api_key, cse_id, country, language, database, serp_table, cluster_table, run_button
|
23 |
|
24 |
+
# Cell 2: Timestamp Selectors
|
25 |
@app.cell
|
26 |
+
def cell_timestamps(mo):
|
27 |
timestamp_options = [
|
28 |
("Latest available (max)", "max"),
|
29 |
("March 2024 core update (2024-04-26 00:00:00.000000)", "2024-04-26 00:00:00.000000"),
|
|
|
37 |
timestamp_2 = mo.ui.dropdown(timestamp_options, label="Choose Timestamp 2")
|
38 |
return timestamp_1, timestamp_2
|
39 |
|
40 |
+
# Cell 3: Read CSV
|
41 |
@app.cell
|
42 |
+
def cell_read_csv(csv_upload):
|
43 |
+
if csv_upload is not None and csv_upload.value:
|
44 |
df_keywords = pd.read_csv(io.BytesIO(csv_upload.value.read()))
|
45 |
keywords = df_keywords.iloc[:, 0].tolist()
|
46 |
+
return df_keywords, keywords
|
47 |
+
return None, []
|
|
|
48 |
|
49 |
+
# Cell 4: Run Search, Cluster, Compare
|
50 |
@app.cell
|
51 |
+
def cell_run(keywords, api_key, cse_id, country, language, database, serp_table, timestamp_1, timestamp_2, run_button):
|
52 |
if run_button.clicked and keywords:
|
53 |
+
# First timestamp cluster
|
54 |
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
55 |
fig1, clusters1 = getClustersWithGraph(database, serp_table, timestamp_1.value)
|
56 |
+
# Second timestamp cluster
|
|
|
57 |
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
58 |
fig2, clusters2 = getClustersWithGraph(database, serp_table, timestamp_2.value)
|
59 |
+
# Compare
|
60 |
movement = compare_clusters(clusters1, clusters2)
|
61 |
return fig1, fig2, movement
|
62 |
return None, None, None
|
63 |
|
64 |
+
# Cell 5: Display Graphs
|
65 |
@app.cell
|
66 |
+
def cell_display_graphs(fig1, fig2):
|
67 |
+
if fig1 is not None and fig2 is not None:
|
68 |
display(fig1)
|
69 |
display(fig2)
|
70 |
return None
|
71 |
|
72 |
+
# Cell 6: Show Movement Table
|
73 |
@app.cell
|
74 |
+
def cell_display_movement(movement):
|
75 |
if movement is not None:
|
76 |
movement.sort_values(by="searchTerms", inplace=True)
|
77 |
movement.reset_index(drop=True, inplace=True)
|