poemsforaphrodite commited on
Commit
81d479b
1 Parent(s): 1307a20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -61
app.py CHANGED
@@ -525,6 +525,67 @@ def calculate_single_relevancy(row):
525
  score = calculate_relevance_score(page_content, query, co)
526
  return score
527
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
528
  def show_tabular_data(df, co, country_code):
529
  st.write("Data Table with Relevancy Scores")
530
 
@@ -664,67 +725,6 @@ def show_tabular_data(df, co, country_code):
664
 
665
  return df # Return the updated dataframe
666
 
667
- def compare_with_top_result(row, co, country_code):
668
- query = row['query']
669
- our_url = row['page']
670
-
671
- # Fetch SERP results
672
- serp_results = get_serp_results(query, country_code)
673
-
674
- if not serp_results:
675
- st.error("Unable to fetch SERP results.")
676
- return
677
-
678
- top_result = serp_results[0]
679
- top_url = top_result['url']
680
-
681
- # Fetch content
682
- our_content = fetch_content(our_url, query)
683
- top_content = top_result['content']
684
-
685
- # Calculate relevancy scores
686
- our_score = calculate_relevance_score(our_content, query, co)
687
- top_score = calculate_relevance_score(top_content, query, co)
688
-
689
- # Prepare prompt for GPT-4
690
- prompt = f"""
691
- Compare the following two pieces of content for the query "{query}":
692
-
693
- 1. Top-ranking page (score: {top_score:.4f}):
694
- {top_content[:1000]}...
695
-
696
- 2. Our page (score: {our_score:.4f}):
697
- {our_content[:1000]}...
698
-
699
- Explain the difference in cosine similarity scores between the top-ranking page and our page.
700
- What can we do to improve our score and make our content more relevant to the query?
701
- Provide specific, actionable recommendations.
702
- """
703
-
704
- # Call GPT-4
705
- try:
706
- response = openai_client.chat.completions.create(
707
- model="gpt-4o-mini",
708
- messages=[
709
- {"role": "system", "content": "You are an SEO expert analyzing content relevance."},
710
- {"role": "user", "content": prompt}
711
- ],
712
- max_tokens=1000
713
- )
714
- analysis = response.choices[0].message.content.strip()
715
-
716
- # Display results
717
- st.subheader("Content Comparison Analysis")
718
- st.write(f"Query: {query}")
719
- st.write(f"Top-ranking URL: {top_url}")
720
- st.write(f"Our URL: {our_url}")
721
- st.write(f"Top-ranking score: {top_score:.4f}")
722
- st.write(f"Our score: {our_score:.4f}")
723
- st.write("Analysis:")
724
- st.write(analysis)
725
- except Exception as e:
726
- st.error(f"Error in GPT-4 analysis: {str(e)}")
727
-
728
  def show_date_range_selector():
729
  # logging.info("Showing date range selector")
730
  return st.selectbox(
 
525
  score = calculate_relevance_score(page_content, query, co)
526
  return score
527
 
528
+ def compare_with_top_result(row, co, country_code):
529
+ query = row['query']
530
+ our_url = row['page']
531
+
532
+ # Fetch SERP results
533
+ serp_results = get_serp_results(query, country_code)
534
+
535
+ if not serp_results:
536
+ st.error("Unable to fetch SERP results.")
537
+ return
538
+
539
+ top_result = serp_results[0]
540
+ top_url = top_result['url']
541
+
542
+ # Fetch content
543
+ our_content = fetch_content(our_url, query)
544
+ top_content = top_result['content']
545
+
546
+ # Calculate relevancy scores
547
+ our_score = calculate_relevance_score(our_content, query, co)
548
+ top_score = calculate_relevance_score(top_content, query, co)
549
+
550
+ # Prepare prompt for GPT-4
551
+ prompt = f"""
552
+ Compare the following two pieces of content for the query "{query}":
553
+
554
+ 1. Top-ranking page (score: {top_score:.4f}):
555
+ {top_content[:1000]}...
556
+
557
+ 2. Our page (score: {our_score:.4f}):
558
+ {our_content[:1000]}...
559
+
560
+ Explain the difference in cosine similarity scores between the top-ranking page and our page.
561
+ What can we do to improve our score and make our content more relevant to the query?
562
+ Provide specific, actionable recommendations.
563
+ """
564
+
565
+ # Call GPT-4
566
+ try:
567
+ response = openai_client.chat.completions.create(
568
+ model="gpt-4o-mini",
569
+ messages=[
570
+ {"role": "system", "content": "You are an SEO expert analyzing content relevance."},
571
+ {"role": "user", "content": prompt}
572
+ ],
573
+ max_tokens=1000
574
+ )
575
+ analysis = response.choices[0].message.content.strip()
576
+
577
+ # Display results
578
+ st.subheader("Content Comparison Analysis")
579
+ st.write(f"Query: {query}")
580
+ st.write(f"Top-ranking URL: {top_url}")
581
+ st.write(f"Our URL: {our_url}")
582
+ st.write(f"Top-ranking score: {top_score:.4f}")
583
+ st.write(f"Our score: {our_score:.4f}")
584
+ st.write("Analysis:")
585
+ st.write(analysis)
586
+ except Exception as e:
587
+ st.error(f"Error in GPT-4 analysis: {str(e)}")
588
+
589
  def show_tabular_data(df, co, country_code):
590
  st.write("Data Table with Relevancy Scores")
591
 
 
725
 
726
  return df # Return the updated dataframe
727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728
  def show_date_range_selector():
729
  # logging.info("Showing date range selector")
730
  return st.selectbox(