poemsforaphrodite commited on
Commit
7bfec1a
1 Parent(s): d341aca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -63
app.py CHANGED
@@ -177,45 +177,26 @@ def calculate_relevance_score(page_content, query, co):
177
  st.error(f"Error calculating relevance score: {str(e)}")
178
  return 0
179
 
180
- def analyze_competitors(row, co):
181
  # logger.info(f"Analyzing competitors for query: {row['query']}")
182
  query = row['query']
183
  our_url = row['page']
184
 
185
- competitor_urls = get_serp_results(query)[:5] # Get top 5 competitors
186
-
187
- results = []
188
 
189
- # Calculate score for our page first
190
- our_content = fetch_content(our_url)
191
- print(our_url)
192
- print(our_content)
193
- if our_content:
194
- our_score = calculate_relevance_score(our_content, query, co)
195
- results.append({'url': our_url, 'relevancy_score': our_score})
196
- #logger.info(f"Our URL: {our_url}, Score: {our_score}")
197
- #logger.warning(f"No content fetched for our URL: {our_url}")
198
 
199
- # Calculate scores for competitor pages
200
  for url in competitor_urls:
201
- try:
202
- # logger.debug(f"Processing competitor URL: {url}")
203
- content = fetch_content(url)
204
- if not content:
205
- # logger.warning(f"No content fetched for competitor URL: {url}")
206
- continue
207
-
208
- score = calculate_relevance_score(content, query, co)
209
-
210
- # logger.info(f"Competitor URL: {url}, Score: {score}")
211
- results.append({'url': url, 'relevancy_score': score})
212
- except Exception as e:
213
- # logger.error(f"Error processing URL {url}: {str(e)}")
214
- st.error(f"Error processing URL {url}: {str(e)}")
215
 
 
216
  results_df = pd.DataFrame(results).sort_values('relevancy_score', ascending=False)
217
 
218
- # logger.info(f"Competitor analysis completed. {len(results)} results obtained.")
219
  return results_df
220
 
221
  def show_competitor_analysis(row, co):
@@ -265,24 +246,6 @@ def show_competitor_analysis(row, co):
265
  elif our_rank > total_results / 2:
266
  st.warning("Your page's relevancy score is in the lower half of the results. Consider optimizing your content.")
267
 
268
- def analyze_competitors(row, co):
269
- query = row['query']
270
- our_url = row['page']
271
- our_score = row['relevancy_score']
272
-
273
- competitor_urls = get_serp_results(query)
274
- competitor_urls = [url for url in competitor_urls if not url.startswith('/search')][:5] # Get top 5 valid competitors
275
-
276
- results = []
277
- for url in competitor_urls:
278
- content = fetch_content(url)
279
- score = calculate_relevance_score(content, query, co)
280
- results.append({'url': url, 'relevancy_score': score})
281
-
282
- results.append({'url': our_url, 'relevancy_score': our_score})
283
- results_df = pd.DataFrame(results).sort_values('relevancy_score', ascending=False)
284
-
285
- return results_df
286
  def process_gsc_data(df):
287
  #logging.info("Processing GSC data")
288
  df_sorted = df.sort_values(['impressions'], ascending=[False])
@@ -500,7 +463,7 @@ def show_tabular_data(df, co):
500
  return score
501
 
502
  # Function to analyze competitors
503
- def analyze_competitors(row):
504
  query = row['query']
505
  our_url = row['page']
506
  our_score = row['relevancy_score']
@@ -508,6 +471,9 @@ def show_tabular_data(df, co):
508
  competitor_urls = get_serp_results(query)
509
  competitor_urls = [url for url in competitor_urls if not url.startswith('/search')][:5] # Get top 5 valid competitors
510
 
 
 
 
511
  results = []
512
  for url in competitor_urls:
513
  content = fetch_content(url)
@@ -539,21 +505,29 @@ def show_tabular_data(df, co):
539
  st.write(f"Relevancy Score: {row['relevancy_score']:.4f}")
540
  with col3:
541
  if st.button(f"Compare Competitors", key=f"comp_{index}"):
