poemsforaphrodite commited on
Commit
1f18d28
1 Parent(s): 230aabc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -183,13 +183,19 @@ def fetch_gsc_data(webproperty, search_type, start_date, end_date, dimensions, d
183
  show_error(e)
184
  return pd.DataFrame()
185
 
186
- def fetch_data_loading(webproperty, search_type, start_date, end_date, dimensions, device_type=None, model_type='english'):
187
- with st.spinner('Fetching data and calculating relevancy scores...'):
188
- df = fetch_gsc_data(webproperty, search_type, start_date, end_date, dimensions, device_type)
189
- if not df.empty:
190
- df = calculate_relevancy_scores(df, model_type)
191
- processed_df = process_gsc_data(df)
192
- return processed_df
 
 
 
 
 
 
193
 
194
  # -------------
195
  # Utility Functions
@@ -412,13 +418,17 @@ def main():
412
  selected_dimensions = show_dimensions_selector(search_type)
413
 
414
  if 'report_data' not in st.session_state:
415
- st.session_state.report_data = None
416
 
417
  if st.button("Fetch Data"):
418
  with st.spinner('Fetching data...'):
419
- st.session_state.report_data = fetch_data_loading(webproperty, search_type, start_date, end_date, selected_dimensions, model_type=model_type) # Update this line
420
 
421
  if st.session_state.report_data is not None and not st.session_state.report_data.empty:
 
 
 
 
422
  show_paginated_dataframe(st.session_state.report_data)
423
  download_csv_link(st.session_state.report_data)
424
  elif st.session_state.report_data is not None:
 
183
  show_error(e)
184
  return pd.DataFrame()
185
 
186
+ def calculate_relevancy_scores(df, model_type):
187
+ with st.spinner('Calculating relevancy scores...'):
188
+ try:
189
+ page_contents = [fetch_content(url) for url in df['page']]
190
+ page_embeddings = generate_embeddings(page_contents, model_type)
191
+ query_embeddings = generate_embeddings(df['query'].tolist(), model_type)
192
+ relevancy_scores = cosine_similarity(query_embeddings, page_embeddings).diagonal()
193
+ df = df.assign(relevancy_score=relevancy_scores)
194
+ except Exception as e:
195
+ st.warning(f"Error calculating relevancy scores: {e}")
196
+ df = df.assign(relevancy_score=0)
197
+ return df
198
+
199
 
200
  # -------------
201
  # Utility Functions
 
418
  selected_dimensions = show_dimensions_selector(search_type)
419
 
420
  if 'report_data' not in st.session_state:
421
+ st.session_state.report_data = None
422
 
423
  if st.button("Fetch Data"):
424
  with st.spinner('Fetching data...'):
425
+ st.session_state.report_data = fetch_gsc_data(webproperty, search_type, start_date, end_date, selected_dimensions)
426
 
427
  if st.session_state.report_data is not None and not st.session_state.report_data.empty:
428
+ st.write("Data fetched successfully. Click the button below to calculate relevancy scores.")
429
+
430
+ if st.button("Calculate Relevancy Scores"):
431
+ st.session_state.report_data = calculate_relevancy_scores(st.session_state.report_data, model_type)
432
  show_paginated_dataframe(st.session_state.report_data)
433
  download_csv_link(st.session_state.report_data)
434
  elif st.session_state.report_data is not None: