Spaces:
Running
Running
File size: 17,007 Bytes
c0cf9ef 8f55464 c0cf9ef 12d556f d0b52bb 32a2f5c d381ade 32a2f5c d381ade c0cf9ef 32a2f5c c0cf9ef 32a2f5c 8f55464 d0b52bb 32a2f5c c0cf9ef 32a2f5c c0cf9ef 32a2f5c c0cf9ef 32a2f5c c0cf9ef 32a2f5c 8f55464 c0cf9ef 32a2f5c c0cf9ef 32a2f5c c0cf9ef 32a2f5c c0cf9ef 32a2f5c d381ade 32a2f5c c0cf9ef 32a2f5c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 20 2023
@author: cyberandy
"""
# ---------------------------------------------------------------------------- #
# Imports
# ---------------------------------------------------------------------------- #
from io import StringIO # for redirect_stdout
from functools import wraps # for caching
import contextlib # for redirect_stdout
import tldextract
import requests
import streamlit as st
import pandas as pd
import streamlit.components.v1 as components
import json
import os
from openai import OpenAI
# ---------------------------------------------------------------------------- #
# App Config. & Styling
# ---------------------------------------------------------------------------- #
PAGE_CONFIG = {
"page_title": "Structured Data Audit - a Free SEO Tool by WordLift",
"page_icon": "img/fav-ico.png",
"layout": "centered"
}
def local_css(file_name):
with open(file_name) as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
st.set_page_config(**PAGE_CONFIG)
local_css("style.css")
# ---------------------------------------------------------------------------- #
# Web Application
# ---------------------------------------------------------------------------- #
# st.title("π₯ Schema Audit π₯")
# ---------------------------------------------------------------------------- #
# Sidebar
# ---------------------------------------------------------------------------- #
# st.sidebar.image("img/logo-wordlift.png", width=200)
# st.sidebar.info("Run the schema audit on any website to quickly get an overview of the available markup. \
# Simply add the naked domain without 'www.' (eg. etsy.com or etsy.com/about) URL and click on ""ANALYZE"" to get the results.")
# st.sidebar.subheader("Configuration")
# ---------------------------------------------------------------------------- #
# Functions
# ---------------------------------------------------------------------------- #
# Set the API endpoint and the API key
API_ENDPOINT = "https://api2.woorank.com/reviews"
API_KEY = os.environ.get("woorank_api_key")
openai_api_key = os.environ.get("openai_api_key")
if not API_KEY:
st.error("The API keys are not properly configured. Check your environment variables!")
elif not openai_api_key:
st.error("The OpenAI API key is not properly configured. Check your environment variables!")
else:
# Generate the report by calling the ChatGPT Turbo API and the WooRank API
def analyze_data(_advice, _items, _topics, _issues, _technologies, openai_api_key):
"""
:param _advice: A list of strings, each string is a piece of advice
:param _items: a list of items that are being analyzed
:param _topics: a list of topics that the user is interested in
:param _issues: a list of issues that the user has selected
:param _technologies: A list of technologies that the user has selected
"""
# Create the system message for ChatGPT Turbo
prefix_messages = [{"role": "system", "content": '''You are a helpful and truthful SEO that is very good at analyzing websites with a specific focus on structured data. /n
You are able to provide a detailed report on the website's structured data and how to improve it. /n
ADD AS LEARN MORE LINKS FOR THE FIRST TEXT BLOCK LINKS TO structured data https://wordlift.io/blog/en/entity/structured-data/ and schema.org https://wordlift.io/blog/en/entity/schema-org/ TO PROVIDE ADDITIONAL HELP./n/n
YOU ARE WRITING THE REPORT IN HTML USING A TEMPLATE.'''}]
client = OpenAI(api_key=openai_api_key)
# Construct messages for the chat API
messages = []
messages.extend(prefix_messages)
# Create the prompt template and the run statement when there are NOT issues
if not _issues and len(_items) > 0:
template = """
First text block of the report./n
Analyze the: {advice}, consider that the site features the following schema classes: {items}./n/n
Second text block of the report./n
The website's homepage also references the following entities: {topics} that could be used to improve the SEO of the website further./n/n
Third text block of the report./n
Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do./n/n
THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:/n
"first": "First text block with schema classes in <i>italic</i>",
"second": "Second text block with entities in <b>bold</b>",
"third": "Third text block with technologies in <i>italic</i>"
"""
prompt = PromptTemplate(template=template, input_variables=[
"advice", "items", "topics", "technologies"])
run_statement = {"advice": _advice, "items": _items,
"topics": _topics, "technologies": _technologies}
# Create the prompt template and the run statement when there ARE NOT schema classes
elif not _items:
template = """
First text block of the report./n
The website homepage doesn't seem to feature any schema class./n/n
Second text block of the report./n
The website's homepage also references the following entities: {topics} that can be used to improve the SEO of the website./n/n
Third text block of the report./n
Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do./n/n
THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:/n
"first": "First text block",
"second": "Second text block with entities in <b>bold</b>"
"third": "Third text block with technologies in <i>italic</i>"
"""
prompt = PromptTemplate(template=template, input_variables=[
"topics", "technologies"])
run_statement = {"topics": _topics, "technologies": _technologies}
# Create the prompt template and the run statement when there ARE issues
else:
template = """
First text block of the report./n
Analyze the: {advice}, consider that the site features the following schema classes: {items}./n/n
Second text block of the report. /n
Describe the following issues with the markup: {issues} and indicate how to fix them./n/n
Third text block of the report./n
The website's homepage also references the following entities: {topics} that could be used to improve the SEO of the website further./n/n
Fourth text block of the report./n
Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do./n/n
THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:/n
"first": "First text block with schema classes in <i>italic</i>",
"second": "Second text block with issues in <u>underline</u>",
"third": "Third text block with entities in <b>bold</b>"
"fourth": "Fourth text block with technologies in <i>italic</i>"
"""
prompt = PromptTemplate(template=template, input_variables=[
"advice", "items", "topics", "issues", "technologies"])
run_statement = {"advice": _advice, "items": _items,
"topics": _topics, "issues": _issues, "technologies": _technologies}
# Format the prompt
user_message = prompt.format(**run_statement)
messages.append({"role": "user", "content": user_message})
# Make the API call
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)
out = response.choices[0].message.content
except Exception as e:
out = f"Sorry, there was an error with the OpenAI API: {e}"
return out
# Call WooRank API to get the data (cached)
@st.cache_data
def get_woorank_data(url):
"""
It takes a URL as input, and returns a dictionary of the data from the Woorank API
:param url: The URL of the website you want to get data for
"""
# Extract the domain from the URL
extracted = tldextract.extract(url)
url = f"{extracted.domain}.{extracted.suffix}"
# Build the API URL
api_url = f"{API_ENDPOINT}?url={url}"
# Set the API key in the headers
headers = {"x-api-key": API_KEY,
"Accept": "application/json"}
# Call the API using HTTP GET and parse the JSON response to extract what we need
response = requests.get(api_url, headers=headers)
data = response.json()
result = data.get("criteria", {}).get("schema_org", {})
advice = result.get("advice", {})
items = result.get("data", {}).get("counts", {})
issues = result.get("data", {}).get("issues", {})
topics_raw = data.get("criteria", {}).get("topics", {}).get("data", {})
technologies_raw = data.get("criteria", {}).get(
"technologies", {}).get("data", {}).get("technologies", {})
# extract the unique English labels into a list
topics = list(
set([label for item in topics_raw for label in item['dbpediaLabelsEn']]))
# extract the technologies that are related to seo and search-engines
technologies = []
for item in technologies_raw:
if "seo" in item["categories"] or "search-engines" in item["categories"]:
technologies.append(item["app"])
# Return now all the items we need
return result, advice, items, issues, topics, technologies
# Here capture the output of the function and write it to the Streamlit app for debugging purposes
def capture_output(func):
"""Capture output from running a function and write using streamlit."""
@wraps(func)
def wrapper(*args, **kwargs):
# Redirect output to string buffers
stdout, stderr = StringIO(), StringIO()
try:
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
return func(*args, **kwargs)
except Exception as err:
print(f"Failure while executing: {err}")
finally:
if _stdout := stdout.getvalue():
print("Execution stdout:")
print(_stdout)
if _stderr := stderr.getvalue():
print("Execution stderr:")
print(_stderr)
return wrapper
# ---------------------------------------------------------------------------- #
# Main Function
# ---------------------------------------------------------------------------- #
def main():
# Set up the Streamlit app
# Adding the input for the URL
url = st.text_input("Enter a URL to analyze")
if st.button("RUN THE STRUCTURED DATA AUDIT"):
# Call the Woorank API
schema_org, advice, items, issues, topics, technologies = get_woorank_data(
url)
if not advice:
st.warning("Whoops, sorry, our bot didn't find any data. It might be that the URL is not accessible (a firewall is in place, for example), or the website does not have structured data.", icon="β οΈ")
else:
msg = analyze_data(advice, items, topics, issues, technologies, openai_api_key)
# Display the results when the button is clicked and the data is available
if schema_org and msg:
st.write("##### Your Findings π")
try:
data_out = json.loads(msg)
# here is the first block of text with the advice
first_block_text = data_out['first']
# here is the second block of text (opportunities if there are no issues, issues if there are)
second_block_text = data_out['second']
# here we create the HTML string for the first block of text (advice)
htmlstr1 = f"""<div class="success">{first_block_text}</div>"""
st.markdown(htmlstr1, unsafe_allow_html=True)
# adding a disclosure message
st.markdown(
"""<div class="disclosure">*These findings are based on the analysis of your website as seen from the "eyes" of a crawler.</div>""", unsafe_allow_html=True)
# if there are no issues, we only have three blocks of text (advice, opportunities, technologies)
if not issues:
# here we get the third block of text with the technologies
third_block_text = data_out['third']
# here we create the HTML string for the second block of text (opportunities)
htmlstr2 = f"""<p class="opportunity">βΉοΈ <b>Opportunities</b></br>{second_block_text}</p>"""
st.markdown(htmlstr2, unsafe_allow_html=True)
# here we create the HTML string for the third block of text (technologies)
htmlstr3 = f"""<p class="technology">π©π½βπ» <b>Technologies</b></br>{third_block_text}</p>"""
st.markdown(htmlstr3, unsafe_allow_html=True)
# if there are issues, we have four blocks of text (advice, issues, opportunities, technologies)
else:
# here we get the third block of text with the opportunities
third_block_text = data_out['third']
# here we get the fourth block of text with the technologies
fourth_block_text = data_out['fourth']
# here we create the HTML string for the second block of text (issues)
htmlstr2 = f"""<p class="warning">β οΈ <b>Warnings</b></br>{second_block_text}</p>"""
st.markdown(htmlstr2, unsafe_allow_html=True)
# here we create the HTML string for the third block of text (opportunities)
htmlstr3 = f"""<p class="opportunity">βΉοΈ <b>Opportunities</b></br>{third_block_text}</p>"""
st.markdown(htmlstr3, unsafe_allow_html=True)
# here we create the HTML string for the fourth block of text (technologies)
htmlstr4 = f"""<p class="technology">π©π½βπ» <b>Technologies</b></br>{fourth_block_text}</p>"""
st.markdown(htmlstr4, unsafe_allow_html=True)
except Exception as e:
st.warning(
"Sorry, something went wrong. Please try again later.", icon="β οΈ")
# Adding debug info
stprint = capture_output(print)
stprint(e)
stprint(msg)
st.write("---")
# Adding an expandable section to display the full response
with st.expander("INSPECT THE REPORT"):
# st.write("#### Advice")
# st.markdown(advice, unsafe_allow_html=True)
st.write("##### Items")
st.write(items)
if not issues:
st.write("No issues found on the structured data")
else:
st.write("#### Issues")
st.write(issues)
st.write("##### Entities")
st.write(topics)
st.write("##### Technologies")
st.write(technologies)
st.write("##### Full response")
st.write(schema_org)
# If the API call fails, display an error message
else:
if len(url) == 0:
st.warning("Please enter a URL to analyze")
# else:
# st.warning("Whoops, sorry, our bot didn't find any data. It might be that the URL is not accessible (a firewall is in place, for example), or the website does not have structured data.", icon="β οΈ")
if __name__ == "__main__":
main() |