542
- with st.spinner('Analyzing competitors...'):
543
- results_df = analyze_competitors(row)
544
- st.write("Competitor Comparison:")
545
- st.dataframe(results_df)
546
-
547
- our_rank = results_df.index[results_df['url'] == row['page']].tolist()[0] + 1
548
- total_results = len(results_df)
549
- st.write(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
550
-
551
- if our_rank == 1:
552
- st.success("Your page has the highest relevancy score!")
553
- elif our_rank <= 3:
554
- st.info("Your page is among the top 3 most relevant results.")
555
- elif our_rank > total_results / 2:
556
- st.warning("Your page's relevancy score is in the lower half of the results. Consider optimizing your content.")
 
 
 
 
 
 
 
 
557
 
558
  return df # Return the updated dataframe
559
 
 
177
  st.error(f"Error calculating relevance score: {str(e)}")
178
  return 0
179
 
180
+ def analyze_competitors(row, co, custom_url=None):
181
  # logger.info(f"Analyzing competitors for query: {row['query']}")
182
  query = row['query']
183
  our_url = row['page']
184
 
185
+ competitor_urls = get_serp_results(query)
186
+ competitor_urls = [url for url in competitor_urls if not url.startswith('/search')][:5] # Get top 5 valid competitors
 
187
 
188
+ if custom_url and custom_url not in competitor_urls:
189
+ competitor_urls.append(custom_url)
 
 
 
 
 
 
 
190
 
191
+ results = []
192
  for url in competitor_urls:
193
+ content = fetch_content(url)
194
+ score = calculate_relevance_score(content, query, co)
195
+ results.append({'url': url, 'relevancy_score': score})
 
 
 
 
 
 
 
 
 
 
 
196
 
197
+ results.append({'url': our_url, 'relevancy_score': row['relevancy_score']})
198
  results_df = pd.DataFrame(results).sort_values('relevancy_score', ascending=False)
199
 
 
200
  return results_df
201
 
202
  def show_competitor_analysis(row, co):
 
246
  elif our_rank > total_results / 2:
247
  st.warning("Your page's relevancy score is in the lower half of the results. Consider optimizing your content.")
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  def process_gsc_data(df):
250
  #logging.info("Processing GSC data")
251
  df_sorted = df.sort_values(['impressions'], ascending=[False])
 
463
  return score
464
 
465
  # Function to analyze competitors
466
+ def analyze_competitors(row, custom_url=None):
467
  query = row['query']
468
  our_url = row['page']
469
  our_score = row['relevancy_score']
 
471
  competitor_urls = get_serp_results(query)
472
  competitor_urls = [url for url in competitor_urls if not url.startswith('/search')][:5] # Get top 5 valid competitors
473
 
474
+ if custom_url and custom_url not in competitor_urls:
475
+ competitor_urls.append(custom_url)
476
+
477
  results = []
478
  for url in competitor_urls:
479
  content = fetch_content(url)
 
505
  st.write(f"Relevancy Score: {row['relevancy_score']:.4f}")
506
  with col3:
507
  if st.button(f"Compare Competitors", key=f"comp_{index}"):
508
+ custom_url = st.text_input("Enter a custom URL to compare (optional):", key=f"custom_url_{index}")
509
+
510
+ if st.button("Analyze", key=f"analyze_{index}"):
511
+ with st.spinner('Analyzing competitors...'):
512
+ results_df = analyze_competitors(row, custom_url)
513
+ st.write("Competitor Comparison:")
514
+ st.dataframe(results_df)
515
+
516
+ our_rank = results_df.index[results_df['url'] == row['page']].tolist()[0] + 1
517
+ total_results = len(results_df)
518
+ st.write(f"Our page ranks {our_rank} out of {total_results} in terms of relevancy score.")
519
+
520
+ if our_rank == 1:
521
+ st.success("Your page has the highest relevancy score!")
522
+ elif our_rank <= 3:
523
+ st.info("Your page is among the top 3 most relevant results.")
524
+ elif our_rank > total_results / 2:
525
+ st.warning("Your page's relevancy score is in the lower half of the results. Consider optimizing your content.")
526
+
527
+ if custom_url:
528
+ custom_rank = results_df.index[results_df['url'] == custom_url].tolist()[0] + 1
529
+ custom_score = results_df[results_df['url'] == custom_url]['relevancy_score'].values[0]
530
+ st.write(f"Custom URL '{custom_url}' ranks {custom_rank} out of {total_results} with a relevancy score of {custom_score:.4f}")
531
 
532
  return df # Return the updated dataframe
533