Spaces:
Configuration error
Configuration error
File size: 1,593 Bytes
4133b77 |
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 |
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."
|