Spaces:
Configuration error
Configuration error
import os | |
import openai | |
import requests | |
from bs4 import BeautifulSoup | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
def quality_score(entity: str) -> int: | |
""" | |
If 'entity' is a URL: | |
β’ Fetch the first 2000 characters of its visible text. | |
β’ Ask GPT-4: "Rate this snippet 0β100 on combined Expertise, Authority, Trustworthiness." | |
β’ Return the integer score. | |
Else, return default 60. | |
""" | |
if not entity.startswith("http"): | |
return 60 | |
try: | |
resp = requests.get(entity, timeout=5) | |
text_snippet = BeautifulSoup(resp.text, "html.parser").get_text()[:2000] | |
prompt = ( | |
"Below is the text snippet of a webpage. Rate it 0β100 on combined " | |
"Expertise, Authority, and Trustworthiness (E-E-A-T), and return just the integer score.\n\n" | |
f"{text_snippet}" | |
) | |
api_resp = openai.ChatCompletion.create( | |
model="gpt-4", | |
messages=[{"role": "user", "content": prompt}] | |
) | |
content = api_resp.choices[0].message.content.strip() | |
digits = "".join(filter(str.isdigit, content)) | |
if digits: | |
score = int(digits) | |
return max(0, min(score, 100)) | |
except Exception: | |
pass | |
return 60 | |
def quality_recommendation(entity: str, score: int) -> str: | |
if score < 50: | |
return ( | |
"Improve E-E-A-T: add author bylines, cite reputable sources, " | |
"and maintain factual consistency." | |
) | |
return "Content quality is strong; keep updating and citing reputable references." | |