poemsforaphrodite commited on
Commit
d079c96
1 Parent(s): 7a8955f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -3
app.py CHANGED
@@ -565,14 +565,14 @@ def show_tabular_data(df, co, country_code):
565
  st.experimental_rerun()
566
 
567
  # Display column headers
568
- cols = st.columns([0.5, 3, 2, 1, 1, 1, 1, 1, 1])
569
- headers = ['Select', 'Page', 'Query', 'Clicks', 'Impressions', 'CTR', 'Position', 'Relevancy Score', 'Competitors']
570
  for col, header in zip(cols, headers):
571
  col.write(f"**{header}**")
572
 
573
  # Display each row
574
  for i, row in enumerate(df.iloc[start_idx:end_idx].itertuples(), start=start_idx):
575
- cols = st.columns([0.5, 3, 2, 1, 1, 1, 1, 1, 1])
576
 
577
  # Checkbox for row selection
578
  cols[0].checkbox("", key=f"select_{i}", value=st.session_state.selected_rows[i],
@@ -631,8 +631,74 @@ def show_tabular_data(df, co, country_code):
631
  else:
632
  st.error(f"Our page '{row.page}' is not in the results. This indicates an error in fetching or processing the page.")
633
 
 
 
 
 
 
634
  return df # Return the updated dataframe
635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
636
  def show_date_range_selector():
637
  # logging.info("Showing date range selector")
638
  return st.selectbox(
 
565
  st.experimental_rerun()
566
 
567
  # Display column headers
568
+ cols = st.columns([0.5, 3, 2, 1, 1, 1, 1, 1, 1, 1]) # Added an extra column
569
+ headers = ['Select', 'Page', 'Query', 'Clicks', 'Impressions', 'CTR', 'Position', 'Relevancy Score', 'Competitors', 'Compare']
570
  for col, header in zip(cols, headers):
571
  col.write(f"**{header}**")
572
 
573
  # Display each row
574
  for i, row in enumerate(df.iloc[start_idx:end_idx].itertuples(), start=start_idx):
575
+ cols = st.columns([0.5, 3, 2, 1, 1, 1, 1, 1, 1, 1]) # Added an extra column
576
 
577
  # Checkbox for row selection
578
  cols[0].checkbox("", key=f"select_{i}", value=st.session_state.selected_rows[i],
 
631
  else:
632
  st.error(f"Our page '{row.page}' is not in the results. This indicates an error in fetching or processing the page.")
633
 
634
+ # New button for content comparison
635
+ compare_button = cols[9].button("Compare", key=f"compare_{i}")
636
+ if compare_button:
637
+ compare_with_top_result(row._asdict(), co, country_code)
638
+
639
  return df # Return the updated dataframe
640
 
641
+ def compare_with_top_result(row, co, country_code):
642
+ query = row['query']
643
+ our_url = row['page']
644
+
645
+ # Fetch SERP results
646
+ serp_results = get_serp_results(query, country_code)
647
+
648
+ if not serp_results:
649
+ st.error("Unable to fetch SERP results.")
650
+ return
651
+
652
+ top_result = serp_results[0]
653
+ top_url = top_result['url']
654
+
655
+ # Fetch content
656
+ our_content = fetch_content(our_url, query)
657
+ top_content = top_result['content']
658
+
659
+ # Calculate relevancy scores
660
+ our_score = calculate_relevance_score(our_content, query, co)
661
+ top_score = calculate_relevance_score(top_content, query, co)
662
+
663
+ # Prepare prompt for GPT-4
664
+ prompt = f"""
665
+ Compare the following two pieces of content for the query "{query}":
666
+
667
+ 1. Top-ranking page (score: {top_score:.4f}):
668
+ {top_content[:1000]}...
669
+
670
+ 2. Our page (score: {our_score:.4f}):
671
+ {our_content[:1000]}...
672
+
673
+ Explain the difference in cosine similarity scores between the top-ranking page and our page.
674
+ What can we do to improve our score and make our content more relevant to the query?
675
+ Provide specific, actionable recommendations.
676
+ """
677
+
678
+ # Call GPT-4
679
+ try:
680
+ response = openai_client.chat.completions.create(
681
+ model="gpt-4o-mini",
682
+ messages=[
683
+ {"role": "system", "content": "You are an SEO expert analyzing content relevance."},
684
+ {"role": "user", "content": prompt}
685
+ ],
686
+ max_tokens=1000
687
+ )
688
+ analysis = response.choices[0].message.content.strip()
689
+
690
+ # Display results
691
+ st.subheader("Content Comparison Analysis")
692
+ st.write(f"Query: {query}")
693
+ st.write(f"Top-ranking URL: {top_url}")
694
+ st.write(f"Our URL: {our_url}")
695
+ st.write(f"Top-ranking score: {top_score:.4f}")
696
+ st.write(f"Our score: {our_score:.4f}")
697
+ st.write("Analysis:")
698
+ st.write(analysis)
699
+ except Exception as e:
700
+ st.error(f"Error in GPT-4 analysis: {str(e)}")
701
+
702
  def show_date_range_selector():
703
  # logging.info("Showing date range selector")
704
  return st.selectbox(