Spaces:
Sleeping
Sleeping
poemsforaphrodite
commited on
Commit
•
2e56ea4
1
Parent(s):
e022de0
Update app.py
Browse files
app.py
CHANGED
@@ -19,6 +19,7 @@ import urllib.parse
|
|
19 |
import openai
|
20 |
from openai import OpenAI
|
21 |
import re
|
|
|
22 |
|
23 |
|
24 |
load_dotenv()
|
@@ -58,6 +59,7 @@ DEVICE_OPTIONS = ["All Devices", "desktop", "mobile", "tablet"]
|
|
58 |
BASE_DIMENSIONS = ["page", "query", "country", "date"]
|
59 |
MAX_ROWS = 250_000
|
60 |
DF_PREVIEW_ROWS = 100
|
|
|
61 |
|
62 |
# -------------
|
63 |
# Streamlit App Configuration
|
@@ -106,7 +108,7 @@ def generate_embeddings(text_list, model_type):
|
|
106 |
return embeddings
|
107 |
|
108 |
|
109 |
-
def get_serp_results(query):
|
110 |
if not APIFY_API_TOKEN:
|
111 |
st.error("Apify API token is not set. Unable to fetch SERP results.")
|
112 |
return []
|
@@ -121,6 +123,7 @@ def get_serp_results(query):
|
|
121 |
"saveHtml": False,
|
122 |
"saveHtmlToKeyValueStore": False,
|
123 |
"includeIcons": False,
|
|
|
124 |
}
|
125 |
|
126 |
try:
|
@@ -208,12 +211,12 @@ def calculate_relevance_score(page_content, query, co):
|
|
208 |
st.error(f"Error calculating relevance score: {str(e)}")
|
209 |
return 0
|
210 |
|
211 |
-
def analyze_competitors(row, co, custom_url=None):
|
212 |
-
|
213 |
query = row['query']
|
214 |
our_url = row['page']
|
215 |
|
216 |
-
competitor_data = get_serp_results(query)
|
217 |
|
218 |
if custom_url and custom_url not in [data['url'] for data in competitor_data]:
|
219 |
custom_content = fetch_content(custom_url, query)
|
@@ -232,11 +235,11 @@ def analyze_competitors(row, co, custom_url=None):
|
|
232 |
|
233 |
return results_df
|
234 |
|
235 |
-
def show_competitor_analysis(row, co):
|
236 |
if st.button("Check Competitors", key=f"comp_{row['page']}"):
|
237 |
-
|
238 |
with st.spinner('Analyzing competitors...'):
|
239 |
-
results_df = analyze_competitors(row, co)
|
240 |
st.write("Relevancy Score Comparison:")
|
241 |
st.dataframe(results_df)
|
242 |
|
@@ -489,7 +492,7 @@ def calculate_single_relevancy(row):
|
|
489 |
score = calculate_relevance_score(page_content, query, co)
|
490 |
return score
|
491 |
|
492 |
-
def show_tabular_data(df, co):
|
493 |
st.write("Data Table with Relevancy Scores")
|
494 |
|
495 |
# Pagination
|
@@ -559,7 +562,7 @@ def show_tabular_data(df, co):
|
|
559 |
if competitor_button:
|
560 |
st.write(f"Competitor Analysis for: {row.query}")
|
561 |
with st.spinner('Analyzing competitors...'):
|
562 |
-
results_df = analyze_competitors(row._asdict(), co)
|
563 |
|
564 |
# Sort the results by relevancy score in descending order
|
565 |
results_df = results_df.sort_values('relevancy_score', ascending=False).reset_index(drop=True)
|
@@ -710,6 +713,16 @@ def main():
|
|
710 |
search_type = show_search_type_selector()
|
711 |
date_range_selection = show_date_range_selector()
|
712 |
model_type = show_model_type_selector()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
713 |
if date_range_selection == 'Custom Range':
|
714 |
show_custom_date_inputs()
|
715 |
start_date, end_date = st.session_state.custom_start_date, st.session_state.custom_end_date
|
@@ -728,11 +741,11 @@ def main():
|
|
728 |
if st.session_state.report_data is not None and not st.session_state.report_data.empty:
|
729 |
st.write("Data fetched successfully.")
|
730 |
|
731 |
-
st.session_state.report_data = show_tabular_data(st.session_state.report_data, co)
|
732 |
|
733 |
download_csv_link(st.session_state.report_data)
|
734 |
elif st.session_state.report_data is not None:
|
735 |
-
|
736 |
st.warning("No data found for the selected criteria.")
|
737 |
|
738 |
if __name__ == "__main__":
|
|
|
19 |
import openai
|
20 |
from openai import OpenAI
|
21 |
import re
|
22 |
+
import pycountry
|
23 |
|
24 |
|
25 |
load_dotenv()
|
|
|
59 |
BASE_DIMENSIONS = ["page", "query", "country", "date"]
|
60 |
MAX_ROWS = 250_000
|
61 |
DF_PREVIEW_ROWS = 100
|
62 |
+
COUNTRY_OPTIONS = sorted([(country.alpha_2, country.name) for country in pycountry.countries], key=lambda x: x[1])
|
63 |
|
64 |
# -------------
|
65 |
# Streamlit App Configuration
|
|
|
108 |
return embeddings
|
109 |
|
110 |
|
111 |
+
def get_serp_results(query, country_code):
|
112 |
if not APIFY_API_TOKEN:
|
113 |
st.error("Apify API token is not set. Unable to fetch SERP results.")
|
114 |
return []
|
|
|
123 |
"saveHtml": False,
|
124 |
"saveHtmlToKeyValueStore": False,
|
125 |
"includeIcons": False,
|
126 |
+
"countryCode": country_code, # Add this line
|
127 |
}
|
128 |
|
129 |
try:
|
|
|
211 |
st.error(f"Error calculating relevance score: {str(e)}")
|
212 |
return 0
|
213 |
|
214 |
+
def analyze_competitors(row, co, custom_url=None, country_code=None):
|
215 |
+
# logger.info(f"Analyzing competitors for query: {row['query']}")
|
216 |
query = row['query']
|
217 |
our_url = row['page']
|
218 |
|
219 |
+
competitor_data = get_serp_results(query, country_code)
|
220 |
|
221 |
if custom_url and custom_url not in [data['url'] for data in competitor_data]:
|
222 |
custom_content = fetch_content(custom_url, query)
|
|
|
235 |
|
236 |
return results_df
|
237 |
|
238 |
+
def show_competitor_analysis(row, co, country_code):
|
239 |
if st.button("Check Competitors", key=f"comp_{row['page']}"):
|
240 |
+
# logger.info(f"Competitor analysis requested for page: {row['page']}")
|
241 |
with st.spinner('Analyzing competitors...'):
|
242 |
+
results_df = analyze_competitors(row, co, country_code=country_code)
|
243 |
st.write("Relevancy Score Comparison:")
|
244 |
st.dataframe(results_df)
|
245 |
|
|
|
492 |
score = calculate_relevance_score(page_content, query, co)
|
493 |
return score
|
494 |
|
495 |
+
def show_tabular_data(df, co, country_code):
|
496 |
st.write("Data Table with Relevancy Scores")
|
497 |
|
498 |
# Pagination
|
|
|
562 |
if competitor_button:
|
563 |
st.write(f"Competitor Analysis for: {row.query}")
|
564 |
with st.spinner('Analyzing competitors...'):
|
565 |
+
results_df = analyze_competitors(row._asdict(), co, country_code=country_code)
|
566 |
|
567 |
# Sort the results by relevancy score in descending order
|
568 |
results_df = results_df.sort_values('relevancy_score', ascending=False).reset_index(drop=True)
|
|
|
713 |
search_type = show_search_type_selector()
|
714 |
date_range_selection = show_date_range_selector()
|
715 |
model_type = show_model_type_selector()
|
716 |
+
|
717 |
+
# Add country selector
|
718 |
+
selected_country = st.selectbox(
|
719 |
+
"Select Country for SERP Results:",
|
720 |
+
COUNTRY_OPTIONS,
|
721 |
+
format_func=lambda x: x[1],
|
722 |
+
key='country_selector'
|
723 |
+
)
|
724 |
+
country_code = selected_country[0]
|
725 |
+
|
726 |
if date_range_selection == 'Custom Range':
|
727 |
show_custom_date_inputs()
|
728 |
start_date, end_date = st.session_state.custom_start_date, st.session_state.custom_end_date
|
|
|
741 |
if st.session_state.report_data is not None and not st.session_state.report_data.empty:
|
742 |
st.write("Data fetched successfully.")
|
743 |
|
744 |
+
st.session_state.report_data = show_tabular_data(st.session_state.report_data, co, country_code)
|
745 |
|
746 |
download_csv_link(st.session_state.report_data)
|
747 |
elif st.session_state.report_data is not None:
|
748 |
+
# logger.warning("No data found for the selected criteria.")
|
749 |
st.warning("No data found for the selected criteria.")
|
750 |
|
751 |
if __name__ == "__main__":
|