poemsforaphrodite commited on
Commit
5b41794
1 Parent(s): f20b94b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -238,33 +238,49 @@ def calculate_relevance_score(page_content, query, co):
238
  st.error(f"Error calculating relevance score: {str(e)}")
239
  return 0
240
 
 
 
 
 
241
  def analyze_competitors(row, co, custom_url=None, country_code=None):
242
  query = row['query']
243
- our_url = row['page']
244
 
245
  competitor_data = get_serp_results(query, country_code)
246
 
247
  results = []
248
  for data in competitor_data:
 
249
  score = calculate_relevance_score(data['content'], query, co)
250
- results.append({'position': data['position'], 'url': data['url'], 'relevancy_score': score})
 
 
 
 
 
251
 
252
  our_content = fetch_content(our_url, query)
253
  our_score = calculate_relevance_score(our_content, query, co)
254
- our_position = next((r['position'] for r in results if r['url'] == our_url), len(results) + 1)
255
- results.append({'position': our_position, 'url': our_url, 'relevancy_score': our_score})
256
 
257
- # Sort results by the original position
258
- results = sorted(results, key=lambda x: x['position'])
 
 
 
 
 
259
 
260
- # Re-number positions starting from 1 and format URLs
261
- for idx, item in enumerate(results, start=1):
262
- item['position'] = idx
263
- if item['url'] == our_url:
264
- item['url'] = f"**{item['url']}**" # Mark our URL in bold
265
 
266
- # Keep only 'position', 'url', and 'relevancy_score' columns
267
- results_df = pd.DataFrame(results)[['position', 'url', 'relevancy_score']]
 
 
 
 
 
 
268
 
269
  return results_df
270
 
@@ -274,20 +290,12 @@ def show_competitor_analysis(row, co, country_code):
274
  with st.spinner('Analyzing competitors...'):
275
  results_df = analyze_competitors(row, co, country_code=country_code)
276
 
277
- # Rename columns to have a proper header
278
- results_df.columns = ['Position', 'URL', 'Score']
279
-
280
- # Convert 'Position' to integer for proper sorting and display
281
- results_df['Position'] = results_df['Position'].astype(int)
282
-
283
- # Convert the DataFrame to Markdown with bold URLs where applicable
284
- markdown_table = results_df.to_markdown(index=False)
285
-
286
  # Display the Markdown table
287
- st.markdown(markdown_table, unsafe_allow_html=True)
288
 
289
  # Extract our result for additional insights
290
- our_result = results_df[results_df['URL'].str.contains("**" + row['page'] + "**")]
 
291
  if not our_result.empty:
292
  our_rank = our_result['Position'].values[0]
293
  total_results = len(results_df)
@@ -586,7 +594,7 @@ def show_tabular_data(df, co, country_code):
586
  st.write(f"Competitor Analysis for: {row.query}")
587
  with st.spinner('Analyzing competitors...'):
588
  results_df = analyze_competitors(row._asdict(), co, country_code=country_code)
589
-
590
  # Sort the results by relevancy score in descending order
591
  results_df = results_df.sort_values('relevancy_score', ascending=False).reset_index(drop=True)
592
 
 
238
  st.error(f"Error calculating relevance score: {str(e)}")
239
  return 0
240
 
241
+ def normalize_url(url):
242
+ return url.rstrip('/').lower()
243
+
244
+ @st.cache_data(ttl=3600) # Cache the results for 1 hour
245
  def analyze_competitors(row, co, custom_url=None, country_code=None):
246
  query = row['query']
247
+ our_url = normalize_url(row['page'])
248
 
249
  competitor_data = get_serp_results(query, country_code)
250
 
251
  results = []
252
  for data in competitor_data:
253
+ competitor_url = normalize_url(data['url'])
254
  score = calculate_relevance_score(data['content'], query, co)
255
+ results.append({
256
+ 'position': data['position'],
257
+ 'url': competitor_url,
258
+ 'relevancy_score': score,
259
+ 'is_our_url': competitor_url == our_url
260
+ })
261
 
262
  our_content = fetch_content(our_url, query)
263
  our_score = calculate_relevance_score(our_content, query, co)
 
 
264
 
265
+ if not any(r['is_our_url'] for r in results):
266
+ results.append({
267
+ 'position': len(results) + 1,
268
+ 'url': our_url,
269
+ 'relevancy_score': our_score,
270
+ 'is_our_url': True
271
+ })
272
 
273
+ # Sort results by position
274
+ results = sorted(results, key=lambda x: x['position'])
 
 
 
275
 
276
+ # Create DataFrame and format
277
+ results_df = pd.DataFrame(results)[['position', 'url', 'relevancy_score', 'is_our_url']]
278
+ results_df['position'] = results_df['position'].astype(int)
279
+ results_df['url'] = results_df.apply(
280
+ lambda x: f"**{x['url']}**" if x['is_our_url'] else x['url'], axis=1
281
+ )
282
+ results_df = results_df[['position', 'url', 'relevancy_score']]
283
+ results_df.columns = ['Position', 'URL', 'Score']
284
 
285
  return results_df
286
 
 
290
  with st.spinner('Analyzing competitors...'):
291
  results_df = analyze_competitors(row, co, country_code=country_code)
292
 
 
 
 
 
 
 
 
 
 
293
  # Display the Markdown table
294
+ st.markdown(results_df.to_markdown(index=False), unsafe_allow_html=True)
295
 
296
  # Extract our result for additional insights
297
+ our_result = results_df[results_df['URL'].str.contains('\*\*')]
298
+
299
  if not our_result.empty:
300
  our_rank = our_result['Position'].values[0]
301
  total_results = len(results_df)
 
594
  st.write(f"Competitor Analysis for: {row.query}")
595
  with st.spinner('Analyzing competitors...'):
596
  results_df = analyze_competitors(row._asdict(), co, country_code=country_code)
597
+ print(results_df)
598
  # Sort the results by relevancy score in descending order
599
  results_df = results_df.sort_values('relevancy_score', ascending=False).reset_index(drop=True)
600