poemsforaphrodite commited on
Commit
d5343ee
1 Parent(s): 14c5146

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -171,25 +171,21 @@ def analyze_competitors(row, co):
171
  logger.info(f"Analyzing competitors for query: {row['query']}")
172
  query = row['query']
173
  our_url = row['page']
174
- our_score = row['relevancy_score']
175
 
176
  competitor_urls = get_serp_results(query)
177
 
178
  results = []
179
- for url in competitor_urls:
180
- if url != our_url: # Skip our URL if it's in the SERP results
181
- content = fetch_content(url)
182
- score = calculate_relevance_score(content, query, co)
183
- results.append({'url': url, 'relevancy_score': score})
184
-
185
- # Add our page's data
186
- results.append({'url': our_url, 'relevancy_score': our_score})
187
 
188
  results_df = pd.DataFrame(results).sort_values('relevancy_score', ascending=False)
189
 
190
  logger.info(f"Competitor analysis completed. {len(results)} results obtained.")
191
  return results_df
192
 
 
193
  def show_competitor_analysis(row, co):
194
  if st.button("Check Competitors", key=f"comp_{row['page']}"):
195
  logger.info(f"Competitor analysis requested for page: {row['page']}")
@@ -200,8 +196,11 @@ def show_competitor_analysis(row, co):
200
 
201
  our_rank = results_df.index[results_df['url'] == row['page']].tolist()[0] + 1
202
  total_results = len(results_df)
 
 
203
  logger.info(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
204
- st.write(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
 
205
 
206
  if our_rank == 1:
207
  st.success("Your page has the highest relevancy score!")
@@ -318,6 +317,19 @@ def fetch_gsc_data(webproperty, search_type, start_date, end_date, dimensions, d
318
  show_error(e)
319
  return pd.DataFrame()
320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321
  def calculate_relevancy_scores(df, model_type):
322
  logging.info("Calculating relevancy scores")
323
  with st.spinner('Calculating relevancy scores...'):
 
171
  logger.info(f"Analyzing competitors for query: {row['query']}")
172
  query = row['query']
173
  our_url = row['page']
 
174
 
175
  competitor_urls = get_serp_results(query)
176
 
177
  results = []
178
+ for url in [our_url] + competitor_urls: # Include our URL at the beginning
179
+ content = fetch_content(url)
180
+ score = calculate_relevance_score(content, query, co)
181
+ results.append({'url': url, 'relevancy_score': score})
 
 
 
 
182
 
183
  results_df = pd.DataFrame(results).sort_values('relevancy_score', ascending=False)
184
 
185
  logger.info(f"Competitor analysis completed. {len(results)} results obtained.")
186
  return results_df
187
 
188
+
189
  def show_competitor_analysis(row, co):
190
  if st.button("Check Competitors", key=f"comp_{row['page']}"):
191
  logger.info(f"Competitor analysis requested for page: {row['page']}")
 
196
 
197
  our_rank = results_df.index[results_df['url'] == row['page']].tolist()[0] + 1
198
  total_results = len(results_df)
199
+ our_score = results_df.loc[results_df['url'] == row['page'], 'relevancy_score'].values[0]
200
+
201
  logger.info(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
202
+ st.write(f"Our page ('{row['page']}') ranks {our_rank} out of {total_results} in terms of relevancy score.")
203
+ st.write(f"Our relevancy score: {our_score:.4f}")
204
 
205
  if our_rank == 1:
206
  st.success("Your page has the highest relevancy score!")
 
317
  show_error(e)
318
  return pd.DataFrame()
319
 
320
+ def calculate_relevance_score(page_content, query, co):
321
+ logger.info(f"Calculating relevance score for query: {query}")
322
+ try:
323
+ page_embedding = co.embed(texts=[page_content], model='embed-english-v3.0', input_type='search_document').embeddings[0]
324
+ query_embedding = co.embed(texts=[query], model='embed-english-v3.0', input_type='search_query').embeddings[0]
325
+ score = cosine_similarity([query_embedding], [page_embedding])[0][0]
326
+ logger.debug(f"Relevance score calculated: {score}")
327
+ return score
328
+ except Exception as e:
329
+ logger.exception(f"Error calculating relevance score: {str(e)}")
330
+ st.error(f"Error calculating relevance score: {str(e)}")
331
+ return 0
332
+
333
  def calculate_relevancy_scores(df, model_type):
334
  logging.info("Calculating relevancy scores")
335
  with st.spinner('Calculating relevancy scores...'):